first.js
2.76 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
define([
"doh/main",
"dojo/Deferred",
"dojo/promise/first"
], function(doh, Deferred, first){
var tests = {
"first() with array argument": function(t){
var obj = {};
var received;
first([this.deferred, new Deferred().resolve(obj), {}]).then(function(result){ received = result; });
this.deferred.resolve({});
t.t(received === obj);
},
"with object argument": function(t){
var obj = {};
var received;
first({ a: this.deferred, b: new Deferred().resolve(obj), c: {} }).then(function(result){ received = result; });
this.deferred.resolve({});
t.t(received === obj);
},
"first() without arguments": function(t){
var received;
first().then(function(){ received = arguments; });
t.is(received.length, 1);
t.t(typeof received[0] === "undefined");
},
"first() with single non-object argument": function(t){
var received;
first(null).then(function(){ received = arguments; });
t.is(received.length, 1);
t.t(typeof received[0] === "undefined");
},
"first() with empty array": function(t){
var received = false;
first([]).then(function(result){ received = result; });
t.t(typeof received === "undefined");
},
"first() with empty object": function(t){
var received = false;
first({}).then(function(result){ received = result; });
t.t(typeof received === "undefined");
},
"first() with one rejected promise": function(t){
var obj = {};
var received;
first([this.deferred, new Deferred().reject(obj), {}]).then(null, function(result){ received = result; });
t.t(received === obj);
},
"first() with one promise rejected later": function(t){
var obj = {};
var received;
first([this.deferred, new Deferred(), new Deferred()]).then(null, function(result){ received = result; });
this.deferred.reject(obj);
t.t(received === obj);
},
"first() with multiple promises rejected later": function(t){
var obj = {};
var deferred2 = new Deferred();
var received;
first([this.deferred, deferred2, new Deferred()]).then(null, function(result){ received = result; });
this.deferred.reject(obj);
deferred2.reject({});
t.t(received === obj);
},
"first() 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 = first([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.first", wrapped);
});