eventemitter.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. (function (root, factory) {
  2. define([
  3. "mock",
  4. "utils"
  5. ], function (mock, utils) {
  6. return factory(mock, utils);
  7. }
  8. );
  9. } (this, function (mock, utils) {
  10. return describe("The Converse Event Emitter", $.proxy(function(mock, utils) {
  11. window.localStorage.clear();
  12. it("allows you to subscribe to emitted events", function () {
  13. this.callback = function () {};
  14. spyOn(this, 'callback');
  15. converse.on('onInitialized', this.callback);
  16. converse.emit('onInitialized');
  17. expect(this.callback).toHaveBeenCalled();
  18. converse.emit('onInitialized');
  19. expect(this.callback.callCount, 2);
  20. converse.emit('onInitialized');
  21. expect(this.callback.callCount, 3);
  22. });
  23. it("allows you to listen once for an emitted event", function () {
  24. this.callback = function () {};
  25. spyOn(this, 'callback');
  26. converse.once('onInitialized', this.callback);
  27. converse.emit('onInitialized');
  28. expect(this.callback).toHaveBeenCalled();
  29. converse.emit('onInitialized');
  30. expect(this.callback.callCount, 1);
  31. converse.emit('onInitialized');
  32. expect(this.callback.callCount, 1);
  33. });
  34. it("allows you to stop listening or subscribing to an event", function () {
  35. this.callback = function () {};
  36. this.anotherCallback = function () {};
  37. this.neverCalled = function () {};
  38. spyOn(this, 'callback');
  39. spyOn(this, 'anotherCallback');
  40. spyOn(this, 'neverCalled');
  41. converse.on('onInitialized', this.callback);
  42. converse.on('onInitialized', this.anotherCallback);
  43. converse.emit('onInitialized');
  44. expect(this.callback).toHaveBeenCalled();
  45. expect(this.anotherCallback).toHaveBeenCalled();
  46. converse.off('onInitialized', this.callback);
  47. converse.emit('onInitialized');
  48. expect(this.callback.callCount, 1);
  49. expect(this.anotherCallback.callCount, 2);
  50. converse.once('onInitialized', this.neverCalled);
  51. converse.off('onInitialized', this.neverCalled);
  52. converse.emit('onInitialized');
  53. expect(this.callback.callCount, 1);
  54. expect(this.anotherCallback.callCount, 3);
  55. expect(this.neverCalled).not.toHaveBeenCalled();
  56. });
  57. }, converse, mock, utils));
  58. }));