upgrade to QUnit 2 for ujs tests

This commit is contained in:
Lachlan Sylvester 2022-09-20 18:03:55 +10:00
parent fd7c773387
commit 935e51949c
15 changed files with 8938 additions and 2954 deletions

View File

@ -1,26 +1,28 @@
(function() {
module('call-ajax', {
setup: function() {
QUnit.module('call-ajax', {
beforeEach: function() {
$('#qunit-fixture')
.append($('<a />', { href: '#' }))
}
})
asyncTest('call ajax without "ajax:beforeSend"', 1, function() {
QUnit.test('call ajax without "ajax:beforeSend"', function(assert) {
const done = assert.async()
var link = $('#qunit-fixture a')
link.bindNative('click', function() {
Rails.ajax({
type: 'get',
url: '/',
success: function() {
ok(true, 'calling request in ajax:success')
assert.ok(true, 'calling request in ajax:success')
done()
}
})
})
link.triggerNative('click')
setTimeout(function() { start() }, 50)
})
})()

View File

@ -1,12 +1,12 @@
(function() {
module('call-remote-callbacks', {
setup: function() {
QUnit.module('call-remote-callbacks', {
beforeEach: function() {
$('#qunit-fixture').append($('<form />', {
action: '/echo', method: 'get', 'data-remote': 'true'
}))
},
teardown: function() {
afterEach: function() {
$(document).undelegate('form[data-remote]', 'ajax:beforeSend')
$(document).undelegate('form[data-remote]', 'ajax:before')
$(document).undelegate('form[data-remote]', 'ajax:send')
@ -17,15 +17,15 @@ module('call-remote-callbacks', {
})
function submit(fn) {
var form = $('form')
var form = $('#qunit-fixture form')
if (fn) fn(form)
form.triggerNative('submit')
setTimeout(function() { start() }, 13)
}
asyncTest('modifying form fields with "ajax:before" sends modified data in request', 3, function() {
QUnit.test('modifying form fields with "ajax:before" sends modified data in request', function(assert) {
const done = assert.async()
$('form[data-remote]')
.append($('<input type="text" name="user_name" value="john">'))
.append($('<input type="text" name="removed_user_name" value="john">'))
@ -40,14 +40,15 @@ asyncTest('modifying form fields with "ajax:before" sends modified data in reque
submit(function(form) {
form.bindNative('ajax:success', function(e, data, status, xhr) {
equal(data.params.user_name, 'steve', 'modified field value should have been submitted')
equal(data.params.other_user_name, 'jonathan', 'added field value should have been submitted')
equal(data.params.removed_user_name, undefined, 'removed field value should be undefined')
assert.equal(data.params.user_name, 'steve', 'modified field value should have been submitted')
assert.equal(data.params.other_user_name, 'jonathan', 'added field value should have been submitted')
assert.equal(data.params.removed_user_name, undefined, 'removed field value should be undefined')
done()
})
})
})
asyncTest('modifying data("type") with "ajax:before" requests new dataType in request', 1, function() {
QUnit.test('modifying data("type") with "ajax:before" requests new dataType in request', function(assert) {
$('form[data-remote]').data('type', 'html')
.bindNative('ajax:before', function() {
this.setAttribute('data-type', 'xml')
@ -55,12 +56,12 @@ asyncTest('modifying data("type") with "ajax:before" requests new dataType in re
submit(function(form) {
form.bindNative('ajax:beforeSend', function(e, xhr, settings) {
equal(settings.dataType, 'xml', 'modified dataType should have been requested')
assert.equal(settings.dataType, 'xml', 'modified dataType should have been requested')
})
})
})
asyncTest('setting data("with-credentials",true) with "ajax:before" uses new setting in request', 1, function() {
QUnit.test('setting data("with-credentials",true) with "ajax:before" uses new setting in request', function(assert) {
$('form[data-remote]').data('with-credentials', false)
.bindNative('ajax:before', function() {
this.setAttribute('data-with-credentials', true)
@ -68,34 +69,38 @@ asyncTest('setting data("with-credentials",true) with "ajax:before" uses new set
submit(function(form) {
form.bindNative('ajax:beforeSend', function(e, xhr, settings) {
equal(settings.withCredentials, true, 'setting modified in ajax:before should have forced withCredentials request')
assert.equal(settings.withCredentials, true, 'setting modified in ajax:before should have forced withCredentials request')
})
})
})
asyncTest('stopping the "ajax:beforeSend" event aborts the request', 1, function() {
QUnit.test('stopping the "ajax:beforeSend" event aborts the request', function(assert) {
const done = assert.async()
submit(function(form) {
form.bindNative('ajax:beforeSend', function(e) {
ok(true, 'aborting request in ajax:beforeSend')
assert.ok(true, 'aborting request in ajax:beforeSend')
e.preventDefault()
})
form.unbind('ajax:send').bindNative('ajax:send', function() {
ok(false, 'ajax:send should not run')
assert.ok(false, 'ajax:send should not run')
})
form.bindNative('ajax:error', function(e, response, status, xhr) {
ok(false, 'ajax:error should not run')
assert.ok(false, 'ajax:error should not run')
})
form.bindNative('ajax:complete', function() {
ok(false, 'ajax:complete should not run')
assert.ok(false, 'ajax:complete should not run')
})
})
setTimeout(function() { done() }, 13)
})
function skipIt() {
// This test cannot work due to the security feature in browsers which makes the value
// attribute of file input fields readonly, so it cannot be set with default value.
// This is what the test would look like though if browsers let us automate this test.
asyncTest('non-blank file form input field should abort remote request, but submit normally', 5, function() {
QUnit.test('non-blank file form input field should abort remote request, but submit normally', function(assert) {
var form = $('form[data-remote]')
.append($('<input type="file" name="attachment" value="default.png">'))
.bindNative('ajax:beforeSend', function() {
@ -118,7 +123,7 @@ function skipIt() {
}, 13)
})
asyncTest('file form input field should not abort remote request if file form input does not have a name attribute', 5, function() {
QUnit.test('file form input field should not abort remote request if file form input does not have a name attribute', function(assert) {
var form = $('form[data-remote]')
.append($('<input type="file" value="default.png">'))
.bindNative('ajax:beforeSend', function() {
@ -139,7 +144,7 @@ function skipIt() {
}, 13)
})
asyncTest('blank file input field should abort request entirely if handler bound to "ajax:aborted:file" event that returns false', 1, function() {
QUnit.test('blank file input field should abort request entirely if handler bound to "ajax:aborted:file" event that returns false', function(assert) {
var form = $('form[data-remote]')
.append($('<input type="file" name="attachment" value="default.png">'))
.bindNative('ajax:beforeSend', function() {
@ -161,79 +166,103 @@ function skipIt() {
})
}
asyncTest('"ajax:beforeSend" can be observed and stopped with event delegation', 1, function() {
QUnit.test('"ajax:beforeSend" can be observed and stopped with event delegation', function(assert) {
const done = assert.async()
$(document).delegate('form[data-remote]', 'ajax:beforeSend', function(e) {
ok(true, 'ajax:beforeSend observed with event delegation')
assert.ok(true, 'ajax:beforeSend observed with event delegation')
e.preventDefault()
})
submit(function(form) {
form.unbind('ajax:send').bindNative('ajax:send', function() {
ok(false, 'ajax:send should not run')
assert.ok(false, 'ajax:send should not run')
})
form.bindNative('ajax:complete', function() {
ok(false, 'ajax:complete should not run')
assert.ok(false, 'ajax:complete should not run')
})
})
setTimeout(function() { done() }, 13)
})
asyncTest('"ajax:beforeSend", "ajax:send", "ajax:success" and "ajax:complete" are triggered', 8, function() {
QUnit.test('"ajax:beforeSend", "ajax:send", "ajax:success" and "ajax:complete" are triggered', function(assert) {
const done = assert.async(4)
submit(function(form) {
form.bindNative('ajax:beforeSend', function(e, xhr, settings) {
ok(xhr.setRequestHeader, 'first argument to "ajax:beforeSend" should be an XHR object')
equal(settings.url, '/echo', 'second argument to "ajax:beforeSend" should be a settings object')
assert.ok(xhr.setRequestHeader, 'first argument to "ajax:beforeSend" should be an XHR object')
assert.equal(settings.url, '/echo', 'second argument to "ajax:beforeSend" should be a settings object')
done()
})
form.bindNative('ajax:send', function(e, xhr) {
ok(xhr.abort, 'first argument to "ajax:send" should be an XHR object')
assert.ok(xhr.abort, 'first argument to "ajax:send" should be an XHR object')
done()
})
form.bindNative('ajax:success', function(e, data, status, xhr) {
ok(data.REQUEST_METHOD, 'first argument to ajax:success should be a data object')
equal(status, 'OK', 'second argument to ajax:success should be a status string')
ok(xhr.getResponseHeader, 'third argument to "ajax:success" should be an XHR object')
assert.ok(data.REQUEST_METHOD, 'first argument to ajax:success should be a data object')
assert.equal(status, 'OK', 'second argument to ajax:success should be a status string')
assert.ok(xhr.getResponseHeader, 'third argument to "ajax:success" should be an XHR object')
done()
})
form.bindNative('ajax:complete', function(e, xhr, status) {
ok(xhr.getResponseHeader, 'first argument to "ajax:complete" should be an XHR object')
equal(status, 'OK', 'second argument to ajax:complete should be a status string')
assert.ok(xhr.getResponseHeader, 'first argument to "ajax:complete" should be an XHR object')
assert.equal(status, 'OK', 'second argument to ajax:complete should be a status string')
done()
})
})
})
asyncTest('"ajax:beforeSend", "ajax:send", "ajax:error" and "ajax:complete" are triggered on error', 8, function() {
QUnit.test('"ajax:beforeSend", "ajax:send", "ajax:error" and "ajax:complete" are triggered on error', function(assert) {
const done = assert.async(4)
submit(function(form) {
form.attr('action', '/error')
form.bindNative('ajax:beforeSend', function(arg) { ok(true, 'ajax:beforeSend') })
form.bindNative('ajax:send', function(arg) { ok(true, 'ajax:send') })
form.bindNative('ajax:beforeSend', function(arg) {
assert.ok(true, 'ajax:beforeSend')
done()
})
form.bindNative('ajax:send', function(arg) {
assert.ok(true, 'ajax:send')
done()
})
form.bindNative('ajax:error', function(e, response, status, xhr) {
equal(response, '', 'first argument to ajax:error should be an HTTP status response')
equal(status, 'Forbidden', 'second argument to ajax:error should be a status string')
ok(xhr.getResponseHeader, 'third argument to "ajax:error" should be an XHR object')
assert.equal(response, '', 'first argument to ajax:error should be an HTTP status response')
assert.equal(status, 'Forbidden', 'second argument to ajax:error should be a status string')
assert.ok(xhr.getResponseHeader, 'third argument to "ajax:error" should be an XHR object')
// Opera returns "0" for HTTP code
equal(xhr.status, window.opera ? 0 : 403, 'status code should be 403')
assert.equal(xhr.status, window.opera ? 0 : 403, 'status code should be 403')
done()
})
form.bindNative('ajax:complete', function(e, xhr, status) {
ok(xhr.getResponseHeader, 'first argument to "ajax:complete" should be an XHR object')
equal(status, 'Forbidden', 'second argument to ajax:complete should be a status string')
assert.ok(xhr.getResponseHeader, 'first argument to "ajax:complete" should be an XHR object')
assert.equal(status, 'Forbidden', 'second argument to ajax:complete should be a status string')
done()
})
})
})
asyncTest('binding to ajax callbacks via .delegate() triggers handlers properly', 4, function() {
QUnit.test('binding to ajax callbacks via .delegate() triggers handlers properly', function(assert) {
const done = assert.async(4)
$(document)
.delegate('form[data-remote]', 'ajax:beforeSend', function() {
ok(true, 'ajax:beforeSend handler is triggered')
assert.ok(true, 'ajax:beforeSend handler is triggered')
done()
})
.delegate('form[data-remote]', 'ajax:send', function() {
ok(true, 'ajax:send handler is triggered')
assert.ok(true, 'ajax:send handler is triggered')
done()
})
.delegate('form[data-remote]', 'ajax:success', function() {
ok(true, 'ajax:success handler is triggered')
assert.ok(true, 'ajax:success handler is triggered')
done()
})
.delegate('form[data-remote]', 'ajax:complete', function() {
ok(true, 'ajax:complete handler is triggered')
assert.ok(true, 'ajax:complete handler is triggered')
done()
})
$('form[data-remote]').triggerNative('submit')
setTimeout(function() { start() }, 13)
})
})()

View File

@ -7,109 +7,149 @@ function buildForm(attrs) {
.find('form').append($('<input type="text" name="user_name" value="john">'))
}
module('call-remote')
QUnit.module('call-remote')
function submit(fn) {
$('form')
$('#qunit-fixture form')
.bindNative('ajax:success', fn)
.bindNative('ajax:complete', function() { start() })
.triggerNative('submit')
}
asyncTest('form method is read from "method" and not from "data-method"', 1, function() {
QUnit.test('form method is read from "method" and not from "data-method"', function(assert) {
const done = assert.async()
buildForm({ method: 'post', 'data-method': 'get' })
submit(function(e, data, status, xhr) {
App.assertPostRequest(data)
assert.postRequest(data)
done()
})
})
asyncTest('form method is not read from "data-method" attribute in case of missing "method"', 1, function() {
QUnit.test('form method is not read from "data-method" attribute in case of missing "method"', function(assert) {
const done = assert.async()
buildForm({ 'data-method': 'put' })
submit(function(e, data, status, xhr) {
App.assertGetRequest(data)
assert.getRequest(data)
done()
})
})
asyncTest('form method is read from submit button "formmethod" if submit is triggered by that button', 1, function() {
QUnit.test('form method is read from submit button "formmethod" if submit is triggered by that button', function(assert) {
const done = assert.async()
var submitButton = $('<input type="submit" formmethod="get">')
buildForm({ method: 'post' })
$('#qunit-fixture').find('form').append(submitButton)
.bindNative('ajax:success', function(e, data, status, xhr) {
App.assertGetRequest(data)
assert.getRequest(data)
})
.bindNative('ajax:complete', function() { start() })
.bindNative('ajax:complete', function() { done() })
submitButton.triggerNative('click')
})
asyncTest('form default method is GET', 1, function() {
QUnit.test('form default method is GET', function(assert) {
const done = assert.async()
buildForm()
submit(function(e, data, status, xhr) {
App.assertGetRequest(data)
assert.getRequest(data)
done()
})
})
asyncTest('form URL is picked up from "action"', 1, function() {
QUnit.test('form URL is picked up from "action"', function(assert) {
const done = assert.async()
buildForm({ method: 'post' })
submit(function(e, data, status, xhr) {
App.assertRequestPath(data, '/echo')
assert.requestPath(data, '/echo')
done()
})
})
asyncTest('form URL is read from "action" not "href"', 1, function() {
QUnit.test('form URL is read from "action" not "href"', function(assert) {
const done = assert.async()
buildForm({ method: 'post', href: '/echo2' })
submit(function(e, data, status, xhr) {
App.assertRequestPath(data, '/echo')
assert.requestPath(data, '/echo')
done()
})
})
asyncTest('form URL is read from submit button "formaction" if submit is triggered by that button', 1, function() {
QUnit.test('form URL is read from submit button "formaction" if submit is triggered by that button', function(assert) {
const done = assert.async()
var submitButton = $('<input type="submit" formaction="/echo">')
buildForm({ method: 'post', href: '/echo2' })
$('#qunit-fixture').find('form').append(submitButton)
.bindNative('ajax:success', function(e, data, status, xhr) {
App.assertRequestPath(data, '/echo')
assert.requestPath(data, '/echo')
})
.bindNative('ajax:complete', function() { start() })
.bindNative('ajax:complete', function() { done() })
submitButton.triggerNative('click')
})
asyncTest('prefer JS, but accept any format', 1, function() {
QUnit.test('prefer JS, but accept any format', function(assert) {
const done = assert.async()
buildForm({ method: 'post' })
submit(function(e, data, status, xhr) {
var accept = data.HTTP_ACCEPT
ok(accept.match(/text\/javascript.+\*\/\*/), 'Accept: ' + accept)
assert.ok(accept.match(/text\/javascript.+\*\/\*/), 'Accept: ' + accept)
done()
})
})
asyncTest('JS code should be executed', 1, function() {
QUnit.test('JS code should be executed', function(assert) {
const done = assert.async()
buildForm({ method: 'post', 'data-type': 'script' })
$('form').append('<input type="text" name="content_type" value="text/javascript">')
$('form').append('<input type="text" name="content" value="ok(true, \'remote code should be run\')">')
window.callback = function() {
assert.ok(true, 'remote code should be run')
window.callback = null
done()
}
$('form').append('<input type="text" name="content_type" value="text/javascript">')
$('form').append('<input type="text" name="content" value="window.callback()">')
submit()
})
asyncTest('ecmascript code should be executed', 1, function() {
QUnit.test('ecmascript code should be executed', function(assert) {
const done = assert.async()
window.callback = function() {
assert.ok(true, 'remote code should be run')
window.callback = null
done()
}
buildForm({ method: 'post', 'data-type': 'script' })
$('form').append('<input type="text" name="content_type" value="application/ecmascript">')
$('form').append('<input type="text" name="content" value="ok(true, \'remote code should be run\')">')
$('form').append('<input type="text" name="content" value="window.callback()">')
submit()
})
asyncTest('execution of JS code does not modify current DOM', 1, function() {
QUnit.test('execution of JS code does not modify current DOM', function(assert) {
const done = assert.async()
var docLength, newDocLength
function getDocLength() {
return document.documentElement.outerHTML.length
@ -124,87 +164,112 @@ asyncTest('execution of JS code does not modify current DOM', 1, function() {
submit(function() {
newDocLength = getDocLength()
ok(docLength === newDocLength, 'executed JS should not present in the document')
assert.ok(docLength === newDocLength, 'executed JS should not present in the document')
done()
})
})
asyncTest('HTML document should be parsed', 1, function() {
QUnit.test('HTML document should be parsed', function(assert) {
const done = assert.async()
buildForm({ method: 'post', 'data-type': 'html' })
$('form').append('<input type="text" name="content_type" value="text/html">')
$('form').append('<input type="text" name="content" value="<p>hello</p>">')
submit(function(e, data, status, xhr) {
ok(data instanceof HTMLDocument, 'returned data should be an HTML document')
assert.ok(data instanceof HTMLDocument, 'returned data should be an HTML document')
done()
})
})
asyncTest('XML document should be parsed', 1, function() {
QUnit.test('XML document should be parsed', function(assert) {
const done = assert.async()
buildForm({ method: 'post', 'data-type': 'html' })
$('form').append('<input type="text" name="content_type" value="application/xml">')
$('form').append('<input type="text" name="content" value="<p>hello</p>">')
submit(function(e, data, status, xhr) {
ok(data instanceof Document, 'returned data should be an XML document')
assert.ok(data instanceof Document, 'returned data should be an XML document')
done()
})
})
asyncTest('accept application/json if "data-type" is json', 1, function() {
QUnit.test('accept application/json if "data-type" is json', function(assert) {
const done = assert.async()
buildForm({ method: 'post', 'data-type': 'json' })
submit(function(e, data, status, xhr) {
equal(data.HTTP_ACCEPT, 'application/json, text/javascript, */*; q=0.01')
assert.equal(data.HTTP_ACCEPT, 'application/json, text/javascript, */*; q=0.01')
done()
})
})
asyncTest('allow empty "data-remote" attribute', 1, function() {
QUnit.test('allow empty "data-remote" attribute', function(assert) {
const done = assert.async()
var form = $('#qunit-fixture').append($('<form action="/echo" data-remote />')).find('form')
submit(function() {
ok(true, 'form with empty "data-remote" attribute is also allowed')
assert.ok(true, 'form with empty "data-remote" attribute is also allowed')
done()
})
})
asyncTest('query string in form action should be stripped in a GET request in normal submit', 1, function() {
QUnit.test('query string in form action should be stripped in a GET request in normal submit', function(assert) {
const done = assert.async()
buildForm({ action: '/echo?param1=abc', 'data-remote': 'false' })
$(document).one('iframe:loaded', function(e, data) {
equal(data.params.param1, undefined, '"param1" should not be passed to server')
start()
assert.equal(data.params.param1, undefined, '"param1" should not be passed to server')
done()
})
$('#qunit-fixture form').triggerNative('submit')
})
asyncTest('query string in form action should be stripped in a GET request in ajax submit', 1, function() {
QUnit.test('query string in form action should be stripped in a GET request in ajax submit', function(assert) {
const done = assert.async()
buildForm({ action: '/echo?param1=abc' })
submit(function(e, data, status, xhr) {
equal(data.params.param1, undefined, '"param1" should not be passed to server')
assert.equal(data.params.param1, undefined, '"param1" should not be passed to server')
done()
})
})
asyncTest('query string in form action should not be stripped in a POST request in normal submit', 1, function() {
QUnit.test('query string in form action should not be stripped in a POST request in normal submit', function(assert) {
const done = assert.async()
buildForm({ action: '/echo?param1=abc', method: 'post', 'data-remote': 'false' })
$(document).one('iframe:loaded', function(e, data) {
equal(data.params.param1, 'abc', '"param1" should be passed to server')
start()
assert.equal(data.params.param1, 'abc', '"param1" should be passed to server')
done()
})
$('#qunit-fixture form').triggerNative('submit')
})
asyncTest('query string in form action should not be stripped in a POST request in ajax submit', 1, function() {
QUnit.test('query string in form action should not be stripped in a POST request in ajax submit', function(assert) {
const done = assert.async()
buildForm({ action: '/echo?param1=abc', method: 'post' })
submit(function(e, data, status, xhr) {
equal(data.params.param1, 'abc', '"param1" should be passed to server')
assert.equal(data.params.param1, 'abc', '"param1" should be passed to server')
done()
})
})
asyncTest('allow empty form "action"', 1, function() {
QUnit.test('allow empty form "action"', function(assert) {
const done = assert.async()
var currentLocation, ajaxLocation
buildForm({ action: '' })
@ -225,7 +290,7 @@ asyncTest('allow empty form "action"', 1, function() {
// HACK: can no longer use settings.data below to see what was appended to URL, as of
// jQuery 1.6.3 (see https://bugs.jquery.com/ticket/10202 and https://github.com/jquery/jquery/pull/544)
ajaxLocation = settings.url.replace('user_name=john', '').replace(/&$/, '').replace(/\?$/, '')
equal(ajaxLocation.match(/^(.*)/)[1], currentLocation, 'URL should be current page by default')
assert.equal(ajaxLocation.match(/^(.*)/)[1], currentLocation, 'URL should be current page by default')
// Prevent the request from actually getting sent to the current page and
// causing an error.
@ -233,19 +298,23 @@ asyncTest('allow empty form "action"', 1, function() {
})
.triggerNative('submit')
setTimeout(function() { start() }, 13)
setTimeout(function() { done() }, 13)
})
asyncTest('sends CSRF token in custom header', 1, function() {
QUnit.test('sends CSRF token in custom header', function(assert) {
const done = assert.async()
buildForm({ method: 'post' })
$('#qunit-fixture').append('<meta name="csrf-token" content="cf50faa3fe97702ca1ae" />')
submit(function(e, data, status, xhr) {
equal(data.HTTP_X_CSRF_TOKEN, 'cf50faa3fe97702ca1ae', 'X-CSRF-Token header should be sent')
assert.equal(data.HTTP_X_CSRF_TOKEN, 'cf50faa3fe97702ca1ae', 'X-CSRF-Token header should be sent')
done()
})
})
asyncTest('intelligently guesses crossDomain behavior when target URL has a different protocol and/or hostname', 1, function() {
QUnit.test('intelligently guesses crossDomain behavior when target URL has a different protocol and/or hostname', function(assert) {
const done = assert.async()
// Don't set data-cross-domain here, just set action to be a different domain than localhost
buildForm({ action: 'http://www.alfajango.com' })
@ -254,17 +323,18 @@ asyncTest('intelligently guesses crossDomain behavior when target URL has a diff
$('#qunit-fixture').find('form')
.bindNative('ajax:beforeSend', function(evt, req, settings) {
equal(settings.crossDomain, true, 'crossDomain should be set to true')
assert.equal(settings.crossDomain, true, 'crossDomain should be set to true')
// prevent request from actually getting sent off-domain
evt.preventDefault()
})
.triggerNative('submit')
setTimeout(function() { start() }, 13)
setTimeout(function() { done() }, 13)
})
asyncTest('intelligently guesses crossDomain behavior when target URL consists of only a path', 1, function() {
QUnit.test('intelligently guesses crossDomain behavior when target URL consists of only a path', function(assert) {
const done = assert.async()
// Don't set data-cross-domain here, just set action to be a different domain than localhost
buildForm({ action: '/just/a/path' })
@ -273,14 +343,14 @@ asyncTest('intelligently guesses crossDomain behavior when target URL consists o
$('#qunit-fixture').find('form')
.bindNative('ajax:beforeSend', function(evt, req, settings) {
equal(settings.crossDomain, false, 'crossDomain should be set to false')
assert.equal(settings.crossDomain, false, 'crossDomain should be set to false')
// prevent request from actually getting sent off-domain
evt.preventDefault()
})
.triggerNative('submit')
setTimeout(function() { start() }, 13)
setTimeout(function() { done() }, 13)
})
})()

View File

@ -1,8 +1,8 @@
(function() {
module('csrf-refresh', {})
QUnit.module('csrf-refresh', {})
asyncTest('refresh all csrf tokens', 1, function() {
QUnit.test('refresh all csrf tokens', function(assert) {
var correctToken = 'cf50faa3fe97702ca1ae'
var form = $('<form />')
@ -17,8 +17,7 @@ asyncTest('refresh all csrf tokens', 1, function() {
$.rails.refreshCSRFTokens()
currentToken = $('#qunit-fixture #authenticity_token').val()
start()
equal(currentToken, correctToken)
assert.equal(currentToken, correctToken)
})
})()

View File

@ -1,27 +1,25 @@
(function() {
module('csrf-token', {})
QUnit.module('csrf-token', {})
asyncTest('find csrf token', 1, function() {
QUnit.test('find csrf token', function(assert) {
var correctToken = 'cf50faa3fe97702ca1ae'
$('#qunit-fixture').append('<meta name="csrf-token" content="' + correctToken + '"/>')
currentToken = $.rails.csrfToken()
start()
equal(currentToken, correctToken)
assert.equal(currentToken, correctToken)
})
asyncTest('find csrf param', 1, function() {
QUnit.test('find csrf param', function(assert) {
var correctParam = 'authenticity_token'
$('#qunit-fixture').append('<meta name="csrf-param" content="' + correctParam + '"/>')
currentParam = $.rails.csrfParam()
start()
equal(currentParam, correctParam)
assert.equal(currentParam, correctParam)
})
})()

View File

@ -1,5 +1,5 @@
module('data-confirm', {
setup: function() {
QUnit.module('data-confirm', {
beforeEach: function() {
$('#qunit-fixture').append($('<a />', {
href: '/echo',
'data-remote': 'true',
@ -35,224 +35,246 @@ module('data-confirm', {
this.windowConfirm = window.confirm
},
teardown: function() {
afterEach: function() {
window.confirm = this.windowConfirm
}
})
asyncTest('clicking on a link with data-confirm attribute. Confirm yes.', 6, function() {
QUnit.test('clicking on a link with data-confirm attribute. Confirm yes.', function(assert) {
const done = assert.async()
var message
// auto-confirm:
window.confirm = function(msg) { message = msg; return true }
$('a[data-confirm]')
.bindNative('confirm:complete', function(e, data) {
App.assertCallbackInvoked('confirm:complete')
ok(data == true, 'confirm:complete passes in confirm answer (true)')
assert.callbackInvoked('confirm:complete')
assert.ok(data == true, 'confirm:complete passes in confirm answer (true)')
})
.bindNative('ajax:success', function(e, data, status, xhr) {
App.assertCallbackInvoked('ajax:success')
App.assertRequestPath(data, '/echo')
App.assertGetRequest(data)
assert.callbackInvoked('ajax:success')
assert.requestPath(data, '/echo')
assert.getRequest(data)
equal(message, 'Are you absolutely sure?')
start()
assert.equal(message, 'Are you absolutely sure?')
done()
})
.triggerNative('click')
})
asyncTest('clicking on a button with data-confirm attribute. Confirm yes.', 6, function() {
QUnit.test('clicking on a button with data-confirm attribute. Confirm yes.', function(assert) {
const done = assert.async()
var message
// auto-confirm:
window.confirm = function(msg) { message = msg; return true }
$('button[data-confirm]')
.bindNative('confirm:complete', function(e, data) {
App.assertCallbackInvoked('confirm:complete')
ok(data == true, 'confirm:complete passes in confirm answer (true)')
assert.callbackInvoked('confirm:complete')
assert.ok(data == true, 'confirm:complete passes in confirm answer (true)')
})
.bindNative('ajax:success', function(e, data, status, xhr) {
App.assertCallbackInvoked('ajax:success')
App.assertRequestPath(data, '/echo')
App.assertGetRequest(data)
assert.callbackInvoked('ajax:success')
assert.requestPath(data, '/echo')
assert.getRequest(data)
equal(message, 'Are you absolutely sure?')
start()
assert.equal(message, 'Are you absolutely sure?')
done()
})
.triggerNative('click')
})
asyncTest('clicking on a link with data-confirm attribute. Confirm No.', 3, function() {
QUnit.test('clicking on a link with data-confirm attribute. Confirm No.', function(assert) {
const done = assert.async()
var message
// auto-decline:
window.confirm = function(msg) { message = msg; return false }
$('a[data-confirm]')
.bindNative('confirm:complete', function(e, data) {
App.assertCallbackInvoked('confirm:complete')
ok(data == false, 'confirm:complete passes in confirm answer (false)')
assert.callbackInvoked('confirm:complete')
assert.ok(data == false, 'confirm:complete passes in confirm answer (false)')
})
.bindNative('ajax:beforeSend', function(e, data, status, xhr) {
App.assertCallbackNotInvoked('ajax:beforeSend')
assert.callbackNotInvoked('ajax:beforeSend')
})
.triggerNative('click')
setTimeout(function() {
equal(message, 'Are you absolutely sure?')
start()
assert.equal(message, 'Are you absolutely sure?')
done()
}, 50)
})
asyncTest('clicking on a button with data-confirm attribute. Confirm No.', 3, function() {
QUnit.test('clicking on a button with data-confirm attribute. Confirm No.', function(assert) {
const done = assert.async()
var message
// auto-decline:
window.confirm = function(msg) { message = msg; return false }
$('button[data-confirm]')
.bindNative('confirm:complete', function(e, data) {
App.assertCallbackInvoked('confirm:complete')
ok(data == false, 'confirm:complete passes in confirm answer (false)')
assert.callbackInvoked('confirm:complete')
assert.ok(data == false, 'confirm:complete passes in confirm answer (false)')
})
.bindNative('ajax:beforeSend', function(e, data, status, xhr) {
App.assertCallbackNotInvoked('ajax:beforeSend')
assert.callbackNotInvoked('ajax:beforeSend')
})
.triggerNative('click')
setTimeout(function() {
equal(message, 'Are you absolutely sure?')
start()
assert.equal(message, 'Are you absolutely sure?')
done()
}, 50)
})
asyncTest('clicking on a button with data-confirm attribute. Confirm error.', 3, function() {
QUnit.test('clicking on a button with data-confirm attribute. Confirm error.', function(assert) {
const done = assert.async()
var message
// auto-decline:
window.confirm = function(msg) { message = msg; throw 'some random error' }
$('button[data-confirm]')
.bindNative('confirm:complete', function(e, data) {
App.assertCallbackInvoked('confirm:complete')
ok(data == false, 'confirm:complete passes in confirm answer (false)')
assert.callbackInvoked('confirm:complete')
assert.ok(data == false, 'confirm:complete passes in confirm answer (false)')
})
.bindNative('ajax:beforeSend', function(e, data, status, xhr) {
App.assertCallbackNotInvoked('ajax:beforeSend')
assert.callbackNotInvoked('ajax:beforeSend')
})
.triggerNative('click')
setTimeout(function() {
equal(message, 'Are you absolutely sure?')
start()
assert.equal(message, 'Are you absolutely sure?')
done()
}, 50)
})
asyncTest('clicking on a submit button with form and data-confirm attributes. Confirm No.', 3, function() {
QUnit.test('clicking on a submit button with form and data-confirm attributes. Confirm No.', function(assert) {
const done = assert.async()
var message
// auto-decline:
window.confirm = function(msg) { message = msg; return false }
$('input[type=submit][form]')
$('#qunit-fixture input[type=submit][form]')
.bindNative('confirm:complete', function(e, data) {
App.assertCallbackInvoked('confirm:complete')
ok(data == false, 'confirm:complete passes in confirm answer (false)')
assert.callbackInvoked('confirm:complete')
assert.ok(data == false, 'confirm:complete passes in confirm answer (false)')
})
.bindNative('ajax:beforeSend', function(e, data, status, xhr) {
App.assertCallbackNotInvoked('ajax:beforeSend')
assert.callbackNotInvoked('ajax:beforeSend')
})
.triggerNative('click')
setTimeout(function() {
equal(message, 'Are you absolutely sure?')
start()
assert.equal(message, 'Are you absolutely sure?')
done()
}, 50)
})
asyncTest('binding to confirm event of a link and returning false', 1, function() {
QUnit.test('binding to confirm event of a link and returning false', function(assert) {
const done = assert.async()
// redefine confirm function so we can make sure it's not called
window.confirm = function(msg) {
ok(false, 'confirm dialog should not be called')
assert.ok(false, 'confirm dialog should not be called')
}
$('a[data-confirm]')
.bindNative('confirm', function(e) {
App.assertCallbackInvoked('confirm')
assert.callbackInvoked('confirm')
e.preventDefault()
})
.bindNative('confirm:complete', function() {
App.assertCallbackNotInvoked('confirm:complete')
assert.callbackNotInvoked('confirm:complete')
})
.triggerNative('click')
setTimeout(function() {
start()
done()
}, 50)
})
asyncTest('binding to confirm event of a button and returning false', 1, function() {
QUnit.test('binding to confirm event of a button and returning false', function(assert) {
const done = assert.async()
// redefine confirm function so we can make sure it's not called
window.confirm = function(msg) {
ok(false, 'confirm dialog should not be called')
assert.ok(false, 'confirm dialog should not be called')
}
$('button[data-confirm]')
.bindNative('confirm', function(e) {
App.assertCallbackInvoked('confirm')
assert.callbackInvoked('confirm')
e.preventDefault()
})
.bindNative('confirm:complete', function() {
App.assertCallbackNotInvoked('confirm:complete')
assert.callbackNotInvoked('confirm:complete')
})
.triggerNative('click')
setTimeout(function() {
start()
done()
}, 50)
})
asyncTest('binding to confirm:complete event of a link and returning false', 2, function() {
QUnit.test('binding to confirm:complete event of a link and returning false', function(assert) {
const done = assert.async()
// auto-confirm:
window.confirm = function(msg) {
ok(true, 'confirm dialog should be called')
assert.ok(true, 'confirm dialog should be called')
return true
}
$('a[data-confirm]')
.bindNative('confirm:complete', function(e) {
App.assertCallbackInvoked('confirm:complete')
assert.callbackInvoked('confirm:complete')
e.preventDefault()
})
.bindNative('ajax:beforeSend', function() {
App.assertCallbackNotInvoked('ajax:beforeSend')
assert.callbackNotInvoked('ajax:beforeSend')
})
.triggerNative('click')
setTimeout(function() {
start()
done()
}, 50)
})
asyncTest('binding to confirm:complete event of a button and returning false', 2, function() {
QUnit.test('binding to confirm:complete event of a button and returning false', function(assert) {
const done = assert.async()
// auto-confirm:
window.confirm = function(msg) {
ok(true, 'confirm dialog should be called')
assert.ok(true, 'confirm dialog should be called')
return true
}
$('button[data-confirm]')
.bindNative('confirm:complete', function(e) {
App.assertCallbackInvoked('confirm:complete')
assert.callbackInvoked('confirm:complete')
e.preventDefault()
})
.bindNative('ajax:beforeSend', function() {
App.assertCallbackNotInvoked('ajax:beforeSend')
assert.callbackNotInvoked('ajax:beforeSend')
})
.triggerNative('click')
setTimeout(function() {
start()
done()
}, 50)
})
asyncTest('a button inside a form only confirms once', 1, function() {
QUnit.test('a button inside a form only confirms once', function(assert) {
const done = assert.async()
var confirmations = 0
window.confirm = function(msg) {
confirmations++
@ -265,13 +287,15 @@ asyncTest('a button inside a form only confirms once', 1, function() {
text: 'Click me'
})))
$('form > button[data-confirm]').triggerNative('click')
$('#qunit-fixture form > button[data-confirm]').triggerNative('click')
ok(confirmations === 1, 'confirmation counter should be 1, but it was ' + confirmations)
start()
assert.ok(confirmations === 1, 'confirmation counter should be 1, but it was ' + confirmations)
done()
})
asyncTest('clicking on the children of a link should also trigger a confirm', 6, function() {
QUnit.test('clicking on the children of a link should also trigger a confirm', function(assert) {
const done = assert.async()
var message
// auto-confirm:
window.confirm = function(msg) { message = msg; return true }
@ -279,22 +303,24 @@ asyncTest('clicking on the children of a link should also trigger a confirm', 6,
$('a[data-confirm]')
.html('<strong>Click me</strong>')
.bindNative('confirm:complete', function(e, data) {
App.assertCallbackInvoked('confirm:complete')
ok(data == true, 'confirm:complete passes in confirm answer (true)')
assert.callbackInvoked('confirm:complete')
assert.ok(data == true, 'confirm:complete passes in confirm answer (true)')
})
.bindNative('ajax:success', function(e, data, status, xhr) {
App.assertCallbackInvoked('ajax:success')
App.assertRequestPath(data, '/echo')
App.assertGetRequest(data)
assert.callbackInvoked('ajax:success')
assert.requestPath(data, '/echo')
assert.getRequest(data)
equal(message, 'Are you absolutely sure?')
start()
assert.equal(message, 'Are you absolutely sure?')
done()
})
.find('strong')
.triggerNative('click')
})
asyncTest('clicking on the children of a disabled button should not trigger a confirm.', 1, function() {
QUnit.test('clicking on the children of a disabled button should not trigger a confirm.', function(assert) {
const done = assert.async()
var message
// auto-decline:
window.confirm = function(msg) { message = msg; return false }
@ -302,41 +328,43 @@ asyncTest('clicking on the children of a disabled button should not trigger a co
$('button[data-confirm][disabled]')
.html('<strong>Click me</strong>')
.bindNative('confirm', function() {
App.assertCallbackNotInvoked('confirm')
assert.callbackNotInvoked('confirm')
})
.find('strong')
.bindNative('click', function() {
App.assertCallbackInvoked('click')
assert.callbackInvoked('click')
})
.triggerNative('click')
setTimeout(function() {
start()
done()
}, 50)
})
asyncTest('clicking on a link with data-confirm attribute with custom confirm handler. Confirm yes.', 7, function() {
QUnit.test('clicking on a link with data-confirm attribute with custom confirm handler. Confirm yes.', function(assert) {
const done = assert.async()
var message, element
// redefine confirm function so we can make sure it's not called
window.confirm = function(msg) {
ok(false, 'confirm dialog should not be called')
assert.ok(false, 'confirm dialog should not be called')
}
// custom auto-confirm:
Rails.confirm = function(msg, elem) { message = msg; element = elem; return true }
$('a[data-confirm]')
.bindNative('confirm:complete', function(e, data) {
App.assertCallbackInvoked('confirm:complete')
ok(data == true, 'confirm:complete passes in confirm answer (true)')
assert.callbackInvoked('confirm:complete')
assert.ok(data == true, 'confirm:complete passes in confirm answer (true)')
})
.bindNative('ajax:success', function(e, data, status, xhr) {
App.assertCallbackInvoked('ajax:success')
App.assertRequestPath(data, '/echo')
App.assertGetRequest(data)
assert.callbackInvoked('ajax:success')
assert.requestPath(data, '/echo')
assert.getRequest(data)
equal(message, 'Are you absolutely sure?')
equal(element, $('a[data-confirm]').get(0))
start()
assert.equal(message, 'Are you absolutely sure?')
assert.equal(element, $('a[data-confirm]').get(0))
done()
})
.triggerNative('click')
})

View File

@ -1,5 +1,5 @@
module('data-disable-with', {
setup: function() {
QUnit.module('data-disable-with', {
beforeEach: function() {
$('#qunit-fixture').append($('<form />', {
action: '/echo',
'data-remote': 'true',
@ -38,106 +38,116 @@ module('data-disable-with', {
'data-disable-with': 'clicking...'
}))
},
teardown: function() {
afterEach: function() {
$(document).unbind('iframe:loaded')
}
})
asyncTest('form input field with "data-disable-with" attribute', 7, function() {
QUnit.test('form input field with "data-disable-with" attribute', function(assert) {
const done = assert.async()
var form = $('form[data-remote]'), input = form.find('input[type=text]')
App.checkEnabledState(input, 'john')
assert.enabledState(input, 'john')
form.bindNative('ajax:success', function(e, data) {
setTimeout(function() {
App.checkEnabledState(input, 'john')
equal(data.params.user_name, 'john')
start()
assert.enabledState(input, 'john')
assert.equal(data.params.user_name, 'john')
done()
}, 13)
})
form.triggerNative('submit')
App.checkDisabledState(input, 'processing ...')
assert.disabledState(input, 'processing ...')
})
asyncTest('blank form input field with "data-disable-with" attribute', 7, function() {
QUnit.test('blank form input field with "data-disable-with" attribute', function(assert) {
const done = assert.async()
var form = $('form[data-remote]'), input = form.find('input[type=text]')
input.val('')
App.checkEnabledState(input, '')
assert.enabledState(input, '')
form.bindNative('ajax:success', function(e, data) {
setTimeout(function() {
App.checkEnabledState(input, '')
equal(data.params.user_name, '')
start()
assert.enabledState(input, '')
assert.equal(data.params.user_name, '')
done()
}, 13)
})
form.triggerNative('submit')
App.checkDisabledState(input, 'processing ...')
assert.disabledState(input, 'processing ...')
})
asyncTest('form button with "data-disable-with" attribute', 6, function() {
QUnit.test('form button with "data-disable-with" attribute', function(assert) {
const done = assert.async()
var form = $('form[data-remote]'), button = $('<button data-disable-with="submitting ..." name="submit2">Submit</button>')
form.append(button)
App.checkEnabledState(button, 'Submit')
assert.enabledState(button, 'Submit')
form.bindNative('ajax:success', function(e, data) {
setTimeout(function() {
App.checkEnabledState(button, 'Submit')
start()
assert.enabledState(button, 'Submit')
done()
}, 13)
})
form.triggerNative('submit')
App.checkDisabledState(button, 'submitting ...')
assert.disabledState(button, 'submitting ...')
})
asyncTest('a[data-remote][data-disable-with] within a form disables and re-enables', 6, function() {
var form = $('form:not([data-remote])'),
QUnit.test('a[data-remote][data-disable-with] within a form disables and re-enables', function(assert) {
const done = assert.async()
var form = $('#qunit-fixture form:not([data-remote])'),
link = $('<a data-remote="true" data-disable-with="clicking...">Click me</a>')
form.append(link)
App.checkEnabledState(link, 'Click me')
assert.enabledState(link, 'Click me')
link
.bindNative('ajax:beforeSend', function() {
App.checkDisabledState(link, 'clicking...')
assert.disabledState(link, 'clicking...')
})
.bindNative('ajax:complete', function() {
setTimeout( function() {
App.checkEnabledState(link, 'Click me')
assert.enabledState(link, 'Click me')
link.remove()
start()
done()
}, 15)
})
.triggerNative('click')
})
asyncTest('form input[type=submit][data-disable-with] disables', 6, function() {
var form = $('form:not([data-remote])'), input = form.find('input[type=submit]')
QUnit.test('form input[type=submit][data-disable-with] disables', function(assert) {
const done = assert.async()
App.checkEnabledState(input, 'Submit')
var form = $('#qunit-fixture form:not([data-remote])'), input = form.find('input[type=submit]')
assert.enabledState(input, 'Submit')
$(document).bind('iframe:loaded', function(e, data) {
setTimeout(function() {
App.checkDisabledState(input, 'submitting ...')
start()
assert.disabledState(input, 'submitting ...')
done()
}, 30)
})
form.triggerNative('submit')
setTimeout(function() {
App.checkDisabledState(input, 'submitting ...')
assert.disabledState(input, 'submitting ...')
}, 30)
})
test('form input[type=submit][data-disable-with] re-enables when `pageshow` event is triggered', function() {
var form = $('form:not([data-remote])'), input = form.find('input[type=submit]')
QUnit.test('form input[type=submit][data-disable-with] re-enables when `pageshow` event is triggered', function(assert) {
var form = $('#qunit-fixture form:not([data-remote])'), input = form.find('input[type=submit]')
App.checkEnabledState(input, 'Submit')
assert.enabledState(input, 'Submit')
// Emulate the disabled state without submitting the form at all, what is the
// state after going back on firefox after submitting a form.
@ -145,14 +155,16 @@ test('form input[type=submit][data-disable-with] re-enables when `pageshow` even
// See https://github.com/rails/jquery-ujs/issues/357
$.rails.disableElement(form[0])
App.checkDisabledState(input, 'submitting ...')
assert.disabledState(input, 'submitting ...')
$(window).triggerNative('pageshow')
App.checkEnabledState(input, 'Submit')
assert.enabledState(input, 'Submit')
})
asyncTest('form[data-remote] input[type=submit][data-disable-with] is replaced in ajax callback', 2, function() {
QUnit.test('form[data-remote] input[type=submit][data-disable-with] is replaced in ajax callback', function(assert) {
const done = assert.async()
var form = $('#qunit-fixture form:not([data-remote])').attr('data-remote', 'true'),
origFormContents = form.html()
@ -161,13 +173,15 @@ asyncTest('form[data-remote] input[type=submit][data-disable-with] is replaced i
setTimeout(function() {
var input = form.find('input[type=submit]')
App.checkEnabledState(input, 'Submit')
start()
assert.enabledState(input, 'Submit')
done()
}, 30)
}).triggerNative('submit')
})
asyncTest('form[data-remote] input[data-disable-with] is replaced with disabled field in ajax callback', 2, function() {
QUnit.test('form[data-remote] input[data-disable-with] is replaced with disabled field in ajax callback', function(assert) {
const done = assert.async()
var form = $('#qunit-fixture form:not([data-remote])').attr('data-remote', 'true'),
input = form.find('input[type=submit]'),
newDisabledInput = input.clone().attr('disabled', 'disabled')
@ -176,139 +190,151 @@ asyncTest('form[data-remote] input[data-disable-with] is replaced with disabled
input.replaceWith(newDisabledInput)
setTimeout(function() {
App.checkEnabledState(newDisabledInput, 'Submit')
start()
assert.enabledState(newDisabledInput, 'Submit')
done()
}, 30)
}).triggerNative('submit')
})
asyncTest('form input[type=submit][data-disable-with] using "form" attribute disables', 6, function() {
QUnit.test('form input[type=submit][data-disable-with] using "form" attribute disables', function(assert) {
var form = $('#not_remote'), input = $('input[form=not_remote]')
App.checkEnabledState(input, 'Form Attr Submit')
assert.enabledState(input, 'Form Attr Submit')
const done = assert.async()
$(document).bind('iframe:loaded', function(e, data) {
setTimeout(function() {
App.checkDisabledState(input, 'form attr submitting')
start()
assert.disabledState(input, 'form attr submitting')
done()
}, 30)
})
form.triggerNative('submit')
setTimeout(function() {
App.checkDisabledState(input, 'form attr submitting')
assert.disabledState(input, 'form attr submitting')
}, 30)
})
asyncTest('form[data-remote] textarea[data-disable-with] attribute', 3, function() {
QUnit.test('form[data-remote] textarea[data-disable-with] attribute', function(assert) {
const done = assert.async()
var form = $('form[data-remote]'),
textarea = $('<textarea data-disable-with="processing ..." name="user_bio">born, lived, died.</textarea>').appendTo(form)
form.bindNative('ajax:success', function(e, data) {
setTimeout(function() {
equal(data.params.user_bio, 'born, lived, died.')
start()
assert.equal(data.params.user_bio, 'born, lived, died.')
done()
}, 13)
})
form.triggerNative('submit')
App.checkDisabledState(textarea, 'processing ...')
assert.disabledState(textarea, 'processing ...')
})
asyncTest('a[data-disable-with] disables', 4, function() {
QUnit.test('a[data-disable-with] disables', function(assert) {
const done = assert.async()
var link = $('a[data-disable-with]')
App.checkEnabledState(link, 'Click me')
assert.enabledState(link, 'Click me')
link.triggerNative('click')
App.checkDisabledState(link, 'clicking...')
start()
assert.disabledState(link, 'clicking...')
done()
})
test('a[data-disable-with] re-enables when `pageshow` event is triggered', function() {
QUnit.test('a[data-disable-with] re-enables when `pageshow` event is triggered', function(assert) {
var link = $('a[data-disable-with]')
App.checkEnabledState(link, 'Click me')
assert.enabledState(link, 'Click me')
link.triggerNative('click')
App.checkDisabledState(link, 'clicking...')
assert.disabledState(link, 'clicking...')
$(window).triggerNative('pageshow')
App.checkEnabledState(link, 'Click me')
assert.enabledState(link, 'Click me')
})
asyncTest('a[data-remote][data-disable-with] disables and re-enables', 6, function() {
var link = $('a[data-disable-with]').attr('data-remote', true)
QUnit.test('a[data-remote][data-disable-with] disables and re-enables', function(assert) {
const done = assert.async()
App.checkEnabledState(link, 'Click me')
var link = $('a[data-disable-with]').attr('data-remote', true)
assert.enabledState(link, 'Click me')
link
.bindNative('ajax:beforeSend', function() {
App.checkDisabledState(link, 'clicking...')
assert.disabledState(link, 'clicking...')
})
.bindNative('ajax:complete', function() {
setTimeout( function() {
App.checkEnabledState(link, 'Click me')
start()
assert.enabledState(link, 'Click me')
done()
}, 15)
})
.triggerNative('click')
})
asyncTest('a[data-remote][data-disable-with] re-enables when `ajax:before` event is cancelled', 6, function() {
QUnit.test('a[data-remote][data-disable-with] re-enables when `ajax:before` event is cancelled', function(assert) {
const done = assert.async()
var link = $('a[data-disable-with]').attr('data-remote', true)
App.checkEnabledState(link, 'Click me')
assert.enabledState(link, 'Click me')
link
.bindNative('ajax:before', function(e) {
App.checkDisabledState(link, 'clicking...')
assert.disabledState(link, 'clicking...')
e.preventDefault()
})
.triggerNative('click')
setTimeout(function() {
App.checkEnabledState(link, 'Click me')
start()
assert.enabledState(link, 'Click me')
done()
}, 30)
})
asyncTest('a[data-remote][data-disable-with] re-enables when `ajax:beforeSend` event is cancelled', 6, function() {
QUnit.test('a[data-remote][data-disable-with] re-enables when `ajax:beforeSend` event is cancelled', function(assert) {
const done = assert.async()
var link = $('a[data-disable-with]').attr('data-remote', true)
App.checkEnabledState(link, 'Click me')
assert.enabledState(link, 'Click me')
link
.bindNative('ajax:beforeSend', function(e) {
App.checkDisabledState(link, 'clicking...')
assert.disabledState(link, 'clicking...')
e.preventDefault()
})
.triggerNative('click')
setTimeout(function() {
App.checkEnabledState(link, 'Click me')
start()
assert.enabledState(link, 'Click me')
done()
}, 30)
})
asyncTest('a[data-remote][data-disable-with] re-enables when `ajax:error` event is triggered', 6, function() {
QUnit.test('a[data-remote][data-disable-with] re-enables when `ajax:error` event is triggered', function(assert) {
const done = assert.async()
var link = $('a[data-disable-with]').attr('data-remote', true).attr('href', '/error')
App.checkEnabledState(link, 'Click me')
assert.enabledState(link, 'Click me')
link
.bindNative('ajax:beforeSend', function() {
App.checkDisabledState(link, 'clicking...')
assert.disabledState(link, 'clicking...')
})
.triggerNative('click')
setTimeout(function() {
App.checkEnabledState(link, 'Click me')
start()
assert.enabledState(link, 'Click me')
done()
}, 30)
})
asyncTest('form[data-remote] input|button|textarea[data-disable-with] does not disable when `ajax:beforeSend` event is cancelled', 8, function() {
QUnit.test('form[data-remote] input|button|textarea[data-disable-with] does not disable when `ajax:beforeSend` event is cancelled', function(assert) {
var form = $('form[data-remote]'),
input = form.find('input:text'),
button = $('<button data-disable-with="submitting ..." name="submit2">Submit</button>').appendTo(form),
@ -322,113 +348,116 @@ asyncTest('form[data-remote] input|button|textarea[data-disable-with] does not d
})
.triggerNative('submit')
App.checkEnabledState(input, 'john')
App.checkEnabledState(button, 'Submit')
App.checkEnabledState(textarea, 'born, lived, died.')
App.checkEnabledState(submit, 'Submit')
start()
assert.enabledState(input, 'john')
assert.enabledState(button, 'Submit')
assert.enabledState(textarea, 'born, lived, died.')
assert.enabledState(submit, 'Submit')
})
asyncTest('ctrl-clicking on a link does not disable the link', 6, function() {
QUnit.test('ctrl-clicking on a link does not disable the link', function(assert) {
var link = $('a[data-disable-with]')
App.checkEnabledState(link, 'Click me')
assert.enabledState(link, 'Click me')
link.triggerNative('click', { metaKey: true })
App.checkEnabledState(link, 'Click me')
assert.enabledState(link, 'Click me')
link.triggerNative('click', { metaKey: true })
App.checkEnabledState(link, 'Click me')
start()
assert.enabledState(link, 'Click me')
})
asyncTest('right/mouse-wheel-clicking on a link does not disable the link', 10, function() {
QUnit.test('right/mouse-wheel-clicking on a link does not disable the link', function(assert) {
var link = $('a[data-disable-with]')
App.checkEnabledState(link, 'Click me')
assert.enabledState(link, 'Click me')
link.triggerNative('click', { button: 1 })
App.checkEnabledState(link, 'Click me')
assert.enabledState(link, 'Click me')
link.triggerNative('click', { button: 1 })
App.checkEnabledState(link, 'Click me')
assert.enabledState(link, 'Click me')
link.triggerNative('click', { button: 2 })
App.checkEnabledState(link, 'Click me')
assert.enabledState(link, 'Click me')
link.triggerNative('click', { button: 2 })
App.checkEnabledState(link, 'Click me')
start()
assert.enabledState(link, 'Click me')
})
asyncTest('button[data-remote][data-disable-with] disables and re-enables', 6, function() {
QUnit.test('button[data-remote][data-disable-with] disables and re-enables', function(assert) {
const done = assert.async()
var button = $('button[data-remote][data-disable-with]')
App.checkEnabledState(button, 'Click me')
assert.enabledState(button, 'Click me')
button
.bindNative('ajax:send', function() {
App.checkDisabledState(button, 'clicking...')
assert.disabledState(button, 'clicking...')
})
.bindNative('ajax:complete', function() {
setTimeout( function() {
App.checkEnabledState(button, 'Click me')
start()
assert.enabledState(button, 'Click me')
done()
}, 15)
})
.triggerNative('click')
})
asyncTest('button[data-remote][data-disable-with] re-enables when `ajax:before` event is cancelled', 6, function() {
QUnit.test('button[data-remote][data-disable-with] re-enables when `ajax:before` event is cancelled', function(assert) {
const done = assert.async()
var button = $('button[data-remote][data-disable-with]')
App.checkEnabledState(button, 'Click me')
assert.enabledState(button, 'Click me')
button
.bindNative('ajax:before', function(e) {
App.checkDisabledState(button, 'clicking...')
assert.disabledState(button, 'clicking...')
e.preventDefault()
})
.triggerNative('click')
setTimeout(function() {
App.checkEnabledState(button, 'Click me')
start()
assert.enabledState(button, 'Click me')
done()
}, 30)
})
asyncTest('button[data-remote][data-disable-with] re-enables when `ajax:beforeSend` event is cancelled', 6, function() {
QUnit.test('button[data-remote][data-disable-with] re-enables when `ajax:beforeSend` event is cancelled', function(assert) {
const done = assert.async()
var button = $('button[data-remote][data-disable-with]')
App.checkEnabledState(button, 'Click me')
assert.enabledState(button, 'Click me')
button
.bindNative('ajax:beforeSend', function(e) {
App.checkDisabledState(button, 'clicking...')
assert.disabledState(button, 'clicking...')
e.preventDefault()
})
.triggerNative('click')
setTimeout(function() {
App.checkEnabledState(button, 'Click me')
start()
assert.enabledState(button, 'Click me')
done()
}, 30)
})
asyncTest('button[data-remote][data-disable-with] re-enables when `ajax:error` event is triggered', 6, function() {
QUnit.test('button[data-remote][data-disable-with] re-enables when `ajax:error` event is triggered', function(assert) {
const done = assert.async()
var button = $('a[data-disable-with]').attr('data-remote', true).attr('href', '/error')
App.checkEnabledState(button, 'Click me')
assert.enabledState(button, 'Click me')
button
.bindNative('ajax:send', function() {
App.checkDisabledState(button, 'clicking...')
assert.disabledState(button, 'clicking...')
})
.triggerNative('click')
setTimeout(function() {
App.checkEnabledState(button, 'Click me')
start()
assert.enabledState(button, 'Click me')
done()
}, 30)
})

View File

@ -1,5 +1,5 @@
module('data-disable', {
setup: function() {
QUnit.module('data-disable', {
beforeEach: function() {
$('#qunit-fixture').append($('<form />', {
action: '/echo',
'data-remote': 'true',
@ -29,68 +29,76 @@ module('data-disable', {
'data-disable': 'true'
}))
},
teardown: function() {
afterEach: function() {
$(document).unbind('iframe:loaded')
}
})
asyncTest('form input field with "data-disable" attribute', 7, function() {
QUnit.test('form input field with "data-disable" attribute', function(assert) {
const done = assert.async()
var form = $('form[data-remote]'), input = form.find('input[type=text]')
App.checkEnabledState(input, 'john')
assert.enabledState(input, 'john')
form.bindNative('ajax:success', function(e, data) {
setTimeout(function() {
App.checkEnabledState(input, 'john')
equal(data.params.user_name, 'john')
start()
assert.enabledState(input, 'john')
assert.equal(data.params.user_name, 'john')
done()
}, 13)
})
form.triggerNative('submit')
App.checkDisabledState(input, 'john')
assert.disabledState(input, 'john')
})
asyncTest('form button with "data-disable" attribute', 7, function() {
QUnit.test('form button with "data-disable" attribute', function(assert) {
const done = assert.async()
var form = $('form[data-remote]'), button = $('<button data-disable name="submit2">Submit</button>')
form.append(button)
App.checkEnabledState(button, 'Submit')
assert.enabledState(button, 'Submit')
form.bindNative('ajax:success', function(e, data) {
setTimeout(function() {
App.checkEnabledState(button, 'Submit')
start()
assert.enabledState(button, 'Submit')
done()
}, 13)
})
form.triggerNative('submit')
App.checkDisabledState(button, 'Submit')
equal(button.data('ujs:enable-with'), undefined)
assert.disabledState(button, 'Submit')
assert.equal(button.data('ujs:enable-with'), undefined)
})
asyncTest('form input[type=submit][data-disable] disables', 6, function() {
var form = $('form:not([data-remote])'), input = form.find('input[type=submit]')
QUnit.test('form input[type=submit][data-disable] disables', function(assert) {
const done = assert.async()
App.checkEnabledState(input, 'Submit')
var form = $('#qunit-fixture form:not([data-remote])'), input = form.find('input[type=submit]')
assert.enabledState(input, 'Submit')
// WEEIRDD: attaching this handler makes the test work in IE7
$(document).bind('iframe:loading', function(e, f) {})
$(document).bind('iframe:loaded', function(e, data) {
setTimeout(function() {
App.checkDisabledState(input, 'Submit')
start()
assert.disabledState(input, 'Submit')
done()
}, 30)
})
form.triggerNative('submit')
setTimeout(function() {
App.checkDisabledState(input, 'Submit')
assert.disabledState(input, 'Submit')
}, 30)
})
asyncTest('form[data-remote] input[type=submit][data-disable] is replaced in ajax callback', 2, function() {
QUnit.test('form[data-remote] input[type=submit][data-disable] is replaced in ajax callback', function(assert) {
const done = assert.async()
var form = $('#qunit-fixture form:not([data-remote])').attr('data-remote', 'true'), origFormContents = form.html()
form.bindNative('ajax:success', function() {
@ -98,13 +106,15 @@ asyncTest('form[data-remote] input[type=submit][data-disable] is replaced in aja
setTimeout(function() {
var input = form.find('input[type=submit]')
App.checkEnabledState(input, 'Submit')
start()
assert.enabledState(input, 'Submit')
done()
}, 30)
}).triggerNative('submit')
})
asyncTest('form[data-remote] input[data-disable] is replaced with disabled field in ajax callback', 2, function() {
QUnit.test('form[data-remote] input[data-disable] is replaced with disabled field in ajax callback', function(assert) {
const done = assert.async()
var form = $('#qunit-fixture form:not([data-remote])').attr('data-remote', 'true'), input = form.find('input[type=submit]'),
newDisabledInput = input.clone().attr('disabled', 'disabled')
@ -112,110 +122,119 @@ asyncTest('form[data-remote] input[data-disable] is replaced with disabled field
input.replaceWith(newDisabledInput)
setTimeout(function() {
App.checkEnabledState(newDisabledInput, 'Submit')
start()
assert.enabledState(newDisabledInput, 'Submit')
done()
}, 30)
}).triggerNative('submit')
})
asyncTest('form[data-remote] textarea[data-disable] attribute', 3, function() {
QUnit.test('form[data-remote] textarea[data-disable] attribute', function(assert) {
const done = assert.async()
var form = $('form[data-remote]'),
textarea = $('<textarea data-disable name="user_bio">born, lived, died.</textarea>').appendTo(form)
form.bindNative('ajax:success', function(e, data) {
setTimeout(function() {
equal(data.params.user_bio, 'born, lived, died.')
start()
assert.equal(data.params.user_bio, 'born, lived, died.')
done()
}, 13)
})
form.triggerNative('submit')
App.checkDisabledState(textarea, 'born, lived, died.')
assert.disabledState(textarea, 'born, lived, died.')
})
asyncTest('a[data-disable] disables', 5, function() {
QUnit.test('a[data-disable] disables', function(assert) {
var link = $('a[data-disable]')
App.checkEnabledState(link, 'Click me')
assert.enabledState(link, 'Click me')
link.triggerNative('click')
App.checkDisabledState(link, 'Click me')
equal(link.data('ujs:enable-with'), undefined)
start()
assert.disabledState(link, 'Click me')
assert.equal(link.data('ujs:enable-with'), undefined)
})
asyncTest('a[data-remote][data-disable] disables and re-enables', 6, function() {
QUnit.test('a[data-remote][data-disable] disables and re-enables', function(assert) {
const done = assert.async()
var link = $('a[data-disable]').attr('data-remote', true)
App.checkEnabledState(link, 'Click me')
assert.enabledState(link, 'Click me')
link
.bindNative('ajax:send', function() {
App.checkDisabledState(link, 'Click me')
assert.disabledState(link, 'Click me')
})
.bindNative('ajax:complete', function() {
setTimeout( function() {
App.checkEnabledState(link, 'Click me')
start()
assert.enabledState(link, 'Click me')
done()
}, 15)
})
.triggerNative('click')
})
asyncTest('a[data-remote][data-disable] re-enables when `ajax:before` event is cancelled', 6, function() {
QUnit.test('a[data-remote][data-disable] re-enables when `ajax:before` event is cancelled', function(assert) {
const done = assert.async()
var link = $('a[data-disable]').attr('data-remote', true)
App.checkEnabledState(link, 'Click me')
assert.enabledState(link, 'Click me')
link
.bindNative('ajax:before', function(e) {
App.checkDisabledState(link, 'Click me')
assert.disabledState(link, 'Click me')
e.preventDefault()
})
.triggerNative('click')
setTimeout(function() {
App.checkEnabledState(link, 'Click me')
start()
assert.enabledState(link, 'Click me')
done()
}, 30)
})
asyncTest('a[data-remote][data-disable] re-enables when `ajax:beforeSend` event is cancelled', 6, function() {
QUnit.test('a[data-remote][data-disable] re-enables when `ajax:beforeSend` event is cancelled', function(assert) {
const done = assert.async()
var link = $('a[data-disable]').attr('data-remote', true)
App.checkEnabledState(link, 'Click me')
assert.enabledState(link, 'Click me')
link
.bindNative('ajax:beforeSend', function(e) {
App.checkDisabledState(link, 'Click me')
assert.disabledState(link, 'Click me')
e.preventDefault()
})
.triggerNative('click')
setTimeout(function() {
App.checkEnabledState(link, 'Click me')
start()
assert.enabledState(link, 'Click me')
done()
}, 30)
})
asyncTest('a[data-remote][data-disable] re-enables when `ajax:error` event is triggered', 6, function() {
QUnit.test('a[data-remote][data-disable] re-enables when `ajax:error` event is triggered', function(assert) {
const done = assert.async()
var link = $('a[data-disable]').attr('data-remote', true).attr('href', '/error')
App.checkEnabledState(link, 'Click me')
assert.enabledState(link, 'Click me')
link
.bindNative('ajax:send', function() {
App.checkDisabledState(link, 'Click me')
assert.disabledState(link, 'Click me')
})
.triggerNative('click')
setTimeout(function() {
App.checkEnabledState(link, 'Click me')
start()
assert.enabledState(link, 'Click me')
done()
}, 30)
})
asyncTest('form[data-remote] input|button|textarea[data-disable] does not disable when `ajax:beforeSend` event is cancelled', 8, function() {
QUnit.test('form[data-remote] input|button|textarea[data-disable] does not disable when `ajax:beforeSend` event is cancelled', function(assert) {
var form = $('form[data-remote]'),
input = form.find('input:text'),
button = $('<button data-disable="submitting ..." name="submit2">Submit</button>').appendTo(form),
@ -229,130 +248,136 @@ asyncTest('form[data-remote] input|button|textarea[data-disable] does not disabl
})
.triggerNative('submit')
App.checkEnabledState(input, 'john')
App.checkEnabledState(button, 'Submit')
App.checkEnabledState(textarea, 'born, lived, died.')
App.checkEnabledState(submit, 'Submit')
start()
assert.enabledState(input, 'john')
assert.enabledState(button, 'Submit')
assert.enabledState(textarea, 'born, lived, died.')
assert.enabledState(submit, 'Submit')
})
asyncTest('ctrl-clicking on a link does not disables the link', 6, function() {
QUnit.test('ctrl-clicking on a link does not disables the link', function(assert) {
var link = $('a[data-disable]')
App.checkEnabledState(link, 'Click me')
assert.enabledState(link, 'Click me')
link.triggerNative('click', { metaKey: true })
App.checkEnabledState(link, 'Click me')
assert.enabledState(link, 'Click me')
link.triggerNative('click', { ctrlKey: true })
App.checkEnabledState(link, 'Click me')
start()
assert.enabledState(link, 'Click me')
})
asyncTest('right/mouse-wheel-clicking on a link does not disable the link', 10, function() {
QUnit.test('right/mouse-wheel-clicking on a link does not disable the link', function(assert) {
var link = $('a[data-disable]')
App.checkEnabledState(link, 'Click me')
assert.enabledState(link, 'Click me')
link.triggerNative('click', { button: 1 })
App.checkEnabledState(link, 'Click me')
assert.enabledState(link, 'Click me')
link.triggerNative('click', { button: 1 })
App.checkEnabledState(link, 'Click me')
assert.enabledState(link, 'Click me')
link.triggerNative('click', { button: 2 })
App.checkEnabledState(link, 'Click me')
assert.enabledState(link, 'Click me')
link.triggerNative('click', { button: 2 })
App.checkEnabledState(link, 'Click me')
start()
assert.enabledState(link, 'Click me')
})
asyncTest('button[data-remote][data-disable] disables and re-enables', 6, function() {
QUnit.test('button[data-remote][data-disable] disables and re-enables', function(assert) {
const done = assert.async()
var button = $('button[data-remote][data-disable]')
App.checkEnabledState(button, 'Click me')
assert.enabledState(button, 'Click me')
button
.bindNative('ajax:send', function() {
App.checkDisabledState(button, 'Click me')
assert.disabledState(button, 'Click me')
})
.bindNative('ajax:complete', function() {
setTimeout( function() {
App.checkEnabledState(button, 'Click me')
start()
assert.enabledState(button, 'Click me')
done()
}, 15)
})
.triggerNative('click')
})
asyncTest('button[data-remote][data-disable] re-enables when `ajax:before` event is cancelled', 6, function() {
QUnit.test('button[data-remote][data-disable] re-enables when `ajax:before` event is cancelled', function(assert) {
const done = assert.async()
var button = $('button[data-remote][data-disable]')
App.checkEnabledState(button, 'Click me')
assert.enabledState(button, 'Click me')
button
.bindNative('ajax:before', function(e) {
App.checkDisabledState(button, 'Click me')
assert.disabledState(button, 'Click me')
e.preventDefault()
})
.triggerNative('click')
setTimeout(function() {
App.checkEnabledState(button, 'Click me')
start()
assert.enabledState(button, 'Click me')
done()
}, 30)
})
asyncTest('button[data-remote][data-disable] re-enables when `ajax:beforeSend` event is cancelled', 6, function() {
QUnit.test('button[data-remote][data-disable] re-enables when `ajax:beforeSend` event is cancelled', function(assert) {
const done = assert.async()
var button = $('button[data-remote][data-disable]')
App.checkEnabledState(button, 'Click me')
assert.enabledState(button, 'Click me')
button
.bindNative('ajax:beforeSend', function(e) {
App.checkDisabledState(button, 'Click me')
assert.disabledState(button, 'Click me')
e.preventDefault()
})
.triggerNative('click')
setTimeout(function() {
App.checkEnabledState(button, 'Click me')
start()
assert.enabledState(button, 'Click me')
done()
}, 30)
})
asyncTest('button[data-remote][data-disable] re-enables when `ajax:error` event is triggered', 6, function() {
QUnit.test('button[data-remote][data-disable] re-enables when `ajax:error` event is triggered', function(assert) {
const done = assert.async()
var button = $('a[data-disable]').attr('data-remote', true).attr('href', '/error')
App.checkEnabledState(button, 'Click me')
assert.enabledState(button, 'Click me')
button
.bindNative('ajax:send', function() {
App.checkDisabledState(button, 'Click me')
assert.disabledState(button, 'Click me')
})
.triggerNative('click')
setTimeout(function() {
App.checkEnabledState(button, 'Click me')
start()
assert.enabledState(button, 'Click me')
done()
}, 30)
})
asyncTest('do not enable elements for XHR redirects', 6, function() {
QUnit.test('do not enable elements for XHR redirects', function(assert) {
const done = assert.async()
var link = $('a[data-disable]').attr('data-remote', true).attr('href', '/echo?with_xhr_redirect=true')
App.checkEnabledState(link, 'Click me')
assert.enabledState(link, 'Click me')
link
.bindNative('ajax:send', function() {
App.checkDisabledState(link, 'Click me')
assert.disabledState(link, 'Click me')
})
.triggerNative('click')
setTimeout(function() {
App.checkDisabledState(link, 'Click me')
start()
assert.disabledState(link, 'Click me')
done()
}, 30)
})

View File

@ -1,12 +1,12 @@
(function() {
module('data-method', {
setup: function() {
QUnit.module('data-method', {
beforeEach: function() {
$('#qunit-fixture').append($('<a />', {
href: '/echo', 'data-method': 'delete', text: 'destroy!'
}))
},
teardown: function() {
afterEach: function() {
$(document).unbind('iframe:loaded')
}
})
@ -14,49 +14,59 @@ module('data-method', {
function submit(fn, options) {
$(document).bind('iframe:loaded', function(e, data) {
fn(data)
start()
})
$('#qunit-fixture').find('a')
.triggerNative('click')
}
asyncTest('link with "data-method" set to "delete"', 3, function() {
QUnit.test('link with "data-method" set to "delete"', function(assert) {
const done = assert.async()
submit(function(data) {
equal(data.REQUEST_METHOD, 'DELETE')
strictEqual(data.params.authenticity_token, undefined)
strictEqual(data.HTTP_X_CSRF_TOKEN, undefined)
assert.equal(data.REQUEST_METHOD, 'DELETE')
assert.strictEqual(data.params.authenticity_token, undefined)
assert.strictEqual(data.HTTP_X_CSRF_TOKEN, undefined)
done()
})
})
asyncTest('click on the child of link with "data-method"', 3, function() {
QUnit.test('click on the child of link with "data-method"', function(assert) {
const done = assert.async()
$(document).bind('iframe:loaded', function(e, data) {
equal(data.REQUEST_METHOD, 'DELETE')
strictEqual(data.params.authenticity_token, undefined)
strictEqual(data.HTTP_X_CSRF_TOKEN, undefined)
start()
assert.equal(data.REQUEST_METHOD, 'DELETE')
assert.strictEqual(data.params.authenticity_token, undefined)
assert.strictEqual(data.HTTP_X_CSRF_TOKEN, undefined)
done()
})
$('#qunit-fixture a').html('<strong>destroy!</strong>').find('strong').triggerNative('click')
})
asyncTest('link with "data-method" and CSRF', 1, function() {
QUnit.test('link with "data-method" and CSRF', function(assert) {
const done = assert.async()
$('#qunit-fixture')
.append('<meta name="csrf-param" content="authenticity_token"/>')
.append('<meta name="csrf-token" content="cf50faa3fe97702ca1ae"/>')
submit(function(data) {
equal(data.params.authenticity_token, 'cf50faa3fe97702ca1ae')
assert.equal(data.params.authenticity_token, 'cf50faa3fe97702ca1ae')
done()
})
})
asyncTest('link "target" should be carried over to generated form', 1, function() {
QUnit.test('link "target" should be carried over to generated form', function(assert) {
const done = assert.async()
$('a[data-method]').attr('target', 'super-special-frame')
submit(function(data) {
equal(data.params._target, 'super-special-frame')
assert.equal(data.params._target, 'super-special-frame')
done()
})
})
asyncTest('link with "data-method" and cross origin', 1, function() {
QUnit.test('link with "data-method" and cross origin', function(assert) {
var data = {}
$('#qunit-fixture')
@ -77,9 +87,7 @@ asyncTest('link with "data-method" and cross origin', 1, function() {
link.triggerNative('click')
start()
notEqual(data.authenticity_token, 'cf50faa3fe97702ca1ae')
assert.notEqual(data.authenticity_token, 'cf50faa3fe97702ca1ae')
})
})()

View File

@ -12,8 +12,8 @@ function buildSelect(attrs) {
)
}
module('data-remote', {
setup: function() {
QUnit.module('data-remote', {
beforeEach: function() {
$('#qunit-fixture')
.append($('<a />', {
href: '/echo',
@ -44,9 +44,10 @@ module('data-remote', {
}
})
asyncTest('ctrl-clicking on a link does not fire ajaxyness', 0, function() {
QUnit.test('ctrl-clicking on a link does not fire ajaxyness', function(assert) {
const done = assert.async()
assert.expect(0)
var link = $('a[data-remote]')
// Ideally, we'd set up an iframe to intercept normal link clicks
// and add a test to make sure the iframe:loaded event is triggered.
// However, jquery doesn't actually cause a native `click` event and
@ -54,16 +55,21 @@ asyncTest('ctrl-clicking on a link does not fire ajaxyness', 0, function() {
link
.removeAttr('data-params')
.bindNative('ajax:beforeSend', function() {
ok(false, 'ajax should not be triggered')
assert.ok(false, 'ajax should not be triggered')
})
link.triggerNative('click', { metaKey: true })
link.triggerNative('click', { ctrlKey: true })
setTimeout(function() { start() }, 13)
setTimeout(function() {
done()
}, 13)
})
asyncTest('right/mouse-wheel-clicking on a link does not fire ajaxyness', 0, function() {
QUnit.test('right/mouse-wheel-clicking on a link does not fire ajaxyness', function(assert) {
const done = assert.async()
assert.expect(0)
var link = $('a[data-remote]')
// Ideally, we'd set up an iframe to intercept normal link clicks
@ -73,37 +79,43 @@ asyncTest('right/mouse-wheel-clicking on a link does not fire ajaxyness', 0, fun
link
.removeAttr('data-params')
.bindNative('ajax:beforeSend', function() {
ok(false, 'ajax should not be triggered')
assert.ok(false, 'ajax should not be triggered')
})
link.triggerNative('click', { button: 1 })
link.triggerNative('click', { button: 2 })
setTimeout(function() { start() }, 13)
setTimeout(function() {
done()
}, 13)
})
asyncTest('clicking on a link via a non-mouse Event (such as from js) works', 1, function() {
QUnit.test('clicking on a link via a non-mouse Event (such as from js) works', function(assert) {
var link = $('a[data-remote]')
const done = assert.async()
link
.removeAttr('data-params')
.bindNative('ajax:beforeSend', function() {
ok(true, 'ajax should be triggered')
assert.ok(true, 'ajax should be triggered')
})
Rails.fire(link[0], 'click')
setTimeout(function() { start() }, 13)
setTimeout(function() { done() }, 13)
})
asyncTest('ctrl-clicking on a link still fires ajax for non-GET links and for links with "data-params"', 2, function() {
QUnit.test('ctrl-clicking on a link still fires ajax for non-GET links and for links with "data-params"', function(assert) {
var link = $('a[data-remote]')
const done = assert.async()
link
.removeAttr('data-params')
.attr('data-method', 'POST')
.bindNative('ajax:beforeSend', function() {
ok(true, 'ajax should be triggered')
assert.ok(true, 'ajax should be triggered')
})
.triggerNative('click', { metaKey: true })
@ -112,76 +124,89 @@ asyncTest('ctrl-clicking on a link still fires ajax for non-GET links and for li
.attr('data-params', 'name=steve')
.triggerNative('click', { metaKey: true })
setTimeout(function() { start() }, 13)
setTimeout(function() { done() }, 13)
})
asyncTest('clicking on a link with data-remote attribute', 5, function() {
QUnit.test('clicking on a link with data-remote attribute', function(assert) {
const done = assert.async()
$('a[data-remote]')
.bindNative('ajax:success', function(e, data, status, xhr) {
App.assertCallbackInvoked('ajax:success')
App.assertRequestPath(data, '/echo')
equal(data.params.data1, 'value1', 'ajax arguments should have key data1 with right value')
equal(data.params.data2, 'value2', 'ajax arguments should have key data2 with right value')
App.assertGetRequest(data)
assert.callbackInvoked('ajax:success')
assert.requestPath(data, '/echo')
assert.equal(data.params.data1, 'value1', 'ajax arguments should have key data1 with right value')
assert.equal(data.params.data2, 'value2', 'ajax arguments should have key data2 with right value')
assert.getRequest(data)
})
.bindNative('ajax:complete', function() { start() })
.bindNative('ajax:complete', function() { done() })
.triggerNative('click')
})
asyncTest('clicking on a link with both query string in href and data-params', 4, function() {
QUnit.test('clicking on a link with both query string in href and data-params', function(assert) {
const done = assert.async()
$('a[data-remote]')
.attr('href', '/echo?data3=value3')
.bindNative('ajax:success', function(e, data, status, xhr) {
App.assertGetRequest(data)
equal(data.params.data1, 'value1', 'ajax arguments should have key data1 with right value')
equal(data.params.data2, 'value2', 'ajax arguments should have key data2 with right value')
equal(data.params.data3, 'value3', 'query string in URL should be passed to server with right value')
assert.getRequest(data)
assert.equal(data.params.data1, 'value1', 'ajax arguments should have key data1 with right value')
assert.equal(data.params.data2, 'value2', 'ajax arguments should have key data2 with right value')
assert.equal(data.params.data3, 'value3', 'query string in URL should be passed to server with right value')
})
.bindNative('ajax:complete', function() { start() })
.bindNative('ajax:complete', function() { done() })
.triggerNative('click')
})
asyncTest('clicking on a link with both query string in href and data-params with POST method', 4, function() {
QUnit.test('clicking on a link with both query string in href and data-params with POST method', function(assert) {
const done = assert.async()
$('a[data-remote]')
.attr('href', '/echo?data3=value3')
.attr('data-method', 'post')
.bindNative('ajax:success', function(e, data, status, xhr) {
App.assertPostRequest(data)
equal(data.params.data1, 'value1', 'ajax arguments should have key data1 with right value')
equal(data.params.data2, 'value2', 'ajax arguments should have key data2 with right value')
equal(data.params.data3, 'value3', 'query string in URL should be passed to server with right value')
assert.postRequest(data)
assert.equal(data.params.data1, 'value1', 'ajax arguments should have key data1 with right value')
assert.equal(data.params.data2, 'value2', 'ajax arguments should have key data2 with right value')
assert.equal(data.params.data3, 'value3', 'query string in URL should be passed to server with right value')
})
.bindNative('ajax:complete', function() { start() })
.bindNative('ajax:complete', function() { done() })
.triggerNative('click')
})
asyncTest('clicking on a link with disabled attribute', 0, function() {
$('a[disabled]')
QUnit.test('clicking on a link with disabled attribute', function(assert) {
const done = assert.async()
assert.expect(0)
$('#qunit-fixture a[disabled]')
.bindNative('ajax:before', function(e, data, status, xhr) {
App.assertCallbackNotInvoked('ajax:success')
assert.callbackNotInvoked('ajax:success')
})
.bindNative('ajax:complete', function() { start() })
.triggerNative('click')
setTimeout(function() {
start()
done()
}, 13)
})
asyncTest('clicking on a button with data-remote attribute', 5, function() {
QUnit.test('clicking on a button with data-remote attribute', function(assert) {
const done = assert.async()
$('button[data-remote]')
.bindNative('ajax:success', function(e, data, status, xhr) {
App.assertCallbackInvoked('ajax:success')
App.assertRequestPath(data, '/echo')
equal(data.params.data1, 'value1', 'ajax arguments should have key data1 with right value')
equal(data.params.data2, 'value2', 'ajax arguments should have key data2 with right value')
App.assertGetRequest(data)
assert.callbackInvoked('ajax:success')
assert.requestPath(data, '/echo')
assert.equal(data.params.data1, 'value1', 'ajax arguments should have key data1 with right value')
assert.equal(data.params.data2, 'value2', 'ajax arguments should have key data2 with right value')
assert.getRequest(data)
})
.bindNative('ajax:complete', function() { start() })
.bindNative('ajax:complete', function() { done() })
.triggerNative('click')
})
asyncTest('right/mouse-wheel-clicking on a button with data-remote attribute does not fire ajaxyness', 0, function() {
QUnit.test('right/mouse-wheel-clicking on a button with data-remote attribute does not fire ajaxyness', function(assert) {
const done = assert.async()
assert.expect(0)
var button = $('button[data-remote]')
// Ideally, we'd set up an iframe to intercept normal link clicks
@ -191,86 +216,98 @@ asyncTest('right/mouse-wheel-clicking on a button with data-remote attribute doe
button
.removeAttr('data-params')
.bindNative('ajax:beforeSend', function() {
ok(false, 'ajax should not be triggered')
assert.ok(false, 'ajax should not be triggered')
})
button.triggerNative('click', { button: 1 })
button.triggerNative('click', { button: 2 })
setTimeout(function() { start() }, 13)
setTimeout(function() {
done()
}, 13)
})
asyncTest('changing a select option with data-remote attribute', 5, function() {
QUnit.test('changing a select option with data-remote attribute', function(assert) {
const done = assert.async()
buildSelect()
$('select[data-remote]')
.bindNative('ajax:success', function(e, data, status, xhr) {
App.assertCallbackInvoked('ajax:success')
App.assertRequestPath(data, '/echo')
equal(data.params.user_data, 'optionValue2', 'ajax arguments should have key term with right value')
equal(data.params.data1, 'value1', 'ajax arguments should have key data1 with right value')
App.assertGetRequest(data)
assert.callbackInvoked('ajax:success')
assert.requestPath(data, '/echo')
assert.equal(data.params.user_data, 'optionValue2', 'ajax arguments should have key term with right value')
assert.equal(data.params.data1, 'value1', 'ajax arguments should have key data1 with right value')
assert.getRequest(data)
})
.bindNative('ajax:complete', function() { start() })
.bindNative('ajax:complete', function() { done() })
.val('optionValue2')
.triggerNative('change')
})
asyncTest('submitting form with data-remote attribute', 4, function() {
QUnit.test('submitting form with data-remote attribute', function(assert) {
const done = assert.async()
$('form[data-remote]')
.bindNative('ajax:success', function(e, data, status, xhr) {
App.assertCallbackInvoked('ajax:success')
App.assertRequestPath(data, '/echo')
equal(data.params.user_name, 'john', 'ajax arguments should have key user_name with right value')
App.assertPostRequest(data)
assert.callbackInvoked('ajax:success')
assert.requestPath(data, '/echo')
assert.equal(data.params.user_name, 'john', 'ajax arguments should have key user_name with right value')
assert.postRequest(data)
})
.bindNative('ajax:complete', function() { start() })
.bindNative('ajax:complete', function() { done() })
.triggerNative('submit')
})
asyncTest('submitting form with data-remote attribute should include inputs in a fieldset only once', 3, function() {
QUnit.test('submitting form with data-remote attribute should include inputs in a fieldset only once', function(assert) {
const done = assert.async()
$('form[data-remote]')
.append('<fieldset><input name="items[]" value="Item" /></fieldset>')
.bindNative('ajax:success', function(e, data, status, xhr) {
App.assertCallbackInvoked('ajax:success')
equal(data.params.items.length, 1, 'ajax arguments should only have the item once')
App.assertPostRequest(data)
assert.callbackInvoked('ajax:success')
assert.equal(data.params.items.length, 1, 'ajax arguments should only have the item once')
assert.postRequest(data)
})
.bindNative('ajax:complete', function() {
$('form[data-remote], fieldset').remove()
start()
done()
})
.triggerNative('submit')
})
asyncTest('submitting form with data-remote attribute submits input with matching [form] attribute', 6, function() {
QUnit.test('submitting form with data-remote attribute submits input with matching [form] attribute', function(assert) {
const done = assert.async()
$('#qunit-fixture')
.append($('<input type="text" name="user_data" value="value1" form="my-remote-form">'))
.append($('<input type="text" name="user_email" value="from@example.com" disabled="disabled" form="my-remote-form">'))
$('form[data-remote]')
.bindNative('ajax:success', function(e, data, status, xhr) {
App.assertCallbackInvoked('ajax:success')
App.assertRequestPath(data, '/echo')
equal(data.params.user_name, 'john', 'ajax arguments should have key user_name with right value')
equal(data.params.user_data, 'value1', 'ajax arguments should have key user_data with right value')
equal(data.params.user_email, undefined, 'ajax arguments should not have disabled field')
App.assertPostRequest(data)
assert.callbackInvoked('ajax:success')
assert.requestPath(data, '/echo')
assert.equal(data.params.user_name, 'john', 'ajax arguments should have key user_name with right value')
assert.equal(data.params.user_data, 'value1', 'ajax arguments should have key user_data with right value')
assert.equal(data.params.user_email, undefined, 'ajax arguments should not have disabled field')
assert.postRequest(data)
})
.bindNative('ajax:complete', function() { start() })
.bindNative('ajax:complete', function() { done() })
.triggerNative('submit')
})
asyncTest('submitting form with data-remote attribute by clicking button with matching [form] attribute', 5, function() {
QUnit.test('submitting form with data-remote attribute by clicking button with matching [form] attribute', function(assert) {
const done = assert.async()
$('form[data-remote]')
.bindNative('ajax:success', function(e, data, status, xhr) {
App.assertCallbackInvoked('ajax:success')
App.assertRequestPath(data, '/echo')
equal(data.params.user_name, 'john', 'ajax arguments should have key user_name with right value')
equal(data.params.user_data, 'value2', 'ajax arguments should have key user_data with right value')
App.assertPostRequest(data)
assert.callbackInvoked('ajax:success')
assert.requestPath(data, '/echo')
assert.equal(data.params.user_name, 'john', 'ajax arguments should have key user_name with right value')
assert.equal(data.params.user_data, 'value2', 'ajax arguments should have key user_data with right value')
assert.postRequest(data)
})
.bindNative('ajax:complete', function() { start() })
.bindNative('ajax:complete', function() { done() })
$('<button />', {
type: 'submit',
@ -290,24 +327,26 @@ asyncTest('submitting form with data-remote attribute by clicking button with ma
.triggerNative('click')
})
asyncTest('form\'s submit bindings in browsers that don\'t support submit bubbling', 5, function() {
QUnit.test('form\'s submit bindings in browsers that don\'t support submit bubbling', function(assert) {
const done = assert.async()
var form = $('form[data-remote]'), directBindingCalled = false
ok(!directBindingCalled, 'nothing is called')
assert.ok(!directBindingCalled, 'nothing is called')
form
.append($('<input type="submit" />'))
.bindNative('submit', function(event) {
ok(event.type == 'submit', 'submit event handlers are called with submit event')
ok(true, 'binding handler is called')
assert.ok(event.type == 'submit', 'submit event handlers are called with submit event')
assert.ok(true, 'binding handler is called')
directBindingCalled = true
})
.bindNative('ajax:beforeSend', function() {
ok(true, 'form being submitted via ajax')
ok(directBindingCalled, 'binding handler already called')
assert.ok(true, 'form being submitted via ajax')
assert.ok(directBindingCalled, 'binding handler already called')
})
.bindNative('ajax:complete', function() {
start()
done()
})
if(!$.support.submitBubbles) {
@ -319,18 +358,20 @@ asyncTest('form\'s submit bindings in browsers that don\'t support submit bubbli
}
})
asyncTest('returning false in form\'s submit bindings in non-submit-bubbling browsers', 1, function() {
QUnit.test('returning false in form\'s submit bindings in non-submit-bubbling browsers', function(assert) {
const done = assert.async()
var form = $('form[data-remote]')
form
.append($('<input type="submit" />'))
.bindNative('submit', function(e) {
ok(true, 'binding handler is called')
assert.ok(true, 'binding handler is called')
e.preventDefault()
e.stopPropagation()
})
.bindNative('ajax:beforeSend', function() {
ok(false, 'form should not be submitted')
assert.ok(false, 'form should not be submitted')
})
if (!$.support.submitBubbles) {
@ -340,32 +381,38 @@ asyncTest('returning false in form\'s submit bindings in non-submit-bubbling bro
form.triggerNative('submit')
}
setTimeout(function() { start() }, 13)
setTimeout(function() { done() }, 13)
})
asyncTest('clicking on a link with falsy "data-remote" attribute does not fire ajaxyness', 0, function() {
QUnit.test('clicking on a link with falsy "data-remote" attribute does not fire ajaxyness', function(assert) {
const done = assert.async()
assert.expect(0)
$('a[data-remote]')
.attr('data-remote', 'false')
.bindNative('ajax:beforeSend', function() {
ok(false, 'ajax should not be triggered')
assert.ok(false, 'ajax should not be triggered')
})
.bindNative('click', function(e) {
e.preventDefault()
})
.triggerNative('click')
setTimeout(function() { start() }, 20)
setTimeout(function() { done() }, 20)
})
asyncTest('ctrl-clicking on a link with falsy "data-remote" attribute does not fire ajaxyness even if "data-params" present', 0, function() {
QUnit.test('ctrl-clicking on a link with falsy "data-remote" attribute does not fire ajaxyness even if "data-params" present', function(assert) {
const done = assert.async()
var link = $('a[data-remote]')
assert.expect(0)
link
.removeAttr('data-params')
.attr('data-remote', 'false')
.attr('data-method', 'POST')
.bindNative('ajax:beforeSend', function() {
ok(false, 'ajax should not be triggered')
assert.ok(false, 'ajax should not be triggered')
})
.bindNative('click', function(e) {
e.preventDefault()
@ -377,52 +424,66 @@ asyncTest('ctrl-clicking on a link with falsy "data-remote" attribute does not f
.attr('data-params', 'name=steve')
.triggerNative('click', { metaKey: true })
setTimeout(function() { start() }, 20)
setTimeout(function() { done() }, 20)
})
asyncTest('clicking on a button with falsy "data-remote" attribute', 0, function() {
QUnit.test('clicking on a button with falsy "data-remote" attribute', function(assert) {
const done = assert.async()
assert.expect(0)
$('button[data-remote]:first')
.attr('data-remote', 'false')
.bindNative('ajax:beforeSend', function() {
ok(false, 'ajax should not be triggered')
assert.ok(false, 'ajax should not be triggered')
})
.bindNative('click', function(e) {
e.preventDefault()
})
.triggerNative('click')
setTimeout(function() { start() }, 20)
setTimeout(function() { done() }, 20)
})
asyncTest('submitting a form with falsy "data-remote" attribute', 0, function() {
QUnit.test('submitting a form with falsy "data-remote" attribute', function(assert) {
const done = assert.async()
assert.expect(0)
$('form[data-remote]:first')
.attr('data-remote', 'false')
.bindNative('ajax:beforeSend', function() {
ok(false, 'ajax should not be triggered')
assert.ok(false, 'ajax should not be triggered')
})
.bindNative('submit', function(e) {
e.preventDefault()
})
.triggerNative('submit')
setTimeout(function() { start() }, 20)
setTimeout(function() { done() }, 20)
})
asyncTest('changing a select option with falsy "data-remote" attribute', 0, function() {
QUnit.test('changing a select option with falsy "data-remote" attribute', function(assert) {
const done = assert.async()
assert.expect(0)
buildSelect({'data-remote': 'false'})
$('select[data-remote=false]:first')
.bindNative('ajax:beforeSend', function() {
ok(false, 'ajax should not be triggered')
assert.ok(false, 'ajax should not be triggered')
})
.val('optionValue2')
.triggerNative('change')
setTimeout(function() { start() }, 20)
setTimeout(function() { done() }, 20)
})
asyncTest('form should be serialized correctly', 6, function() {
$('form')
QUnit.test('form should be serialized correctly', function(assert) {
const done = assert.async()
$('#qunit-fixture form')
.append('<textarea name="textarea">textarea</textarea>')
.append('<input type="checkbox" name="checkbox[]" value="0" />')
.append('<input type="checkbox" checked="checked" name="checkbox[]" value="1" />')
@ -435,35 +496,39 @@ asyncTest('form should be serialized correctly', 6, function() {
<option selected>4</option>\
</select>')
.bindNative('ajax:success', function(e, data, status, xhr) {
equal(data.params.checkbox.length, 1)
equal(data.params.checkbox[0], '1')
equal(data.params.radio, '0')
equal(data.params.select.length, 3)
equal(data.params.select[2], '4')
equal(data.params.textarea, 'textarea')
assert.equal(data.params.checkbox.length, 1)
assert.equal(data.params.checkbox[0], '1')
assert.equal(data.params.radio, '0')
assert.equal(data.params.select.length, 3)
assert.equal(data.params.select[2], '4')
assert.equal(data.params.textarea, 'textarea')
start()
done()
})
.triggerNative('submit')
})
asyncTest('form buttons should only be serialized when clicked', 4, function() {
$('form')
QUnit.test('form buttons should only be serialized when clicked', function(assert) {
const done = assert.async()
$('#qunit-fixture form')
.append('<input type="submit" name="submit1" value="submit1" />')
.append('<button name="submit2" value="submit2" />')
.append('<button name="submit3" value="submit3" />')
.bindNative('ajax:success', function(e, data, status, xhr) {
equal(data.params.submit1, undefined)
equal(data.params.submit2, 'submit2')
equal(data.params.submit3, undefined)
equal(data['rack.request.form_vars'], 'user_name=john&submit2=submit2')
assert.equal(data.params.submit1, undefined)
assert.equal(data.params.submit2, 'submit2')
assert.equal(data.params.submit3, undefined)
assert.equal(data['rack.request.form_vars'], 'user_name=john&submit2=submit2')
start()
done()
})
.find('[name=submit2]').triggerNative('click')
})
asyncTest('changing a select option without "data-url" attribute still fires ajax request to current location', 1, function() {
QUnit.test('changing a select option without "data-url" attribute still fires ajax request to current location', function(assert) {
const done = assert.async()
var currentLocation, ajaxLocation
buildSelect({'data-url': ''})
@ -480,18 +545,20 @@ asyncTest('changing a select option without "data-url" attribute still fires aja
}
ajaxLocation = settings.url.replace(settings.data, '').replace(/&$/, '').replace(/\?$/, '')
equal(ajaxLocation, currentLocation, 'URL should be current page by default')
assert.equal(ajaxLocation, currentLocation, 'URL should be current page by default')
e.preventDefault()
})
.val('optionValue2')
.triggerNative('change')
setTimeout(function() { start() }, 20)
setTimeout(function() { done() }, 20)
})
asyncTest('inputs inside disabled fieldset are not submitted on remote forms', 3, function() {
$('form')
QUnit.test('inputs inside disabled fieldset are not submitted on remote forms', function(assert) {
const done = assert.async()
$('#qunit-fixture form')
.append('<fieldset>\
<input name="description" value="A wise man" />\
</fieldset>')
@ -499,11 +566,11 @@ asyncTest('inputs inside disabled fieldset are not submitted on remote forms', 3
<input name="age" />\
</fieldset>')
.bindNative('ajax:success', function(e, data, status, xhr) {
equal(data.params.user_name, 'john')
equal(data.params.description, 'A wise man')
equal(data.params.age, undefined)
assert.equal(data.params.user_name, 'john')
assert.equal(data.params.description, 'A wise man')
assert.equal(data.params.age, undefined)
start()
done()
})
.triggerNative('submit')
})

View File

@ -2,55 +2,53 @@
var realHref
module('override', {
setup: function() {
QUnit.module('override', {
beforeEach: function() {
realHref = $.rails.href
$('#qunit-fixture')
.append($('<a />', {
href: '/real/href', 'data-remote': 'true', 'data-method': 'delete', 'data-href': '/data/href'
}))
},
teardown: function() {
afterEach: function() {
$.rails.href = realHref
}
})
asyncTest('the getter for an element\'s href is publicly accessible', 1, function() {
ok($.rails.href)
start()
QUnit.test('the getter for an element\'s href is publicly accessible', function(assert) {
assert.ok($.rails.href)
})
asyncTest('the getter for an element\'s href is overridable', 1, function() {
QUnit.test('the getter for an element\'s href is overridable', function(assert) {
$.rails.href = function(element) { return $(element).data('href') }
$('#qunit-fixture a')
.bindNative('ajax:beforeSend', function(e, xhr, options) {
equal('/data/href', options.url)
assert.equal('/data/href', options.url)
e.preventDefault()
})
.triggerNative('click')
start()
})
asyncTest('the getter for an element\'s href works normally if not overridden', 1, function() {
QUnit.test('the getter for an element\'s href works normally if not overridden', function(assert) {
$('#qunit-fixture a')
.bindNative('ajax:beforeSend', function(e, xhr, options) {
equal(location.protocol + '//' + location.host + '/real/href', options.url)
assert.equal(location.protocol + '//' + location.host + '/real/href', options.url)
e.preventDefault()
})
.triggerNative('click')
start()
})
asyncTest('the event selector strings are overridable', 1, function() {
ok($.rails.linkClickSelector.indexOf(', a[data-custom-remote-link]') != -1, 'linkClickSelector contains custom selector')
start()
QUnit.test('the event selector strings are overridable', function(assert) {
assert.ok($.rails.linkClickSelector.indexOf(', a[data-custom-remote-link]') != -1, 'linkClickSelector contains custom selector')
})
asyncTest('including rails-ujs multiple times throws error', 1, function() {
throws(function() {
QUnit.test('including rails-ujs multiple times throws error', function(assert) {
const done = assert.async()
assert.throws(function() {
Rails.start()
}, 'appending rails.js again throws error')
setTimeout(function() { start() }, 50)
setTimeout(function() { done() }, 50)
})
})()

View File

@ -1,24 +1,24 @@
var App = App || {}
var Turbolinks = Turbolinks || {}
App.assertCallbackInvoked = function(callbackName) {
ok(true, callbackName + ' callback should have been invoked')
QUnit.assert.callbackInvoked = function(callbackName) {
this.ok(true, callbackName + ' callback should have been invoked')
}
App.assertCallbackNotInvoked = function(callbackName) {
ok(false, callbackName + ' callback should not have been invoked')
QUnit.assert.callbackNotInvoked = function(callbackName) {
this.ok(false, callbackName + ' callback should not have been invoked')
}
App.assertGetRequest = function(requestEnv) {
equal(requestEnv['REQUEST_METHOD'], 'GET', 'request type should be GET')
QUnit.assert.getRequest = function(requestEnv) {
this.equal(requestEnv['REQUEST_METHOD'], 'GET', 'request type should be GET')
}
App.assertPostRequest = function(requestEnv) {
equal(requestEnv['REQUEST_METHOD'], 'POST', 'request type should be POST')
QUnit.assert.postRequest = function(requestEnv) {
this.equal(requestEnv['REQUEST_METHOD'], 'POST', 'request type should be POST')
}
App.assertRequestPath = function(requestEnv, path) {
equal(requestEnv['PATH_INFO'], path, 'request should be sent to right URL')
QUnit.assert.requestPath = function(requestEnv, path) {
this.equal(requestEnv['PATH_INFO'], path, 'request should be sent to right URL')
}
App.getVal = function(el) {
@ -31,14 +31,14 @@ App.disabled = function(el) {
$.rails.getData(el[0], 'ujs:disabled')
}
App.checkEnabledState = function(el, text) {
ok(!App.disabled(el), el.get(0).tagName + ' should not be disabled')
equal(App.getVal(el), text, el.get(0).tagName + ' text should be original value')
QUnit.assert.enabledState = function(el, text) {
this.ok(!App.disabled(el), el.get(0).tagName + ' should not be disabled')
this.equal(App.getVal(el), text, el.get(0).tagName + ' text should be original value')
}
App.checkDisabledState = function(el, text) {
ok(App.disabled(el), el.get(0).tagName + ' should be disabled')
equal(App.getVal(el), text, el.get(0).tagName + ' text should be disabled value')
QUnit.assert.disabledState = function(el, text) {
this.ok(App.disabled(el), el.get(0).tagName + ' should be disabled')
this.equal(App.getVal(el), text, el.get(0).tagName + ' text should be disabled value')
}
// hijacks normal form submit; lets it submit to an iframe to prevent

View File

@ -1,38 +1,77 @@
/*!
* QUnit 1.14.0
* http://qunitjs.com/
* QUnit 2.19.1
* https://qunitjs.com/
*
* Copyright 2013 jQuery Foundation and other contributors
* Copyright OpenJS Foundation and other contributors
* Released under the MIT license
* http://jquery.org/license
*
* Date: 2014-01-31T16:40Z
* https://jquery.org/license
*/
/** Font Family and Sizes */
#qunit-tests, #qunit-header, #qunit-banner, #qunit-testrunner-toolbar, #qunit-userAgent, #qunit-testresult {
#qunit-tests, #qunit-header, #qunit-banner, #qunit-testrunner-toolbar, #qunit-filteredTest, #qunit-userAgent, #qunit-testresult {
font-family: "Helvetica Neue Light", "HelveticaNeue-Light", "Helvetica Neue", Calibri, Helvetica, Arial, sans-serif;
}
#qunit-testrunner-toolbar, #qunit-userAgent, #qunit-testresult, #qunit-tests li { font-size: small; }
#qunit-testrunner-toolbar, #qunit-filteredTest, #qunit-userAgent, #qunit-testresult, #qunit-tests li { font-size: small; }
#qunit-tests { font-size: smaller; }
/** Resets */
#qunit-tests, #qunit-header, #qunit-banner, #qunit-userAgent, #qunit-testresult, #qunit-modulefilter {
#qunit-tests, #qunit-header, #qunit-banner, #qunit-filteredTest, #qunit-userAgent, #qunit-testresult, #qunit-modulefilter {
margin: 0;
padding: 0;
}
/* Style our buttons in a simple way, uninfluenced by the styles
the tested app might load. Don't affect buttons in #qunit-fixture!
https://github.com/qunitjs/qunit/pull/1395
https://github.com/qunitjs/qunit/issues/1437 */
#qunit-testrunner-toolbar button,
#qunit-testresult button {
all: unset; /* best effort, modern browsers only */
font: inherit;
color: initial;
border: initial;
background-color: buttonface;
padding: 0 4px;
}
/** Header */
/** Fixed headers with scrollable tests */
@supports (display: flex) or (display: -webkit-box) {
@media (min-height: 500px) {
#qunit {
position: fixed;
left: 0px;
right: 0px;
top: 0px;
bottom: 0px;
padding: 8px;
display: -webkit-box;
display: flex;
flex-direction: column;
}
#qunit-tests {
overflow: scroll;
}
#qunit-banner {
flex: 5px 0 0;
}
}
}
/** Header (excluding toolbar) */
#qunit-header {
padding: 0.5em 0 0.5em 1em;
color: #8699A4;
color: #C2CCD1;
background-color: #0D3349;
font-size: 1.5em;
@ -44,7 +83,7 @@
#qunit-header a {
text-decoration: none;
color: #C2CCD1;
color: inherit;
}
#qunit-header a:hover,
@ -52,33 +91,205 @@
color: #FFF;
}
#qunit-testrunner-toolbar label {
display: inline-block;
padding: 0 0.5em 0 0.1em;
}
#qunit-banner {
height: 5px;
}
#qunit-testrunner-toolbar {
padding: 0.5em 0 0.5em 2em;
color: #5E740B;
background-color: #EEE;
overflow: hidden;
#qunit-filteredTest {
padding: 0.5em 1em 0.5em 1em;
color: #366097;
background-color: #F4FF77;
}
#qunit-userAgent {
padding: 0.5em 0 0.5em 2.5em;
background-color: #2B81AF;
padding: 0.5em 1em 0.5em 1em;
color: #FFF;
background-color: #2B81AF;
text-shadow: rgba(0, 0, 0, 0.5) 2px 2px 1px;
}
#qunit-modulefilter-container {
float: right;
/** Toolbar */
#qunit-testrunner-toolbar {
padding: 0.5em 1em 0.5em 1em;
color: #5E740B;
background-color: #EEE;
}
#qunit-testrunner-toolbar .clearfix {
height: 0;
clear: both;
}
#qunit-testrunner-toolbar label {
display: inline-block;
}
#qunit-testrunner-toolbar input[type=checkbox],
#qunit-testrunner-toolbar input[type=radio] {
margin: 3px;
vertical-align: -2px;
}
#qunit-testrunner-toolbar input[type=text] {
box-sizing: border-box;
height: 1.6em;
}
#qunit-testrunner-toolbar button,
#qunit-testresult button {
border-radius: .25em;
border: 1px solid #AAA;
background-color: #F8F8F8;
color: #222;
line-height: 1.6;
cursor: pointer;
}
#qunit-testrunner-toolbar button:hover,
#qunit-testresult button:hover {
border-color: #AAA;
background-color: #FFF;
color: #444;
}
#qunit-testrunner-toolbar button:active,
#qunit-testresult button:active {
border-color: #777;
background-color: #CCC;
color: #000;
}
#qunit-testrunner-toolbar button:focus,
#qunit-testresult button:focus {
border-color: #2F68DA;
/* emulate 2px border without a layout shift */
box-shadow: inset 0 0 0 1px #2F68DA
}
#qunit-testrunner-toolbar button:disabled,
#qunit-testresult button:disabled {
border-color: #CCC;
background-color: #CCC;
color: #FFF;
cursor: default;
}
#qunit-toolbar-filters {
float: right;
/* aligning right avoids overflows and inefficient use of space
around the dropdown menu on narrow viewports */
text-align: right;
}
.qunit-url-config,
.qunit-filter,
#qunit-modulefilter {
display: inline-block;
line-height: 2.1em;
text-align: left;
}
.qunit-filter,
#qunit-modulefilter {
position: relative;
margin-left: 1em;
}
.qunit-url-config label {
margin-right: 0.5em;
}
#qunit-modulefilter-search {
box-sizing: border-box;
min-width: 400px;
min-width: min(400px, 80vw);
}
#qunit-modulefilter-search-container {
position: relative;
}
#qunit-modulefilter-search-container:after {
position: absolute;
right: 0.3em;
bottom: 0;
line-height: 100%;
content: "\25bc";
color: black;
}
#qunit-modulefilter-dropdown {
/* align with #qunit-modulefilter-search */
box-sizing: border-box;
min-width: 400px;
min-width: min(400px, 80vw);
max-width: 80vw;
position: absolute;
right: 0;
top: 100%;
margin-top: 2px;
/* ensure that when on a narrow viewports and having only one result,
that #qunit-modulefilter-actions fall outside the dropdown rectangle. */
min-height: 3em;
border: 1px solid #AAA;
border-top-color: transparent;
border-radius: 0 0 .25em .25em;
color: #0D3349;
background-color: #F5F5F5;
z-index: 99;
}
#qunit-modulefilter-actions {
display: block;
overflow: auto;
/* align with #qunit-modulefilter-dropdown-list */
font: smaller/1.5em sans-serif;
}
@media (min-width: 350px) {
#qunit-modulefilter-actions {
position: absolute;
right: 0;
}
}
#qunit-modulefilter-dropdown #qunit-modulefilter-actions > * {
box-sizing: border-box;
max-height: 2.8em;
display: block;
padding: 0.4em;
}
#qunit-modulefilter-dropdown #qunit-modulefilter-actions > button {
float: right;
margin: 0.25em;
}
#qunit-modulefilter-dropdown-list {
margin: 0;
padding: 0;
font: smaller/1.5em sans-serif;
}
#qunit-modulefilter-dropdown-list li {
list-style: none;
}
#qunit-modulefilter-dropdown-list .clickable {
display: block;
padding: 0.25em 0.50em 0.25em 0.15em;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
#qunit-modulefilter-dropdown-list .clickable.checked {
font-weight: bold;
background-color: #E2F0F7;
color: #0D3349;
}
#qunit-modulefilter-dropdown .clickable:hover {
background-color: #FFF;
color: #444;
}
/** Tests: Pass/Fail */
#qunit-tests {
@ -86,27 +297,54 @@
}
#qunit-tests li {
padding: 0.4em 0.5em 0.4em 2.5em;
padding: 0.4em 1em 0.4em 1em;
border-bottom: 1px solid #FFF;
list-style-position: inside;
}
#qunit-tests.hidepass li.pass, #qunit-tests.hidepass li.running {
#qunit-tests > li {
display: none;
}
#qunit-tests li.running,
#qunit-tests li.pass,
#qunit-tests li.fail,
#qunit-tests li.skipped,
#qunit-tests li.aborted {
display: list-item;
}
#qunit-tests.hidepass {
position: relative;
}
#qunit-tests.hidepass li.running,
#qunit-tests.hidepass li.pass:not(.todo) {
visibility: hidden;
position: absolute;
width: 0;
height: 0;
padding: 0;
border: 0;
margin: 0;
}
#qunit-tests li strong {
cursor: pointer;
}
#qunit-tests li.skipped strong {
cursor: default;
}
#qunit-tests li a {
padding: 0.5em;
color: #C2CCD1;
text-decoration: none;
color: inherit;
text-decoration: underline;
}
#qunit-tests li a:hover,
#qunit-tests li a:focus {
color: #000;
color: #0D3349;
}
#qunit-tests li .runtime {
@ -123,6 +361,10 @@
border-radius: 5px;
}
.qunit-source {
margin: 0.6em 0 0.3em;
}
.qunit-collapsed {
display: none;
}
@ -149,20 +391,20 @@
}
#qunit-tests del {
background-color: #E0F2BE;
color: #374E0C;
background-color: #E0F2BE;
text-decoration: none;
}
#qunit-tests ins {
background-color: #FFCACA;
color: #500;
background-color: #FFCACA;
text-decoration: none;
}
/*** Test Counts */
#qunit-tests b.counts { color: #000; }
#qunit-tests b.counts { color: #0D3349; }
#qunit-tests b.passed { color: #5E740B; }
#qunit-tests b.failed { color: #710909; }
@ -175,15 +417,22 @@
/*** Passing Styles */
#qunit-tests .pass {
color: #2F68DA;
background-color: #E2F0F7;
}
#qunit-tests .pass .test-name {
color: #366097;
}
#qunit-tests li li.pass {
color: #3C510C;
background-color: #FFF;
border-left: 10px solid #C6E746;
}
#qunit-tests .pass { color: #528CE0; background-color: #D2E0E6; }
#qunit-tests .pass .test-name { color: #366097; }
#qunit-tests .pass .test-actual,
#qunit-tests .pass .test-expected { color: #999; }
@ -191,6 +440,11 @@
/*** Failing Styles */
#qunit-tests .fail {
color: #000;
background-color: #EE5757;
}
#qunit-tests li li.fail {
color: #710909;
background-color: #FFF;
@ -202,29 +456,63 @@
border-radius: 0 0 5px 5px;
}
#qunit-tests .fail { color: #000; background-color: #EE5757; }
#qunit-tests .fail .test-name,
#qunit-tests .fail .module-name { color: #000; }
#qunit-tests .fail .test-actual { color: #EE5757; }
#qunit-tests .fail .test-expected { color: #008000; }
#qunit-banner.qunit-fail { background-color: #EE5757; }
/*** Aborted tests */
#qunit-tests .aborted { color: #000; background-color: orange; }
/*** Skipped tests */
#qunit-tests .skipped {
background-color: #EBECE9;
}
#qunit-tests .qunit-todo-label,
#qunit-tests .qunit-skipped-label {
background-color: #F4FF77;
display: inline-block;
font-style: normal;
color: #366097;
line-height: 1.8em;
padding: 0 0.5em;
margin: -0.4em 0.4em -0.4em 0;
}
#qunit-tests .qunit-todo-label {
background-color: #EEE;
}
/** Result */
#qunit-testresult {
padding: 0.5em 0.5em 0.5em 2.5em;
color: #2B81AF;
background-color: #D2E0E6;
color: #366097;
background-color: #E2F0F7;
border-bottom: 1px solid #FFF;
}
#qunit-testresult a {
color: #2F68DA;
}
#qunit-testresult .clearfix {
height: 0;
clear: both;
}
#qunit-testresult .module-name {
font-weight: 700;
}
#qunit-testresult-display {
padding: 0.5em 1em 0.5em 1em;
width: 85%;
float:left;
}
#qunit-testresult-controls {
padding: 0.5em 1em 0.5em 1em;
width: 10%;
float:left;
}
/** Fixture */

File diff suppressed because it is too large Load Diff

View File

@ -35,7 +35,8 @@ class TestRun
end
def completed?
@qunit_testresult.text.include?("Tests completed")
@qunit_testresult.text.include?("tests completed") &&
@qunit_testresult.find_elements(:class, "total").any?
end
def result
@ -50,7 +51,7 @@ class TestRun
end
def duration
match = /Tests completed in (?<milliseconds>\d+) milliseconds/.match @qunit_testresult.text
match = /tests completed in (?<milliseconds>\d+) milliseconds/.match @qunit_testresult.text
match[:milliseconds].to_i / 1000
end