-
Notifications
You must be signed in to change notification settings - Fork 0
/
Eventful.test.ts
215 lines (155 loc) · 4.19 KB
/
Eventful.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
import {Eventful} from './Eventful.js'
// TODO move type def to @lume/cli, map @types/jest's `expect` type into the
// global env.
declare global {
function expect(...args: any[]): any
}
let eventCount = 0
let eventCount2 = 0
describe('Eventful', () => {
describe('provides an event pattern', () => {
it('triggers an event handler based on event names', () => {
const MyClass = Eventful()
const o = new MyClass()
const eventHandler = () => {
eventCount += 1
}
const eventHandler2 = () => {
eventCount2 += 1
}
o.on('foo', eventHandler)
o.on('bar', eventHandler2)
o.emit('foo')
expect(eventCount).toBe(1)
expect(eventCount2).toBe(0)
o.emit('foo')
expect(eventCount).toBe(2)
expect(eventCount2).toBe(0)
o.emit('foo')
expect(eventCount).toBe(3)
expect(eventCount2).toBe(0)
o.emit('foo')
o.emit('foo')
o.emit('foo')
expect(eventCount).toBe(6)
expect(eventCount2).toBe(0)
o.emit('bar')
expect(eventCount).toBe(6)
expect(eventCount2).toBe(1)
o.emit('bar')
expect(eventCount).toBe(6)
expect(eventCount2).toBe(2)
o.emit('bar')
expect(eventCount).toBe(6)
expect(eventCount2).toBe(3)
o.off('foo', eventHandler)
o.emit('foo')
o.emit('foo')
o.emit('foo')
expect(eventCount).toBe(6)
expect(eventCount2).toBe(3)
o.emit('bar')
o.emit('bar')
o.emit('bar')
expect(eventCount).toBe(6)
expect(eventCount2).toBe(6)
o.off('bar', eventHandler)
o.emit('bar')
o.emit('bar')
o.emit('bar')
expect(eventCount).toBe(6)
expect(eventCount2).toBe(9)
o.off('bar', eventHandler2)
o.emit('bar')
o.emit('bar')
o.emit('bar')
expect(eventCount).toBe(6)
expect(eventCount2).toBe(9)
})
it('passes event payloads to event handlers', () => {
class MyClass extends Eventful() {}
const o = new MyClass()
let thePayload: string | number | undefined
let thePayload2: number | undefined
const handler = (payload: any) => {
thePayload = payload
}
const handler2 = (payload: any) => {
thePayload2 = payload
}
o.on('foo', handler)
o.on('bar', handler2)
o.emit('foo', 56)
expect(thePayload).toBe(56)
expect(thePayload2).toBe(undefined)
o.emit('bar', 42)
expect(thePayload).toBe(56)
expect(thePayload2).toBe(42)
o.off('bar', handler2)
o.emit('foo', 'oh yeah')
o.emit('bar', 123)
expect(thePayload).toBe('oh yeah')
expect(thePayload2).toBe(42)
})
it('allows callbacks to be paired with contexts with which to be called', () => {
class MyClass extends Eventful() {}
const o = new MyClass()
let obj = {n: 0}
const o1 = {}
const o2 = {}
const callback = function (this: any, data: {n: number}) {
// the first two calls
if (data.n <= 1) {
if (data.n === 0) {
data.n++
expect(this).toBe(o1)
} else {
data.n++
expect(this).toBe(o2)
}
}
// the 3rd and 4th calls
else {
data.n++
expect(this).toBe(o2)
}
}
o.on('foo', callback, o1)
o.on('foo', callback, o2)
o.emit('foo', obj)
expect(obj.n).toBe(2)
o.off('foo', callback, o1)
o.emit('foo', obj)
o.emit('foo', obj)
expect(obj.n).toBe(4)
o.off('foo', callback, o2)
o.emit('foo', obj)
o.emit('foo', obj)
o.emit('foo', obj)
expect(obj.n).toBe(4)
})
it('allows us to check objects are instances of the mixin class or composed classes', () => {
const MyClass = Eventful()
class OtherClass extends Eventful() {}
class AnotherClass extends Eventful(Array) {}
let o = new MyClass()
expect(o).toBeInstanceOf(Eventful)
expect(o).toBeInstanceOf(MyClass)
expect(o).not.toBeInstanceOf(OtherClass)
expect(o).not.toBeInstanceOf(AnotherClass)
expect(o).not.toBeInstanceOf(Array)
o = new OtherClass()
expect(o).toBeInstanceOf(Eventful)
expect(o).not.toBeInstanceOf(MyClass)
expect(o).toBeInstanceOf(OtherClass)
expect(o).not.toBeInstanceOf(AnotherClass)
expect(o).not.toBeInstanceOf(Array)
o = new AnotherClass()
expect(o).toBeInstanceOf(Eventful)
expect(o).not.toBeInstanceOf(MyClass)
expect(o).not.toBeInstanceOf(OtherClass)
expect(o).toBeInstanceOf(AnotherClass)
expect(o).toBeInstanceOf(Array)
})
})
})