confirm.test.coffee 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. describe "bootbox.confirm", ->
  2. describe "basic usage tests", ->
  3. describe "with one argument", ->
  4. describe "where the argument is not an object", ->
  5. beforeEach ->
  6. @create = -> bootbox.confirm "Are you sure?"
  7. it "throws an error", ->
  8. expect(@create).to.throw /confirm requires a callback/
  9. describe "where the argument is an object", ->
  10. beforeEach ->
  11. @options = {}
  12. @create = => @dialog = bootbox.confirm @options
  13. describe "with a message property", ->
  14. beforeEach ->
  15. @options.message = "Are you sure?"
  16. it "throws an error requiring a callback", ->
  17. expect(@create).to.throw /confirm requires a callback/
  18. describe "with a callback property", ->
  19. describe "where the callback is not a function", ->
  20. beforeEach ->
  21. @options.callback = "Are you sure?"
  22. it "throws an error requiring a callback", ->
  23. expect(@create).to.throw /confirm requires a callback/
  24. describe "where the callback is a function", ->
  25. beforeEach ->
  26. @options.callback = -> true
  27. it "throws an error requiring a message", ->
  28. expect(@create).to.throw /Please specify a message/
  29. describe "with a message and a callback", ->
  30. beforeEach ->
  31. @options =
  32. callback: -> true
  33. message: "Are you sure?"
  34. it "does not throw an error", ->
  35. expect(@create).not.to.throw Error
  36. it "creates a dialog object", ->
  37. expect(@dialog).to.be.an "object"
  38. it "adds the correct button labels", ->
  39. expect(@dialog.find(".btn:first").text()).to.equal "Cancel"
  40. expect(@dialog.find(".btn:last").text()).to.equal "OK"
  41. it "adds the correct button classes", ->
  42. expect(@dialog.find(".btn:first").hasClass("btn-default")).to.be.true
  43. expect(@dialog.find(".btn:last").hasClass("btn-primary")).to.be.true
  44. describe "with two arguments", ->
  45. describe "where the second argument is not a function", ->
  46. beforeEach ->
  47. @create = =>
  48. @dialog = bootbox.confirm "Are you sure?", "callback here"
  49. it "throws an error requiring a callback", ->
  50. expect(@create).to.throw /confirm requires a callback/
  51. describe "where the second argument is a function", ->
  52. beforeEach ->
  53. @create = =>
  54. @dialog = bootbox.confirm "Are you sure?", -> true
  55. it "does not throw an error", ->
  56. expect(@create).not.to.throw Error
  57. it "creates a dialog object", ->
  58. expect(@dialog).to.be.an "object"
  59. it "applies the bootbox-confirm class to the dialog", ->
  60. expect(@dialog.hasClass("bootbox-confirm")).to.be.true
  61. it "adds the correct button labels", ->
  62. expect(@dialog.find(".btn:first").text()).to.equal "Cancel"
  63. expect(@dialog.find(".btn:last").text()).to.equal "OK"
  64. it "adds the correct button classes", ->
  65. expect(@dialog.find(".btn:first").hasClass("btn-default")).to.be.true
  66. expect(@dialog.find(".btn:last").hasClass("btn-primary")).to.be.true
  67. it "shows the dialog", ->
  68. expect(@dialog.is(":visible")).to.be.true
  69. describe "configuration options tests", ->
  70. beforeEach ->
  71. @options =
  72. message: "Are you sure?"
  73. callback: -> true
  74. @create = =>
  75. @dialog = bootbox.confirm @options
  76. describe "with a custom cancel button", ->
  77. beforeEach ->
  78. @options.buttons =
  79. cancel:
  80. label: "Custom cancel"
  81. className: "btn-danger"
  82. @create()
  83. @button = @dialog.find(".btn:first")
  84. it "adds the correct cancel button", ->
  85. expect(@button.text()).to.equal "Custom cancel"
  86. expect(@button.hasClass("btn-danger")).to.be.true
  87. describe "with a custom confirm button", ->
  88. beforeEach ->
  89. @options.buttons =
  90. confirm:
  91. label: "Custom confirm"
  92. className: "btn-warning"
  93. @create()
  94. @button = @dialog.find(".btn:last")
  95. it "adds the correct confirm button", ->
  96. expect(@button.text()).to.equal "Custom confirm"
  97. expect(@button.hasClass("btn-warning")).to.be.true
  98. describe "with an unrecognised button key", ->
  99. beforeEach ->
  100. @options.buttons =
  101. "Bad key":
  102. label: "Custom confirm"
  103. className: "btn-warning"
  104. it "throws an error", ->
  105. expect(@create).to.throw /key is not allowed/
  106. describe "callback tests", ->
  107. describe "with a simple callback", ->
  108. beforeEach ->
  109. @callback = sinon.spy()
  110. @dialog = bootbox.confirm
  111. message: "Are you sure?"
  112. callback: @callback
  113. @hidden = sinon.spy @dialog, "modal"
  114. describe "when dismissing the dialog by clicking OK", ->
  115. beforeEach ->
  116. @dialog.find(".btn-primary").trigger "click"
  117. it "should invoke the callback", ->
  118. expect(@callback).to.have.been.called
  119. it "should pass the dialog as `this`", ->
  120. expect(@callback.thisValues[0]).to.equal @dialog
  121. it "with the correct value", ->
  122. expect(@callback).to.have.been.calledWithExactly true
  123. it "should hide the modal", ->
  124. expect(@hidden).to.have.been.calledWithExactly "hide"
  125. describe "when dismissing the dialog by clicking Cancel", ->
  126. beforeEach ->
  127. @dialog.find(".btn-default").trigger "click"
  128. it "should invoke the callback", ->
  129. expect(@callback).to.have.been.called
  130. it "should pass the dialog as `this`", ->
  131. expect(@callback.thisValues[0]).to.equal @dialog
  132. it "with the correct value", ->
  133. expect(@callback).to.have.been.calledWithExactly false
  134. it "should hide the modal", ->
  135. expect(@hidden).to.have.been.calledWithExactly "hide"
  136. describe "when triggering the escape event", ->
  137. beforeEach ->
  138. @dialog.trigger "escape.close.bb"
  139. it "should invoke the callback", ->
  140. expect(@callback).to.have.been.called
  141. it "should pass the dialog as `this`", ->
  142. expect(@callback.thisValues[0]).to.equal @dialog
  143. it "with the correct value", ->
  144. expect(@callback).to.have.been.calledWithExactly false
  145. it "should hide the modal", ->
  146. expect(@hidden).to.have.been.calledWithExactly "hide"
  147. describe "with a callback which returns false", ->
  148. beforeEach ->
  149. @callback = sinon.stub()
  150. @callback.returns false
  151. @dialog = bootbox.confirm
  152. message: "Are you sure?"
  153. callback: @callback
  154. @hidden = sinon.spy @dialog, "modal"
  155. describe "when dismissing the dialog by clicking OK", ->
  156. beforeEach ->
  157. @dialog.find(".btn-primary").trigger "click"
  158. it "should invoke the callback", ->
  159. expect(@callback).to.have.been.called
  160. it "should pass the dialog as `this`", ->
  161. expect(@callback.thisValues[0]).to.equal @dialog
  162. it "with the correct value", ->
  163. expect(@callback).to.have.been.calledWithExactly true
  164. it "should not hide the modal", ->
  165. expect(@hidden).not.to.have.been.called
  166. describe "when dismissing the dialog by clicking Cancel", ->
  167. beforeEach ->
  168. @dialog.find(".btn-default").trigger "click"
  169. it "should invoke the callback", ->
  170. expect(@callback).to.have.been.called
  171. it "should pass the dialog as `this`", ->
  172. expect(@callback.thisValues[0]).to.equal @dialog
  173. it "with the correct value", ->
  174. expect(@callback).to.have.been.calledWithExactly false
  175. it "should not hide the modal", ->
  176. expect(@hidden).not.to.have.been.called
  177. describe "when triggering the escape event", ->
  178. beforeEach ->
  179. @dialog.trigger "escape.close.bb"
  180. it "should invoke the callback", ->
  181. expect(@callback).to.have.been.called
  182. it "should pass the dialog as `this`", ->
  183. expect(@callback.thisValues[0]).to.equal @dialog
  184. it "with the correct value", ->
  185. expect(@callback).to.have.been.calledWithExactly false
  186. it "should not hide the modal", ->
  187. expect(@hidden).not.to.have.been.called