2014-02-12 01:02:25 +01:00
|
|
|
(function (root, factory) {
|
2017-06-19 11:08:57 +02:00
|
|
|
define(["jasmine", "mock", "converse-core", "test-utils"], factory);
|
|
|
|
} (this, function (jasmine, mock, converse, test_utils) {
|
2014-02-12 01:02:25 +01:00
|
|
|
|
2016-12-20 10:30:20 +01:00
|
|
|
return describe("The _converse Event Emitter", function() {
|
2016-11-03 11:01:09 +01:00
|
|
|
|
2016-12-20 10:30:20 +01:00
|
|
|
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-12-20 10:30:20 +01:00
|
|
|
_converse.on('connected', this.callback);
|
|
|
|
_converse.emit('connected');
|
2014-02-12 01:02:25 +01:00
|
|
|
expect(this.callback).toHaveBeenCalled();
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.emit('connected');
|
2017-04-05 11:01:31 +02:00
|
|
|
expect(this.callback.calls.count(), 2);
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.emit('connected');
|
2017-04-05 11:01:31 +02:00
|
|
|
expect(this.callback.calls.count(), 3);
|
2016-11-03 11:01:09 +01:00
|
|
|
}));
|
2014-02-12 01:02:25 +01:00
|
|
|
|
2016-12-20 10:30:20 +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-12-20 10:30:20 +01:00
|
|
|
_converse.once('connected', this.callback);
|
|
|
|
_converse.emit('connected');
|
2014-02-12 01:02:25 +01:00
|
|
|
expect(this.callback).toHaveBeenCalled();
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.emit('connected');
|
2017-04-05 11:01:31 +02:00
|
|
|
expect(this.callback.calls.count(), 1);
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.emit('connected');
|
2017-04-05 11:01:31 +02:00
|
|
|
expect(this.callback.calls.count(), 1);
|
2016-11-03 11:01:09 +01:00
|
|
|
}));
|
2014-02-12 01:02:25 +01:00
|
|
|
|
2016-12-20 10:30:20 +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-12-20 10:30:20 +01:00
|
|
|
_converse.on('connected', this.callback);
|
|
|
|
_converse.on('connected', this.anotherCallback);
|
2014-02-12 01:02:25 +01:00
|
|
|
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.emit('connected');
|
2014-02-12 01:02:25 +01:00
|
|
|
expect(this.callback).toHaveBeenCalled();
|
|
|
|
expect(this.anotherCallback).toHaveBeenCalled();
|
|
|
|
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.off('connected', this.callback);
|
2014-02-12 01:02:25 +01:00
|
|
|
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.emit('connected');
|
2017-04-05 11:01:31 +02:00
|
|
|
expect(this.callback.calls.count(), 1);
|
|
|
|
expect(this.anotherCallback.calls.count(), 2);
|
2014-02-12 01:02:25 +01:00
|
|
|
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.once('connected', this.neverCalled);
|
|
|
|
_converse.off('connected', this.neverCalled);
|
2014-02-12 01:02:25 +01:00
|
|
|
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.emit('connected');
|
2017-04-05 11:01:31 +02:00
|
|
|
expect(this.callback.calls.count(), 1);
|
|
|
|
expect(this.anotherCallback.calls.count(), 3);
|
2014-02-12 01:02:25 +01:00
|
|
|
expect(this.neverCalled).not.toHaveBeenCalled();
|
2016-11-03 11:01:09 +01:00
|
|
|
}));
|
|
|
|
});
|
2014-02-12 01:02:25 +01:00
|
|
|
}));
|