Skip to content

Instantly share code, notes, and snippets.

@andrewk

andrewk/confirm.js Secret

Created Jul 23, 2014
Embed
What would you like to do?
Confirm Component
define(function(require) {
'use strict';
var defineComponent = require('flight/lib/component');
function Confirm() {
this.defaultAttrs({
'event': 'click'
});
this.after('initialize', function() {
this.$node.on(this.attr.event, this.confirm.bind(this));
});
this.confirm = function(e, data) {
if (window.confirm(this.$node.data('confirm'))) {
return true;
} else {
e.preventDefault();
}
};
}
return defineComponent(Confirm);
});
describeComponent('component_ui/confirm', function () {
beforeEach(function () {
setupComponent('<button data-confirm="Are you sure?">Delete</button>');
});
describe('#confirm', function() {
it('presents a confirm dialog with message', function() {
var spy = spyOn(window, 'confirm').andReturn(false);
this.component.$node.click();
expect(spy.mostRecentCall.args[0]).toEqual('Are you sure?');
});
it('represses default event behaviour when confirm is false', function() {
var event = jasmine.createSpyObj('e', ['preventDefault']);
spyOn(window, 'confirm').andReturn(false);
this.component.confirm(event);
expect(event.preventDefault).toHaveBeenCalled();
});
it('allows event behaviour when confirm is true', function() {
var event = jasmine.createSpyObj('e', ['preventDefault']);
spyOn(window, 'confirm').andReturn(true);
this.component.confirm(event);
expect(event.preventDefault).not.toHaveBeenCalled();
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.