-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.ts
225 lines (209 loc) · 6.24 KB
/
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
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/* eslint-disable max-nested-callbacks */
import { describe, it, beforeEach } from 'mocha'
import { expect } from 'chai'
import createUse from './index'
type Emitter<T> = import('./index').Emitter<T>
interface EmitMap {
foo: [any]
[key: string]: any[]
}
describe('createUse', () => {
describe('when given object', () => {
it('calls `handleObject` for each key/value of the object', () => {
const handleCalls: { key: string | number; value: any }[] = []
const use = createUse<EmitMap, any>({
handleObject(emitter: Emitter<EmitMap>, key: string | number, value: any) {
handleCalls.push({ key, value })
},
})
/* eslint-disable id-length, no-empty-function */
const a = () => {}
const b = () => {}
const c = () => {}
/* eslint-enable */
use({ a, b, c })
expect(handleCalls).to.deep.equal([
{ key: 'a', value: a },
{ key: 'b', value: b },
{ key: 'c', value: c },
])
})
})
describe('when given a function', () => {
let use = createUse<EmitMap, any>({
handleObject(emitter: Emitter<EmitMap>, key: string | number, value: any) {},
})
beforeEach(() => {
use = createUse<EmitMap, any>({
handleObject(emitter: Emitter<EmitMap>, key: string | number, value: any) {},
})
})
it('immediately calls the given callback', done => {
use(() => done())
})
it('calls optional `handleReturn` with the functions return value', () => {
let handledValue: any
const use = createUse<EmitMap, any>({
handleObject(emitter: Emitter<EmitMap>, key: string | number, value: any) {},
handleReturn(emitter: Emitter<EmitMap>, value: any) {
handledValue = value
},
})
const returnValue = {}
use(emitter => returnValue)
expect(handledValue).to.equal(returnValue)
})
describe('on', () => {
it('can be used to listen to events from other plugins', () => {
let value = null
let listened = 0
use(({ on }) => {
on('foo', emitted => {
value = emitted
listened += 1
})
})
const emitValue = {}
use(({ emit }) => {
emit('foo', emitValue)
})
expect(value).to.equal(emitValue)
expect(listened).to.equal(1)
})
it('backfills events from previously loaded plugins', () => {
let listened = 0
const values: any[] = []
use(({ emit }) => {
emit('foo', 1)
emit('foo', 2)
emit('foo', 3)
})
use(({ on }) => {
on('foo', value => {
listened += 1
values.push(value)
})
})
expect(listened).to.equal(3)
expect(values).to.deep.equal([1, 2, 3])
})
it('cannot listen to its own events', () => {
let listened = 0
use(({ on, emit }) => {
on('foo', () => {
listened += 1
})
emit('foo', 1)
emit('foo', 2)
emit('foo', 3)
emit('foo', 4)
})
expect(listened).to.equal(0)
})
it('cannot listen to its own events even with multiple listeners', () => {
let listened = 0
use(({ on, emit }) => {
on('foo', () => {
listened += 1
})
on('foo', () => {
listened += 1
})
emit('foo', 1)
emit('foo', 2)
emit('foo', 3)
emit('foo', 4)
})
expect(listened).to.equal(0)
})
})
describe('off', () => {
it('stops a listener being called for an event', () => {
let listened = 0
use(({ on, off }) => {
const listener = () => {
listened += 1
off('foo', listener)
}
on('foo', listener)
})
use(({ emit }) => {
emit('foo', 1)
emit('foo', 2)
emit('foo', 3)
emit('foo', 4)
})
expect(listened).to.equal(1)
})
it('does not throw even when trying to turn off an event that has not been added', () => {
expect(() => {
use(({ off }) => {
// @ts-ignore
off('foo')
off('foo', () => {})
})
}).to.not.throw()
})
})
describe('emit', () => {
it('calls handleEmit if available, for each emitted event', () => {
const emitted: { name: any; args: any }[] = []
const use = createUse<EmitMap, any>({
handleObject(emitter: Emitter<EmitMap>, key: string | number, value: any) {},
handleEmit<K extends keyof EmitMap>(emitter: Emitter<EmitMap>, name: K, ...args: EmitMap[K]): boolean {
emitted.push({ name, args })
return false
},
})
use(({ emit }) => {
emit('foo', 1)
emit('foo', 2)
emit('foo', 3)
})
expect(emitted).to.deep.equal([
{ name: 'foo', args: [1] },
{ name: 'foo', args: [2] },
{ name: 'foo', args: [3] },
])
})
it('does not emit if handleEmit returned true', () => {
const use = createUse<EmitMap, any>({
handleObject(emitter: Emitter<EmitMap>, key: string | number, value: any) {},
handleEmit<K extends keyof EmitMap>(emitter: Emitter<EmitMap>, name: K, ...args: EmitMap[K]): boolean {
if (args[0] === 2) return true
return false
},
})
const values: any[] = []
use(({ on }) => {
on('foo', (n: any) => values.push(n))
})
use(({ emit }) => {
emit('foo', 1)
emit('foo', 2)
emit('foo', 3)
})
expect(values).to.deep.equal([1, 3])
})
})
describe('count', () => {
it('gets the count of all registered listeners', () => {
use(({ on }) => {
on('foo', () => {})
})
use(({ on }) => {
on('foo', () => {})
})
let fooCount = NaN
let barCount = NaN
use(({ on, count }) => {
on('foo', () => {})
fooCount = count('foo')
barCount = count('bar')
})
expect(fooCount).to.equal(3)
expect(barCount).to.equal(0)
})
})
})
})