-
Notifications
You must be signed in to change notification settings - Fork 3
/
test.js
210 lines (165 loc) · 5.09 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
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
import test from 'ava';
import sinon from 'sinon';
import batcher from './';
test('should throw if created without a method as first argument', t => {
t.plan(3);
t.throws(() => batcher(), TypeError, 'The first argument should be a function');
t.throws(() => batcher(1), TypeError, 'The first argument should be a function');
t.notThrows(() => batcher(function () {}));
});
test('should not throw if no parameters are passed to the batched call', t => {
t.plan(3);
const batch = batcher(function () {});
t.notThrows(() => batch()); // No callback, e.g. batch MobX action (synchronous).
t.notThrows(() => batch(undefined, () => 123));
t.notThrows(() => batch({}, () => 123));
});
test.cb('should batch multiple function calls in a given interval - default is zero', t => {
t.plan(2);
const myMethod = sinon.spy();
const batch = batcher(myMethod);
const callback = () => undefined;
batch({id: 1}, callback);
batch({id: 2}, callback);
batch({id: 3}, callback);
batch({id: 4}, callback);
batch({id: 5}, callback);
setTimeout(() => {
t.true(myMethod.calledOnce);
t.true(myMethod.calledWith([{id: 1}, {id: 2}, {id: 3}, {id: 4}, {id: 5}]));
t.end();
});
});
test.cb('should not batch function calls out of the interval', t => {
t.plan(3);
const myMethod = sinon.spy();
const batch = batcher(myMethod);
const callback = () => undefined;
batch({id: 1}, callback);
batch({id: 2}, callback);
batch({id: 3}, callback);
batch({id: 4}, callback);
batch({id: 5}, callback);
setTimeout(() => {
batch({id: 6}, callback);
setTimeout(() => {
t.true(myMethod.calledTwice);
t.true(myMethod.firstCall.calledWith([{id: 1}, {id: 2}, {id: 3}, {id: 4}, {id: 5}]));
t.true(myMethod.secondCall.calledWith([{id: 6}]));
t.end();
}, 15);
}, 5);
});
test.cb('should not batch together function calls with different callback', t => {
t.plan(5);
const myMethod = sinon.spy();
const batch = batcher(myMethod);
const callback1 = function () {
return 'hit me';
};
const callback2 = function () {
return 'baby';
};
const callback3 = function () {
return 'one more time';
};
batch({id: 1}, callback1);
batch({id: 2}, callback1);
batch({id: 3}, callback1);
batch({id: 4}, callback1);
batch({id: 5}, callback1);
batch({id: 8}, callback2);
batch({id: 9}, callback2);
batch({id: 10}, callback2);
batch({id: 123}, callback2);
setTimeout(() => {
batch({id: 6}, callback1);
batch({id: 890}, callback3);
batch({id: 8}, callback3);
setTimeout(() => {
t.true(myMethod.callCount === 4);
t.true(myMethod.firstCall.calledWith([{id: 1}, {id: 2}, {id: 3}, {id: 4}, {id: 5}]));
t.true(myMethod.secondCall.calledWith([{id: 8}, {id: 9}, {id: 10}, {id: 123}]));
t.true(myMethod.thirdCall.calledWith([{id: 6}]));
t.true(myMethod.lastCall.calledWith([{id: 890}, {id: 8}]));
t.end();
}, 15);
}, 5);
});
test.cb('should allow to define a custom interval', t => {
t.plan(2);
const myMethod = sinon.spy();
const batch = batcher(myMethod, 10);
const callback = () => undefined;
batch({id: 1}, callback);
batch({id: 2}, callback);
batch({id: 3}, callback);
batch({id: 4}, callback);
batch({id: 5}, callback);
setTimeout(() => {
batch({id: 6}, callback);
setTimeout(() => {
t.true(myMethod.calledOnce);
t.true(myMethod.calledWith([{id: 1}, {id: 2}, {id: 3}, {id: 4}, {id: 5}, {id: 6}]));
t.end();
}, 10);
}, 5);
});
test.cb('should allow to define a maximum limit', t => {
t.plan(4);
const myMethod = sinon.spy();
const batch = batcher(myMethod, {maximum: 2});
const callback = () => undefined;
batch({id: 1}, callback);
batch({id: 2}, callback);
batch({id: 3}, callback);
batch({id: 4}, callback);
batch({id: 5}, callback);
setTimeout(() => {
t.true(myMethod.calledThrice);
t.true(myMethod.calledWith([{id: 1}, {id: 2}]));
t.true(myMethod.calledWith([{id: 3}, {id: 4}]));
t.true(myMethod.calledWith([{id: 5}]));
t.end();
}, 5);
});
test.cb('should allow to define a maximum limit with custom intervals', t => {
t.plan(5);
const myMethod = sinon.spy();
const batch = batcher(myMethod, {maximum: 2, interval: 1});
const callback = () => undefined;
batch({id: 1}, callback);
batch({id: 2}, callback);
batch({id: 3}, callback);
batch({id: 4}, callback);
batch({id: 5}, callback);
setTimeout(() => {
batch({id: 6}, callback);
t.true(myMethod.calledWith([{id: 1}, {id: 2}]));
t.true(myMethod.calledWith([{id: 3}, {id: 4}]));
t.true(myMethod.calledWith([{id: 5}]));
setTimeout(() => {
t.true(myMethod.calledWith([{id: 6}]));
t.true(myMethod.getCalls().length === 4);
t.end();
}, 5);
}, 5);
});
test.cb('should aggregate by callback instance', t => {
t.plan(5);
const myMethod = sinon.spy();
const batch = batcher(myMethod);
batch({id: 1}, () => undefined);
batch({id: 2}, () => undefined);
batch({id: 3}, () => undefined);
batch({id: 4}, () => undefined);
batch({id: 5}, () => undefined);
setTimeout(() => {
t.true(myMethod.calledWith([{id: 1}]));
t.true(myMethod.calledWith([{id: 2}]));
t.true(myMethod.calledWith([{id: 3}]));
t.true(myMethod.calledWith([{id: 4}]));
t.true(myMethod.calledWith([{id: 5}]));
t.end();
}, 0);
});