-
Notifications
You must be signed in to change notification settings - Fork 463
Expand file tree
/
Copy pathevents.spec.ts
More file actions
195 lines (173 loc) · 6.14 KB
/
Copy pathevents.spec.ts
File metadata and controls
195 lines (173 loc) · 6.14 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
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import { Context, Events } from '../src'
import { expect, describe, it } from 'vitest'
import { mock } from 'node:test'
import { event, symbolEvent, 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.logger as any).warn = warn
return { root, warn }
}
describe('Events', () => {
it('ctx.on()', async () => {
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)
dispose()
root.emit(event)
expect(callback.mock.calls).to.have.length(2)
})
it('ctx.once()', async () => {
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)
dispose()
root.emit(event)
expect(callback.mock.calls).to.have.length(1)
})
it('symbol events', async () => {
const { root } = setup()
const callback = mock.fn()
const dispose = root.on(symbolEvent, callback)
root.emit(symbolEvent)
expect(callback.mock.calls).to.have.length(1)
root.emit(symbolEvent)
expect(callback.mock.calls).to.have.length(2)
dispose()
root.emit(symbolEvent)
expect(callback.mock.calls).to.have.length(2)
})
it('symbol events dispatch across modes', async () => {
const { root } = setup()
const callback = mock.fn()
root.on(symbolEvent, callback)
root.emit(symbolEvent)
await root.parallel(symbolEvent)
await root.serial(symbolEvent)
root.bail(symbolEvent)
expect(callback.mock.calls).to.have.length(4)
})
it('prototype-named events', async () => {
const { root } = setup()
const callback = mock.fn()
const dispose = root.on('__proto__', callback)
root.emit('__proto__')
expect(callback.mock.calls).to.have.length(1)
dispose()
root.emit('__proto__')
expect(callback.mock.calls).to.have.length(1)
// the last listener disposal should release the event bucket
expect(root.events._hooks['__proto__']).to.have.length(0)
})
it('ctx.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)
// a rejecting listener must not short-circuit the others
let settled = false
const dispose = root.on(event, async () => {
await new Promise(resolve => setTimeout(resolve, 0))
settled = true
throw new Error('async')
})
callback.mock.mockImplementation(() => {
throw new Error('test')
})
const error = await root.parallel(event).catch(e => e)
expect(error).to.be.instanceof(AggregateError)
expect(error.errors.map((e: Error) => e.message)).to.have.members(['test', 'async'])
expect(settled).to.be.true
dispose()
})
it('ctx.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('ctx.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)).rejects.toThrow('message')
})
it('ctx.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')
})
it('ctx.waterfall()', async () => {
const { root } = setup()
const cb1 = mock.fn<Events['test/waterfall']>((value, next) => value + next())
root.on('test/waterfall', cb1)
const cb2 = mock.fn<Events['test/waterfall']>((value, next) => value + next())
root.on('test/waterfall', cb2)
expect(root.waterfall('test/waterfall', 1, () => 2)).to.equal(4)
expect(cb1.mock.calls).to.have.length(1)
expect(cb2.mock.calls).to.have.length(1)
cb1.mock.resetCalls()
cb2.mock.resetCalls()
const cb3 = mock.fn<Events['test/waterfall']>((value, next) => value)
root.on('test/waterfall', cb3)
const cb4 = mock.fn<Events['test/waterfall']>((value, next) => value + next())
root.on('test/waterfall', cb4)
expect(root.waterfall('test/waterfall', 1, () => 2)).to.equal(3)
expect(cb1.mock.calls).to.have.length(1)
expect(cb2.mock.calls).to.have.length(1)
expect(cb3.mock.calls).to.have.length(1)
expect(cb4.mock.calls).to.have.length(0)
cb1.mock.resetCalls()
cb2.mock.resetCalls()
cb3.mock.resetCalls()
cb4.mock.resetCalls()
})
})