forked from vercel/next.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEventEmitter.test.js
More file actions
82 lines (60 loc) · 1.87 KB
/
Copy pathEventEmitter.test.js
File metadata and controls
82 lines (60 loc) · 1.87 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/* global describe, it, expect */
import EventEmitter from '../../dist/lib/EventEmitter'
describe('EventEmitter', () => {
describe('With listeners', () => {
it('should listen to a event', (done) => {
const ev = new EventEmitter()
ev.on('sample', done)
ev.emit('sample')
})
it('should listen to multiple listeners', () => {
const ev = new EventEmitter()
let cnt = 0
ev.on('sample', () => { cnt += 1 })
ev.on('sample', () => { cnt += 1 })
ev.emit('sample')
expect(cnt).toBe(2)
})
it('should listen to multiple events', () => {
const ev = new EventEmitter()
const data = []
const cb = (name) => { data.push(name) }
ev.on('sample1', cb)
ev.on('sample2', cb)
ev.emit('sample1', 'one')
ev.emit('sample2', 'two')
expect(data).toEqual(['one', 'two'])
})
it('should support multiple arguments', () => {
const ev = new EventEmitter()
let data
ev.on('sample', (...args) => { data = args })
ev.emit('sample', 'one', 'two')
expect(data).toEqual(['one', 'two'])
})
it('should possible to stop listening an event', () => {
const ev = new EventEmitter()
let cnt = 0
const cb = () => { cnt += 1 }
ev.on('sample', cb)
ev.emit('sample')
expect(cnt).toBe(1)
ev.off('sample', cb)
ev.emit('sample')
expect(cnt).toBe(1)
})
it('should throw when try to add the same listener multiple times', () => {
const ev = new EventEmitter()
const cb = () => {}
ev.on('sample', cb)
const run = () => ev.on('sample', cb)
expect(run).toThrow(/The listener already exising in event: sample/)
})
})
describe('Without a listener', () => {
it('should not fail to emit', () => {
const ev = new EventEmitter()
ev.emit('aaaa', 10, 20)
})
})
})