Skip to content

Commit e92d548

Browse files
committed
Add tests
1 parent a7f99f3 commit e92d548

File tree

3 files changed

+310
-139
lines changed

3 files changed

+310
-139
lines changed

test/css/test.css

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
/*
2+
* jQuery async plugin - TestRun
3+
*/
4+
5+
* {
6+
font-family: tahoma, verdana, sans-serif;
7+
}
8+
9+
html, body {
10+
margin: 0;
11+
padding: 0;
12+
}
13+
14+
body {
15+
color: #555;
16+
background: #f8f8ff;
17+
text-align: center;
18+
}
19+
20+
.container {
21+
margin: 0 auto;
22+
text-align: left;
23+
max-width: 960px;
24+
}
25+
26+
pre, code {
27+
font-family: Consolas, 'Courier New', Courier, Monaco, monospace;
28+
font-size: 14px;
29+
line-height: 1.2;
30+
}
31+
32+
.test-unit {
33+
padding: 1em;
34+
margin: 1em;
35+
}
36+
37+
.test-unit-title {
38+
font-weight: bold;
39+
}
40+
41+
.test-unit-code {
42+
margin: 1em;
43+
padding: 1em;
44+
color: #333;
45+
background: #f2f2fc;
46+
border: 1px solid #7575c1;
47+
white-space: pre;
48+
white-space: pre-wrap;
49+
}
50+
51+
.test-unit-expect {
52+
float: left;
53+
color: #117733;
54+
padding: 0.5em;
55+
}
56+
57+
.test-unit-actual {
58+
float: left;
59+
margin-top: 1em;
60+
padding: 0.5em;
61+
}
62+
63+
.test-unit-expect-wrapper, .test-unit-result-wrapper {
64+
margin: 1em;
65+
padding: 1em;
66+
border: 1px solid #7575c1;
67+
}
68+
69+
.test-unit-expect-wrapper {
70+
background: #e2f8e8;
71+
}
72+
73+
.test-unit-result-wrapper {
74+
background: #e2e2f8;
75+
}
76+
77+
.test-unit-expect-text, .test-unit-actual-text {
78+
float: left;
79+
color: #333;
80+
margin-right: 1em;
81+
}
82+
83+
.test-unit-success {
84+
color: #3544d1;
85+
}
86+
87+
.test-unit-failure {
88+
color: #c94754;
89+
}
90+
91+
hr {
92+
border: 1px solid #ccc;
93+
}
94+
95+
.test-unit-result-end {
96+
font-size: 130%;
97+
font-weight: bold;
98+
color: #333;
99+
}
100+
101+
.test-unit-result-length {
102+
font-size: 110%;
103+
font-weight: bold;
104+
}
105+
106+
.test-unit-result-success {
107+
color: #3544d1;
108+
font-size: 120%;
109+
font-weight: bold;
110+
}
111+
112+
.test-unit-result-failure {
113+
color: #c94754;
114+
font-size: 120%;
115+
font-weight: bold;
116+
}
117+
118+
.clearfix:after {
119+
content: ".";
120+
display: block;
121+
width: 0;
122+
height: 0;
123+
max-width: 1px;
124+
max-height: 1px;
125+
font-size: 0;
126+
line-height: 0;
127+
clear: both;
128+
overflow: hidden;
129+
visibility: hidden;
130+
}

test/test.js renamed to test/js/test.js

Lines changed: 171 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
/*
22
* jQuery async plugin - TestRun
3-
*
43
*/
5-
var TestRun = function() {
4+
var TestRun = (function() {
65
'use strict';
76

87
var slice = Array.prototype.slice,
@@ -43,6 +42,16 @@ var TestRun = function() {
4342
});
4443
},
4544
expect: 2
45+
}, {
46+
title: '$.async.fail() test',
47+
code: function() {
48+
return $.async.fail(1).addCallback(function(res) {
49+
return 'error';
50+
}).addErrback(function(err) {
51+
return $.async.isError(err);
52+
})
53+
},
54+
expect: true
4655
}, {
4756
title: 'Deferred callback response',
4857
code: function() {
@@ -69,13 +78,92 @@ var TestRun = function() {
6978
return 'hoge';
7079
}).addBoth(function(res) {
7180
r.push(res);
72-
return res + 1;
81+
return $.Deferred().addCallback(function(i) {
82+
return res + i;
83+
}).callback(1);
7384
}).addCallback(function(res) {
7485
r.push(res);
7586
return r;
7687
}).callback(1);
7788
},
7889
expect: [1, 2, new Error('error'), 'hoge', 'hoge1']
90+
}, {
91+
title: 'Native $.Deferred() test',
92+
code: function() {
93+
var test = function() {
94+
var d = $.Deferred();
95+
96+
setTimeout(function() {
97+
d.resolve();
98+
}, 1000);
99+
100+
return d.promise();
101+
};
102+
103+
var d = $.Deferred();
104+
var i = 0;
105+
var start = Date.now();
106+
107+
test().then(function() {
108+
d.callback(++i);
109+
});
110+
111+
if (i !== 0) {
112+
throw new Error('error');
113+
}
114+
115+
return d.addCallback(function(i) {
116+
return Date.now() - start >= 1000 && i;
117+
});
118+
},
119+
expect: 1
120+
}, {
121+
title: 'Cancel Deferred chain',
122+
code: function() {
123+
var d = $.Deferred();
124+
125+
d.addCallback(function(res) {
126+
return res + 1;
127+
});
128+
129+
d.cancel();
130+
d.addCallback(function(res) {
131+
return res + 1;
132+
});
133+
134+
return d.callback(1);
135+
},
136+
expect: 2
137+
}, {
138+
title: 'Cancel Deferred chain in callback',
139+
code: function() {
140+
var d = $.Deferred();
141+
142+
return d.addCallback(function(res) {
143+
return res + 1;
144+
}).addCallback(function(res) {
145+
d.cancel();
146+
return res + 1;
147+
}).addCallback(function(res) {
148+
return res + 1;
149+
}).callback(1);
150+
},
151+
expect: 3
152+
}, {
153+
title: 'Cancel Deferred chain in callback use this',
154+
code: function() {
155+
var d = $.Deferred();
156+
157+
return d.addCallback(function(res) {
158+
return res + 1;
159+
}).addCallback(function(res) {
160+
this.cancel();
161+
return res + 1;
162+
}).addCallback(function(res) {
163+
return res + 1;
164+
}).callback(1);
165+
},
166+
expect: 3
79167
}, {
80168
title: '$.async() test',
81169
code: function() {
@@ -87,21 +175,82 @@ var TestRun = function() {
87175
},
88176
expect: 2
89177
}, {
90-
title: 'testパターン思いつかない',
178+
title: '$.async.maybeDeferred() test',
91179
code: function() {
92-
return $.async(function() {
93-
return '少しずつ';
94-
}).addCallback(function(res) {
95-
return res + 'パターン';
180+
return $.async.maybeDeferred(function() {
181+
return 1;
96182
}).addCallback(function(res) {
97-
return res + '増やし中';
98-
}).addBoth(function(res) {
99-
return res + '・・・';
183+
return $.async.maybeDeferred(1).addCallback(function(r) {
184+
return res + r;
185+
});
100186
});
101187
},
102-
expect: 'tesst'
103-
}];
188+
expect: 2
189+
}, {
190+
title: '$.async.maybeDeferreds() test',
191+
code: function() {
192+
var list = $.async.maybeDeferreds(1, 2, 'foo', 'bar',
193+
function() { return 5 },
194+
$.async.succeed(100));
195+
196+
for (var i = 0, len = list.length; i < len; i++) {
197+
if (!$.async.isDeferred(list[i])) {
198+
return false;
199+
}
200+
}
201+
202+
return true;
203+
},
204+
expect: true
205+
}, {
206+
title: '$.async.wait() test',
207+
code: function() {
208+
var start = Date.now();
209+
210+
return $.async.wait(1).addCallback(function() {
211+
return Date.now() - start >= 1000;
212+
});
213+
},
214+
expect: true
215+
}, {
216+
title: '$.async.callLater() test',
217+
code: function() {
218+
var start = Date.now();
104219

220+
return $.async.callLater(1, function() {
221+
return 1000;
222+
}).addCallback(function(time) {
223+
return time === 1000 && Date.now() - start >= time;
224+
});
225+
},
226+
expect: true
227+
}, {
228+
title: '$.async.till() test',
229+
code: function() {
230+
var value = null;
231+
232+
$.async.wait(1).addCallback(function() {
233+
value = 1;
234+
});
235+
236+
if (value !== null) {
237+
throw new Error('error');
238+
}
239+
240+
var start = Date.now();
241+
return $.async.till(function() {
242+
return value !== null;
243+
}).addCallback(function() {
244+
if (Date.now() - start >= 1000) {
245+
return true;
246+
}
247+
return false;
248+
}).addCallback(function(res) {
249+
return res === true && value === 1;
250+
})
251+
},
252+
expect: true
253+
}];
105254

106255

107256
function exec() {
@@ -178,6 +327,13 @@ var TestRun = function() {
178327
], function(i, el) {
179328
el.appendTo('#result');
180329
});
330+
331+
if (results.success.length === 0) {
332+
$('.test-unit-result-success').hide();
333+
}
334+
if (results.failure.length === 0) {
335+
$('.test-unit-result-failure').hide();
336+
}
181337
}
182338
}
183339

@@ -385,9 +541,8 @@ var TestRun = function() {
385541
exec: exec,
386542
results: results
387543
};
388-
}();
544+
}());
389545

390-
$(function() {
546+
$(document).ready(function() {
391547
TestRun.exec();
392548
});
393-

0 commit comments

Comments
 (0)