bootbox
Version:
Wrappers for JavaScript alert(), confirm() and other flexible dialogs using the Bootstrap framework
243 lines (175 loc) • 7.83 kB
text/coffeescript
describe "bootbox.confirm", ->
describe "basic usage tests", ->
describe "with one argument", ->
describe "where the argument is not an object", ->
beforeEach ->
= -> bootbox.confirm "Are you sure?"
it "throws an error", ->
expect().to.throw /confirm requires a callback/
describe "where the argument is an object", ->
beforeEach ->
= {}
= => = bootbox.confirm
describe "with a message property", ->
beforeEach ->
.message = "Are you sure?"
it "throws an error requiring a callback", ->
expect().to.throw /confirm requires a callback/
describe "with a callback property", ->
describe "where the callback is not a function", ->
beforeEach ->
.callback = "Are you sure?"
it "throws an error requiring a callback", ->
expect().to.throw /confirm requires a callback/
describe "where the callback is a function", ->
beforeEach ->
.callback = -> true
it "throws an error requiring a message", ->
expect().to.throw /Please specify a message/
describe "with a message and a callback", ->
beforeEach ->
=
callback: -> true
message: "Are you sure?"
it "does not throw an error", ->
expect().not.to.throw Error
it "creates a dialog object", ->
expect().to.be.an "object"
it "adds the correct button labels", ->
expect(.find(".btn:first").text()).to.equal "Cancel"
expect(.find(".btn:last").text()).to.equal "OK"
it "adds the correct button classes", ->
expect(.find(".btn:first").hasClass("btn-default")).to.be.true
expect(.find(".btn:last").hasClass("btn-primary")).to.be.true
describe "with two arguments", ->
describe "where the second argument is not a function", ->
beforeEach ->
= =>
= bootbox.confirm "Are you sure?", "callback here"
it "throws an error requiring a callback", ->
expect().to.throw /confirm requires a callback/
describe "where the second argument is a function", ->
beforeEach ->
= =>
= bootbox.confirm "Are you sure?", -> true
it "does not throw an error", ->
expect().not.to.throw Error
it "creates a dialog object", ->
expect().to.be.an "object"
it "applies the bootbox-confirm class to the dialog", ->
expect(.hasClass("bootbox-confirm")).to.be.true
it "adds the correct button labels", ->
expect(.find(".btn:first").text()).to.equal "Cancel"
expect(.find(".btn:last").text()).to.equal "OK"
it "adds the correct button classes", ->
expect(.find(".btn:first").hasClass("btn-default")).to.be.true
expect(.find(".btn:last").hasClass("btn-primary")).to.be.true
it "shows the dialog", ->
expect(.is(":visible")).to.be.true
describe "configuration options tests", ->
beforeEach ->
=
message: "Are you sure?"
callback: -> true
= =>
= bootbox.confirm
describe "with a custom cancel button", ->
beforeEach ->
.buttons =
cancel:
label: "Custom cancel"
className: "btn-danger"
= .find(".btn:first")
it "adds the correct cancel button", ->
expect(.text()).to.equal "Custom cancel"
expect(.hasClass("btn-danger")).to.be.true
describe "with a custom confirm button", ->
beforeEach ->
.buttons =
confirm:
label: "Custom confirm"
className: "btn-warning"
= .find(".btn:last")
it "adds the correct confirm button", ->
expect(.text()).to.equal "Custom confirm"
expect(.hasClass("btn-warning")).to.be.true
describe "with an unrecognised button key", ->
beforeEach ->
.buttons =
"Bad key":
label: "Custom confirm"
className: "btn-warning"
it "throws an error", ->
expect().to.throw /key is not allowed/
describe "callback tests", ->
describe "with a simple callback", ->
beforeEach ->
= sinon.spy()
= bootbox.confirm
message: "Are you sure?"
callback:
= sinon.spy , "modal"
describe "when dismissing the dialog by clicking OK", ->
beforeEach ->
.find(".btn-primary").trigger "click"
it "should invoke the callback", ->
expect().to.have.been.called
it "with the correct value", ->
expect().to.have.been.calledWithExactly true
it "should hide the modal", ->
expect().to.have.been.calledWithExactly "hide"
describe "when dismissing the dialog by clicking Cancel", ->
beforeEach ->
.find(".btn-default").trigger "click"
it "should invoke the callback", ->
expect().to.have.been.called
it "with the correct value", ->
expect().to.have.been.calledWithExactly false
it "should hide the modal", ->
expect().to.have.been.calledWithExactly "hide"
describe "when triggering the escape event", ->
beforeEach ->
.trigger "escape.close.bb"
it "should invoke the callback", ->
expect().to.have.been.called
it "with the correct value", ->
expect().to.have.been.calledWithExactly false
it "should hide the modal", ->
expect().to.have.been.calledWithExactly "hide"
describe "with a callback which returns false", ->
beforeEach ->
= sinon.stub()
.returns false
= bootbox.confirm
message: "Are you sure?"
callback:
= sinon.spy , "modal"
describe "when dismissing the dialog by clicking OK", ->
beforeEach ->
.find(".btn-primary").trigger "click"
it "should invoke the callback", ->
expect().to.have.been.called
it "with the correct value", ->
expect().to.have.been.calledWithExactly true
it "should not hide the modal", ->
expect().not.to.have.been.called
describe "when dismissing the dialog by clicking Cancel", ->
beforeEach ->
.find(".btn-default").trigger "click"
it "should invoke the callback", ->
expect().to.have.been.called
it "with the correct value", ->
expect().to.have.been.calledWithExactly false
it "should not hide the modal", ->
expect().not.to.have.been.called
describe "when triggering the escape event", ->
beforeEach ->
.trigger "escape.close.bb"
it "should invoke the callback", ->
expect().to.have.been.called
it "with the correct value", ->
expect().to.have.been.calledWithExactly false
it "should not hide the modal", ->
expect().not.to.have.been.called