-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevents.test.ts
37 lines (34 loc) · 1.03 KB
/
events.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
import { Events } from './events.js';
describe('Events', () => {
test('emit returns true or false', () => {
const events = new Events();
expect(events.emit('event')).toBe(false);
const off = events.on('event', () => {});
expect(events.emit('event')).toBe(true);
off();
expect(events.emit('event')).toBe(false);
});
test('emit calls listeners', () => {
const events = new Events();
const a = vi.fn();
const b = vi.fn();
const off = events.on('event', a);
events.on('event', b);
events.emit('event', 1, 2);
expect(a).toHaveBeenLastCalledWith(1, 2);
expect(b).toHaveBeenLastCalledWith(1, 2);
a.mockClear();
b.mockClear();
off();
events.emit('event', 'a', 'b');
expect(a).not.toHaveBeenCalled();
expect(b).toHaveBeenLastCalledWith('a', 'b');
});
test('emit only calls listeners for the emitted event', () => {
const events = new Events();
const a = vi.fn();
events.on('event', a);
events.emit('other');
expect(a).not.toHaveBeenCalled();
});
});