-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
q-spec.js
1936 lines (1635 loc) · 55.4 KB
/
q-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
"use strict";
/*global Q: true, describe: false, it: false, expect: false, afterEach: false,
require: false, jasmine: false, waitsFor: false, runs: false */
if (typeof Q === "undefined" && typeof require !== "undefined") {
// For Node compatibility.
global.Q = require("../q");
require("./lib/jasmine-promise");
}
var REASON = "this is not an error, but it might show up in the console";
afterEach(function () {
Q.onerror = null;
});
describe("defer and when", function () {
it("resolve before when", function () {
var turn = 0;
var deferred = Q.defer();
deferred.resolve(10);
var promise = Q.when(deferred.promise, function (value) {
expect(turn).toEqual(1);
expect(value).toEqual(10);
});
turn++;
return promise;
});
it("reject before when", function () {
var turn = 0;
var deferred = Q.defer();
deferred.reject(-1);
var promise = Q.when(deferred.promise, function () {
expect(true).toBe(false);
}, function (value) {
expect(turn).toEqual(1);
expect(value).toEqual(-1);
});
turn++;
return promise;
});
it("when before resolve", function () {
var turn = 0;
var deferred = Q.defer();
var promise = deferred.promise.then(function (value) {
expect(turn).toEqual(2);
expect(value).toEqual(10);
turn++;
});
Q.nextTick(function () {
expect(turn).toEqual(1);
deferred.resolve(10);
turn++;
});
turn++;
return promise;
});
it("when before reject", function () {
var turn = 0;
var deferred = Q.defer();
var promise = deferred.promise.then(function () {
expect(true).toBe(false);
}, function (value) {
expect(turn).toEqual(2);
expect(value).toEqual(-1);
turn++;
});
Q.nextTick(function () {
expect(turn).toEqual(1);
deferred.reject(-1);
turn++;
});
turn++;
return promise;
});
it("resolves multiple observers", function (done) {
var nextTurn = false;
var resolution = 'Taram pam param!';
var deferred = Q.defer();
var count = 10;
var i = 0;
function resolve(value) {
i++;
expect(value).toBe(resolution);
expect(nextTurn).toBe(true);
if (i === count) {
done();
}
}
while (++i <= count) {
Q.when(deferred.promise, resolve);
}
deferred.resolve(resolution);
i = 0;
nextTurn = true;
});
it("observers called even after throw", function () {
var threw = false;
var deferred = Q.defer();
Q.when(deferred.promise, function () {
threw = true;
throw new Error(REASON);
});
var promise = Q.when(deferred.promise, function (value) {
expect(value).toEqual(10);
}, function () {
expect("not").toEqual("here");
});
deferred.resolve(10);
return promise;
});
it("returns `undefined` from the deferred's methods", function () {
expect(Q.defer().resolve()).toBe(undefined);
expect(Q.defer().reject()).toBe(undefined);
});
});
describe("always next tick", function () {
it("generated by `resolve`", function () {
var turn = 0;
var promise = Q.when(Q.resolve(), function () {
expect(turn).toEqual(1);
});
turn++;
return promise;
});
it("generated by `reject`", function () {
var turn = 0;
var promise = Q.when(Q.reject(), function () {
expect(true).toBe(false);
}, function () {
expect(turn).toEqual(1);
});
turn++;
return promise;
});
});
describe("progress", function () {
it("calls a single progress listener", function () {
var progressed = false;
var deferred = Q.defer();
var promise = Q.when(
deferred.promise,
function () {
expect(progressed).toBe(true);
},
function () {
expect(true).toBe(false);
},
function () {
progressed = true;
}
);
deferred.notify();
deferred.resolve();
return promise;
});
it("calls multiple progress listeners", function () {
var progressed1 = false;
var progressed2 = false;
var deferred = Q.defer();
var promise = Q.when(
deferred.promise,
function () {
expect(progressed1).toBe(true);
expect(progressed2).toBe(true);
},
function () {
expect(true).toBe(false);
},
function () {
progressed1 = true;
}
);
Q.when(deferred.promise, null, null, function () {
progressed2 = true;
});
deferred.notify();
deferred.resolve();
return promise;
});
it("calls all progress listeners even if one throws", function () {
var progressed1 = false;
var progressed2 = false;
var progressed3 = false;
var deferred = Q.defer();
var promise = Q.when(
deferred.promise,
function () {
expect(progressed1).toBe(true);
expect(progressed2).toBe(true);
expect(progressed3).toBe(true);
},
function () {
expect(true).toBe(false);
},
function () {
progressed1 = true;
}
);
Q.when(deferred.promise, null, null, function () {
progressed2 = true;
throw new Error("just a test, ok if it shows up in the console");
});
Q.when(deferred.promise, null, null, function () {
progressed3 = true;
});
deferred.notify();
deferred.resolve();
// In Node, swallow the eventually-thrown error.
function uncaughtExceptionHandler() { }
if (typeof process === "object") {
process.on("uncaughtException", uncaughtExceptionHandler);
promise.fin(function () {
process.removeListener("uncaughtException", uncaughtExceptionHandler);
});
}
return promise;
});
it("calls the progress listener even if later rejected", function () {
var progressed = false;
var deferred = Q.defer();
var promise = Q.when(
deferred.promise,
function () {
expect(true).toBe(false);
},
function () {
expect(progressed).toEqual(true);
},
function () {
progressed = true;
}
);
deferred.notify();
deferred.reject();
return promise;
});
it("calls the progress listener with the notify values", function () {
var progressValues = [];
var desiredProgressValues = [{}, {}, "foo", 5];
var deferred = Q.defer();
var promise = Q.when(
deferred.promise,
function () {
for (var i = 0; i < desiredProgressValues.length; ++i) {
var desired = desiredProgressValues[i];
var actual = progressValues[i];
expect(actual).toBe(desired);
}
},
function () {
expect(true).toBe(false);
},
function (value) {
progressValues.push(value);
}
);
for (var i = 0; i < desiredProgressValues.length; ++i) {
deferred.notify(desiredProgressValues[i]);
}
deferred.resolve();
return promise;
});
it("does not call the progress listener if notify is called after fulfillment", function () {
var deferred = Q.defer();
var called = false;
Q.when(deferred.promise, null, null, function () {
called = true;
});
deferred.resolve();
deferred.notify();
return Q.delay(10).then(function () {
expect(called).toBe(false);
});
});
it("does not call the progress listener if notify is called after rejection", function () {
var deferred = Q.defer();
var called = false;
Q.when(deferred.promise, null, null, function () {
called = true;
});
deferred.reject();
deferred.notify();
return Q.delay(10).then(function () {
expect(called).toBe(false);
});
});
it("should not save and re-emit progress notifications", function () {
var deferred = Q.defer();
var progressValues = [];
deferred.notify(1);
var promise = Q.when(
deferred.promise,
function () {
expect(progressValues).toEqual([2]);
},
function () {
expect(true).toBe(false);
},
function (progressValue) {
progressValues.push(progressValue);
}
);
deferred.notify(2);
deferred.resolve();
return promise;
});
it("should allow attaching progress listeners w/ .progress", function () {
var progressed = false;
var deferred = Q.defer();
deferred.promise.progress(function () {
progressed = true;
});
deferred.notify();
deferred.resolve();
return deferred.promise;
});
it("should allow attaching progress listeners w/ Q.progress", function () {
var progressed = false;
var deferred = Q.defer();
Q.progress(deferred.promise, function () {
progressed = true;
});
deferred.notify();
deferred.resolve();
return deferred.promise;
});
it("should call the progress listener with undefined context", function () {
var progressed = false;
var progressContext = {};
var deferred = Q.defer();
var promise = Q.when(
deferred.promise,
function () {
expect(progressed).toBe(true);
expect(progressContext).toBe(undefined);
},
function () {
expect(true).toBe(false);
},
function () {
progressed = true;
progressContext = this;
}
);
deferred.notify();
deferred.resolve();
return promise;
});
it("should forward only the first notify argument to listeners", function () {
var progressValueArrays = [];
var deferred = Q.defer();
var promise = Q.when(
deferred.promise,
function () {
expect(progressValueArrays).toEqual([[1], [2], [4]]);
},
function () {
expect(true).toBe(false);
},
function () {
var args = Array.prototype.slice.call(arguments);
progressValueArrays.push(args);
}
);
deferred.notify(1);
deferred.notify(2, 3);
deferred.notify(4, 5, 6);
deferred.resolve();
return promise;
});
it("should work with .then as well", function () {
var progressed = false;
var deferred = Q.defer();
var promise = deferred.promise.then(
function () {
expect(progressed).toBe(true);
},
function () {
expect(true).toBe(false);
},
function () {
progressed = true;
}
);
deferred.notify();
deferred.resolve();
return promise;
});
it("should re-throw all errors thrown by listeners to Q.onerror", function () {
var theError = new Error("boo!");
var def = Q.defer();
def.promise.progress(function () {
throw theError;
});
var deferred = Q.defer();
Q.onerror = function (error) {
expect(error).toBe(theError);
deferred.resolve();
};
Q.delay(100).then(deferred.reject);
def.notify();
return deferred.promise;
});
});
describe("promises for objects", function () {
describe("get", function () {
it("fulfills a promise", function () {
var deferred = Q.defer();
deferred.resolve({a: 1});
return deferred.promise.get("a")
.then(function (a) {
expect(a).toBe(1);
});
});
it("propagates a rejection", function () {
var exception = new Error("boo!");
return Q.fcall(function () {
throw exception;
})
.get("a")
.then(function () {
expect("be").toBe("not to be");
}, function (_exception) {
expect(_exception).toBe(exception);
});
});
});
describe("put", function () {
it("fulfills a promise", function () {
var object = {};
return Q.resolve(object)
.put('a', 1)
.then(function (result) {
expect(result).toBe(object);
expect(object.a).toBe(1);
});
});
it("propagates a rejection", function () {
var exception = new Error("Gah!");
return Q.reject(exception)
.put("a", 1)
.then(function (result) {
expect("frozen over").toBe("quite warm");
}, function (_exception) {
expect(_exception).toBe(exception);
});
});
});
describe("del", function () {
it("fulfills a promise", function () {
var object = {a: 10};
return Q.fcall(function () {
return object;
})
.del('a')
.then(function (result) {
expect('a' in object).toBe(false);
expect(result).toBe(object);
}, function (exception) {
expect("up").toBe("down");
});
});
it("propagates a rejection", function () {
var exception = new Error("hah-hah");
return Q.fcall(function () {
throw exception;
})
.del('a')
.then(function () {
expect(true).toBe(false);
}, function (_exception) {
expect(_exception).toBe(exception);
});
});
});
describe("post", function () {
it("fulfills a promise", function () {
var subject = {
a: function a(value) {
this._a = value;
return 1 + value;
}
};
return Q.when(Q.post(subject, 'a', [1]), function (two) {
expect(subject._a).toBe(1);
expect(two).toBe(2);
});
});
});
describe("invoke", function () {
it("fulfills a promise", function () {
var foo;
var subject = {
foo: function (_bar) {
return _bar;
},
bar: function (_foo, _bar) {
foo = _foo;
return this.foo(_bar);
}
};
return Q.invoke(subject, 'bar', 1, 2)
.then(function (two) {
expect(foo).toEqual(1);
expect(two).toEqual(2);
});
});
it("is rejected for undefined method", function () {
var subject = {};
return Q.resolve(subject)
.invoke('foo')
.then(function () {
expect("here").toEqual("not here");
}, function (exception) {
});
});
it("is rejected for undefined object", function () {
return Q.resolve()
.invoke('foo')
.then(function () {
expect("here").toEqual("not here");
}, function (exception) {
});
});
});
describe("keys", function () {
it("fulfills a promise", function () {
return Q.keys({a: 10, b: 20})
.then(function (keys) {
expect(keys).toEqual(['a', 'b']);
});
});
});
});
describe("promises for functions", function () {
describe("fapply", function () {
it("fulfills a promise using arguments", function () {
return Q.resolve(function (a, b, c) {
return a + b + c;
})
.fapply([1, 2, 3])
.then(function (sum) {
expect(sum).toEqual(6);
});
});
});
describe("fcall", function () {
it("fulfills a promise using arguments", function () {
return Q.resolve(function (a, b, c) {
return a + b + c;
})
.fcall(1, 2, 3)
.then(function (sum) {
expect(sum).toEqual(6);
});
});
});
describe("fbind", function () {
it("accepts a promise for a function", function () {
return Q.fbind(Q.resolve(function (high, low) {
return high - low;
}))
(2, 1)
.then(function (difference) {
expect(difference).toEqual(1);
});
});
it("chains partial application on a promise for a function", function () {
return Q.resolve(function (a, b) {
return a * b;
})
.fbind(2)(3)
.then(function (product) {
expect(product).toEqual(6);
});
});
it("returns a fulfilled promise", function () {
var result = {};
var bound = Q.fbind(function () {
return result;
});
return bound()
.then(function (_result) {
expect(_result).toBe(result);
});
});
it("returns a rejected promise from a thrown error", function () {
var exception = new Error("Boo!");
var bound = Q.fbind(function () {
throw exception;
});
return bound()
.then(function () {
expect("flying pigs").toBe("swillin' pigs");
}, function (_exception) {
expect(_exception).toBe(exception);
});
});
it("passes arguments through", function () {
var x = {}, y = {};
var bound = Q.fbind(function (a, b) {
expect(a).toBe(x);
expect(b).toBe(y);
});
return bound(x, y);
});
it("passes and also partially applies arguments", function () {
var x = {}, y = {};
var bound = Q.fbind(function (a, b) {
expect(a).toBe(x);
expect(b).toBe(y);
}, x);
return bound(y);
});
});
});
describe("valueOf", function () {
it("of fulfillment", function () {
expect(Q.resolve(10).valueOf()).toEqual(10);
});
it("of rejection", function () {
var error = new Error("In your face.");
var rejection = Q.reject(error);
expect(rejection.valueOf()).toBe(rejection);
expect(rejection.valueOf().exception).toBe(error);
});
it("of deferred", function () {
var deferred = Q.defer();
expect(deferred.promise.valueOf()).toBe(deferred.promise);
});
it("of deferred rejection", function () {
var deferred = Q.defer();
var error = new Error("Rejected!");
var rejection = Q.reject(error);
deferred.resolve(rejection);
expect(deferred.promise.valueOf()).toBe(rejection);
expect(deferred.promise.valueOf().exception).toBe(error);
});
it("of deferred fulfillment", function () {
var deferred = Q.defer();
deferred.resolve(10);
expect(deferred.promise.valueOf()).toBe(10);
});
it("of deferred deferred", function () {
var a = Q.defer();
var b = Q.defer();
a.resolve(b.promise);
expect(a.promise.valueOf()).toBe(b.promise);
});
it("should not convert `Date` instances to milliseconds", function () {
var promise = Q.resolve(new Date(2012, 10, 4));
expect(promise.valueOf()).toEqual(new Date(2012, 10, 4));
});
});
describe("promise states", function () {
it("of fulfilled value", function () {
expect(Q.isFulfilled(void 0)).toBe(true);
expect(Q.isRejected(false)).toBe(false);
expect(Q.isResolved(true)).toBe(true);
});
it("of fulfillment", function () {
var promise = Q.resolve(10);
expect(Q.isFulfilled(promise)).toBe(true);
expect(promise.isFulfilled()).toBe(true);
expect(Q.isRejected(promise)).toBe(false);
expect(promise.isRejected()).toBe(false);
expect(Q.isResolved(promise)).toBe(true);
expect(promise.isResolved()).toBe(true);
});
it("of rejection", function () {
var error = new Error("Oh, snap.");
var promise = Q.reject(error);
expect(promise.isFulfilled()).toBe(false);
expect(promise.isRejected()).toBe(true);
expect(promise.isResolved()).toBe(true);
});
it("of deferred", function () {
var deferred = Q.defer();
var promise = deferred.promise;
expect(promise.isFulfilled()).toBe(false);
expect(promise.isRejected()).toBe(false);
expect(promise.isResolved()).toBe(false);
});
it("of deferred rejection", function () {
var deferred = Q.defer();
var rejection = Q.reject(new Error("Rejected!"));
deferred.resolve(rejection);
var promise = deferred.promise;
expect(promise.isFulfilled()).toBe(false);
expect(promise.isRejected()).toBe(true);
expect(promise.isResolved()).toBe(true);
});
it("of deferred fulfillment", function () {
var deferred = Q.defer();
deferred.resolve(10);
var promise = deferred.promise;
expect(promise.isFulfilled()).toBe(true);
expect(promise.isRejected()).toBe(false);
expect(promise.isResolved()).toBe(true);
});
it("of deferred deferred", function () {
var a = Q.defer();
var b = Q.defer();
a.resolve(b.promise);
var promise = a.promise;
expect(promise.isFulfilled()).toBe(false);
expect(promise.isRejected()).toBe(false);
expect(promise.isResolved()).toBe(false);
});
});
describe("propagation", function () {
it("propagate through then with no callback", function () {
return Q.resolve(10)
.then()
.then(function (ten) {
expect(ten).toBe(10);
});
});
it("propagate through then with modifying callback", function () {
return Q.resolve(10)
.then(function (ten) {
return ten + 10;
})
.then(function (twen) {
expect(twen).toBe(20);
});
});
it("errback recovers from exception", function () {
var error = new Error("Bah!");
return Q.reject(error)
.then(null, function (_error) {
expect(_error).toBe(error);
return 10;
})
.then(function (value) {
expect(value).toBe(10);
});
});
it("rejection propagates through then with no errback", function () {
var error = new Error("Foolish mortals!");
return Q.reject(error)
.then()
.then(null, function (_error) {
expect(_error).toBe(error);
});
});
it("rejection intercepted and rethrown", function () {
var error = new Error("Foolish mortals!");
var nextError = new Error("Silly humans!");
return Q.reject(error)
.fail(function () {
throw nextError;
})
.then(null, function (_error) {
expect(_error).toBe(nextError);
});
});
it("resolution is forwarded through deferred promise", function () {
var a = Q.defer();
var b = Q.defer();
a.resolve(b.promise);
b.resolve(10);
return a.promise.then(function (eh) {
expect(eh).toEqual(10);
});
});
it("should propagate progress by default", function () {
var d = Q.defer();
var progressValues = [];
var promise = d.promise
.then()
.then(
function () {
expect(progressValues).toEqual([1]);
},
function () {
expect(true).toBe(false);
},
function (progressValue) {
progressValues.push(progressValue);
}
);
d.notify(1);
d.resolve();
return promise;
});
it("should allow translation of progress in the progressback", function () {
var d = Q.defer();
var progressValues = [];
var promise = d.promise
.progress(function (p) {
return p + 5;
})
.then(
function () {
expect(progressValues).toEqual([10]);
},
function () {
expect(true).toBe(false);
},
function (progressValue) {
progressValues.push(progressValue);
}
);
d.notify(5);
d.resolve();
return promise;
});
it("should stop progress propagation if an error is thrown", function () {
var def = Q.defer();
var p2 = def.promise.progress(function () {
throw new Error("boo!");
});
Q.onerror = function () { /* just swallow it for this test */ };
var progressValues = [];
var result = p2.then(
function () {
expect(progressValues).toEqual([]);
},
function () {
expect(true).toBe(false);
},
function (progressValue) {
progressValues.push(progressValue);
}
);
def.notify();
def.resolve();
return result;
});
});
describe("all", function () {
it("resolves when passed an empty array", function () {
return Q.all([]);
});
it("resolves after any constituent promise is rejected", function () {
var toResolve = Q.defer(); // never resolve
var toReject = Q.defer();
var promises = [toResolve.promise, toReject.promise];
var promise = Q.all(promises);
toReject.reject(new Error("Rejected"));
return Q.delay(250)
.then(function () {
expect(promise.isRejected()).toBe(true);
})
.timeout(1000);
});
});