Skip to content

Commit 781a722

Browse files
authored
Merge pull request rails#46077 from lsylvester/qunit2
upgrade to QUnit 2 for UJS tests
2 parents 5fb3e72 + 935e519 commit 781a722

15 files changed

+8937
-2953
lines changed
Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,28 @@
11
(function() {
22

3-
module('call-ajax', {
4-
setup: function() {
3+
QUnit.module('call-ajax', {
4+
beforeEach: function() {
55
$('#qunit-fixture')
66
.append($('<a />', { href: '#' }))
77
}
88
})
99

10-
asyncTest('call ajax without "ajax:beforeSend"', 1, function() {
10+
QUnit.test('call ajax without "ajax:beforeSend"', function(assert) {
11+
const done = assert.async()
12+
1113
var link = $('#qunit-fixture a')
1214
link.bindNative('click', function() {
1315
Rails.ajax({
1416
type: 'get',
1517
url: '/',
1618
success: function() {
17-
ok(true, 'calling request in ajax:success')
19+
assert.ok(true, 'calling request in ajax:success')
20+
done()
1821
}
1922
})
2023
})
2124

2225
link.triggerNative('click')
23-
setTimeout(function() { start() }, 50)
2426
})
2527

2628
})()

actionview/test/ujs/public/test/call-remote-callbacks.js

Lines changed: 80 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
(function() {
22

3-
module('call-remote-callbacks', {
4-
setup: function() {
3+
QUnit.module('call-remote-callbacks', {
4+
beforeEach: function() {
55
$('#qunit-fixture').append($('<form />', {
66
action: '/echo', method: 'get', 'data-remote': 'true'
77
}))
88
},
9-
teardown: function() {
9+
afterEach: function() {
1010
$(document).undelegate('form[data-remote]', 'ajax:beforeSend')
1111
$(document).undelegate('form[data-remote]', 'ajax:before')
1212
$(document).undelegate('form[data-remote]', 'ajax:send')
@@ -17,15 +17,15 @@ module('call-remote-callbacks', {
1717
})
1818

1919
function submit(fn) {
20-
var form = $('form')
20+
var form = $('#qunit-fixture form')
2121

2222
if (fn) fn(form)
2323
form.triggerNative('submit')
24-
25-
setTimeout(function() { start() }, 13)
2624
}
2725

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+
2929
$('form[data-remote]')
3030
.append($('<input type="text" name="user_name" value="john">'))
3131
.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
4040

4141
submit(function(form) {
4242
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()
4647
})
4748
})
4849
})
4950

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) {
5152
$('form[data-remote]').data('type', 'html')
5253
.bindNative('ajax:before', function() {
5354
this.setAttribute('data-type', 'xml')
5455
})
5556

5657
submit(function(form) {
5758
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')
5960
})
6061
})
6162
})
6263

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) {
6465
$('form[data-remote]').data('with-credentials', false)
6566
.bindNative('ajax:before', function() {
6667
this.setAttribute('data-with-credentials', true)
6768
})
6869

6970
submit(function(form) {
7071
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')
7273
})
7374
})
7475
})
7576

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+
7780
submit(function(form) {
7881
form.bindNative('ajax:beforeSend', function(e) {
79-
ok(true, 'aborting request in ajax:beforeSend')
82+
assert.ok(true, 'aborting request in ajax:beforeSend')
8083
e.preventDefault()
8184
})
8285
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')
8487
})
8588
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')
8790
})
8891
form.bindNative('ajax:complete', function() {
89-
ok(false, 'ajax:complete should not run')
92+
assert.ok(false, 'ajax:complete should not run')
9093
})
9194
})
95+
96+
setTimeout(function() { done() }, 13)
9297
})
9398

9499
function skipIt() {
95100
// This test cannot work due to the security feature in browsers which makes the value
96101
// attribute of file input fields readonly, so it cannot be set with default value.
97102
// 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) {
99104
var form = $('form[data-remote]')
100105
.append($('<input type="file" name="attachment" value="default.png">'))
101106
.bindNative('ajax:beforeSend', function() {
@@ -118,7 +123,7 @@ function skipIt() {
118123
}, 13)
119124
})
120125

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) {
122127
var form = $('form[data-remote]')
123128
.append($('<input type="file" value="default.png">'))
124129
.bindNative('ajax:beforeSend', function() {
@@ -139,7 +144,7 @@ function skipIt() {
139144
}, 13)
140145
})
141146

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) {
143148
var form = $('form[data-remote]')
144149
.append($('<input type="file" name="attachment" value="default.png">'))
145150
.bindNative('ajax:beforeSend', function() {
@@ -161,79 +166,103 @@ function skipIt() {
161166
})
162167
}
163168

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+
165172
$(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')
167174
e.preventDefault()
168175
})
169176

170177
submit(function(form) {
171178
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')
173180
})
174181
form.bindNative('ajax:complete', function() {
175-
ok(false, 'ajax:complete should not run')
182+
assert.ok(false, 'ajax:complete should not run')
176183
})
177184
})
185+
186+
setTimeout(function() { done() }, 13)
178187
})
179188

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+
181192
submit(function(form) {
182193
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()
185197
})
186198
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()
188201
})
189202
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()
193207
})
194208
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()
197212
})
198213
})
199214
})
200215

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+
202219
submit(function(form) {
203220
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+
})
206229
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')
210233
// 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()
212236
})
213237
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()
216241
})
217242
})
218243
})
219244

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+
221248
$(document)
222249
.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()
224252
})
225253
.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()
227256
})
228257
.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()
230260
})
231261
.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()
233264
})
234265
$('form[data-remote]').triggerNative('submit')
235-
236-
setTimeout(function() { start() }, 13)
237266
})
238267

239268
})()

0 commit comments

Comments
 (0)