Skip to content

Commit

Permalink
Added tests for 'confirm' and 'confirm:complete' event hooks.
Browse files Browse the repository at this point in the history
Reformatted and updated description for 'allowAction' function.
  • Loading branch information
JangoSteve committed May 18, 2011
1 parent a96c4e9 commit e303452
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 13 deletions.
21 changes: 10 additions & 11 deletions src/rails.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,17 +181,16 @@
});
},

/*
For 'data-confirm' attribute:
- fires `confirm` event
- shows the confirmation dialog
- fires the `confirm:complete` event
Returns `true` if no function stops the chain and user chose yes; `false` otherwise.
Attaching a handler to the element's `confirm` event that returns a `falsy` value cancels the confirmation dialog.
Attaching a handler to the element's `confirm:complete` event that returns a `falsy` value makes this function
return false.
*/
/* For 'data-confirm' attribute:
- Fires `confirm` event
- Shows the confirmation dialog
- Fires the `confirm:complete` event
Returns `true` if no function stops the chain and user chose yes; `false` otherwise.
Attaching a handler to the element's `confirm` event that returns a `falsy` value cancels the confirmation dialog.
Attaching a handler to the element's `confirm:complete` event that returns a `falsy` value makes this function
return false. The `confirm:complete` event is fired whether or not the user answered true or false to the dialog.
*/
allowAction: function(element) {
var message = element.data('confirm'),
answer = false, callback;
Expand Down
56 changes: 54 additions & 2 deletions test/public/test/data-confirm.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,16 @@ module('data-confirm', {
}
});

asyncTest('clicking on a link with data-confirm attribute. Confirm yes.', 4, function() {
asyncTest('clicking on a link with data-confirm attribute. Confirm yes.', 6, function() {
var message;
// auto-confirm:
window.confirm = function(msg) { message = msg; return true };

$('a[data-confirm]')
.bind('confirm:complete', function(e, data) {
App.assert_callback_invoked('confirm:complete');
ok(data == true, 'confirm:complete passes in confirm answer (true)');
})
.bind('ajax:success', function(e, data, status, xhr) {
App.assert_callback_invoked('ajax:success');
App.assert_request_path(data, '/echo');
Expand All @@ -31,12 +35,16 @@ asyncTest('clicking on a link with data-confirm attribute. Confirm yes.', 4, fun
.trigger('click');
});

asyncTest('clicking on a link with data-confirm attribute. Confirm No.', 1, function() {
asyncTest('clicking on a link with data-confirm attribute. Confirm No.', 3, function() {
var message;
// auto-decline:
window.confirm = function(msg) { message = msg; return false };

$('a[data-confirm]')
.bind('confirm:complete', function(e, data) {
App.assert_callback_invoked('confirm:complete');
ok(data == false, 'confirm:complete passes in confirm answer (false)');
})
.bind('ajax:beforeSend', function(e, data, status, xhr) {
App.assert_callback_not_invoked('ajax:beforeSend');
})
Expand All @@ -47,3 +55,47 @@ asyncTest('clicking on a link with data-confirm attribute. Confirm No.', 1, func
start();
}, 50);
});


asyncTest('binding to confirm event and returning false', 1, function() {
// redefine confirm function so we can make sure it's not called
window.confirm = function(msg) {
ok(false, 'confirm dialog should not be called');
};

$('a[data-confirm]')
.bind('confirm', function() {
App.assert_callback_invoked('confirm');
return false;
})
.bind('confirm:complete', function() {
App.assert_callback_not_invoked('confirm:complete')
})
.trigger('click');

setTimeout(function() {
start();
}, 50);
});

asyncTest('binding to confirm:complete event and returning false', 2, function() {
// auto-confirm:
window.confirm = function(msg) {
ok(true, 'confirm dialog should be called');
return true;
};

$('a[data-confirm]')
.bind('confirm:complete', function() {
App.assert_callback_invoked('confirm:complete');
return false;
})
.bind('ajax:beforeSend', function() {
App.assert_callback_not_invoked('ajax:beforeSend');
})
.trigger('click');

setTimeout(function() {
start();
}, 50);
});

0 comments on commit e303452

Please sign in to comment.