2014-02-12 01:02:25 +01:00
|
|
|
(function (root, factory) {
|
2016-11-30 13:18:58 +01:00
|
|
|
define(["mock", "converse-api", "test_utils"], factory);
|
|
|
|
} (this, function (mock, converse_api, test_utils) {
|
2014-02-12 01:02:25 +01:00
|
|
|
|
2016-11-03 11:01:09 +01:00
|
|
|
return describe("The Converse Event Emitter", function() {
|
|
|
|
|
|
|
|
it("allows you to subscribe to emitted events", mock.initConverse(function (converse) {
|
2014-02-12 01:02:25 +01:00
|
|
|
this.callback = function () {};
|
|
|
|
spyOn(this, 'callback');
|
2016-09-23 08:24:19 +02:00
|
|
|
converse.on('connected', this.callback);
|
|
|
|
converse.emit('connected');
|
2014-02-12 01:02:25 +01:00
|
|
|
expect(this.callback).toHaveBeenCalled();
|
2016-09-23 08:24:19 +02:00
|
|
|
converse.emit('connected');
|
2014-02-12 01:02:25 +01:00
|
|
|
expect(this.callback.callCount, 2);
|
2016-09-23 08:24:19 +02:00
|
|
|
converse.emit('connected');
|
2014-02-12 01:02:25 +01:00
|
|
|
expect(this.callback.callCount, 3);
|
2016-11-03 11:01:09 +01:00
|
|
|
}));
|
2014-02-12 01:02:25 +01:00
|
|
|
|
2016-11-03 11:01:09 +01:00
|
|
|
it("allows you to listen once for an emitted event", mock.initConverse(function (converse) {
|
2014-02-12 01:02:25 +01:00
|
|
|
this.callback = function () {};
|
|
|
|
spyOn(this, 'callback');
|
2016-09-23 08:24:19 +02:00
|
|
|
converse.once('connected', this.callback);
|
|
|
|
converse.emit('connected');
|
2014-02-12 01:02:25 +01:00
|
|
|
expect(this.callback).toHaveBeenCalled();
|
2016-09-23 08:24:19 +02:00
|
|
|
converse.emit('connected');
|
2014-02-12 01:02:25 +01:00
|
|
|
expect(this.callback.callCount, 1);
|
2016-09-23 08:24:19 +02:00
|
|
|
converse.emit('connected');
|
2014-02-12 01:02:25 +01:00
|
|
|
expect(this.callback.callCount, 1);
|
2016-11-03 11:01:09 +01:00
|
|
|
}));
|
2014-02-12 01:02:25 +01:00
|
|
|
|
2016-11-03 11:01:09 +01:00
|
|
|
it("allows you to stop listening or subscribing to an event", mock.initConverse(function (converse) {
|
2014-02-12 01:02:25 +01:00
|
|
|
this.callback = function () {};
|
|
|
|
this.anotherCallback = function () {};
|
|
|
|
this.neverCalled = function () {};
|
|
|
|
|
|
|
|
spyOn(this, 'callback');
|
|
|
|
spyOn(this, 'anotherCallback');
|
|
|
|
spyOn(this, 'neverCalled');
|
2016-09-23 08:24:19 +02:00
|
|
|
converse.on('connected', this.callback);
|
|
|
|
converse.on('connected', this.anotherCallback);
|
2014-02-12 01:02:25 +01:00
|
|
|
|
2016-09-23 08:24:19 +02:00
|
|
|
converse.emit('connected');
|
2014-02-12 01:02:25 +01:00
|
|
|
expect(this.callback).toHaveBeenCalled();
|
|
|
|
expect(this.anotherCallback).toHaveBeenCalled();
|
|
|
|
|
2016-09-23 08:24:19 +02:00
|
|
|
converse.off('connected', this.callback);
|
2014-02-12 01:02:25 +01:00
|
|
|
|
2016-09-23 08:24:19 +02:00
|
|
|
converse.emit('connected');
|
2014-02-12 01:02:25 +01:00
|
|
|
expect(this.callback.callCount, 1);
|
|
|
|
expect(this.anotherCallback.callCount, 2);
|
|
|
|
|
2016-09-23 08:24:19 +02:00
|
|
|
converse.once('connected', this.neverCalled);
|
|
|
|
converse.off('connected', this.neverCalled);
|
2014-02-12 01:02:25 +01:00
|
|
|
|
2016-09-23 08:24:19 +02:00
|
|
|
converse.emit('connected');
|
2014-02-12 01:02:25 +01:00
|
|
|
expect(this.callback.callCount, 1);
|
|
|
|
expect(this.anotherCallback.callCount, 3);
|
|
|
|
expect(this.neverCalled).not.toHaveBeenCalled();
|
2016-11-03 11:01:09 +01:00
|
|
|
}));
|
|
|
|
});
|
2014-02-12 01:02:25 +01:00
|
|
|
}));
|