Brak opisu
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. var tape = require("tape");
  2. var EventEmitter = require("..");
  3. tape.test("eventemitter", function(test) {
  4. var ee = new EventEmitter();
  5. var fn;
  6. var ctx = {};
  7. test.doesNotThrow(function() {
  8. ee.emit("a", 1);
  9. ee.off();
  10. ee.off("a");
  11. ee.off("a", function() {});
  12. }, "should not throw if no listeners are registered");
  13. test.equal(ee.on("a", function(arg1) {
  14. test.equal(this, ctx, "should be called with this = ctx");
  15. test.equal(arg1, 1, "should be called with arg1 = 1");
  16. }, ctx), ee, "should return itself when registering events");
  17. ee.emit("a", 1);
  18. ee.off("a");
  19. test.same(ee._listeners, { a: [] }, "should remove all listeners of the respective event when calling off(evt)");
  20. ee.off();
  21. test.same(ee._listeners, {}, "should remove all listeners when just calling off()");
  22. ee.on("a", fn = function(arg1) {
  23. test.equal(this, ctx, "should be called with this = ctx");
  24. test.equal(arg1, 1, "should be called with arg1 = 1");
  25. }, ctx).emit("a", 1);
  26. ee.off("a", fn);
  27. test.same(ee._listeners, { a: [] }, "should remove the exact listener when calling off(evt, fn)");
  28. ee.on("a", function() {
  29. test.equal(this, ee, "should be called with this = ee");
  30. }).emit("a");
  31. test.doesNotThrow(function() {
  32. ee.off("a", fn);
  33. }, "should not throw if no such listener is found");
  34. test.end();
  35. });