|
| 1 | +module('data-disable-with', { |
| 2 | + setup: function() { |
| 3 | + $('#qunit-fixture').append($('<form />', { |
| 4 | + action: '/echo', |
| 5 | + 'data-remote': 'true', |
| 6 | + method: 'post' |
| 7 | + })) |
| 8 | + .find('form') |
| 9 | + .append($('<input type="text" data-disable-with="processing ..." name="user_name" value="john" />')); |
| 10 | + |
| 11 | + $('#qunit-fixture').append($('<form />', { |
| 12 | + action: '/echo', |
| 13 | + method: 'post' |
| 14 | + })) |
| 15 | + .find('form:last') |
| 16 | + // WEEIRDD: the form won't submit to an iframe if the button is name="submit" (??!) |
| 17 | + .append($('<input type="submit" data-disable-with="submitting ..." name="submit2" value="Submit" />')); |
| 18 | + |
| 19 | + $('#qunit-fixture').append($('<a />', { |
| 20 | + text: 'Click me', |
| 21 | + href: '/echo', |
| 22 | + 'data-disable-with': 'clicking...' |
| 23 | + })); |
| 24 | + }, |
| 25 | + teardown: function() { |
| 26 | + $(document).unbind('iframe:loaded'); |
| 27 | + } |
| 28 | +}); |
| 29 | + |
| 30 | + |
| 31 | +asyncTest('form input field with "data-disable-with" attribute', 7, function() { |
| 32 | + var form = $('form[data-remote]'), input = form.find('input[type=text]'); |
| 33 | + |
| 34 | + App.checkEnabledState(input, 'john'); |
| 35 | + |
| 36 | + form.bind('ajax:success', function(e, data) { |
| 37 | + setTimeout(function() { |
| 38 | + App.checkEnabledState(input, 'john'); |
| 39 | + equal(data.params.user_name, 'john'); |
| 40 | + start(); |
| 41 | + }, 13) |
| 42 | + }) |
| 43 | + form.trigger('submit'); |
| 44 | + |
| 45 | + App.checkDisabledState(input, 'processing ...'); |
| 46 | +}); |
| 47 | + |
| 48 | +asyncTest('form button with "data-disable-with" attribute', 6, function() { |
| 49 | + var form = $('form[data-remote]'), button = $('<button data-disable-with="submitting ..." name="submit2">Submit</button>'); |
| 50 | + form.append(button); |
| 51 | + |
| 52 | + App.checkEnabledState(button, 'Submit'); |
| 53 | + |
| 54 | + form.bind('ajax:success', function(e, data) { |
| 55 | + setTimeout(function() { |
| 56 | + App.checkEnabledState(button, 'Submit'); |
| 57 | + start(); |
| 58 | + }, 13) |
| 59 | + }) |
| 60 | + form.trigger('submit'); |
| 61 | + |
| 62 | + App.checkDisabledState(button, 'submitting ...'); |
| 63 | +}); |
| 64 | + |
| 65 | +asyncTest('form input[type=submit][data-disable-with] disables', 6, function(){ |
| 66 | + var form = $('form:not([data-remote])'), input = form.find('input[type=submit]'); |
| 67 | + |
| 68 | + App.checkEnabledState(input, 'Submit'); |
| 69 | + |
| 70 | + // WEEIRDD: attaching this handler makes the test work in IE7 |
| 71 | + $(document).bind('iframe:loading', function(e, form) {}); |
| 72 | + |
| 73 | + $(document).bind('iframe:loaded', function(e, data) { |
| 74 | + setTimeout(function() { |
| 75 | + App.checkDisabledState(input, 'submitting ...'); |
| 76 | + start(); |
| 77 | + }, 30); |
| 78 | + }); |
| 79 | + form.trigger('submit'); |
| 80 | + |
| 81 | + setTimeout(function() { |
| 82 | + App.checkDisabledState(input, 'submitting ...'); |
| 83 | + }, 30); |
| 84 | +}); |
| 85 | + |
| 86 | +asyncTest('form[data-remote] input[type=submit][data-disable-with] is replaced in ajax callback', 2, function(){ |
| 87 | + var form = $('form:not([data-remote])').attr('data-remote', 'true'), origFormContents = form.html(); |
| 88 | + |
| 89 | + form.bind('ajax:success', function(){ |
| 90 | + form.html(origFormContents); |
| 91 | + |
| 92 | + setTimeout(function(){ |
| 93 | + var input = form.find('input[type=submit]'); |
| 94 | + App.checkEnabledState(input, 'Submit'); |
| 95 | + start(); |
| 96 | + }, 30); |
| 97 | + }).trigger('submit'); |
| 98 | +}); |
| 99 | + |
| 100 | +asyncTest('form[data-remote] input[data-disable-with] is replaced with disabled field in ajax callback', 2, function(){ |
| 101 | + var form = $('form:not([data-remote])').attr('data-remote', 'true'), input = form.find('input[type=submit]'), |
| 102 | + newDisabledInput = input.clone().attr('disabled', 'disabled'); |
| 103 | + |
| 104 | + form.bind('ajax:success', function(){ |
| 105 | + input.replaceWith(newDisabledInput); |
| 106 | + |
| 107 | + setTimeout(function(){ |
| 108 | + App.checkEnabledState(newDisabledInput, 'Submit'); |
| 109 | + start(); |
| 110 | + }, 30); |
| 111 | + }).trigger('submit'); |
| 112 | +}); |
| 113 | + |
| 114 | +asyncTest('form[data-remote] textarea[data-disable-with] attribute', 3, function() { |
| 115 | + var form = $('form[data-remote]'), |
| 116 | + textarea = $('<textarea data-disable-with="processing ..." name="user_bio">born, lived, died.</textarea>').appendTo(form); |
| 117 | + |
| 118 | + form.bind('ajax:success', function(e, data) { |
| 119 | + setTimeout(function() { |
| 120 | + equal(data.params.user_bio, 'born, lived, died.'); |
| 121 | + start(); |
| 122 | + }, 13) |
| 123 | + }) |
| 124 | + form.trigger('submit'); |
| 125 | + |
| 126 | + App.checkDisabledState(textarea, 'processing ...'); |
| 127 | +}); |
| 128 | + |
| 129 | +asyncTest('a[data-disable-with] disables', 4, function() { |
| 130 | + var link = $('a[data-disable-with]'); |
| 131 | + |
| 132 | + App.checkEnabledState(link, 'Click me'); |
| 133 | + |
| 134 | + link.trigger('click'); |
| 135 | + App.checkDisabledState(link, 'clicking...'); |
| 136 | + start(); |
| 137 | +}); |
| 138 | + |
| 139 | +asyncTest('a[data-remote][data-disable-with] disables and re-enables', 6, function() { |
| 140 | + var link = $('a[data-disable-with]').attr('data-remote', true); |
| 141 | + |
| 142 | + App.checkEnabledState(link, 'Click me'); |
| 143 | + |
| 144 | + link |
| 145 | + .bind('ajax:beforeSend', function() { |
| 146 | + App.checkDisabledState(link, 'clicking...'); |
| 147 | + }) |
| 148 | + .bind('ajax:complete', function() { |
| 149 | + setTimeout( function() { |
| 150 | + App.checkEnabledState(link, 'Click me'); |
| 151 | + start(); |
| 152 | + }, 15); |
| 153 | + }) |
| 154 | + .trigger('click'); |
| 155 | +}); |
| 156 | + |
| 157 | +asyncTest('a[data-remote][data-disable-with] re-enables when `ajax:before` event is cancelled', 6, function() { |
| 158 | + var link = $('a[data-disable-with]').attr('data-remote', true); |
| 159 | + |
| 160 | + App.checkEnabledState(link, 'Click me'); |
| 161 | + |
| 162 | + link |
| 163 | + .bind('ajax:before', function() { |
| 164 | + App.checkDisabledState(link, 'clicking...'); |
| 165 | + return false; |
| 166 | + }) |
| 167 | + .trigger('click'); |
| 168 | + |
| 169 | + setTimeout(function() { |
| 170 | + App.checkEnabledState(link, 'Click me'); |
| 171 | + start(); |
| 172 | + }, 30); |
| 173 | +}); |
| 174 | + |
| 175 | +asyncTest('a[data-remote][data-disable-with] re-enables when `ajax:beforeSend` event is cancelled', 6, function() { |
| 176 | + var link = $('a[data-disable-with]').attr('data-remote', true); |
| 177 | + |
| 178 | + App.checkEnabledState(link, 'Click me'); |
| 179 | + |
| 180 | + link |
| 181 | + .bind('ajax:beforeSend', function() { |
| 182 | + App.checkDisabledState(link, 'clicking...'); |
| 183 | + return false; |
| 184 | + }) |
| 185 | + .trigger('click'); |
| 186 | + |
| 187 | + setTimeout(function() { |
| 188 | + App.checkEnabledState(link, 'Click me'); |
| 189 | + start(); |
| 190 | + }, 30); |
| 191 | +}); |
| 192 | + |
| 193 | +asyncTest('a[data-remote][data-disable-with] re-enables when `ajax:error` event is triggered', 6, function() { |
| 194 | + var link = $('a[data-disable-with]').attr('data-remote', true).attr('href', '/error'); |
| 195 | + |
| 196 | + App.checkEnabledState(link, 'Click me'); |
| 197 | + |
| 198 | + link |
| 199 | + .bind('ajax:beforeSend', function() { |
| 200 | + App.checkDisabledState(link, 'clicking...'); |
| 201 | + }) |
| 202 | + .trigger('click'); |
| 203 | + |
| 204 | + setTimeout(function() { |
| 205 | + App.checkEnabledState(link, 'Click me'); |
| 206 | + start(); |
| 207 | + }, 30); |
| 208 | +}); |
| 209 | + |
| 210 | +asyncTest('form[data-remote] input|button|textarea[data-disable-with] does not disable when `ajax:beforeSend` event is cancelled', 8, function() { |
| 211 | + var form = $('form[data-remote]'), |
| 212 | + input = form.find('input:text'), |
| 213 | + button = $('<button data-disable-with="submitting ..." name="submit2">Submit</button>').appendTo(form), |
| 214 | + textarea = $('<textarea data-disable-with="processing ..." name="user_bio">born, lived, died.</textarea>').appendTo(form), |
| 215 | + submit = $('<input type="submit" data-disable-with="submitting ..." name="submit2" value="Submit" />').appendTo(form); |
| 216 | + |
| 217 | + form |
| 218 | + .bind('ajax:beforeSend', function() { |
| 219 | + return false; |
| 220 | + }) |
| 221 | + .trigger('submit'); |
| 222 | + |
| 223 | + App.checkEnabledState(input, 'john'); |
| 224 | + App.checkEnabledState(button, 'Submit'); |
| 225 | + App.checkEnabledState(textarea, 'born, lived, died.'); |
| 226 | + App.checkEnabledState(submit, 'Submit'); |
| 227 | + |
| 228 | + start(); |
| 229 | + |
| 230 | +}); |
| 231 | + |
| 232 | +asyncTest('ctrl-clicking on a link does not disables the link', 6, function() { |
| 233 | + var link = $('a[data-disable-with]'), e; |
| 234 | + e = $.Event('click'); |
| 235 | + e.metaKey = true; |
| 236 | + |
| 237 | + App.checkEnabledState(link, 'Click me'); |
| 238 | + |
| 239 | + link.trigger(e); |
| 240 | + App.checkEnabledState(link, 'Click me'); |
| 241 | + |
| 242 | + e = $.Event('click'); |
| 243 | + e.ctrlKey = true; |
| 244 | + |
| 245 | + link.trigger(e); |
| 246 | + App.checkEnabledState(link, 'Click me'); |
| 247 | + start(); |
| 248 | +}); |
0 commit comments