-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
175 lines (142 loc) · 3.72 KB
/
test.js
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
// @ts-check
const test = require('ava').default
const aggregate = require('./index.js').aggregate
const delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms))
const setupTest = () => {
const emitter = new (require('events').EventEmitter)()
return {
emitter,
add: (file, stat) => emitter.emit('add', file, stat),
change: (file, stat) => emitter.emit('change', file, stat),
unlink: (file, stat) => emitter.emit('unlink', file, stat),
}
}
function flow(handlers) {
let calls = 0
return (...args) => {
const handler = handlers[calls++]
if (handler) return handler(...args)
throw Error(
`Callback called ${calls} times but expected only ${handlers.length}`,
)
}
}
test.cb('should invoke callback upon adding', (t) => {
const { emitter, add, change, unlink } = setupTest()
t.plan(3)
aggregate(emitter, (changes) => {
t.deepEqual(changes.added, [['a', {}]])
t.deepEqual(changes.changed, [])
t.deepEqual(changes.unlinked, [])
t.end()
})
add('a', {})
})
test.cb('should invoke callback upon changing', (t) => {
const { emitter, add, change, unlink } = setupTest()
t.plan(3)
aggregate(emitter, (changes) => {
t.deepEqual(changes.added, [])
t.deepEqual(changes.changed, [['a', {}]])
t.deepEqual(changes.unlinked, [])
t.end()
})
change('a', {})
})
test.cb('should invoke callback upon unlinking', (t) => {
const { emitter, add, change, unlink } = setupTest()
t.plan(3)
aggregate(emitter, (changes) => {
t.deepEqual(changes.added, [])
t.deepEqual(changes.changed, [])
t.deepEqual(changes.unlinked, [['a', {}]])
t.end()
})
unlink('a', {})
})
test.cb(
'should allow passing setup callback and aggregate events until it completes',
(t) => {
const { emitter, add, change, unlink } = setupTest()
let setupCalls = 0
t.plan(6)
aggregate(
emitter,
(changes) => {
t.deepEqual(changes.added, [['a', {}]])
t.deepEqual(changes.changed, [['b', {}]])
t.deepEqual(changes.unlinked, [['c', {}]])
t.end()
},
() => {
t.is(++setupCalls, 1)
return Promise.all([
delay(100).then(() => {
t.pass('setup complete')
unlink('c', {})
}),
delay(50).then(() => {
t.pass('change during setup')
change('b', {})
}),
])
},
)
add('a', {})
},
)
test.cb('should aggregate events while callback is executing', (t) => {
const { emitter, add, change, unlink } = setupTest()
t.plan(9)
aggregate(
emitter,
flow([
async (changes) => {
add('a')
t.deepEqual(changes.added, [['a']])
t.deepEqual(changes.changed, [])
t.deepEqual(changes.unlinked, [])
await delay(50)
add('b')
},
async (changes) => {
change('a')
t.deepEqual(changes.added, [['a'], ['b']])
t.deepEqual(changes.changed, [])
t.deepEqual(changes.unlinked, [])
await delay(1)
unlink('b')
},
(changes) => {
t.deepEqual(changes.added, [])
t.deepEqual(changes.changed, [['a']])
t.deepEqual(changes.unlinked, [['b']])
t.end()
},
]),
)
add('a')
})
test.cb(
'should "flatten" out opposite events in single aggregation frame',
(t) => {
const { emitter, add, change, unlink } = setupTest()
t.plan(3)
aggregate(emitter, (changes) => {
t.deepEqual(changes.added, [['c']])
t.deepEqual(changes.changed, [['a']])
t.deepEqual(changes.unlinked, [['d']])
t.end()
})
unlink('a')
add('a')
add('b')
unlink('b')
add('c')
change('c')
unlink('d')
add('d')
change('d')
unlink('d')
},
)