-
-
Save andrewk/db8a7a6cb24bb0e13a4a to your computer and use it in GitHub Desktop.
Confirm Component
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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