(function() { function buildSelect(attrs) { attrs = $.extend({ 'name': 'user_data', 'data-remote': 'true', 'data-url': '/echo', 'data-params': 'data1=value1' }, attrs) $('#qunit-fixture').append( $('')) } }) asyncTest('ctrl-clicking on a link does not fire ajaxyness', 0, function() { var link = $('a[data-remote]') // Ideally, we'd setup 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 // follow links using `trigger('click')`, it only fires bindings. link .removeAttr('data-params') .bindNative('ajax:beforeSend', function() { ok(false, 'ajax should not be triggered') }) link.triggerNative('click', { metaKey: true }) link.triggerNative('click', { ctrlKey: true }) setTimeout(function() { start() }, 13) }) asyncTest('right/mouse-wheel-clicking on a link does not fire ajaxyness', 0, function() { var link = $('a[data-remote]') // Ideally, we'd setup 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 // follow links using `trigger('click')`, it only fires bindings. link .removeAttr('data-params') .bindNative('ajax:beforeSend', function() { ok(false, 'ajax should not be triggered') }) link.triggerNative('click', { button: 1 }) link.triggerNative('click', { button: 2 }) setTimeout(function() { start() }, 13) }) asyncTest('clicking on a link via a non-mouse Event (such as from js) works', 1, function() { var link = $('a[data-remote]') link .removeAttr('data-params') .bindNative('ajax:beforeSend', function() { ok(true, 'ajax should be triggered') }) Rails.fire(link[0], 'click') setTimeout(function() { start() }, 13) }) asyncTest('ctrl-clicking on a link still fires ajax for non-GET links and for links with "data-params"', 2, function() { var link = $('a[data-remote]') link .removeAttr('data-params') .attr('data-method', 'POST') .bindNative('ajax:beforeSend', function() { ok(true, 'ajax should be triggered') }) .triggerNative('click', { metaKey: true }) link .removeAttr('data-method') .attr('data-params', 'name=steve') .triggerNative('click', { metaKey: true }) setTimeout(function() { start() }, 13) }) asyncTest('clicking on a link with data-remote attribute', 5, function() { $('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) }) .bindNative('ajax:complete', function() { start() }) .triggerNative('click') }) asyncTest('clicking on a link with both query string in href and data-params', 4, function() { $('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') }) .bindNative('ajax:complete', function() { start() }) .triggerNative('click') }) asyncTest('clicking on a link with both query string in href and data-params with POST method', 4, function() { $('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') }) .bindNative('ajax:complete', function() { start() }) .triggerNative('click') }) asyncTest('clicking on a link with disabled attribute', 0, function() { $('a[disabled]') .bindNative('ajax:before', function(e, data, status, xhr) { App.assertCallbackNotInvoked('ajax:success') }) .bindNative('ajax:complete', function() { start() }) .triggerNative('click') setTimeout(function() { start() }, 13) }) asyncTest('clicking on a button with data-remote attribute', 5, function() { $('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) }) .bindNative('ajax:complete', function() { start() }) .triggerNative('click') }) asyncTest('right/mouse-wheel-clicking on a button with data-remote attribute does not fire ajaxyness', 0, function() { var button = $('button[data-remote]') // Ideally, we'd setup 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 // follow links using `trigger('click')`, it only fires bindings. button .removeAttr('data-params') .bindNative('ajax:beforeSend', function() { ok(false, 'ajax should not be triggered') }) button.triggerNative('click', { button: 1 }) button.triggerNative('click', { button: 2 }) setTimeout(function() { start() }, 13) }) asyncTest('changing a select option with data-remote attribute', 5, function() { 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) }) .bindNative('ajax:complete', function() { start() }) .val('optionValue2') .triggerNative('change') }) asyncTest('submitting form with data-remote attribute', 4, function() { $('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) }) .bindNative('ajax:complete', function() { start() }) .triggerNative('submit') }) asyncTest('submitting form with data-remote attribute should include inputs in a fieldset only once', 3, function() { $('form[data-remote]') .append('
') .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) }) .bindNative('ajax:complete', function() { $('form[data-remote], fieldset').remove() start() }) .triggerNative('submit') }) asyncTest('submitting form with data-remote attribute submits input with matching [form] attribute', 6, function() { $('#qunit-fixture') .append($('')) .append($('')) $('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) }) .bindNative('ajax:complete', function() { start() }) .triggerNative('submit') }) asyncTest('submitting form with data-remote attribute by clicking button with matching [form] attribute', 5, function() { $('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) }) .bindNative('ajax:complete', function() { start() }) $('