1
1
/*
2
2
* jQuery async plugin - TestRun
3
- *
4
3
*/
5
- var TestRun = function ( ) {
4
+ var TestRun = ( function ( ) {
6
5
'use strict' ;
7
6
8
7
var slice = Array . prototype . slice ,
@@ -43,6 +42,16 @@ var TestRun = function() {
43
42
} ) ;
44
43
} ,
45
44
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
46
55
} , {
47
56
title : 'Deferred callback response' ,
48
57
code : function ( ) {
@@ -69,13 +78,92 @@ var TestRun = function() {
69
78
return 'hoge' ;
70
79
} ) . addBoth ( function ( res ) {
71
80
r . push ( res ) ;
72
- return res + 1 ;
81
+ return $ . Deferred ( ) . addCallback ( function ( i ) {
82
+ return res + i ;
83
+ } ) . callback ( 1 ) ;
73
84
} ) . addCallback ( function ( res ) {
74
85
r . push ( res ) ;
75
86
return r ;
76
87
} ) . callback ( 1 ) ;
77
88
} ,
78
89
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
79
167
} , {
80
168
title : '$.async() test' ,
81
169
code : function ( ) {
@@ -87,21 +175,82 @@ var TestRun = function() {
87
175
} ,
88
176
expect : 2
89
177
} , {
90
- title : 'testパターン思いつかない ' ,
178
+ title : '$.async.maybeDeferred() test ' ,
91
179
code : function ( ) {
92
- return $ . async ( function ( ) {
93
- return '少しずつ' ;
94
- } ) . addCallback ( function ( res ) {
95
- return res + 'パターン' ;
180
+ return $ . async . maybeDeferred ( function ( ) {
181
+ return 1 ;
96
182
} ) . 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
+ } ) ;
100
186
} ) ;
101
187
} ,
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 ( ) ;
104
219
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
+ } ] ;
105
254
106
255
107
256
function exec ( ) {
@@ -178,6 +327,13 @@ var TestRun = function() {
178
327
] , function ( i , el ) {
179
328
el . appendTo ( '#result' ) ;
180
329
} ) ;
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
+ }
181
337
}
182
338
}
183
339
@@ -385,9 +541,8 @@ var TestRun = function() {
385
541
exec : exec ,
386
542
results : results
387
543
} ;
388
- } ( ) ;
544
+ } ( ) ) ;
389
545
390
- $ ( function ( ) {
546
+ $ ( document ) . ready ( function ( ) {
391
547
TestRun . exec ( ) ;
392
548
} ) ;
393
-
0 commit comments