-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevent-emitter.spec.ts
40 lines (36 loc) · 1.01 KB
/
event-emitter.spec.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
import EventEmitter from 'events'
describe('EventEmitter', () => {
test('emit and handler', () => {
const handler = jest.fn()
const emitter = new EventEmitter()
emitter.on('sth-happen', handler)
emitter.emit('sth-happen', 'abc')
expect(handler).toBeCalledWith('abc')
emitter.emit('sth-happen')
expect(handler).toHaveBeenCalledTimes(2)
const handler2 = jest.fn()
emitter.once('sth-happen2', handler2)
emitter.emit('sth-happen2')
emitter.emit('sth-happen2')
emitter.emit('sth-happen2')
emitter.emit('sth-happen2')
expect(handler2).toHaveBeenCalledTimes(1)
})
test('extend EventEmitter', () => {
class MyEmitter extends EventEmitter {
constructor(msg: string) {
super()
this.msg = msg
}
msg: string
fire() {
this.emit('fire', this.msg)
}
}
const handler = jest.fn()
const emitter = new MyEmitter('aaa')
emitter.on('fire', handler)
emitter.fire()
expect(handler).toBeCalledWith('aaa')
})
})