forked from fluffynuts/synchronous-promise
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.spec.js
482 lines (463 loc) · 14.3 KB
/
index.spec.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
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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
/*
* linting omitted on spec mainly because jslint doesn"t seem to allow the
* mocha expression-like syntax
*/
"use strict";
var
expect = require("chai").expect,
sut = require("./index"),
SynchronousPromise = sut.SynchronousPromise,
_argumentsToArray = sut._argumentsToArray;
describe("synchronous-promise", function () {
it("should be constructable", function () {
expect(SynchronousPromise).to.exist;
expect(new SynchronousPromise(function () { })).to.exist;
});
it("should have a then function", function () {
expect(SynchronousPromise.prototype.then).to.be.a("function");
});
it("should have a catch function", function () {
expect(SynchronousPromise.prototype.catch).to.be.a("function");
});
function create(ctor) {
return new SynchronousPromise(ctor);
}
function createResolved(data) {
return SynchronousPromise.resolve(data);
}
function createRejected(data) {
return SynchronousPromise.reject(data);
}
describe("then", function () {
it("should return the same promise", function () {
var sut = createResolved();
expect(sut.then(function () { })).to.equal(sut);
});
it("should return the same promise v2", function () {
var
result = createResolved().then(function () {
/* purposely don"t return anything */
});
expect(result).to.be.instanceOf(SynchronousPromise);
});
it("should call into the catch function when the function given to then throws", function () {
var
sut = createResolved(),
expected = "the error",
received = null;
sut.then(function () {
throw new Error(expected);
}).then(function () {
return 42; // not a thrower
}).catch(function (err) {
received = err;
});
expect(received).to.eql(new Error(expected));
});
it("should call into the failure function when the predecessor fails", function () {
var
sut = createResolved(),
expected = "the error",
captured = null,
catchCaptured = null;
sut.then(function () {
throw expected;
}, function (e) {
captured = e;
}).catch(function (e) {
catchCaptured = e;
});
expect(captured).to.equal(expected);
expect(catchCaptured).to.be.null;
});
it("should bring the first resolve value into the first then", function () {
var
initial = "123",
captured = null;
createResolved(initial).then(function (data) {
captured = data;
});
expect(captured).to.equal(initial);
});
it("should resolve when the first resolution is a resolved promise", function () {
var
initial = createResolved("123"),
captured = null;
createResolved(initial).then(function (data) {
captured = data;
});
expect(captured).to.equal("123");
})
it("should catch when the first resolution is a rejected promise", function () {
var
initial = createRejected("123"),
captured = null;
createResolved(initial).catch(function (data) {
captured = data;
});
expect(captured).to.equal("123");
})
it("should catch when a subsequent resolution returns a rejected promise", function () {
var
initial = createResolved("123"),
captured = null,
expected = "le error";
initial.then(function () {
return createRejected(expected);
}).catch(function (e) {
captured = e;
})
expect(captured).to.equal(expected);
});
it("should run a simple chain", function () {
var
initial = "123",
second = "abc",
captured = null;
createResolved(initial).then(function (data) {
return createResolved(second);
}).then(function (data) {
captured = data;
});
expect(captured).to.equal(second);
});
it("should run a longer chain", function () {
var
initial = "123",
second = "abc",
third = "---",
expected = second + third,
captured = null;
createResolved(initial).then(function (data) {
return createResolved(second);
}).then(function (data) {
return second;
}).then(function (data) {
captured = data + third;
});
expect(captured).to.equal(expected);
});
it("should run a longer chain v2", function () {
var
initial = "123",
second = "abc",
third = "---",
expected = second + third,
captured = null;
createResolved(initial).then(function (data) {
return createResolved(second);
}).then(function (data) {
return createResolved(second);
}).then(function (data) {
captured = data + third;
});
expect(captured).to.equal(expected);
});
it("should run a longer chain v3", function () {
var
initial = "123",
second = "abc",
third = "---",
expected = second + third,
captured = null;
createResolved(initial).then(function (data) {
return second;
}).then(function (data) {
return createResolved(second);
}).then(function (data) {
captured = data + third;
});
expect(captured).to.equal(expected);
});
it("should resolve when the ctor function resolves", function () {
var
providedResolve = null,
captured = null,
expected = "xyz",
promise = create(function (resolve, reject) {
providedResolve = resolve;
}).then(function (data) {
captured = data;
});
expect(captured).to.be.null;
expect(providedResolve).to.be.a("function");
providedResolve(expected)
expect(captured).to.equal(expected);
});
});
describe("catch", function () {
it("should be called if the initial reject is called", function () {
var
expected = "123",
captured = null;
createRejected(expected).catch(function (e) {
captured = e;
})
expect(captured).to.equal(expected);
});
it("should be called on a delayed rejection", function () {
var
providedReject = null,
captured = null,
expected = "123",
promise = create(function (resolve, reject) {
providedReject = reject;
}).catch(function (e) {
captured = e;
});
expect(captured).to.be.null;
expect(providedReject).to.be.a("function");
providedReject(expected);
expect(captured).to.equal(expected);
});
it("should return the same promise", function () {
var
promise = createRejected("123"),
result = promise.catch(function (data) {
expect(data).to.equal("123");
});
expect(result).to.exist;
expect(result).to.be.instanceOf(SynchronousPromise);
expect(result).to.equal(promise);
});
it("should not interfere with a later then if there is no error", function () {
var
captured = null,
expected = "123",
capturedError = null;
createResolved(expected).catch(function (e) {
capturedError = e;
}).then(function (data) {
captured = data;
})
expect(capturedError).to.be.null;
expect(captured).to.equal(expected);
})
it("should prevent then handlers after the error from being called", function () {
var
captured = null;
createResolved("123").catch(function (e) {
}).then(function (data) {
throw "foo";
}).then(function (data) {
captured = "abc";
})
expect(captured).to.be.null;
})
});
describe("prototype pause", function () {
it("should exist as a function on the prototype", function () {
expect(SynchronousPromise.prototype.pause).to.be.a("function");
});
it("should return the promise", function () {
const
promise = createResolved("123"),
result = promise.pause();
expect(result).to.equal(promise);
});
it("should prevent resolution from continuing at that point", function () {
var calls = 0;
createResolved("123").then(function () {
return calls++;
}).pause().then(function () {
return calls++;
});
expect(calls).to.equal(1);
});
it("should prevent rejection from being caught at that point", function () {
var calls = 0;
createRejected("123").pause().catch(function (e) {
calls++;
})
expect(calls).to.equal(0);
});
it("should prevent rejection from continuing past at that point", function () {
var
calls = 0,
captured = null;
createRejected("123").then(function () {
// should not be called
calls++;
}).catch(function (e) {
captured = e;
}).pause().then(function () {
calls++;
});
expect(captured).to.equal("123");
expect(calls).to.equal(0);
})
describe("starting paused", function () {
it("should return a promise in paused state with no initial data", function () {
var
captured,
promise = SynchronousPromise.resolve().pause().then(function () {
return "moo";
}).then(function (data) {
captured = data;
});
expect(captured).not.to.be.defined;
promise.resume();
expect(captured).to.equal("moo");
});
});
});
describe("resume", function () {
it("should exist as a function on the prototype", function () {
expect(SynchronousPromise.prototype.resume).to.be.a("function");
});
it("should return the promise", function () {
var
promise = createResolved("123").pause(),
result = promise.resume();
expect(result).to.equal(promise);
});
it("should not barf if the promise is not already paused", function () {
var promise = createResolved("123");
expect(function () {
promise.resume();
}).not.to.throw;
})
it("should resume resolution operations after the last pause", function () {
var
calls = 0,
promise = createResolved("123").then(function () {
return calls++;
}).pause().then(function () {
return calls++;
});
expect(calls).to.equal(1);
promise.resume();
expect(calls).to.equal(2);
});
it("should resume rejection operations after the last pause", function () {
var
calls = 0,
captured = null,
expected = "die, scum!",
promise = createResolved("123").then(function () {
throw expected;
}).pause().then(function () {
return calls++;
}).catch(function (e) {
captured = e;
});
expect(calls).to.equal(0);
expect(captured).to.be.null;
promise.resume();
expect(calls).to.equal(0);
expect(captured).to.equal(expected);
});
it("should resume a promise which was started rejected as rejected", function () {
var
calls = 0,
captured = null,
expected = "it\"s the end of the world!",
promise = SynchronousPromise.reject(expected).pause().then(function () {
calls++;
}).catch(function (e) {
captured = e;
});
expect(calls).to.equal(0);
expect(captured).to.be.null;
promise.resume();
expect(calls).to.equal(0);
expect(captured).to.equal(expected);
})
})
describe("static resolve", function () {
it("should be a function", function () {
expect(SynchronousPromise.resolve).to.be.a("function");
});
it("should return a resolved promise", function () {
var
expected = "foo",
result = SynchronousPromise.resolve(expected);
expect(result.status).to.equal("resolved");
var captured = null;
result.then(function (data) {
captured = data;
});
expect(captured).to.equal(expected);
})
});
describe("static reject", function () {
it("should be a function", function () {
expect(SynchronousPromise.reject).to.be.a("function");
});
it("should return a rejected promise", function () {
var
expected = "moo",
result = SynchronousPromise.reject(expected);
expect(result.status).to.equal("rejected");
var captured = null;
result.catch(function (err) {
captured = err;
});
expect(captured).to.equal(expected);
});
});
describe("static all", function () {
it("should be a function", function () {
expect(SynchronousPromise.all).to.be.a("function")
})
it("should resolve with all values from given resolved promises as variable args", function () {
var
p1 = createResolved("abc"),
p2 = createResolved("123"),
all = SynchronousPromise.all(p1, p2),
captured = null;
all.then(function (data) {
captured = data;
});
expect(captured).to.have.length(2);
expect(captured).to.contain("abc");
expect(captured).to.contain("123");
});
it("should resolve with all values from given resolved promises as an array", function () {
var
p1 = createResolved("abc"),
p2 = createResolved("123"),
all = SynchronousPromise.all([p1, p2]),
captured = null;
all.then(function (data) {
captured = data;
});
expect(captured).to.have.length(2);
expect(captured).to.contain("abc");
expect(captured).to.contain("123");
});
it("should resolve with values in the correct order", function() {
var
resolve1,
resolve2,
captured;
var p1 = create(function(resolve) {
resolve1 = resolve;
});
var p2 = create(function(resolve) {
resolve2 = resolve;
});
SynchronousPromise.all([p1, p2]).then(function(data) {
captured = data;
});
resolve2("a");
resolve1("b");
expect(captured).to.deep.equal(["b", "a"]);
});
it("should reject if any promise rejects", function () {
var
p1 = createResolved("abc"),
p2 = createRejected("123"),
all = SynchronousPromise.all(p1, p2),
capturedData = null,
capturedError = null;
all.then(function (data) {
capturedData = data;
}).catch(function (err) {
capturedError = err;
});
expect(capturedData).to.be.null;
expect(capturedError).to.equal("123");
});
})
})