all.js
2.85 KB
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
define([
"doh/main",
"dojo/Deferred",
"dojo/promise/all"
], function(doh, Deferred, all){
var tests = {
"all() with array argument": function(t){
var obj1 = {}, obj2 = {}, obj3 = {};
var received;
all([this.deferred, new Deferred().resolve(obj2), obj3]).then(function(result){ received = result; });
this.deferred.resolve(obj1);
t.t(received[0] === obj1);
t.t(received[1] === obj2);
t.t(received[2] === obj3);
},
"all() with object argument": function(t){
var obj1 = {}, obj2 = {}, obj3 = {};
var received;
all({ a: this.deferred, b: new Deferred().resolve(obj2), c: obj3 }).then(function(result){ received = result; });
this.deferred.resolve(obj1);
t.t(received.a === obj1);
t.t(received.b === obj2);
t.t(received.c === obj3);
},
"all() without arguments": function(t){
var received;
all().then(function(){ received = arguments; });
t.is(received.length, 1);
t.t(typeof received[0] === "undefined");
},
"all() with single non-object argument": function(t){
var received;
all(null).then(function(){ received = arguments; });
t.is(received.length, 1);
t.t(typeof received[0] === "undefined");
},
"all() with empty array": function(t){
var received;
all([]).then(function(result){ received = result; });
t.is(received, []);
},
"all() with empty object": function(t){
var received;
all({}).then(function(result){ received = result; });
t.is(received, {});
},
"all() with one rejected promise": function(t){
var obj = {};
var received;
all([this.deferred, new Deferred().reject(obj), {}]).then(null, function(result){ received = result; });
t.t(received === obj);
},
"all() with one promise rejected later": function(t){
var obj = {};
var received;
all([this.deferred, new Deferred(), new Deferred()]).then(null, function(result){ received = result; });
this.deferred.reject(obj);
t.t(received === obj);
},
"all() with multiple promises rejected later": function(t){
var obj = {};
var deferred2 = new Deferred();
var received;
all([this.deferred, deferred2, new Deferred()]).then(null, function(result){ received = result; });
this.deferred.reject(obj);
deferred2.reject({});
t.t(received === obj);
},
"all() cancel only affects returned promise, not those we're waiting for": function(t){
var obj = {};
var canceled = false;
var deferred2 = new Deferred(function(){ canceled = true; });
var received;
var promise = all([this.deferred, deferred2, new Deferred()]).then(null, function(result){ received = result; });
promise.cancel(obj);
t.t(received === obj);
t.f(canceled);
}
};
var wrapped = [];
for(var name in tests){
wrapped.push({
name: name,
setUp: setUp,
runTest: tests[name]
});
}
function setUp(){
this.deferred = new Deferred;
}
doh.register("tests.promise.all", wrapped);
});