This repository has been archived by the owner on Feb 10, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathObserver.test.ts
52 lines (44 loc) · 2.04 KB
/
Observer.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import { Observer } from './Observer';
describe('Observer', () => {
describe('#getName', () => {
it('should return the name of the Observer', () => {
const observer = new Observer('my-observer');
expect(observer.getName()).toBe('my-observer');
});
});
describe('destroy', () => {
it('should remove all data associated with the Observer', () => {
const observer = new Observer('my-observer');
observer.destroy();
// @ts-ignore
expect(observer._name).toBeUndefined();
// @ts-ignore
expect(observer._callbacks).toBeUndefined();
});
});
describe('waitForEvent', () => {
it('should resolve the returned Promise when the specified event is triggered', async () => {
const observer = new Observer('my-observer');
const eventPromise = observer.waitForEvent('my-event', 100);
observer.triggerEvent('my-event');
await eventPromise;
});
it('should reject the returned Promise if the specified event is not triggered within the specified timeout', async () => {
const observer = new Observer('my-observer');
const eventPromise = observer.waitForEvent('my-event', 100);
eventPromise.catch(error => {
expect(error).toBeInstanceOf(Error);
expect(error.message).toMatch(/timed out/i);
});
});
it('should resolve the returned Promise when the specified event is triggered multiple times', async () => {
const observer = new Observer('my-observer');
console.warn = jest.fn();
observer.triggerEvent('my-event');
const eventPromise = observer.waitForEvent('my-event', 100);
observer.triggerEvent('my-event');
await eventPromise;
expect(console.warn).toHaveBeenCalledWith(`Warning! The observer for "${observer.getName()}" did not have anything listening "${'my-event'}"`);
});
});
});