/
events.spec.ts
133 lines (116 loc) · 3.95 KB
/
events.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
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import { Context } from '../src'
import { expect } from 'chai'
import { mock } from 'node:test'
import { event, Filter, Session } from './utils'
export function createArray<T>(length: number, create: (index: number) => T) {
return [...new Array(length).keys()].map(create)
}
function setup() {
const root = new Context()
const warn = mock.fn()
root.on('internal/warning', warn)
return { root, warn }
}
describe('Event Listener', () => {
it('context.prototype.on', () => {
const { root } = setup()
const callback = mock.fn()
const dispose = root.on(event, callback)
root.emit(event)
expect(callback.mock.calls).to.have.length(1)
root.emit(event)
expect(callback.mock.calls).to.have.length(2)
expect(dispose()).to.be.ok
root.emit(event)
expect(callback.mock.calls).to.have.length(2)
})
it('context.prototype.once', () => {
const { root } = setup()
const callback = mock.fn()
const dispose = root.once(event, callback)
root.emit(event)
expect(callback.mock.calls).to.have.length(1)
root.emit(event)
expect(callback.mock.calls).to.have.length(1)
expect(dispose()).to.be.not.ok
root.emit(event)
expect(callback.mock.calls).to.have.length(1)
})
it('context.prototype.off', () => {
const { root } = setup()
const callback = mock.fn()
root.on(event, callback)
root.emit(event)
expect(callback.mock.calls).to.have.length(1)
expect(root.off(event, callback)).to.be.ok
root.emit(event)
expect(callback.mock.calls).to.have.length(1)
expect(root.off(event, callback)).to.be.not.ok
})
})
describe('Events Emitter', () => {
it('context.prototype.parallel', async () => {
const { root } = setup()
await root.parallel(event)
const callback = mock.fn()
root.extend(new Filter(true)).on(event, callback)
await root.parallel(event)
expect(callback.mock.calls).to.have.length(1)
await root.parallel(new Session(false), event)
expect(callback.mock.calls).to.have.length(1)
await root.parallel(new Session(true), event)
expect(callback.mock.calls).to.have.length(2)
callback.mock.mockImplementation(() => {
throw new Error('test')
})
await expect(root.parallel(event)).to.be.rejectedWith('test')
})
it('context.prototype.emit', async () => {
const { root } = setup()
root.emit(event)
const callback = mock.fn()
root.extend(new Filter(true)).on(event, callback)
root.emit(event)
expect(callback.mock.calls).to.have.length(1)
root.emit(new Session(false), event)
expect(callback.mock.calls).to.have.length(1)
root.emit(new Session(true), event)
expect(callback.mock.calls).to.have.length(2)
callback.mock.mockImplementation(() => {
throw new Error('test')
})
expect(() => root.emit(event)).to.throw('test')
})
it('context.prototype.serial', async () => {
const { root } = setup()
root.serial(event)
const callback = mock.fn()
root.extend(new Filter(true)).on(event, callback)
root.serial(event)
expect(callback.mock.calls).to.have.length(1)
root.serial(new Session(false), event)
expect(callback.mock.calls).to.have.length(1)
root.serial(new Session(true), event)
expect(callback.mock.calls).to.have.length(2)
callback.mock.mockImplementation(() => {
throw new Error('message')
})
await expect(root.serial(event)).to.be.rejectedWith('message')
})
it('context.prototype.bail', async () => {
const { root } = setup()
root.bail(event)
const callback = mock.fn()
root.extend(new Filter(true)).on(event, callback)
root.bail(event)
expect(callback.mock.calls).to.have.length(1)
root.bail(new Session(false), event)
expect(callback.mock.calls).to.have.length(1)
root.bail(new Session(true), event)
expect(callback.mock.calls).to.have.length(2)
callback.mock.mockImplementation(() => {
throw new Error('message')
})
expect(() => root.bail(event)).to.throw('message')
})
})