signal-desktop/ts/test-electron/backbone/reliable_trigger_test.ts

139 lines
3.2 KiB
TypeScript
Raw Normal View History

2023-01-03 19:55:46 +00:00
// Copyright 2017 Signal Messenger, LLC
2020-10-30 20:34:04 +00:00
// SPDX-License-Identifier: AGPL-3.0-only
import { assert } from 'chai';
import { Model } from 'backbone';
2018-11-02 18:02:53 +00:00
/* eslint-disable @typescript-eslint/no-explicit-any */
describe('reliable trigger', () => {
2018-11-02 18:02:53 +00:00
describe('trigger', () => {
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', () => {
(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', () => {
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', () => {
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', () => {
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', () => {
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);
});
2018-04-27 21:25:04 +00:00
});
});