-
Notifications
You must be signed in to change notification settings - Fork 11
/
index.spec.js
1855 lines (1627 loc) · 56.4 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
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* linting omitted on spec mainly because jslint doesn"t seem to allow the
* mocha expression-like syntax
*/
"use strict";
const expect = require("chai").expect,
sut = require("./index"),
SynchronousPromise = sut.SynchronousPromise;
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("when resolved, should return a new resolved promise", function () {
const sut = createResolved();
expect(sut.then(null, function () {
})).to.be.instanceOf(SynchronousPromise);
});
it("should return the new resolved promise v2", function () {
const result = createResolved().then(function () {
/* purposely don't return anything */
});
expect(result).to.be.instanceOf(SynchronousPromise);
});
it("should return a new rejected promise", function () {
const sut = createRejected();
expect(sut.then(function () {
})).to.be.instanceOf(SynchronousPromise);
});
it("should return a new rejected promise v2", function () {
const result = createRejected().then(function () {
/* purposely don't return anything */
});
expect(result).to.be.instanceOf(SynchronousPromise)
});
it("should bring the first resolve value into the first then", function () {
const initial = "123";
let captured = null;
createResolved(initial).then(function (data) {
captured = data;
});
expect(captured).to.equal(initial);
});
it("should call into the immediate catch function when the function given to then throws", function () {
const sut = createResolved(),
expected = "the error";
let received = null;
sut.then(function () {
throw new Error(expected);
}).catch(function (err) {
received = err;
});
expect(received.message).to.equal(expected);
});
it(`should allow re-throwing in a .catch and re-catching later`, () => {
// Arrange
const sut = createResolved(),
error1 = "moo";
let received = null;
const expected = "moo-cow";
// Act
sut.then(function () {
throw new Error(error1);
}).catch(function (err) {
throw new Error(err.message + "-cow");
}).catch(function (err) {
received = err.message;
});
// Assert
expect(received).to.equal(expected);
});
it("should call the catch from a rejection invoke", () => {
// Arrange
const expected = "moo",
sut = new SynchronousPromise(function (resolve, reject) {
reject(expected);
});
let captured = null;
// Act
sut.catch(function (e) {
captured = e;
});
// Assert
expect(captured).to.equal(expected);
});
it("should call into the later catch function when the function given to then throws", function () {
const sut = createResolved(),
expected = "the error";
let received = null;
sut.then(function () {
throw new Error(expected);
}).then(function () {
return 42; // not a thrower
}).catch(function (err) {
received = err;
});
expect(received.message).to.equal(expected);
});
it("should prefer to call into onRejected over the .catch handler on failure", function () {
const sut = createResolved(),
expected = "the error";
let captured = null,
catchCaptured = null;
sut.then(function () {
throw expected;
}, function (e) {
captured = e;
}).catch(function (e) {
console.log(".catch handler");
catchCaptured = e;
});
expect(captured).to.equal(expected);
expect(catchCaptured).to.be.null;
});
it("should bring the first rejected value into the first onRejected then handler", function () {
const initial = new Error("123");
let captured = null;
createRejected(initial).then(function () {
}, function (e) {
captured = e
});
expect(captured).to.equal(initial);
});
it("should resolve when the first resolution is a resolved promise", function () {
const initial = createResolved("123");
let 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 () {
const initial = createRejected("123");
let 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 () {
const initial = createResolved("123");
let captured = null;
const 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 () {
const initial = "123",
second = "abc";
let captured = null;
createResolved(initial).then(function () {
return createResolved(second);
}).then(function (data) {
captured = data;
});
expect(captured).to.equal(second);
});
it("should run a longer chain", function () {
const initial = "123",
second = "abc",
third = "---",
expected = second + third;
let captured = null;
createResolved(initial).then(function () {
return createResolved(second);
}).then(function () {
return second;
}).then(function (data) {
captured = data + third;
});
expect(captured).to.equal(expected);
});
it("should run a longer chain v2", function () {
const initial = "123",
second = "abc",
third = "---",
expected = second + third;
let captured = null;
createResolved(initial).then(function () {
return createResolved(second);
}).then(function () {
return createResolved(second);
}).then(function (data) {
captured = data + third;
});
expect(captured).to.equal(expected);
});
it("should run a longer chain v3", function () {
const initial = "123",
second = "abc",
third = "---",
expected = second + third;
let captured = null;
createResolved(initial).then(function () {
return second;
}).then(function () {
return createResolved(second);
}).then(function (data) {
captured = data + third;
});
expect(captured).to.equal(expected);
});
it("should resolve when the ctor function resolves", function () {
let providedResolve = null,
captured = null;
const expected = "xyz",
promise = create(function (resolve) {
providedResolve = resolve;
}).then(function (data) {
captured = data;
});
expect(promise).to.exist;
expect(captured).to.be.null;
expect(providedResolve).to.be.a("function");
providedResolve(expected);
expect(captured).to.equal(expected);
});
it("should resolve the same value from the same promise multiple times", () => {
// Arrange
const expected = "multi-pass",
sut = SynchronousPromise.resolve(expected);
let captured1 = null,
captured2 = null;
// Act
sut.then(result => captured1 = result);
sut.then(result => captured2 = result);
// Assert
expect(captured1).to.equal(expected);
expect(captured2).to.equal(expected);
});
describe(`es6 native Promise compatibility`, () => {
it(`should pass on the prior result when no function provided`, async () => {
// Arrange
// Act
const result = await SynchronousPromise.resolve("expected")
.then();
// Assert
expect(result)
.to.equal("expected");
});
});
});
describe("catch", function () {
it("should be called if the initial reject is called", function () {
const expected = "123";
let captured = null;
createRejected(expected).catch(function (e) {
captured = e;
});
expect(captured).to.equal(expected);
});
it("should call handler if the promise resolves", function () {
// Arrange
const sut = SynchronousPromise.unresolved();
let resolved = false,
caught = false;
// Act
sut.then(() => resolved = true).catch(() => caught = true);
sut.resolve();
// Assert
expect(resolved).to.be.true;
expect(caught).to.be.false;
});
it("should be called on a delayed rejection", function () {
let providedReject = null,
captured = null;
const expected = "123",
promise = create(function (resolve, reject) {
providedReject = reject;
}).catch(function (e) {
captured = e;
});
expect(promise).to.exist;
expect(captured).to.be.null;
expect(providedReject).to.be.a("function");
providedReject(expected);
expect(captured).to.equal(expected);
});
it("should return a resolved promise if doesn't throw an error", function () {
const 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.status).to.be.equal("resolved");
});
it("should not interfere with a later then if there is no error", function () {
let captured = null;
const expected = "123";
let 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 not be called if the promise is handled successful by a previous onRejected handler", function () {
const expected = new Error("123"),
notExpected = new Error("Not expected");
let capturedError = null;
createRejected(expected).then(
function () {
},
function (e) {
capturedError = e
})
.catch(function () {
/* purposely don't return anything */
capturedError = notExpected;
});
expect(capturedError).to.equal(expected);
});
it("should prevent the handlers after the error from being called", function () {
let captured = null;
createResolved("123").catch(function () {
}).then(function () {
throw "foo";
}).then(function () {
captured = "abc";
});
expect(captured).to.be.null;
});
it("should re-catch if a catch handler returns a rejected promise", function (done) {
// Arrange
const expected = "123",
pausedRejectedPromise = SynchronousPromise.reject(expected).pause();
let capturedA = null,
capturedB = null;
pausedRejectedPromise.catch(function (e) {
capturedA = e;
// prove that this works even from an async promise
return Promise.reject(e);
}).catch(function (e) {
capturedB = e;
});
// Act
pausedRejectedPromise.resume();
// Assert
setTimeout(function () {
try {
expect(capturedA).to.equal(expected);
expect(capturedB).to.equal(expected);
done();
} catch (e) {
done(e);
}
}, 100);
});
it("should re-catch if a then onRejected handler returns a rejected promise", function (done) {
// Arrange
const expected = "123",
pausedRejectedPromise = SynchronousPromise.reject(expected).pause();
let capturedA = null,
capturedB = null;
pausedRejectedPromise.then(function () {
/* purposely don't return anything */
}, function (e) {
capturedA = e;
// prove that this works even from an async promise
return Promise.reject(e);
}).catch(function (e) {
capturedB = e;
});
// Act
pausedRejectedPromise.resume();
// Assert
setTimeout(function () {
expect(capturedA).to.equal(expected);
expect(capturedB).to.equal(expected);
done();
}, 100);
});
it("should continue if a catch handler returns a resolved promise", function (done) {
// Arrange
const expected = "123",
pausedRejectedPromise = SynchronousPromise.reject(expected).pause();
let capturedA = null,
capturedB = null,
secondResolve;
pausedRejectedPromise.catch(function (e) {
capturedA = e;
// prove that this works even from an async promise
return Promise.resolve("456");
}).catch(function (e) {
capturedB = e;
}).then(function (data) {
secondResolve = data;
});
// Act
pausedRejectedPromise.resume();
// Assert
setTimeout(function () {
try {
expect(capturedA).to.equal(expected);
expect(capturedB).to.be.null;
expect(secondResolve).to.equal("456");
done();
} catch (e) {
done(e);
}
}, 100);
});
});
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 () {
let 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 () {
let calls = 0;
createRejected("123").pause().catch(function () {
calls++;
});
expect(calls).to.equal(0);
});
it("should prevent rejection from continuing past at that point", function () {
let 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 and being resolved on resume", function () {
let captured = undefined;
const promise = SynchronousPromise.resolve().pause().then(function () {
return "moo";
}).then(function (data) {
captured = data;
});
expect(captured).to.be.undefined;
promise.resume();
expect(captured).to.equal("moo");
});
it("should return a promise in paused state with no initial data and being rejected on resume", function () {
let captured = undefined;
const expected = new Error("moon"),
promise = SynchronousPromise.resolve().pause().then(function () {
throw expected
}).catch(function (e) {
captured = e;
});
expect(captured).to.be.undefined;
promise.resume();
expect(captured).to.equal(expected);
});
it("should return a promise in paused state with no initial data and being resolved after a catch on resume", function () {
let captured = undefined;
const error = new Error("moon"),
promise = SynchronousPromise.resolve().pause().then(function () {
throw error
}).catch(function (e) {
return e.message;
}).then(function (m) {
captured = m;
});
expect(captured).to.be.undefined;
promise.resume();
expect(captured).to.equal("moon");
});
});
});
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 () {
const promise = createResolved("123").pause(),
result = promise.resume();
expect(result).to.equal(promise);
});
it("should not barf if the promise is not already paused", function () {
const promise = createResolved("123");
expect(function () {
promise.resume();
}).not.to.throw();
});
it("should resume resolution operations after the last pause", function () {
let calls = 0;
const 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 () {
let calls = 0,
captured = null;
const 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 () {
let calls = 0,
captured = null;
const 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 () {
const expected = "foo",
result = SynchronousPromise.resolve(expected);
expect(result.status).to.equal("resolved");
let 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 () {
const expected = "moo",
result = SynchronousPromise.reject(expected);
expect(result.status).to.equal("rejected");
let 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 () {
const p1 = createResolved("abc"),
p2 = createResolved("123"),
all = SynchronousPromise.all(p1, p2);
let 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 promise or none promise variable args", function () {
const all = SynchronousPromise.all(["123", createResolved("abc")]);
let 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 () {
const p1 = createResolved("abc"),
p2 = createResolved("123"),
all = SynchronousPromise.all([p1, p2]);
let 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 empty promise array", function () {
const all = SynchronousPromise.all([]);
let captured = null;
all.then(function (data) {
captured = data;
});
expect(captured).to.have.length(0);
});
it("should resolve with values in the correct order", function () {
let resolve1 = undefined,
resolve2 = undefined,
captured = undefined;
const p1 = create(function (resolve) {
resolve1 = resolve;
});
const 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 () {
const p1 = createResolved("abc"),
p2 = createRejected("123"),
all = SynchronousPromise.all(p1, p2);
let 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");
});
});
describe("static any", function () {
it("should be a function", function () {
expect(SynchronousPromise.any).to.be.a("function")
});
it("should resolve with first value from given resolved promises as variable args", function () {
const p1 = createResolved("abc"),
p2 = createResolved("123"),
any = SynchronousPromise.any(p1, p2);
let captured = null;
any.then(function (data) {
captured = data;
});
expect(captured).to.equal("abc");
});
it("should resolve with first value from given promise or none promise variable args", function () {
const any = SynchronousPromise.any(["123", createResolved("abc")]);
let captured = null;
any.then(function (data) {
captured = data;
});
expect(captured).to.equal("123");
});
it("should reject empty promise array", function () {
const any = SynchronousPromise.any([]);
let capturedData = null,
capturedError = null;
any.then(function (data) {
capturedData = data;
}, function (err) {
capturedError = err;
});
expect(capturedData).to.be.null;
expect(capturedError).to.have.property("errors");
expect(capturedError).property("errors").to.have.length(0);
});
it("should resolve if any promise resolves", function () {
const p1 = createResolved("abc"),
p2 = createRejected("123"),
any = SynchronousPromise.any(p1, p2);
let capturedData = null,
capturedError = null;
any.then(function (data) {
capturedData = data;
}).catch(function (err) {
capturedError = err;
});
expect(capturedData).to.equal("abc");
expect(capturedError).to.be.null;
});
it("should reject if all promises reject", function () {
const p1 = createRejected("abc"),
p2 = createRejected("123"),
any = SynchronousPromise.any(p1, p2);
let capturedData = null,
capturedError = null;
any.then(function (data) {
capturedData = data;
}).catch(function (err) {
capturedError = err;
});
expect(capturedData).to.be.null;
expect(capturedError).to.have.property("errors");
expect(capturedError).property("errors").to.have.length(2);
expect(capturedError).property("errors").to.contain("abc");
expect(capturedError).property("errors").to.contain("123");
});
it("should reject with values in the correct order", function () {
let reject1 = undefined,
reject2 = undefined,
capturedError = undefined;
const p1 = create(function (resolve, reject) {
reject1 = reject;
});
const p2 = create(function (resolve, reject) {
reject2 = reject;
});
SynchronousPromise.any([p1, p2]).catch(function (data) {
capturedError = data;
});
reject2("a");
reject1("b");
expect(capturedError).to.have.property("errors");
expect(capturedError).property("errors").to.deep.equal(["b", "a"]);
});
describe("in browsers supporting AggregateError", function() {
// Used to restore previous global.window value
let windowRef = null;
/** Mock of AggregateError */
class AggregateError extends Error {
constructor(errors, message) {
super(message);
this.name = "AggregateError";
this.errors = errors;
}
}
beforeEach(function() {
// Mock window object with AggregateError
windowRef = global.window;
// noinspection JSConstantReassignment
global.window = { AggregateError };
});
afterEach(function () {
// Restore window object
// noinspection JSConstantReassignment
global.window = windowRef;
windowRef = null;
});
it("should reject with AggregateError for empty promise array", function () {
const anyEmpty = SynchronousPromise.any([]);
let capturedError = null;
anyEmpty.catch(function (err) {
capturedError = err;
});
expect(capturedError).to.be.instanceOf(AggregateError);
});
it("should reject with AggregateError for rejected promises as variable args", function () {
const p1 = createRejected("abc"),
any = SynchronousPromise.any(p1);
let capturedError = null;
any.catch(function (err) {
capturedError = err;
});
expect(capturedError).to.be.instanceOf(AggregateError);
});
});
});
describe("static allSettled", function () {
it("should be a function", function () {
expect(SynchronousPromise.allSettled).to.be.a("function")
});
it("should resolve with all values from given resolved promises as variable args", function () {
const p1 = createResolved("abc"),
p2 = createResolved("123"),
allSettled = SynchronousPromise.allSettled(p1, p2);
let captured = null;
allSettled.then(function (data) {
captured = data;
});
expect(captured).to.have.length(2);
expect(captured).to.deep.contain({ status: "fulfilled", value: "abc" });
expect(captured).to.deep.contain({ status: "fulfilled", value: "123" });
});
it("should resolve with all values from given rejected promises as variable args", function () {
const error1 = new Error("error1");
const error2 = new Error("error2");
const p1 = createRejected(error1),
p2 = createRejected(error2),
allSettled = SynchronousPromise.allSettled(p1, p2);
let captured = null;
allSettled.then(function (data) {
captured = data;
});
expect(captured).to.have.length(2);
expect(captured).to.deep.contain({ status: "rejected", reason: error1 });
expect(captured).to.deep.contain({ status: "rejected", reason: error2 });
});
it("should resolve with all values from given promise or none promise variable args", function () {
const allSettled = SynchronousPromise.allSettled(["123", createResolved("abc")]);
let captured = null;
allSettled.then(function (data) {
captured = data;
});
expect(captured).to.have.length(2);
expect(captured).to.deep.contain({ status: "fulfilled", value: "abc" });
expect(captured).to.deep.contain({ status: "fulfilled", value: "123" });
});
it("should resolve with all values from given resolved promises as an array", function () {
const p1 = createResolved("abc"),
p2 = createResolved("123"),
allSettled = SynchronousPromise.allSettled([p1, p2]);
let captured = null;
allSettled.then(function (data) {
captured = data;
});
expect(captured).to.have.length(2);
expect(captured).to.deep.contain({ status: "fulfilled", value: "abc" });
expect(captured).to.deep.contain({ status: "fulfilled", value: "123" });
});
it("should resolve empty promise array", function () {
const allSettled = SynchronousPromise.allSettled([]);
let captured = null;
allSettled.then(function (data) {
captured = data;
});
expect(captured).to.have.length(0);
});
it("should resolve with values in the correct order", function () {
let resolve1 = undefined,
resolve2 = undefined,
captured = undefined;
const p1 = create(function (resolve) {