eventemitter.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. (function (root, factory) {
  2. define(["jasmine", "mock", "test-utils"], factory);
  3. } (this, function (jasmine, mock, test_utils) {
  4. return describe("The _converse Event Emitter", function() {
  5. it("allows you to subscribe to emitted events", mock.initConverse(function (_converse) {
  6. this.callback = function () {};
  7. spyOn(this, 'callback');
  8. _converse.on('connected', this.callback);
  9. _converse.emit('connected');
  10. expect(this.callback).toHaveBeenCalled();
  11. _converse.emit('connected');
  12. expect(this.callback.calls.count(), 2);
  13. _converse.emit('connected');
  14. expect(this.callback.calls.count(), 3);
  15. }));
  16. it("allows you to listen once for an emitted event", mock.initConverse(function (_converse) {
  17. this.callback = function () {};
  18. spyOn(this, 'callback');
  19. _converse.once('connected', this.callback);
  20. _converse.emit('connected');
  21. expect(this.callback).toHaveBeenCalled();
  22. _converse.emit('connected');
  23. expect(this.callback.calls.count(), 1);
  24. _converse.emit('connected');
  25. expect(this.callback.calls.count(), 1);
  26. }));
  27. it("allows you to stop listening or subscribing to an event", mock.initConverse(function (_converse) {
  28. this.callback = function () {};
  29. this.anotherCallback = function () {};
  30. this.neverCalled = function () {};
  31. spyOn(this, 'callback');
  32. spyOn(this, 'anotherCallback');
  33. spyOn(this, 'neverCalled');
  34. _converse.on('connected', this.callback);
  35. _converse.on('connected', this.anotherCallback);
  36. _converse.emit('connected');
  37. expect(this.callback).toHaveBeenCalled();
  38. expect(this.anotherCallback).toHaveBeenCalled();
  39. _converse.off('connected', this.callback);
  40. _converse.emit('connected');
  41. expect(this.callback.calls.count(), 1);
  42. expect(this.anotherCallback.calls.count(), 2);
  43. _converse.once('connected', this.neverCalled);
  44. _converse.off('connected', this.neverCalled);
  45. _converse.emit('connected');
  46. expect(this.callback.calls.count(), 1);
  47. expect(this.anotherCallback.calls.count(), 3);
  48. expect(this.neverCalled).not.toHaveBeenCalled();
  49. }));
  50. });
  51. }));