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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
|
local promise = require "util.promise";
describe("util.promise", function ()
--luacheck: ignore 212/resolve 212/reject
describe("new()", function ()
it("returns a promise object", function ()
assert(promise.new());
end);
end);
it("notifies immediately for fulfilled promises", function ()
local p = promise.new(function (resolve)
resolve("foo");
end);
local cb = spy.new(function (v)
assert.equal("foo", v);
end);
p:next(cb);
assert.spy(cb).was_called(1);
end);
it("notifies on fulfilment of pending promises", function ()
local r;
local p = promise.new(function (resolve)
r = resolve;
end);
local cb = spy.new(function (v)
assert.equal("foo", v);
end);
p:next(cb);
assert.spy(cb).was_called(0);
r("foo");
assert.spy(cb).was_called(1);
end);
it("allows chaining :next() calls", function ()
local r;
local result;
local p = promise.new(function (resolve)
r = resolve;
end);
local cb1 = spy.new(function (v)
assert.equal("foo", v);
return "bar";
end);
local cb2 = spy.new(function (v)
assert.equal("bar", v);
result = v;
end);
p:next(cb1):next(cb2);
assert.spy(cb1).was_called(0);
assert.spy(cb2).was_called(0);
r("foo");
assert.spy(cb1).was_called(1);
assert.spy(cb2).was_called(1);
assert.equal("bar", result);
end);
it("supports multiple :next() calls on the same promise", function ()
local r;
local result;
local p = promise.new(function (resolve)
r = resolve;
end);
local cb1 = spy.new(function (v)
assert.equal("foo", v);
result = v;
end);
local cb2 = spy.new(function (v)
assert.equal("foo", v);
result = v;
end);
p:next(cb1);
p:next(cb2);
assert.spy(cb1).was_called(0);
assert.spy(cb2).was_called(0);
r("foo");
assert.spy(cb1).was_called(1);
assert.spy(cb2).was_called(1);
assert.equal("foo", result);
end);
it("automatically rejects on error", function ()
local r;
local p = promise.new(function (resolve)
r = resolve;
error("oh no");
end);
local cb = spy.new(function () end);
local err_cb = spy.new(function (v)
assert.equal("oh no", v);
end);
p:next(cb, err_cb);
assert.spy(cb).was_called(0);
assert.spy(err_cb).was_called(1);
r("foo");
assert.spy(cb).was_called(0);
assert.spy(err_cb).was_called(1);
end);
it("supports reject()", function ()
local r, result;
local p = promise.new(function (resolve, reject)
r = reject;
end);
local cb = spy.new(function () end);
local err_cb = spy.new(function (v)
result = v;
assert.equal("oh doh", v);
end);
p:next(cb, err_cb);
assert.spy(cb).was_called(0);
assert.spy(err_cb).was_called(0);
r("oh doh");
assert.spy(cb).was_called(0);
assert.spy(err_cb).was_called(1);
assert.equal("oh doh", result);
end);
it("supports chaining of rejected promises", function ()
local r, result;
local p = promise.new(function (resolve, reject)
r = reject;
end);
local cb = spy.new(function () end);
local err_cb = spy.new(function (v)
result = v;
assert.equal("oh doh", v);
return "ok"
end);
local cb2 = spy.new(function (v)
result = v;
end);
local err_cb2 = spy.new(function () end);
p:next(cb, err_cb):next(cb2, err_cb2)
assert.spy(cb).was_called(0);
assert.spy(err_cb).was_called(0);
assert.spy(cb2).was_called(0);
assert.spy(err_cb2).was_called(0);
r("oh doh");
assert.spy(cb).was_called(0);
assert.spy(err_cb).was_called(1);
assert.spy(cb2).was_called(1);
assert.spy(err_cb2).was_called(0);
assert.equal("ok", result);
end);
it("propagates errors down the chain, even when some handlers are not provided", function ()
local r, result;
local test_error = {};
local p = promise.new(function (resolve, reject)
r = reject;
end);
local cb = spy.new(function () end);
local err_cb = spy.new(function (e) result = e end);
local p2 = p:next(function () error(test_error) end);
local p3 = p2:next(cb)
p3:catch(err_cb);
assert.spy(cb).was_called(0);
assert.spy(err_cb).was_called(0);
r("oh doh");
assert.spy(cb).was_called(0);
assert.spy(err_cb).was_called(1);
assert.spy(err_cb).was_called_with("oh doh");
assert.equal("oh doh", result);
end);
it("propagates values down the chain, even when some handlers are not provided", function ()
local r;
local p = promise.new(function (resolve, reject)
r = resolve;
end);
local cb = spy.new(function () end);
local err_cb = spy.new(function () end);
local p2 = p:next(function (v) return v; end);
local p3 = p2:catch(err_cb)
p3:next(cb);
assert.spy(cb).was_called(0);
assert.spy(err_cb).was_called(0);
r(1337);
assert.spy(cb).was_called(1);
assert.spy(cb).was_called_with(1337);
assert.spy(err_cb).was_called(0);
end);
it("fulfilled promises do not call error handlers and do propagate value", function ()
local p = promise.resolve("foo");
local cb = spy.new(function () end);
local p2 = p:catch(cb);
assert.spy(cb).was_called(0);
local cb2 = spy.new(function () end);
local p3 = p2:catch(cb2);
assert.spy(cb2).was_called(0);
end);
it("rejected promises do not call fulfilled handlers and do propagate reason", function ()
local p = promise.reject("foo");
local cb = spy.new(function () end);
local p2 = p:next(cb);
assert.spy(cb).was_called(0);
local cb2 = spy.new(function () end);
local cb2_err = spy.new(function () end);
local p3 = p2:next(cb2, cb2_err);
assert.spy(cb2).was_called(0);
assert.spy(cb2_err).was_called(1);
assert.spy(cb2_err).was_called_with("foo");
end);
describe("race()", function ()
it("works with fulfilled promises", function ()
local p1, p2 = promise.resolve("yep"), promise.resolve("nope");
local p = promise.race({ p1, p2 });
local result;
p:next(function (v)
result = v;
end);
assert.equal("yep", result);
end);
it("works with pending promises", function ()
local r1, r2;
local p1, p2 = promise.new(function (resolve) r1 = resolve end), promise.new(function (resolve) r2 = resolve end);
local p = promise.race({ p1, p2 });
local result;
local cb = spy.new(function (v)
result = v;
end);
p:next(cb);
assert.spy(cb).was_called(0);
r2("yep");
r1("nope");
assert.spy(cb).was_called(1);
assert.equal("yep", result);
end);
end);
describe("all()", function ()
it("works with fulfilled promises", function ()
local p1, p2 = promise.resolve("yep"), promise.resolve("nope");
local p = promise.all({ p1, p2 });
local result;
p:next(function (v)
result = v;
end);
assert.same({ "yep", "nope" }, result);
end);
it("works with pending promises", function ()
local r1, r2;
local p1, p2 = promise.new(function (resolve) r1 = resolve end), promise.new(function (resolve) r2 = resolve end);
local p = promise.all({ p1, p2 });
local result;
local cb = spy.new(function (v)
result = v;
end);
p:next(cb);
assert.spy(cb).was_called(0);
r2("yep");
assert.spy(cb).was_called(0);
r1("nope");
assert.spy(cb).was_called(1);
assert.same({ "nope", "yep" }, result);
end);
it("rejects if any promise rejects", function ()
local r1, r2;
local p1 = promise.new(function (resolve, reject) r1 = reject end);
local p2 = promise.new(function (resolve, reject) r2 = reject end);
local p = promise.all({ p1, p2 });
local result;
local cb = spy.new(function (v)
result = v;
end);
local cb_err = spy.new(function (v)
result = v;
end);
p:next(cb, cb_err);
assert.spy(cb).was_called(0);
assert.spy(cb_err).was_called(0);
r2("fail");
assert.spy(cb).was_called(0);
assert.spy(cb_err).was_called(1);
r1("nope");
assert.spy(cb).was_called(0);
assert.spy(cb_err).was_called(1);
assert.equal("fail", result);
end);
end);
describe("catch()", function ()
it("works", function ()
local result;
local p = promise.new(function (resolve)
error({ foo = true });
end);
local cb1 = spy.new(function (v)
result = v;
end);
assert.spy(cb1).was_called(0);
p:catch(cb1);
assert.spy(cb1).was_called(1);
assert.same({ foo = true }, result);
end);
end);
it("promises may be resolved by other promises", function ()
local r1, r2;
local p1, p2 = promise.new(function (resolve) r1 = resolve end), promise.new(function (resolve) r2 = resolve end);
local result;
local cb = spy.new(function (v)
result = v;
end);
p1:next(cb);
assert.spy(cb).was_called(0);
r1(p2);
assert.spy(cb).was_called(0);
r2("yep");
assert.spy(cb).was_called(1);
assert.equal("yep", result);
end);
describe("reject()", function ()
it("returns a rejected promise", function ()
local p = promise.reject("foo");
local cb = spy.new(function () end);
p:catch(cb);
assert.spy(cb).was_called(1);
assert.spy(cb).was_called_with("foo");
end);
it("returns a rejected promise and does not call on_fulfilled", function ()
local p = promise.reject("foo");
local cb = spy.new(function () end);
p:next(cb);
assert.spy(cb).was_called(0);
end);
end);
describe("finally()", function ()
local p, p2, resolve, reject, on_finally;
before_each(function ()
p = promise.new(function (_resolve, _reject)
resolve, reject = _resolve, _reject;
end);
on_finally = spy.new(function () end);
p2 = p:finally(on_finally);
end);
it("runs when a promise is resolved", function ()
assert.spy(on_finally).was_called(0);
resolve("foo");
assert.spy(on_finally).was_called(1);
assert.spy(on_finally).was_not_called_with("foo");
end);
it("runs when a promise is rejected", function ()
assert.spy(on_finally).was_called(0);
reject("foo");
assert.spy(on_finally).was_called(1);
assert.spy(on_finally).was_not_called_with("foo");
end);
it("returns a promise that fulfills with the original value", function ()
local cb2 = spy.new(function () end);
p2:next(cb2);
assert.spy(on_finally).was_called(0);
assert.spy(cb2).was_called(0);
resolve("foo");
assert.spy(on_finally).was_called(1);
assert.spy(cb2).was_called(1);
assert.spy(on_finally).was_not_called_with("foo");
assert.spy(cb2).was_called_with("foo");
end);
it("returns a promise that rejects with the original error", function ()
local on_finally_err = spy.new(function () end);
local on_finally_ok = spy.new(function () end);
p2:catch(on_finally_err);
p2:next(on_finally_ok);
assert.spy(on_finally).was_called(0);
assert.spy(on_finally_err).was_called(0);
reject("foo");
assert.spy(on_finally).was_called(1);
-- Since the original promise was rejected, the finally promise should also be
assert.spy(on_finally_ok).was_called(0);
assert.spy(on_finally_err).was_called(1);
assert.spy(on_finally).was_not_called_with("foo");
assert.spy(on_finally_err).was_called_with("foo");
end);
it("returns a promise that rejects with an uncaught error inside on_finally", function ()
p = promise.new(function (_resolve, _reject)
resolve, reject = _resolve, _reject;
end);
local test_error = {};
on_finally = spy.new(function () error(test_error) end);
p2 = p:finally(on_finally);
local on_finally_err = spy.new(function () end);
p2:catch(on_finally_err);
assert.spy(on_finally).was_called(0);
assert.spy(on_finally_err).was_called(0);
reject("foo");
assert.spy(on_finally).was_called(1);
assert.spy(on_finally_err).was_called(1);
assert.spy(on_finally).was_not_called_with("foo");
assert.spy(on_finally).was_not_called_with(test_error);
assert.spy(on_finally_err).was_called_with(test_error);
end);
end);
describe("try()", function ()
it("works with functions that return a promise", function ()
local resolve;
local p = promise.try(function ()
return promise.new(function (_resolve)
resolve = _resolve;
end);
end);
assert.is_function(resolve);
local on_resolved = spy.new(function () end);
p:next(on_resolved);
assert.spy(on_resolved).was_not_called();
resolve("foo");
assert.spy(on_resolved).was_called_with("foo");
end);
it("works with functions that return a value", function ()
local p = promise.try(function ()
return "foo";
end);
local on_resolved = spy.new(function () end);
p:next(on_resolved);
assert.spy(on_resolved).was_called_with("foo");
end);
it("works with functions that return a promise that rejects", function ()
local reject;
local p = promise.try(function ()
return promise.new(function (_, _reject)
reject = _reject;
end);
end);
assert.is_function(reject);
local on_rejected = spy.new(function () end);
p:catch(on_rejected);
assert.spy(on_rejected).was_not_called();
reject("foo");
assert.spy(on_rejected).was_called_with("foo");
end);
it("works with functions that throw errors", function ()
local test_error = {};
local p = promise.try(function ()
error(test_error);
end);
local on_rejected = spy.new(function () end);
p:catch(on_rejected);
assert.spy(on_rejected).was_called(1);
assert.spy(on_rejected).was_called_with(test_error);
end);
end);
end);
|