/
events.test.js
47 lines (33 loc) · 1.17 KB
/
events.test.js
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
import EventObserver from '../src/events';
describe('Event Observer', () => {
jest.spyOn(console, 'log');
afterEach(() => {
jest.clearAllMocks();
});
test('can subscribe to and broadcast an event', () => {
const observer = new EventObserver();
const name = 'my-thing';
const func = data => {
console.log('hello', data);
};
const myData = { awesome: true };
expect(observer.observers.length).toBe(0);
observer.subscribe(name, func);
observer.subscribe(`${name}-other`, func);
expect(observer.observers.length).toBe(2);
expect(console.log.mock.calls.length).toBe(0);
observer.broadcast(name, myData);
expect(console.log.mock.calls.length).toBe(1);
expect(console.log.mock.calls[0][0]).toBe('hello');
expect(console.log.mock.calls[0][1]).toBe(myData);
});
test('can unsubscribe to an event', () => {
const observer = new EventObserver(),
myType = 'hello';
expect(observer.observers.length).toBe(0);
observer.subscribe(myType, () => console.log('awesome'));
expect(observer.observers.length).toBe(1);
observer.unsubscribe(myType);
expect(observer.observers.length).toBe(0);
});
});