Permalink
Browse files

small test overhaul: "/echo" resource, remote forms

Changes:
 - replace "/show", "/update" actions with "/echo"
 - changed some expectations about "remote" form method lookup
 - stop using unnecessary `App.url()`
  • Loading branch information...
1 parent 51272c6 commit 498b35e24cdb14f2d94486e8a1f4a1f661091426 @mislav mislav committed Jan 7, 2011
@@ -1,75 +1,56 @@
-var App = App || {};
-
-App.build_form = function(opt) {
- var defaults = {
- 'data-remote': 'true'
- };
-
- var options = $.extend(defaults, opt);
-
- $('#fixtures').append($('<form />', options));
-
- $('form').append($('<input />', {
- id: 'user_name',
- type: 'text',
- size: '30',
- 'name': 'user_name',
- 'value': 'john'
- }));
-};
+(function(){
module('call-remote-callbacks', {
-
- teardown: App.teardown,
-
- setup: function() {
- App.build_form({
- 'action': App.url('show')
- });
- }
+ setup: function() {
+ $('#fixtures').append($('<form />', {
+ action: '/echo', method: 'get', 'data-remote': 'true'
+ }));
+ },
+ teardown: App.teardown
});
-test('ajax:beforeSend returns false do not proceed', function() {
- expect(0);
- stop();
-
- $('form')
- .bind('ajax:beforeSend', function() { return false; })
+function submit(fn) {
+ stop(App.ajax_timeout);
+
+ var form = $('form')
.bind('ajax:complete', function(){
- ok(false, 'ajax call should not have been made since ajax:beforeSend callback returns false');
+ ok(true, 'ajax:complete');
+ start();
});
+
+ if (fn) fn(form);
+ form.trigger('submit');
+}
- $('form[data-remote]').trigger('submit');
-
- App.short_timeout();
+test('stopping the "ajax:beforeSend" event aborts the request', function() {
+ expect(0);
+ submit(function(form) {
+ form.bind('ajax:beforeSend', function() { return false });
+ form.unbind('ajax:complete').bind('ajax:complete', function() {
+ ok(false, 'ajax:complete should not run');
+ });
+ });
+ setTimeout(function(){ start() }, 200);
});
-test('beforeSend, success and complete callbacks should be called', function() {
- expect(3);
- stop(App.ajax_timeout);
-
- $('form')
- .bind('ajax:beforeSend', function(arg) { ok(true, 'ajax:beforeSend'); })
- .bind('ajax:success', function(arg) { ok(true, 'ajax:success'); })
- .bind('ajax:complete', function(arg) { ok(true, 'ajax:complete'); start(); });
-
- $('form[data-remote]').trigger('submit');
+test('"ajax:beforeSend", "ajax:success" and "ajax:complete" are triggered', function() {
+ expect(3);
+ submit(function(form) {
+ form.bind('ajax:beforeSend', function(arg) { ok(true, 'ajax:beforeSend') });
+ form.bind('ajax:success', function(arg) { ok(true, 'ajax:success') });
+ });
});
-test('beforeSend, error and complete callbacks should be called in case of error', function() {
- expect(4);
- $('form').attr('action', App.url('error'));
- stop(App.ajax_timeout);
-
- $('form')
- .bind('ajax:beforeSend', function(arg) { ok(true, 'ajax:beforeSend'); })
- .bind('ajax:error', function(e, xhr, status, error) {
+test('"ajax:beforeSend", "ajax:error" and "ajax:complete" are triggered on error', function() {
+ expect(4);
+ submit(function(form) {
+ form.attr('action', '/error');
+ form.bind('ajax:beforeSend', function(arg) { ok(true, 'ajax:beforeSend') });
+ form.bind('ajax:error', function(e, xhr, status, error) {
ok(true, 'ajax:error');
equals(xhr.status, 403, 'status code should be 403');
- })
- .bind('ajax:complete', function(arg) { ok(true, 'ajax:complete'); start(); });
-
- $('form[data-remote]').trigger('submit');
+ });
+ });
});
-
+})();
@@ -1,181 +1,110 @@
-var App = App || {};
+(function(){
-App.build_form = function(opt) {
+function build_form(attrs) {
+ attrs = $.extend({ action: '/echo', 'data-remote': 'true' }, attrs);
- var defaults = {
- 'data-remote': 'true'
- };
+ $('#fixtures').append($('<form />', attrs));
- var options = $.extend(defaults, opt);
-
- $('#fixtures').append($('<form />', options));
-
- $('#fixtures form').append($('<input />', {
- id: 'user_name',
- type: 'text',
- size: '30',
- 'name': 'user_name',
- 'value': 'john'
- }));
+ $('#fixtures form').append($('<input />', {
+ id: 'user_name',
+ type: 'text',
+ size: '30',
+ 'name': 'user_name',
+ 'value': 'john'
+ }));
};
module('call-remote', {
teardown: App.teardown
});
-test('method should be picked up from method attribute and not from data-method', function() {
+test('form method is read from "method" and not from "data-method"', function() {
expect(2);
- App.build_form({
- 'method': 'post',
- 'data-method': 'get',
- 'action': App.url('update')
- });
+ build_form({ method: 'post', 'data-method': 'get' });
stop(App.ajax_timeout);
$('form[data-remote]')
.live('ajax:success', function(e, data, status, xhr) {
App.assert_callback_invoked('ajax:success');
- var request_env = data.request_env;
- App.assert_post_request(request_env);
-
- start();
- })
- .trigger('submit');
-});
-
-test('method should be picked up from data-method attribute if method is missing', function() {
- expect(2);
-
- App.build_form({
- 'data-method': 'post',
- 'action': App.url('update')
- });
-
- stop(App.ajax_timeout);
-
- $('form[data-remote]')
- .live('ajax:success', function(e, data, status, xhr) {
- App.assert_callback_invoked('ajax:success');
- var request_env = data.request_env;
- App.assert_post_request(request_env);
-
+ App.assert_post_request(data.request_env);
start();
})
.trigger('submit');
});
-test('default method GET should be picked up if no method or data-method is supplied', function() {
+test('form method is not read from "data-method" attribute in case of missing "method"', function() {
expect(2);
-
- App.build_form({
- action: App.url('show')
- });
-
+ build_form({ 'data-method': 'put' });
stop(App.ajax_timeout);
$('form[data-remote]')
.live('ajax:success', function(e, data, status, xhr) {
App.assert_callback_invoked('ajax:success');
- var request_env = data.request_env;
- App.assert_get_request(request_env);
+ App.assert_post_request(data.request_env);
start();
})
.trigger('submit');
});
-test('url should be picked up from action', function() {
+test('form default method is POST', function() {
expect(2);
-
- App.build_form({
- 'action': App.url('show')
- });
-
+ build_form();
stop(App.ajax_timeout);
$('form[data-remote]')
.live('ajax:success', function(e, data, status, xhr) {
App.assert_callback_invoked('ajax:success');
- var request_env = data.request_env;
- App.assert_request_path(request_env, '/show');
-
+ App.assert_post_request(data.request_env);
start();
})
.trigger('submit');
});
-test('url should be picked up from action if both action and href are mentioned ', function() {
+test('form url is picked up from "action"', function() {
expect(2);
-
- App.build_form({
- 'action': App.url('show'),
- 'href': 'http://example.org'
- });
+ build_form();
stop(App.ajax_timeout);
$('form[data-remote]')
.live('ajax:success', function(e, data, status, xhr) {
App.assert_callback_invoked('ajax:success');
- var request_env = data.request_env;
- App.assert_request_path(request_env, '/show');
-
+ App.assert_request_path(data.request_env, '/echo');
start();
})
.trigger('submit');
});
-test('url should be picked up from href if no action is provided', function() {
+test('form url is read from "action" not "href"', function() {
expect(2);
-
- App.build_form({
- 'href': App.url('show')
- });
+ build_form({ href: '/echo2' });
stop(App.ajax_timeout);
-
$('form[data-remote]')
.live('ajax:success', function(e, data, status, xhr) {
App.assert_callback_invoked('ajax:success');
- var request_env = data.request_env;
- App.assert_request_path(request_env, '/show');
-
+ App.assert_request_path(data.request_env, '/echo');
start();
})
.trigger('submit');
});
-test('exception should be thrown if both action and url are missing', function() {
- expect(1);
- var exception_was_raised = false;
-
- App.build_form({});
-
- try {
- $('form[data-remote]').trigger('submit');
- } catch(err) {
- exception_was_raised = true;
- }
-
- ok(exception_was_raised, 'exception should have been raised');
-});
-
-test('data should be availabe in JSON format if datat-type is json', function() {
+test('data should be availabe in JSON format if data-type is json', function() {
expect(2);
- App.build_form({
+ build_form({
'method': 'post',
'data-method': 'get',
- 'data-type': 'json',
- 'action': App.url('update')
+ 'data-type': 'json'
});
stop(App.ajax_timeout);
$('form[data-remote]')
.live('ajax:success', function(e, data, status, xhr) {
App.assert_callback_invoked('ajax:success');
- var request_env = data['request_env'];
- App.assert_post_request(request_env);
-
+ App.assert_post_request(data.request_env);
start();
})
.trigger('submit');
});
+
+})();
Oops, something went wrong.

0 comments on commit 498b35e

Please sign in to comment.