1
1
( function ( ) {
2
2
3
- module ( 'call-remote-callbacks' , {
4
- setup : function ( ) {
3
+ QUnit . module ( 'call-remote-callbacks' , {
4
+ beforeEach : function ( ) {
5
5
$ ( '#qunit-fixture' ) . append ( $ ( '<form />' , {
6
6
action : '/echo' , method : 'get' , 'data-remote' : 'true'
7
7
} ) )
8
8
} ,
9
- teardown : function ( ) {
9
+ afterEach : function ( ) {
10
10
$ ( document ) . undelegate ( 'form[data-remote]' , 'ajax:beforeSend' )
11
11
$ ( document ) . undelegate ( 'form[data-remote]' , 'ajax:before' )
12
12
$ ( document ) . undelegate ( 'form[data-remote]' , 'ajax:send' )
@@ -17,15 +17,15 @@ module('call-remote-callbacks', {
17
17
} )
18
18
19
19
function submit ( fn ) {
20
- var form = $ ( 'form' )
20
+ var form = $ ( '#qunit-fixture form' )
21
21
22
22
if ( fn ) fn ( form )
23
23
form . triggerNative ( 'submit' )
24
-
25
- setTimeout ( function ( ) { start ( ) } , 13 )
26
24
}
27
25
28
- asyncTest ( 'modifying form fields with "ajax:before" sends modified data in request' , 3 , function ( ) {
26
+ QUnit . test ( 'modifying form fields with "ajax:before" sends modified data in request' , function ( assert ) {
27
+ const done = assert . async ( )
28
+
29
29
$ ( 'form[data-remote]' )
30
30
. append ( $ ( '<input type="text" name="user_name" value="john">' ) )
31
31
. append ( $ ( '<input type="text" name="removed_user_name" value="john">' ) )
@@ -40,62 +40,67 @@ asyncTest('modifying form fields with "ajax:before" sends modified data in reque
40
40
41
41
submit ( function ( form ) {
42
42
form . bindNative ( 'ajax:success' , function ( e , data , status , xhr ) {
43
- equal ( data . params . user_name , 'steve' , 'modified field value should have been submitted' )
44
- equal ( data . params . other_user_name , 'jonathan' , 'added field value should have been submitted' )
45
- equal ( data . params . removed_user_name , undefined , 'removed field value should be undefined' )
43
+ assert . equal ( data . params . user_name , 'steve' , 'modified field value should have been submitted' )
44
+ assert . equal ( data . params . other_user_name , 'jonathan' , 'added field value should have been submitted' )
45
+ assert . equal ( data . params . removed_user_name , undefined , 'removed field value should be undefined' )
46
+ done ( )
46
47
} )
47
48
} )
48
49
} )
49
50
50
- asyncTest ( 'modifying data("type") with "ajax:before" requests new dataType in request' , 1 , function ( ) {
51
+ QUnit . test ( 'modifying data("type") with "ajax:before" requests new dataType in request' , function ( assert ) {
51
52
$ ( 'form[data-remote]' ) . data ( 'type' , 'html' )
52
53
. bindNative ( 'ajax:before' , function ( ) {
53
54
this . setAttribute ( 'data-type' , 'xml' )
54
55
} )
55
56
56
57
submit ( function ( form ) {
57
58
form . bindNative ( 'ajax:beforeSend' , function ( e , xhr , settings ) {
58
- equal ( settings . dataType , 'xml' , 'modified dataType should have been requested' )
59
+ assert . equal ( settings . dataType , 'xml' , 'modified dataType should have been requested' )
59
60
} )
60
61
} )
61
62
} )
62
63
63
- asyncTest ( 'setting data("with-credentials",true) with "ajax:before" uses new setting in request' , 1 , function ( ) {
64
+ QUnit . test ( 'setting data("with-credentials",true) with "ajax:before" uses new setting in request' , function ( assert ) {
64
65
$ ( 'form[data-remote]' ) . data ( 'with-credentials' , false )
65
66
. bindNative ( 'ajax:before' , function ( ) {
66
67
this . setAttribute ( 'data-with-credentials' , true )
67
68
} )
68
69
69
70
submit ( function ( form ) {
70
71
form . bindNative ( 'ajax:beforeSend' , function ( e , xhr , settings ) {
71
- equal ( settings . withCredentials , true , 'setting modified in ajax:before should have forced withCredentials request' )
72
+ assert . equal ( settings . withCredentials , true , 'setting modified in ajax:before should have forced withCredentials request' )
72
73
} )
73
74
} )
74
75
} )
75
76
76
- asyncTest ( 'stopping the "ajax:beforeSend" event aborts the request' , 1 , function ( ) {
77
+ QUnit . test ( 'stopping the "ajax:beforeSend" event aborts the request' , function ( assert ) {
78
+ const done = assert . async ( )
79
+
77
80
submit ( function ( form ) {
78
81
form . bindNative ( 'ajax:beforeSend' , function ( e ) {
79
- ok ( true , 'aborting request in ajax:beforeSend' )
82
+ assert . ok ( true , 'aborting request in ajax:beforeSend' )
80
83
e . preventDefault ( )
81
84
} )
82
85
form . unbind ( 'ajax:send' ) . bindNative ( 'ajax:send' , function ( ) {
83
- ok ( false , 'ajax:send should not run' )
86
+ assert . ok ( false , 'ajax:send should not run' )
84
87
} )
85
88
form . bindNative ( 'ajax:error' , function ( e , response , status , xhr ) {
86
- ok ( false , 'ajax:error should not run' )
89
+ assert . ok ( false , 'ajax:error should not run' )
87
90
} )
88
91
form . bindNative ( 'ajax:complete' , function ( ) {
89
- ok ( false , 'ajax:complete should not run' )
92
+ assert . ok ( false , 'ajax:complete should not run' )
90
93
} )
91
94
} )
95
+
96
+ setTimeout ( function ( ) { done ( ) } , 13 )
92
97
} )
93
98
94
99
function skipIt ( ) {
95
100
// This test cannot work due to the security feature in browsers which makes the value
96
101
// attribute of file input fields readonly, so it cannot be set with default value.
97
102
// This is what the test would look like though if browsers let us automate this test.
98
- asyncTest ( 'non-blank file form input field should abort remote request, but submit normally' , 5 , function ( ) {
103
+ QUnit . test ( 'non-blank file form input field should abort remote request, but submit normally' , function ( assert ) {
99
104
var form = $ ( 'form[data-remote]' )
100
105
. append ( $ ( '<input type="file" name="attachment" value="default.png">' ) )
101
106
. bindNative ( 'ajax:beforeSend' , function ( ) {
@@ -118,7 +123,7 @@ function skipIt() {
118
123
} , 13 )
119
124
} )
120
125
121
- asyncTest ( 'file form input field should not abort remote request if file form input does not have a name attribute' , 5 , function ( ) {
126
+ QUnit . test ( 'file form input field should not abort remote request if file form input does not have a name attribute' , function ( assert ) {
122
127
var form = $ ( 'form[data-remote]' )
123
128
. append ( $ ( '<input type="file" value="default.png">' ) )
124
129
. bindNative ( 'ajax:beforeSend' , function ( ) {
@@ -139,7 +144,7 @@ function skipIt() {
139
144
} , 13 )
140
145
} )
141
146
142
- asyncTest ( 'blank file input field should abort request entirely if handler bound to "ajax:aborted:file" event that returns false' , 1 , function ( ) {
147
+ QUnit . test ( 'blank file input field should abort request entirely if handler bound to "ajax:aborted:file" event that returns false' , function ( assert ) {
143
148
var form = $ ( 'form[data-remote]' )
144
149
. append ( $ ( '<input type="file" name="attachment" value="default.png">' ) )
145
150
. bindNative ( 'ajax:beforeSend' , function ( ) {
@@ -161,79 +166,103 @@ function skipIt() {
161
166
} )
162
167
}
163
168
164
- asyncTest ( '"ajax:beforeSend" can be observed and stopped with event delegation' , 1 , function ( ) {
169
+ QUnit . test ( '"ajax:beforeSend" can be observed and stopped with event delegation' , function ( assert ) {
170
+ const done = assert . async ( )
171
+
165
172
$ ( document ) . delegate ( 'form[data-remote]' , 'ajax:beforeSend' , function ( e ) {
166
- ok ( true , 'ajax:beforeSend observed with event delegation' )
173
+ assert . ok ( true , 'ajax:beforeSend observed with event delegation' )
167
174
e . preventDefault ( )
168
175
} )
169
176
170
177
submit ( function ( form ) {
171
178
form . unbind ( 'ajax:send' ) . bindNative ( 'ajax:send' , function ( ) {
172
- ok ( false , 'ajax:send should not run' )
179
+ assert . ok ( false , 'ajax:send should not run' )
173
180
} )
174
181
form . bindNative ( 'ajax:complete' , function ( ) {
175
- ok ( false , 'ajax:complete should not run' )
182
+ assert . ok ( false , 'ajax:complete should not run' )
176
183
} )
177
184
} )
185
+
186
+ setTimeout ( function ( ) { done ( ) } , 13 )
178
187
} )
179
188
180
- asyncTest ( '"ajax:beforeSend", "ajax:send", "ajax:success" and "ajax:complete" are triggered' , 8 , function ( ) {
189
+ QUnit . test ( '"ajax:beforeSend", "ajax:send", "ajax:success" and "ajax:complete" are triggered' , function ( assert ) {
190
+ const done = assert . async ( 4 )
191
+
181
192
submit ( function ( form ) {
182
193
form . bindNative ( 'ajax:beforeSend' , function ( e , xhr , settings ) {
183
- ok ( xhr . setRequestHeader , 'first argument to "ajax:beforeSend" should be an XHR object' )
184
- equal ( settings . url , '/echo' , 'second argument to "ajax:beforeSend" should be a settings object' )
194
+ assert . ok ( xhr . setRequestHeader , 'first argument to "ajax:beforeSend" should be an XHR object' )
195
+ assert . equal ( settings . url , '/echo' , 'second argument to "ajax:beforeSend" should be a settings object' )
196
+ done ( )
185
197
} )
186
198
form . bindNative ( 'ajax:send' , function ( e , xhr ) {
187
- ok ( xhr . abort , 'first argument to "ajax:send" should be an XHR object' )
199
+ assert . ok ( xhr . abort , 'first argument to "ajax:send" should be an XHR object' )
200
+ done ( )
188
201
} )
189
202
form . bindNative ( 'ajax:success' , function ( e , data , status , xhr ) {
190
- ok ( data . REQUEST_METHOD , 'first argument to ajax:success should be a data object' )
191
- equal ( status , 'OK' , 'second argument to ajax:success should be a status string' )
192
- ok ( xhr . getResponseHeader , 'third argument to "ajax:success" should be an XHR object' )
203
+ assert . ok ( data . REQUEST_METHOD , 'first argument to ajax:success should be a data object' )
204
+ assert . equal ( status , 'OK' , 'second argument to ajax:success should be a status string' )
205
+ assert . ok ( xhr . getResponseHeader , 'third argument to "ajax:success" should be an XHR object' )
206
+ done ( )
193
207
} )
194
208
form . bindNative ( 'ajax:complete' , function ( e , xhr , status ) {
195
- ok ( xhr . getResponseHeader , 'first argument to "ajax:complete" should be an XHR object' )
196
- equal ( status , 'OK' , 'second argument to ajax:complete should be a status string' )
209
+ assert . ok ( xhr . getResponseHeader , 'first argument to "ajax:complete" should be an XHR object' )
210
+ assert . equal ( status , 'OK' , 'second argument to ajax:complete should be a status string' )
211
+ done ( )
197
212
} )
198
213
} )
199
214
} )
200
215
201
- asyncTest ( '"ajax:beforeSend", "ajax:send", "ajax:error" and "ajax:complete" are triggered on error' , 8 , function ( ) {
216
+ QUnit . test ( '"ajax:beforeSend", "ajax:send", "ajax:error" and "ajax:complete" are triggered on error' , function ( assert ) {
217
+ const done = assert . async ( 4 )
218
+
202
219
submit ( function ( form ) {
203
220
form . attr ( 'action' , '/error' )
204
- form . bindNative ( 'ajax:beforeSend' , function ( arg ) { ok ( true , 'ajax:beforeSend' ) } )
205
- form . bindNative ( 'ajax:send' , function ( arg ) { ok ( true , 'ajax:send' ) } )
221
+ form . bindNative ( 'ajax:beforeSend' , function ( arg ) {
222
+ assert . ok ( true , 'ajax:beforeSend' )
223
+ done ( )
224
+ } )
225
+ form . bindNative ( 'ajax:send' , function ( arg ) {
226
+ assert . ok ( true , 'ajax:send' )
227
+ done ( )
228
+ } )
206
229
form . bindNative ( 'ajax:error' , function ( e , response , status , xhr ) {
207
- equal ( response , '' , 'first argument to ajax:error should be an HTTP status response' )
208
- equal ( status , 'Forbidden' , 'second argument to ajax:error should be a status string' )
209
- ok ( xhr . getResponseHeader , 'third argument to "ajax:error" should be an XHR object' )
230
+ assert . equal ( response , '' , 'first argument to ajax:error should be an HTTP status response' )
231
+ assert . equal ( status , 'Forbidden' , 'second argument to ajax:error should be a status string' )
232
+ assert . ok ( xhr . getResponseHeader , 'third argument to "ajax:error" should be an XHR object' )
210
233
// Opera returns "0" for HTTP code
211
- equal ( xhr . status , window . opera ? 0 : 403 , 'status code should be 403' )
234
+ assert . equal ( xhr . status , window . opera ? 0 : 403 , 'status code should be 403' )
235
+ done ( )
212
236
} )
213
237
form . bindNative ( 'ajax:complete' , function ( e , xhr , status ) {
214
- ok ( xhr . getResponseHeader , 'first argument to "ajax:complete" should be an XHR object' )
215
- equal ( status , 'Forbidden' , 'second argument to ajax:complete should be a status string' )
238
+ assert . ok ( xhr . getResponseHeader , 'first argument to "ajax:complete" should be an XHR object' )
239
+ assert . equal ( status , 'Forbidden' , 'second argument to ajax:complete should be a status string' )
240
+ done ( )
216
241
} )
217
242
} )
218
243
} )
219
244
220
- asyncTest ( 'binding to ajax callbacks via .delegate() triggers handlers properly' , 4 , function ( ) {
245
+ QUnit . test ( 'binding to ajax callbacks via .delegate() triggers handlers properly' , function ( assert ) {
246
+ const done = assert . async ( 4 )
247
+
221
248
$ ( document )
222
249
. delegate ( 'form[data-remote]' , 'ajax:beforeSend' , function ( ) {
223
- ok ( true , 'ajax:beforeSend handler is triggered' )
250
+ assert . ok ( true , 'ajax:beforeSend handler is triggered' )
251
+ done ( )
224
252
} )
225
253
. delegate ( 'form[data-remote]' , 'ajax:send' , function ( ) {
226
- ok ( true , 'ajax:send handler is triggered' )
254
+ assert . ok ( true , 'ajax:send handler is triggered' )
255
+ done ( )
227
256
} )
228
257
. delegate ( 'form[data-remote]' , 'ajax:success' , function ( ) {
229
- ok ( true , 'ajax:success handler is triggered' )
258
+ assert . ok ( true , 'ajax:success handler is triggered' )
259
+ done ( )
230
260
} )
231
261
. delegate ( 'form[data-remote]' , 'ajax:complete' , function ( ) {
232
- ok ( true , 'ajax:complete handler is triggered' )
262
+ assert . ok ( true , 'ajax:complete handler is triggered' )
263
+ done ( )
233
264
} )
234
265
$ ( 'form[data-remote]' ) . triggerNative ( 'submit' )
235
-
236
- setTimeout ( function ( ) { start ( ) } , 13 )
237
266
} )
238
267
239
268
} ) ( )
0 commit comments