Skip to content

Instantly share code, notes, and snippets.

@andrewk
Created July 23, 2014 06:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrewk/db8a7a6cb24bb0e13a4a to your computer and use it in GitHub Desktop.
Save andrewk/db8a7a6cb24bb0e13a4a to your computer and use it in GitHub Desktop.
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