-
Notifications
You must be signed in to change notification settings - Fork 127
/
tests.js
201 lines (169 loc) · 4.24 KB
/
tests.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
/*
* Useful functions
*/
function success(name){
console.log("Success: ", name);
}
function failure(name){
console.log("Error: ", name);
}
function assert(bool, name) {
if (bool)
success(name);
else
failure(name);
}
function sync_return(value) {
var p = new promise.Promise();
p.done(null, value);
return p;
}
function async_return(value) {
var p = new promise.Promise();
setTimeout(function(){
p.done(null, value);
});
return p;
}
function late(n) {
var p = new promise.Promise();
setTimeout(function() {
p.done(null, n);
}, n);
return p;
}
/*
* Tests
*/
function test_simple_synchronous() {
sync_return(123).then(function(error, result) {
assert(result === 123, 'simple synchronous test');
});
}
function test_simple_asynchronous() {
async_return(123).then(function(error, result) {
assert(result === 123, 'simple asynchronous test');
});
}
function test_multi_results() {
p = new promise.Promise();
p.then(function (res, a, b, c) {
assert(a === 1, 'multiple results (1/3)');
});
setTimeout(
function () {
p.then(function (res, a, b, c) {
assert(b === 2, 'multiple results (2/3)');
});
p.done(null, 1, 2, 3);
p.then(function (res, a, b, c) {
assert(c === 3, 'multiple results (3/3)');
});
});
}
function test_join() {
var d = new Date();
promise.join([late(400), late(800)]).then(
function(results) {
var delay = new Date() - d;
assert(results[0][1] === 400 && results[1][1] === 800,
"join() result");
assert(700 < delay && delay < 900, "joining functions");
}
);
}
function test_join_empty() {
var joined = false;
promise.join([]).then(
function() {
joined = true;
}
);
setTimeout(
function() {
assert(joined, "empty join");
}, 200);
}
var to_chain = {
d: new Date(),
f1: function() {
return late(100);
},
f2 : function(err, n) {
return late(n + 200);
},
f3: function(err, n) {
return late(n + 300);
},
f4: function(err, n) {
return late(n + 400);
},
check: function(err, n) {
var delay = new Date() - to_chain.d;
assert(n === 1000, "chain() result");
assert(1900 < delay && delay < 2400, "chaining functions()");
}
};
function test_then_then() {
var p = new promise.Promise();
p.then(
to_chain.f1
).then(
to_chain.f2
).then(
to_chain.f3
).then(
to_chain.f4
).then(
to_chain.check
);
}
function test_chain() {
promise.chain(
[to_chain.f1,
to_chain.f2,
to_chain.f3,
to_chain.f4]
).then(
to_chain.check
);
}
function test_ajax_timeout () {
var realXMLHttpRequest = window.XMLHttpRequest;
var isAborted = false;
var defaultTimeout = promise.ajaxTimeout;
promise.ajaxTimeout = 2000;
window.XMLHttpRequest = function () {
this.readyState = 4;
this.status = 200;
this.responseText = 'a response text';
this.open = function () {};
this.setRequestHeader = function () {};
this.abort = function () { isAborted = true; };
this.onreadystatechange = function () {};
var self = this;
this.send = function () {
setTimeout(function() {
self.onreadystatechange();
}, 3000);
};
};
promise.get('/').then(
function(err, text, xhr) {
assert(isAborted === true, 'Ajax timeout must abort xhr');
assert(err === promise.ETIMEOUT, 'Ajax timeout must report error');
assert(text === '', 'Ajax timeout must return empty response');
window.XMLHttpRequest = realXMLHttpRequest;
promise.ajaxTimeout = defaultTimeout;
});
}
function test() {
test_simple_synchronous();
test_simple_asynchronous();
test_multi_results();
test_join();
test_join_empty();
test_then_then();
test_chain();
test_ajax_timeout();
}