dialog.test.coffee 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. describe "bootbox.dialog", ->
  2. beforeEach ->
  3. # need to take care with these helpers; don't want too much
  4. # cleverness in the tests which runs the risk of making them
  5. # harder to read. Could we look at custom expectations instead?
  6. @find = (s) -> @dialog.find s
  7. @exists = (s) -> @find(s).length isnt 0
  8. @class = (s, c) -> @find(s).hasClass(c)
  9. @invoke = (s, m) -> @find(s)[m]()
  10. @text = (s) -> @invoke s, "text"
  11. @html = (s) -> @invoke s, "html"
  12. describe "invalid usage tests", ->
  13. describe "with no arguments", ->
  14. beforeEach ->
  15. @create = -> bootbox.dialog()
  16. it "throws an error", ->
  17. expect(@create).to.throw /supply an object/
  18. describe "with one argument", ->
  19. describe "where the argument is not an object", ->
  20. beforeEach ->
  21. @create = -> bootbox.dialog "test"
  22. it "throws an error", ->
  23. expect(@create).to.throw /supply an object/
  24. describe "where the argument has no message property", ->
  25. beforeEach ->
  26. @create = ->
  27. bootbox.dialog
  28. invalid: "options"
  29. it "throws an error", ->
  30. expect(@create).to.throw /specify a message/
  31. describe "where the argument has a button with an invalid value", ->
  32. beforeEach ->
  33. @create = ->
  34. bootbox.dialog
  35. message: "test"
  36. buttons:
  37. ok: "foo"
  38. it "throws an error", ->
  39. expect(@create).to.throw /button with key ok must be an object/
  40. describe "when creating a minimal dialog", ->
  41. beforeEach ->
  42. @dialog = bootbox.dialog
  43. message: "test"
  44. it "adds the bootbox class to the dialog", ->
  45. expect(@dialog.hasClass("bootbox")).to.be.true
  46. it "adds the bootstrap modal class to the dialog", ->
  47. expect(@dialog.hasClass("modal")).to.be.true
  48. it "adds the fade class to the dialog", ->
  49. expect(@dialog.hasClass("fade")).to.be.true
  50. it "shows the expected message", ->
  51. expect(@text(".bootbox-body")).to.equal "test"
  52. it "does not have a header", ->
  53. expect(@exists(".modal-header")).not.to.be.ok
  54. it "has a close button inside the body", ->
  55. expect(@exists(".modal-body .close")).to.be.ok
  56. it "does not have a footer", ->
  57. expect(@exists(".modal-footer")).not.to.be.ok
  58. it "has a backdrop", ->
  59. # bootstrap < 3.3.x
  60. # expect(@dialog.next(".modal-backdrop").length).to.equal 1
  61. # bootstrap >= 3.3.x
  62. expect(@dialog.children(".modal-backdrop").length).to.equal 1
  63. describe "when creating a dialog with a button", ->
  64. beforeEach ->
  65. @create = (button = {}) =>
  66. @dialog = bootbox.dialog
  67. message: "test"
  68. buttons:
  69. one: button
  70. describe "when the button has no callback", ->
  71. beforeEach ->
  72. @create
  73. label: "My Label"
  74. @hidden = sinon.spy @dialog, "modal"
  75. it "shows a footer", ->
  76. expect(@exists(".modal-footer")).to.be.ok
  77. it "shows one button", ->
  78. expect(@find(".btn").length).to.equal 1
  79. it "shows the correct button text", ->
  80. expect(@text(".btn")).to.equal "My Label"
  81. it "applies the correct button class", ->
  82. expect(@class(".btn", "btn-primary")).to.be.true
  83. describe "when triggering the escape event", ->
  84. beforeEach ->
  85. @dialog.trigger "escape.close.bb"
  86. it "should not hide the modal", ->
  87. expect(@hidden).not.to.have.been.called
  88. describe "when clicking the close button", ->
  89. beforeEach ->
  90. @dialog.find(".close").trigger "click"
  91. it "should hide the modal", ->
  92. expect(@hidden).to.have.been.calledWithExactly "hide"
  93. describe "when the button has a label and callback", ->
  94. beforeEach ->
  95. @callback = sinon.spy()
  96. @create
  97. label: "Another Label"
  98. callback: @callback
  99. @hidden = sinon.spy @dialog, "modal"
  100. it "shows a footer", ->
  101. expect(@exists(".modal-footer")).to.be.ok
  102. it "shows the correct button text", ->
  103. expect(@text(".btn")).to.equal "Another Label"
  104. describe "when dismissing the dialog by clicking OK", ->
  105. beforeEach ->
  106. @dialog.find(".btn-primary").trigger "click"
  107. it "should invoke the callback", ->
  108. expect(@callback).to.have.been.called
  109. it "should pass the dialog as `this`", ->
  110. expect(@callback.thisValues[0]).to.equal @dialog
  111. it "should hide the modal", ->
  112. expect(@hidden).to.have.been.calledWithExactly "hide"
  113. describe "when triggering the escape event", ->
  114. beforeEach ->
  115. @dialog.trigger "escape.close.bb"
  116. it "should not invoke the callback", ->
  117. expect(@callback).not.to.have.been.called
  118. it "should not hide the modal", ->
  119. expect(@hidden).not.to.have.been.called
  120. describe "when clicking the close button", ->
  121. beforeEach ->
  122. @dialog.find(".close").trigger "click"
  123. it "should not invoke the callback", ->
  124. expect(@callback).not.to.have.been.called
  125. it "should hide the modal", ->
  126. expect(@hidden).to.have.been.called
  127. describe "when the button has a custom class", ->
  128. beforeEach ->
  129. @create
  130. label: "Test Label"
  131. className: "btn-custom"
  132. it "shows the correct button text", ->
  133. expect(@text(".btn")).to.equal "Test Label"
  134. it "adds the custom class to the button", ->
  135. expect(@class(".btn", "btn-custom")).to.be.true
  136. describe "when the button has no explicit label", ->
  137. beforeEach ->
  138. @create = (buttons) ->
  139. @dialog = bootbox.dialog
  140. message: "test"
  141. buttons: buttons
  142. describe "when its value is an object", ->
  143. beforeEach ->
  144. @create
  145. "Short form":
  146. className: "btn-custom"
  147. callback: -> true
  148. it "uses the key name as the button text", ->
  149. expect(@text(".btn")).to.equal "Short form"
  150. it "adds the custom class to the button", ->
  151. expect(@class(".btn", "btn-custom")).to.be.true
  152. describe "when its value is a function", ->
  153. beforeEach ->
  154. @callback = sinon.spy()
  155. @create
  156. my_label: @callback
  157. it "uses the key name as the button text", ->
  158. expect(@text(".btn")).to.equal "my_label"
  159. describe "when dismissing the dialog by clicking the button", ->
  160. beforeEach ->
  161. @dialog.find(".btn-primary").trigger "click"
  162. it "should invoke the callback", ->
  163. expect(@callback).to.have.been.called
  164. it "should pass the dialog as `this`", ->
  165. expect(@callback.thisValues[0]).to.equal @dialog
  166. describe "when its value is not an object or function", ->
  167. beforeEach ->
  168. @badCreate = =>
  169. @create
  170. "Short form": "hello world"
  171. it "throws an error", ->
  172. expect(@badCreate).to.throw /button with key Short form must be an object/
  173. describe "when creating a dialog with a title", ->
  174. beforeEach ->
  175. @dialog = bootbox.dialog
  176. title: "My Title"
  177. message: "test"
  178. it "has a header", ->
  179. expect(@exists(".modal-header")).to.be.ok
  180. it "shows the correct title text", ->
  181. expect(@text(".modal-title")).to.equal "My Title"
  182. it "has a close button inside the header", ->
  183. expect(@exists(".modal-header .close")).to.be.ok
  184. describe "when creating a dialog with no backdrop", ->
  185. beforeEach ->
  186. @dialog = bootbox.dialog
  187. message: "No backdrop in sight"
  188. backdrop: false
  189. it "does not have a backdrop", ->
  190. expect(@dialog.next(".modal-backdrop").length).to.equal 0
  191. describe "when creating a dialog with no close button", ->
  192. beforeEach ->
  193. @dialog = bootbox.dialog
  194. message: "No backdrop in sight"
  195. closeButton: false
  196. it "does not have a close button inside the body", ->
  197. expect(@exists(".modal-body .close")).not.to.be.ok
  198. describe "when creating a dialog with an onEscape handler", ->
  199. beforeEach ->
  200. @e = (keyCode) ->
  201. $(@dialog).trigger($.Event "keyup", which: keyCode)
  202. describe "with a simple callback", ->
  203. beforeEach ->
  204. @callback = sinon.spy()
  205. @dialog = bootbox.dialog
  206. message: "Are you sure?"
  207. onEscape: @callback
  208. @hidden = sinon.spy @dialog, "modal"
  209. @trigger = sinon.spy(@dialog, "trigger").withArgs "escape.close.bb"
  210. describe "when triggering the keyup event", ->
  211. describe "when the key is not the escape key", ->
  212. beforeEach -> @e 15
  213. it "does not trigger the escape event", ->
  214. expect(@trigger).not.to.have.been.called
  215. it "should not hide the modal", ->
  216. expect(@hidden).not.to.have.been.called
  217. describe "when the key is the escape key", ->
  218. beforeEach -> @e 27
  219. it "triggers the escape event", ->
  220. expect(@trigger).to.have.been.calledWithExactly "escape.close.bb"
  221. it "should invoke the callback", ->
  222. expect(@callback).to.have.been.called
  223. it "should pass the dialog as `this`", ->
  224. expect(@callback.thisValues[0]).to.equal @dialog
  225. it "should hide the modal", ->
  226. expect(@hidden).to.have.been.calledWithExactly "hide"
  227. describe "with a callback which returns false", ->
  228. beforeEach ->
  229. @callback = sinon.stub().returns false
  230. @dialog = bootbox.dialog
  231. message: "Are you sure?"
  232. onEscape: @callback
  233. @hidden = sinon.spy @dialog, "modal"
  234. describe "when triggering the escape keyup event", ->
  235. beforeEach -> @e 27
  236. it "should invoke the callback", ->
  237. expect(@callback).to.have.been.called
  238. it "should pass the dialog as `this`", ->
  239. expect(@callback.thisValues[0]).to.equal @dialog
  240. it "should not hide the modal", ->
  241. expect(@hidden).not.to.have.been.called
  242. describe "with size option", ->
  243. describe "when the size option is set to large", ->
  244. beforeEach ->
  245. @dialog = bootbox.dialog
  246. message: "test"
  247. size: "large"
  248. it "adds the large class to the innerDialog", ->
  249. expect(@dialog.children(".modal-dialog").hasClass("modal-lg")).to.be.true
  250. describe "when the size option is set to small", ->
  251. beforeEach ->
  252. @dialog = bootbox.dialog
  253. message: "test"
  254. size: "small"
  255. it "adds the large class to the innerDialog", ->
  256. expect(@dialog.children(".modal-dialog").hasClass("modal-sm")).to.be.true