2014-02-12 01:02:25 +01:00
|
|
|
(function (root, factory) {
|
|
|
|
define([
|
|
|
|
"mock",
|
2014-08-08 22:06:01 +02:00
|
|
|
"test_utils"
|
|
|
|
], function (mock, test_utils) {
|
|
|
|
return factory(mock, test_utils);
|
2014-02-12 01:02:25 +01:00
|
|
|
}
|
|
|
|
);
|
2014-08-08 22:06:01 +02:00
|
|
|
} (this, function (mock, test_utils) {
|
|
|
|
return describe("The Converse Event Emitter", $.proxy(function(mock, test_utils) {
|
2014-02-12 01:02:25 +01:00
|
|
|
window.localStorage.clear();
|
2014-06-30 19:55:26 +02:00
|
|
|
window.sessionStorage.clear();
|
2014-02-12 01:02:25 +01:00
|
|
|
|
|
|
|
it("allows you to subscribe to emitted events", function () {
|
|
|
|
this.callback = function () {};
|
|
|
|
spyOn(this, 'callback');
|
2014-07-14 23:29:08 +02:00
|
|
|
converse.on('initialized', this.callback);
|
|
|
|
converse.emit('initialized');
|
2014-02-12 01:02:25 +01:00
|
|
|
expect(this.callback).toHaveBeenCalled();
|
2014-07-14 23:29:08 +02:00
|
|
|
converse.emit('initialized');
|
2014-02-12 01:02:25 +01:00
|
|
|
expect(this.callback.callCount, 2);
|
2014-07-14 23:29:08 +02:00
|
|
|
converse.emit('initialized');
|
2014-02-12 01:02:25 +01:00
|
|
|
expect(this.callback.callCount, 3);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("allows you to listen once for an emitted event", function () {
|
|
|
|
this.callback = function () {};
|
|
|
|
spyOn(this, 'callback');
|
2014-07-14 23:29:08 +02:00
|
|
|
converse.once('initialized', this.callback);
|
|
|
|
converse.emit('initialized');
|
2014-02-12 01:02:25 +01:00
|
|
|
expect(this.callback).toHaveBeenCalled();
|
2014-07-14 23:29:08 +02:00
|
|
|
converse.emit('initialized');
|
2014-02-12 01:02:25 +01:00
|
|
|
expect(this.callback.callCount, 1);
|
2014-07-14 23:29:08 +02:00
|
|
|
converse.emit('initialized');
|
2014-02-12 01:02:25 +01:00
|
|
|
expect(this.callback.callCount, 1);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("allows you to stop listening or subscribing to an event", function () {
|
|
|
|
this.callback = function () {};
|
|
|
|
this.anotherCallback = function () {};
|
|
|
|
this.neverCalled = function () {};
|
|
|
|
|
|
|
|
spyOn(this, 'callback');
|
|
|
|
spyOn(this, 'anotherCallback');
|
|
|
|
spyOn(this, 'neverCalled');
|
2014-07-14 23:29:08 +02:00
|
|
|
converse.on('initialized', this.callback);
|
|
|
|
converse.on('initialized', this.anotherCallback);
|
2014-02-12 01:02:25 +01:00
|
|
|
|
2014-07-14 23:29:08 +02:00
|
|
|
converse.emit('initialized');
|
2014-02-12 01:02:25 +01:00
|
|
|
expect(this.callback).toHaveBeenCalled();
|
|
|
|
expect(this.anotherCallback).toHaveBeenCalled();
|
|
|
|
|
2014-07-14 23:29:08 +02:00
|
|
|
converse.off('initialized', this.callback);
|
2014-02-12 01:02:25 +01:00
|
|
|
|
2014-07-14 23:29:08 +02:00
|
|
|
converse.emit('initialized');
|
2014-02-12 01:02:25 +01:00
|
|
|
expect(this.callback.callCount, 1);
|
|
|
|
expect(this.anotherCallback.callCount, 2);
|
|
|
|
|
2014-07-14 23:29:08 +02:00
|
|
|
converse.once('initialized', this.neverCalled);
|
|
|
|
converse.off('initialized', this.neverCalled);
|
2014-02-12 01:02:25 +01:00
|
|
|
|
2014-07-14 23:29:08 +02:00
|
|
|
converse.emit('initialized');
|
2014-02-12 01:02:25 +01:00
|
|
|
expect(this.callback.callCount, 1);
|
|
|
|
expect(this.anotherCallback.callCount, 3);
|
|
|
|
expect(this.neverCalled).not.toHaveBeenCalled();
|
|
|
|
});
|
2014-08-08 22:06:01 +02:00
|
|
|
}, converse, mock, test_utils));
|
2014-02-12 01:02:25 +01:00
|
|
|
}));
|