2022-06-01 19:54:51 +00:00
|
|
|
// Copyright 2017-2022 Signal Messenger, LLC
|
2020-10-30 20:34:04 +00:00
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2022-06-01 19:54:51 +00:00
|
|
|
import { assert } from 'chai';
|
|
|
|
import { Model } from 'backbone';
|
2018-11-02 18:02:53 +00:00
|
|
|
|
2022-06-01 19:54:51 +00:00
|
|
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
2017-07-19 18:59:08 +00:00
|
|
|
|
2022-06-01 19:54:51 +00:00
|
|
|
describe('reliable trigger', () => {
|
2018-11-02 18:02:53 +00:00
|
|
|
describe('trigger', () => {
|
2022-06-01 19:54:51 +00:00
|
|
|
let model: Model;
|
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', () => {
|
2022-06-01 19:54:51 +00:00
|
|
|
(model as any)._events = null;
|
2018-04-27 21:25:04 +00:00
|
|
|
model.trigger('click');
|
|
|
|
});
|
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', () => {
|
2022-06-01 19:54:51 +00:00
|
|
|
let args: Array<unknown> = [];
|
2018-11-02 18:02:53 +00:00
|
|
|
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', () => {
|
2022-06-01 19:54:51 +00:00
|
|
|
let args: Array<unknown> = [];
|
2018-11-02 18:02:53 +00:00
|
|
|
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', () => {
|
2022-06-01 19:54:51 +00:00
|
|
|
let args: Array<unknown> = [];
|
2018-11-02 18:02:53 +00:00
|
|
|
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', () => {
|
2022-06-01 19:54:51 +00:00
|
|
|
let args: Array<unknown> = [];
|
2018-11-02 18:02:53 +00:00
|
|
|
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);
|
2017-07-19 18:59:08 +00:00
|
|
|
});
|
2018-04-27 21:25:04 +00:00
|
|
|
});
|
2017-07-19 18:59:08 +00:00
|
|
|
});
|