signal-desktop/test/reliable_trigger_test.js

159 lines
3.3 KiB
JavaScript
Raw Normal View History

2018-11-02 18:02:53 +00:00
/* global Backbone */
'use strict';
2018-11-02 18:02:53 +00:00
describe('ReliableTrigger', () => {
describe('trigger', () => {
let Model;
let model;
2018-04-27 21:25:04 +00:00
2018-11-02 18:02:53 +00:00
before(() => {
({ Model } = Backbone);
2018-04-27 21:25:04 +00:00
});
2018-11-02 18:02:53 +00:00
beforeEach(() => {
2018-04-27 21:25:04 +00:00
model = new Model();
});
2018-11-02 18:02:53 +00:00
it('returns successfully if this._events is falsey', () => {
2018-04-27 21:25:04 +00:00
model._events = null;
model.trigger('click');
});
2018-11-02 18:02:53 +00:00
it('handles map of events to trigger', () => {
let a = 0;
let b = 0;
model.on('a', arg => {
2018-04-27 21:25:04 +00:00
a = arg;
});
2018-11-02 18:02:53 +00:00
model.on('b', arg => {
2018-04-27 21:25:04 +00:00
b = arg;
});
model.trigger({
a: 1,
b: 2,
});
assert.strictEqual(a, 1);
assert.strictEqual(b, 2);
});
2018-11-02 18:02:53 +00:00
it('handles space-separated list of events to trigger', () => {
let a = false;
let b = false;
model.on('a', () => {
2018-04-27 21:25:04 +00:00
a = true;
});
2018-11-02 18:02:53 +00:00
model.on('b', () => {
2018-04-27 21:25:04 +00:00
b = true;
});
model.trigger('a b');
assert.strictEqual(a, true);
assert.strictEqual(b, true);
});
2018-11-02 18:02:53 +00:00
it('calls all clients registered for "all" event', () => {
let count = 0;
model.on('all', () => {
2018-04-27 21:25:04 +00:00
count += 1;
});
model.trigger('left');
model.trigger('right');
assert.strictEqual(count, 2);
});
2018-11-02 18:02:53 +00:00
it('calls all clients registered for target event', () => {
let a = false;
let b = false;
model.on('event', () => {
2018-04-27 21:25:04 +00:00
a = true;
});
2018-11-02 18:02:53 +00:00
model.on('event', () => {
2018-04-27 21:25:04 +00:00
b = true;
});
model.trigger('event');
assert.strictEqual(a, true);
assert.strictEqual(b, true);
});
2018-11-02 18:02:53 +00:00
it('successfully returns and calls all clients even if first failed', () => {
let a = false;
let b = false;
model.on('event', () => {
2018-04-27 21:25:04 +00:00
a = true;
throw new Error('a is set, but exception is thrown');
});
2018-11-02 18:02:53 +00:00
model.on('event', () => {
2018-04-27 21:25:04 +00:00
b = true;
});
model.trigger('event');
assert.strictEqual(a, true);
assert.strictEqual(b, true);
});
2018-11-02 18:02:53 +00:00
it('calls clients with no args', () => {
let called = false;
model.on('event', () => {
2018-04-27 21:25:04 +00:00
called = true;
});
model.trigger('event');
assert.strictEqual(called, true);
});
2018-11-02 18:02:53 +00:00
it('calls clients with 1 arg', () => {
let args;
model.on('event', (...eventArgs) => {
args = eventArgs;
2018-04-27 21:25:04 +00:00
});
model.trigger('event', 1);
assert.strictEqual(args[0], 1);
});
2018-11-02 18:02:53 +00:00
it('calls clients with 2 args', () => {
let args;
model.on('event', (...eventArgs) => {
args = eventArgs;
2018-04-27 21:25:04 +00:00
});
model.trigger('event', 1, 2);
assert.strictEqual(args[0], 1);
assert.strictEqual(args[1], 2);
});
2018-11-02 18:02:53 +00:00
it('calls clients with 3 args', () => {
let args;
model.on('event', (...eventArgs) => {
args = eventArgs;
2018-04-27 21:25:04 +00:00
});
model.trigger('event', 1, 2, 3);
assert.strictEqual(args[0], 1);
assert.strictEqual(args[1], 2);
assert.strictEqual(args[2], 3);
});
2018-11-02 18:02:53 +00:00
it('calls clients with 4+ args', () => {
let args;
model.on('event', (...eventArgs) => {
args = eventArgs;
2018-04-27 21:25:04 +00:00
});
model.trigger('event', 1, 2, 3, 4);
assert.strictEqual(args[0], 1);
assert.strictEqual(args[1], 2);
assert.strictEqual(args[2], 3);
assert.strictEqual(args[3], 4);
});
2018-04-27 21:25:04 +00:00
});
});