prompt.test.coffee 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306
  1. describe "bootbox.prompt", ->
  2. beforeEach ->
  3. window.bootbox = bootbox.init()
  4. @find = (selector) -> @dialog.find selector
  5. @text = (selector) -> @find(selector).text()
  6. @exists = (selector) -> @find(selector).length isnt 0
  7. describe "basic usage tests", ->
  8. describe "with one argument", ->
  9. describe "where the argument is not an object", ->
  10. beforeEach ->
  11. @create = -> bootbox.prompt "What is your name?"
  12. it "throws an error", ->
  13. expect(@create).to.throw /prompt requires a callback/
  14. describe "where the argument is an object", ->
  15. beforeEach ->
  16. @options = {}
  17. @create = => @dialog = bootbox.prompt @options
  18. describe "with a title property", ->
  19. beforeEach ->
  20. @options.title = "What is your name?"
  21. it "throws an error requiring a callback", ->
  22. expect(@create).to.throw /prompt requires a callback/
  23. describe "and a callback property", ->
  24. describe "where the callback is not a function", ->
  25. beforeEach ->
  26. @options.callback = "Not a function"
  27. it "throws an error requiring a callback", ->
  28. expect(@create).to.throw /prompt requires a callback/
  29. describe "with a callback function", ->
  30. beforeEach ->
  31. @options.callback = -> true
  32. it "throws an error requiring a title", ->
  33. expect(@create).to.throw /prompt requires a title/
  34. describe "with a title and a callback", ->
  35. beforeEach ->
  36. @options =
  37. callback: -> true
  38. title: "What is your name?"
  39. it "does not throw an error", ->
  40. expect(@create).not.to.throw Error
  41. it "creates a dialog object", ->
  42. expect(@dialog).to.be.an "object"
  43. it "applies the bootbox-prompt class to the dialog", ->
  44. expect(@dialog.hasClass("bootbox-prompt")).to.be.true
  45. it "adds the correct button labels", ->
  46. expect(@dialog.find(".btn:first").text()).to.equal "Cancel"
  47. expect(@dialog.find(".btn:last").text()).to.equal "OK"
  48. it "adds the correct button classes", ->
  49. expect(@dialog.find(".btn:first").hasClass("btn-default")).to.be.true
  50. expect(@dialog.find(".btn:last").hasClass("btn-primary")).to.be.true
  51. describe "with two arguments", ->
  52. describe "where the second argument is not a function", ->
  53. beforeEach ->
  54. @create = =>
  55. @dialog = bootbox.prompt "What is your name?", "callback here"
  56. it "throws an error requiring a callback", ->
  57. expect(@create).to.throw /prompt requires a callback/
  58. describe "where the second argument is a function", ->
  59. beforeEach ->
  60. @create = =>
  61. @dialog = bootbox.prompt "What is your name?", -> true
  62. it "does not throw an error", ->
  63. expect(@create).not.to.throw Error
  64. it "creates a dialog object", ->
  65. expect(@dialog).to.be.an "object"
  66. it "adds the correct button labels", ->
  67. expect(@text(".btn:first")).to.equal "Cancel"
  68. expect(@text(".btn:last")).to.equal "OK"
  69. it "adds the correct button classes", ->
  70. expect(@dialog.find(".btn:first").hasClass("btn-default")).to.be.true
  71. expect(@dialog.find(".btn:last").hasClass("btn-primary")).to.be.true
  72. it "adds the expected dialog title", ->
  73. expect(@text("h4")).to.equal "What is your name?"
  74. it "adds a close button", ->
  75. expect(@dialog.find(".modal-header .close")).to.be.ok
  76. it "creates a form with a text input", ->
  77. expect(@dialog.find("form input[type=text]")).to.be.ok
  78. it "with no default value", ->
  79. expect(@dialog.find("form input[type=text]").val()).to.equal ""
  80. it "shows the dialog", ->
  81. expect(@dialog.is(":visible")).to.be.true
  82. describe "configuration options tests", ->
  83. beforeEach ->
  84. @options =
  85. title: "What is your name?"
  86. callback: -> true
  87. @create = =>
  88. @dialog = bootbox.prompt @options
  89. describe "with a custom cancel button", ->
  90. beforeEach ->
  91. @options.buttons =
  92. cancel:
  93. label: "Custom cancel"
  94. className: "btn-danger"
  95. @create()
  96. @button = @dialog.find(".btn:first")
  97. it "adds the correct cancel button", ->
  98. expect(@button.text()).to.equal "Custom cancel"
  99. expect(@button.hasClass("btn-danger")).to.be.true
  100. describe "with a custom confirm button", ->
  101. beforeEach ->
  102. @options.buttons =
  103. confirm:
  104. label: "Custom confirm"
  105. className: "btn-warning"
  106. @create()
  107. @button = @dialog.find(".btn:last")
  108. it "adds the correct confirm button", ->
  109. expect(@button.text()).to.equal "Custom confirm"
  110. expect(@button.hasClass("btn-warning")).to.be.true
  111. describe "with an unrecognised button key", ->
  112. beforeEach ->
  113. @options.buttons =
  114. prompt:
  115. label: "Custom confirm"
  116. className: "btn-warning"
  117. it "throws an error", ->
  118. expect(@create).to.throw /key prompt is not allowed/
  119. describe "setting show to false", ->
  120. beforeEach ->
  121. @options.show = false
  122. @shown = sinon.spy()
  123. sinon.stub bootbox, "dialog", =>
  124. on: ->
  125. off: ->
  126. modal: @shown
  127. @create()
  128. it "does not show the dialog", ->
  129. expect(@shown).not.to.have.been.called
  130. describe "invalid prompt type", ->
  131. beforeEach ->
  132. @options.inputType = 'foobar'
  133. it "throws an error", ->
  134. expect(@create).to.throw /invalid prompt type/
  135. describe "setting inputType text", ->
  136. beforeEach ->
  137. @options.inputType = "text"
  138. describe "without default value", ->
  139. beforeEach ->
  140. @create()
  141. it "shows text input ", ->
  142. expect(@exists("input[type='text']")).to.be.ok
  143. it "has proper class", ->
  144. expect(@find("input[type='text']").hasClass("bootbox-input")).to.be.ok
  145. expect(@find("input[type='text']").hasClass("bootbox-input-text")).to.be.ok
  146. describe "with default value", ->
  147. beforeEach ->
  148. @options.value = "John Smith"
  149. @create()
  150. it "has correct default value", ->
  151. expect(@find("input[type='text']").val()).to.equal "John Smith"
  152. describe "with placeholder", ->
  153. beforeEach ->
  154. @options.placeholder = "enter your name"
  155. @create()
  156. it "has correct placeholder value", ->
  157. expect(@find("input[type='text']").prop("placeholder")).to.equal "enter your name"
  158. describe "with pattern", ->
  159. beforeEach ->
  160. @options.pattern = "\d{1,2}/\d{1,2}/\d{4}"
  161. @create()
  162. it "has correct pattern value", ->
  163. expect(@find("input[type='text']").prop("pattern")).to.equal "\d{1,2}/\d{1,2}/\d{4}"
  164. describe "with maxlength", ->
  165. beforeEach ->
  166. @options.maxlength = 5
  167. @create()
  168. it "has correct maxlength value", ->
  169. expect(@find("input[type='text']").prop("maxlength")).to.equal 5
  170. describe "setting inputType textarea", ->
  171. beforeEach ->
  172. @options.inputType = "textarea"
  173. describe "without default value", ->
  174. beforeEach ->
  175. @create()
  176. it "shows text input", ->
  177. expect(@exists("textarea")).to.be.ok
  178. it "has proper class", ->
  179. expect(@find("textarea").hasClass("bootbox-input")).to.be.ok
  180. expect(@find("textarea").hasClass("bootbox-input-textarea")).to.be.ok
  181. describe "with default value", ->
  182. beforeEach ->
  183. @options.value = "Once upon a time..."
  184. @create()
  185. it "has correct default value", ->
  186. expect(@find("textarea").val()).to.equal "Once upon a time..."
  187. describe "with placeholder", ->
  188. beforeEach ->
  189. @options.placeholder = "enter your favorite fairy tale"
  190. @create()
  191. it "has correct placeholder value", ->
  192. expect(@find("textarea").prop("placeholder")).to.equal "enter your favorite fairy tale"
  193. describe "setting inputType email", ->
  194. beforeEach ->
  195. @options.inputType = "email"
  196. describe "without default value", ->
  197. beforeEach ->
  198. @create()
  199. it "shows email input", ->
  200. expect(@exists("input[type='email']")).to.be.ok
  201. it "has proper class", ->
  202. expect(@find("input[type='email']").hasClass("bootbox-input")).to.be.ok
  203. expect(@find("input[type='email']").hasClass("bootbox-input-email")).to.be.ok
  204. describe "with default value", ->
  205. beforeEach ->
  206. @options.value = "john@smith.com"
  207. @create()
  208. it "has correct default value", ->
  209. expect(@find("input[type='email']").val()).to.equal "john@smith.com"
  210. describe "with placeholder", ->
  211. beforeEach ->
  212. @options.placeholder = "enter your email"
  213. @create()
  214. it "has correct placeholder value", ->
  215. expect(@find("input[type='email']").prop("placeholder")).to.equal "enter your email"
  216. describe "with pattern", ->
  217. beforeEach ->
  218. @options.pattern = "\d{1,2}/\d{1,2}/\d{4}"
  219. @create()
  220. it "has correct pattern value", ->
  221. expect(@find("input[type='email']").prop("pattern")).to.equal "\d{1,2}/\d{1,2}/\d{4}"
  222. describe "setting inputType password", ->
  223. beforeEach ->
  224. @options.inputType = "password"
  225. describe "without default value", ->
  226. beforeEach ->
  227. @create()
  228. it "shows password input", ->
  229. expect(@exists("input[type='password']")).to.be.ok
  230. it "has proper class", ->
  231. expect(@find("input[type='password']").hasClass("bootbox-input")).to.be.ok
  232. expect(@find("input[type='password']").hasClass("bootbox-input-password")).to.be.ok
  233. describe "with default value", ->
  234. beforeEach ->
  235. @options.value = "qwerty"
  236. @create()
  237. it "has correct default value", ->
  238. expect(@find("input[type='password']").val()).to.equal "qwerty"
  239. describe "with placeholder", ->
  240. beforeEach ->
  241. @options.placeholder = "enter your password"
  242. @create()
  243. it "has correct placeholder value", ->
  244. expect(@find("input[type='password']").prop("placeholder")).to.equal "enter your password"
  245. describe "setting inputType select", ->
  246. describe "without options", ->
  247. beforeEach ->
  248. @options.inputType = 'select'
  249. it "throws an error", ->
  250. expect(@create).to.throw /prompt with select requires options/
  251. describe "with invalid options", ->
  252. beforeEach ->
  253. @options.inputType = 'select'
  254. @options.inputOptions = 'foo'
  255. it "throws an error", ->
  256. expect(@create).to.throw "Please pass an array of input options"
  257. describe "with empty options", ->
  258. beforeEach ->
  259. @options.inputType = 'select'
  260. @options.inputOptions = []
  261. it "throws an error", ->
  262. expect(@create).to.throw /prompt with select requires options/
  263. describe "with options in the wrong format", ->
  264. beforeEach ->
  265. @options.inputType = "select"
  266. @options.inputOptions = [{foo: "bar"}]
  267. it "throws an error", ->
  268. expect(@create).to.throw /given options in wrong format/
  269. describe "with a value but no text", ->
  270. beforeEach ->
  271. @options.inputType = 'select'
  272. @options.inputOptions = [{value: 'bar'}]
  273. it "throws an error", ->
  274. expect(@create).to.throw /given options in wrong format/
  275. describe "with an invalid second options", ->
  276. beforeEach ->
  277. @options.inputType = 'select'
  278. @options.inputOptions = [
  279. {value: "bar", text: "bar"}
  280. {text: "foo"}
  281. ]
  282. it "throws an error", ->
  283. expect(@create).to.throw /given options in wrong format/
  284. describe "with valid options", ->
  285. beforeEach ->
  286. @options.inputType = "select"
  287. @options.inputOptions = [{value: 1, text: 'foo'},{value: 2, text: 'bar'},{value: 3, text: 'foobar'}]
  288. @create()
  289. it "shows select input", ->
  290. expect(@exists("select")).to.be.ok
  291. it "has proper class", ->
  292. expect(@find("select").hasClass("bootbox-input")).to.be.ok
  293. expect(@find("select").hasClass("bootbox-input-select")).to.be.ok
  294. it "with three options", ->
  295. expect(@find("option").length).to.equal 3
  296. describe "with zero as the first option", ->
  297. beforeEach ->
  298. @options.inputType = "select"
  299. @options.inputOptions = [{value: 0, text: "foo"}]
  300. @create()
  301. it "shows the select input", ->
  302. expect(@exists("select")).to.be.ok
  303. describe "with false as the first option", ->
  304. beforeEach ->
  305. @options.inputType = "select"
  306. @options.inputOptions = [{value: false, text: "foo"}]
  307. @create()
  308. it "shows the select input", ->
  309. expect(@exists("select")).to.be.ok
  310. describe "with option groups", ->
  311. beforeEach ->
  312. @options.inputType = 'select'
  313. @options.inputOptions = [
  314. {value: 1, group: 'foo', text: 'foo'}
  315. {value: 2, group: 'bar', text: 'bar'}
  316. {value: 3, group: 'foo', text: 'foobar'}
  317. {value: 4, group: 'bar', text: 'barfoo'}
  318. ]
  319. @create()
  320. it "shows select input", ->
  321. expect(@exists("select")).to.be.ok
  322. it "has proper class", ->
  323. expect(@find("select").hasClass("bootbox-input")).to.be.ok
  324. expect(@find("select").hasClass("bootbox-input-select")).to.be.ok
  325. it "with two option group", ->
  326. expect(@find("optgroup").length).to.equal 2
  327. it "with four options", ->
  328. expect(@find("option").length).to.equal 4
  329. describe "setting inputType checkbox", ->
  330. describe "without options", ->
  331. beforeEach ->
  332. @options.inputType = 'checkbox'
  333. it "throws an error", ->
  334. expect(@create).to.throw /prompt with checkbox requires options/
  335. describe "with options in the wrong format", ->
  336. beforeEach ->
  337. @options.inputType = "checkbox"
  338. @options.inputOptions = [{foo: "bar"}]
  339. it "throws an error", ->
  340. expect(@create).to.throw /given options in wrong format/
  341. describe "with options", ->
  342. beforeEach ->
  343. @options.inputType = 'checkbox'
  344. @options.inputOptions = [
  345. {value: 1, text: 'foo'}
  346. {value: 2, text: 'bar'}
  347. {value: 3, text: 'foobar'}
  348. ]
  349. @create()
  350. it "shows checkbox input", ->
  351. expect(@exists("input[type='checkbox']")).to.be.ok
  352. it "has proper class", ->
  353. expect(@find("input[type='checkbox']").hasClass("bootbox-input")).to.be.ok
  354. expect(@find("input[type='checkbox']").hasClass("bootbox-input-checkbox")).to.be.ok
  355. it "with three checkboxes", ->
  356. expect(@find("input[type='checkbox']").length).to.equal 3
  357. describe "setting inputType date", ->
  358. beforeEach ->
  359. @options.inputType = "date"
  360. describe "without default value", ->
  361. beforeEach ->
  362. @create()
  363. it "shows date input ", ->
  364. expect(@exists("input[type='date']")).to.be.ok
  365. it "has proper class", ->
  366. expect(@find("input[type='date']").hasClass("bootbox-input")).to.be.ok
  367. expect(@find("input[type='date']").hasClass("bootbox-input-date")).to.be.ok
  368. describe "with default value", ->
  369. beforeEach ->
  370. @options.value = "17/08/2005"
  371. @create()
  372. it "has correct default value", ->
  373. expect(@find("input[type='date']").val()).to.equal "17/08/2005"
  374. describe "with placeholder", ->
  375. beforeEach ->
  376. @options.placeholder = "enter the date"
  377. @create()
  378. it "has correct placeholder value", ->
  379. expect(@find("input[type='date']").prop("placeholder")).to.equal "enter the date"
  380. describe "with pattern", ->
  381. beforeEach ->
  382. @options.pattern = "\d{1,2}/\d{1,2}/\d{4}"
  383. @create()
  384. it "has correct pattern value", ->
  385. expect(@find("input[type='date']").prop("pattern")).to.equal "\d{1,2}/\d{1,2}/\d{4}"
  386. describe "setting inputType time", ->
  387. beforeEach ->
  388. @options.inputType = "time"
  389. describe "without default value", ->
  390. beforeEach ->
  391. @create()
  392. it "shows time input", ->
  393. expect(@exists("input[type='time']")).to.be.ok
  394. it "has proper class", ->
  395. expect(@find("input[type='time']").hasClass("bootbox-input")).to.be.ok
  396. expect(@find("input[type='time']").hasClass("bootbox-input-time")).to.be.ok
  397. describe "with default value", ->
  398. beforeEach ->
  399. @options.value = "19:02"
  400. @create()
  401. it "has correct default value", ->
  402. expect(@find("input[type='time']").val()).to.equal "19:02"
  403. describe "with placeholder", ->
  404. beforeEach ->
  405. @options.placeholder = "enter the time"
  406. @create()
  407. it "has correct placeholder value", ->
  408. expect(@find("input[type='time']").prop("placeholder")).to.equal "enter the time"
  409. describe "with pattern", ->
  410. beforeEach ->
  411. @options.pattern = "\d{1,2}/\d{1,2}/\d{4}"
  412. @create()
  413. it "has correct pattern value", ->
  414. expect(@find("input[type='time']").prop("pattern")).to.equal "\d{1,2}/\d{1,2}/\d{4}"
  415. describe "setting inputType number", ->
  416. beforeEach ->
  417. @options.inputType = "number"
  418. describe "without default value", ->
  419. beforeEach ->
  420. @create()
  421. it "shows number input ", ->
  422. expect(@exists("input[type='number']")).to.be.ok
  423. it "has proper class", ->
  424. expect(@find("input[type='number']").hasClass("bootbox-input")).to.be.ok
  425. expect(@find("input[type='number']").hasClass("bootbox-input-number")).to.be.ok
  426. describe "with default value", ->
  427. beforeEach ->
  428. @options.value = "300"
  429. @create()
  430. it "has correct default value", ->
  431. expect(@find("input[type='number']").val()).to.equal "300"
  432. describe "with placeholder", ->
  433. beforeEach ->
  434. @options.placeholder = "enter the number"
  435. @create()
  436. it "has correct placeholder value", ->
  437. expect(@find("input[type='number']").prop("placeholder")).to.equal "enter the number"
  438. describe "callback tests", ->
  439. describe "with a simple callback", ->
  440. beforeEach ->
  441. @callback = sinon.spy()
  442. @dialog = bootbox.prompt
  443. title: "What is your name?"
  444. callback: @callback
  445. @hidden = sinon.spy @dialog, "modal"
  446. describe "when entering no value in the text input", ->
  447. describe "when dismissing the dialog by clicking OK", ->
  448. beforeEach ->
  449. @dialog.find(".btn-primary").trigger "click"
  450. it "should invoke the callback", ->
  451. expect(@callback).to.have.been.called
  452. it "should pass the dialog as `this`", ->
  453. expect(@callback.thisValues[0]).to.equal @dialog
  454. it "with the correct value", ->
  455. expect(@callback).to.have.been.calledWithExactly ""
  456. it "should hide the modal", ->
  457. expect(@hidden).to.have.been.calledWithExactly "hide"
  458. describe "when submitting the form", ->
  459. beforeEach ->
  460. @dialog.find(".bootbox-form").trigger "submit"
  461. it "invokes the callback with the correct value", ->
  462. expect(@callback).to.have.been.calledWithExactly ""
  463. it "should pass the dialog as `this`", ->
  464. expect(@callback.thisValues[0]).to.equal @dialog
  465. it "should hide the modal", ->
  466. expect(@hidden).to.have.been.calledWithExactly "hide"
  467. describe "when entering a value in the text input", ->
  468. beforeEach ->
  469. @dialog.find(".bootbox-input").val "Test input"
  470. describe "when dismissing the dialog by clicking OK", ->
  471. beforeEach ->
  472. @dialog.find(".btn-primary").trigger "click"
  473. it "should invoke the callback", ->
  474. expect(@callback).to.have.been.called
  475. it "should pass the dialog as `this`", ->
  476. expect(@callback.thisValues[0]).to.equal @dialog
  477. it "with the correct value", ->
  478. expect(@callback).to.have.been.calledWithExactly "Test input"
  479. it "should hide the modal", ->
  480. expect(@hidden).to.have.been.calledWithExactly "hide"
  481. describe "when submitting the form", ->
  482. beforeEach ->
  483. @dialog.find(".bootbox-form").trigger "submit"
  484. it "invokes the callback with the correct value", ->
  485. expect(@callback).to.have.been.calledWithExactly "Test input"
  486. it "should pass the dialog as `this`", ->
  487. expect(@callback.thisValues[0]).to.equal @dialog
  488. it "should hide the modal", ->
  489. expect(@hidden).to.have.been.calledWithExactly "hide"
  490. describe "when dismissing the dialog by clicking Cancel", ->
  491. beforeEach ->
  492. @dialog.find(".btn-default").trigger "click"
  493. it "should invoke the callback", ->
  494. expect(@callback).to.have.been.called
  495. it "should pass the dialog as `this`", ->
  496. expect(@callback.thisValues[0]).to.equal @dialog
  497. it "with the correct value", ->
  498. expect(@callback).to.have.been.calledWithExactly null
  499. it "should hide the modal", ->
  500. expect(@hidden).to.have.been.calledWithExactly "hide"
  501. describe "when triggering the escape event", ->
  502. beforeEach ->
  503. @dialog.trigger "escape.close.bb"
  504. it "should invoke the callback", ->
  505. expect(@callback).to.have.been.called
  506. it "should pass the dialog as `this`", ->
  507. expect(@callback.thisValues[0]).to.equal @dialog
  508. it "with the correct value", ->
  509. expect(@callback).to.have.been.calledWithExactly null
  510. it "should hide the modal", ->
  511. expect(@hidden).to.have.been.calledWithExactly "hide"
  512. describe "when dismissing the dialog by clicking the close button", ->
  513. beforeEach ->
  514. @dialog.find(".close").trigger "click"
  515. it "should invoke the callback", ->
  516. expect(@callback).to.have.been.called
  517. it "should pass the dialog as `this`", ->
  518. expect(@callback.thisValues[0]).to.equal @dialog
  519. it "with the correct value", ->
  520. expect(@callback).to.have.been.calledWithExactly null
  521. it "should hide the modal", ->
  522. expect(@hidden).to.have.been.calledWithExactly "hide"
  523. describe "with a callback which returns false", ->
  524. beforeEach ->
  525. @callback = sinon.stub()
  526. @callback.returns false
  527. @dialog = bootbox.prompt
  528. title: "What is your name?"
  529. callback: @callback
  530. @hidden = sinon.spy @dialog, "modal"
  531. describe "when entering no value in the text input", ->
  532. describe "when dismissing the dialog by clicking OK", ->
  533. beforeEach ->
  534. @dialog.find(".btn-primary").trigger "click"
  535. it "should invoke the callback", ->
  536. expect(@callback).to.have.been.called
  537. it "should pass the dialog as `this`", ->
  538. expect(@callback.thisValues[0]).to.equal @dialog
  539. it "with the correct value", ->
  540. expect(@callback).to.have.been.calledWithExactly ""
  541. it "should not hide the modal", ->
  542. expect(@hidden).not.to.have.been.called
  543. describe "when entering a value in the text input", ->
  544. beforeEach ->
  545. @dialog.find(".bootbox-input").val "Test input"
  546. describe "when dismissing the dialog by clicking OK", ->
  547. beforeEach ->
  548. @dialog.find(".btn-primary").trigger "click"
  549. it "should invoke the callback", ->
  550. expect(@callback).to.have.been.called
  551. it "should pass the dialog as `this`", ->
  552. expect(@callback.thisValues[0]).to.equal @dialog
  553. it "with the correct value", ->
  554. expect(@callback).to.have.been.calledWithExactly "Test input"
  555. it "should not hide the modal", ->
  556. expect(@hidden).not.to.have.been.called
  557. describe "when dismissing the dialog by clicking Cancel", ->
  558. beforeEach ->
  559. @dialog.find(".btn-default").trigger "click"
  560. it "should invoke the callback", ->
  561. expect(@callback).to.have.been.called
  562. it "should pass the dialog as `this`", ->
  563. expect(@callback.thisValues[0]).to.equal @dialog
  564. it "with the correct value", ->
  565. expect(@callback).to.have.been.calledWithExactly null
  566. it "should not hide the modal", ->
  567. expect(@hidden).not.to.have.been.called
  568. describe "when triggering the escape event", ->
  569. beforeEach ->
  570. @dialog.trigger "escape.close.bb"
  571. it "should invoke the callback", ->
  572. expect(@callback).to.have.been.called
  573. it "should pass the dialog as `this`", ->
  574. expect(@callback.thisValues[0]).to.equal @dialog
  575. it "with the correct value", ->
  576. expect(@callback).to.have.been.calledWithExactly null
  577. it "should not hide the modal", ->
  578. expect(@hidden).not.to.have.been.called
  579. describe "when dismissing the dialog by clicking the close button", ->
  580. beforeEach ->
  581. @dialog.find(".close").trigger "click"
  582. it "should invoke the callback", ->
  583. expect(@callback).to.have.been.called
  584. it "should pass the dialog as `this`", ->
  585. expect(@callback.thisValues[0]).to.equal @dialog
  586. it "with the correct value", ->
  587. expect(@callback).to.have.been.calledWithExactly null
  588. it "should not hide the modal", ->
  589. expect(@hidden).not.to.have.been.called
  590. describe "with a default value", ->
  591. beforeEach ->
  592. @callback = sinon.spy()
  593. @dialog = bootbox.prompt
  594. title: "What is your name?"
  595. value: "Bob"
  596. callback: @callback
  597. @hidden = sinon.spy @dialog, "modal"
  598. it "populates the input with the default value", ->
  599. expect(@dialog.find(".bootbox-input").val()).to.equal "Bob"
  600. describe "when entering no value in the text input", ->
  601. describe "when dismissing the dialog by clicking OK", ->
  602. beforeEach ->
  603. @dialog.find(".btn-primary").trigger "click"
  604. it "should invoke the callback", ->
  605. expect(@callback).to.have.been.called
  606. it "should pass the dialog as `this`", ->
  607. expect(@callback.thisValues[0]).to.equal @dialog
  608. it "with the correct value", ->
  609. expect(@callback).to.have.been.calledWithExactly "Bob"
  610. describe "when dismissing the dialog by clicking Cancel", ->
  611. beforeEach ->
  612. @dialog.find(".btn-default").trigger "click"
  613. it "should invoke the callback", ->
  614. expect(@callback).to.have.been.called
  615. it "should pass the dialog as `this`", ->
  616. expect(@callback.thisValues[0]).to.equal @dialog
  617. it "with the correct value", ->
  618. expect(@callback).to.have.been.calledWithExactly null
  619. describe "when entering a value in the text input", ->
  620. beforeEach ->
  621. @dialog.find(".bootbox-input").val "Alice"
  622. describe "when dismissing the dialog by clicking OK", ->
  623. beforeEach ->
  624. @dialog.find(".btn-primary").trigger "click"
  625. it "should invoke the callback", ->
  626. expect(@callback).to.have.been.called
  627. it "should pass the dialog as `this`", ->
  628. expect(@callback.thisValues[0]).to.equal @dialog
  629. it "with the correct value", ->
  630. expect(@callback).to.have.been.calledWithExactly "Alice"
  631. describe "when dismissing the dialog by clicking Cancel", ->
  632. beforeEach ->
  633. @dialog.find(".btn-default").trigger "click"
  634. it "should invoke the callback", ->
  635. expect(@callback).to.have.been.called
  636. it "should pass the dialog as `this`", ->
  637. expect(@callback.thisValues[0]).to.equal @dialog
  638. it "with the correct value", ->
  639. expect(@callback).to.have.been.calledWithExactly null
  640. describe "with a placeholder", ->
  641. beforeEach ->
  642. @callback = sinon.spy()
  643. @dialog = bootbox.prompt
  644. title: "What is your name?"
  645. placeholder: "e.g. Bob Smith"
  646. callback: -> true
  647. it "populates the input with the placeholder attribute", ->
  648. expect(@dialog.find(".bootbox-input").attr("placeholder")).to.equal "e.g. Bob Smith"
  649. describe "with inputType select", ->
  650. describe "without a default value", ->
  651. beforeEach ->
  652. @callback = sinon.spy()
  653. @dialog = bootbox.prompt
  654. title: "What is your IDE?"
  655. callback: @callback
  656. inputType: "select"
  657. inputOptions: [
  658. {value: '#', text: 'Choose one'},
  659. {value: 1, text: 'Vim'},
  660. {value: 2, text: 'Sublime Text'},
  661. {value: 3, text: 'WebStorm/PhpStorm'},
  662. {value: 4, text: 'Komodo IDE'},
  663. ]
  664. @hidden = sinon.spy @dialog, "modal"
  665. it "has correct number values in list", ->
  666. expect(@find(".bootbox-input-select option").length).to.equal 5
  667. describe "when dismissing the dialog by clicking OK", ->
  668. beforeEach ->
  669. @dialog.find(".btn-primary").trigger "click"
  670. it "should invoke the callback", ->
  671. expect(@callback).to.have.been.called
  672. it "should pass the dialog as `this`", ->
  673. expect(@callback.thisValues[0]).to.equal @dialog
  674. it "with the correct value", ->
  675. expect(@callback).to.have.been.calledWithExactly "#"
  676. describe "when dismissing the dialog by clicking Cancel", ->
  677. beforeEach ->
  678. @dialog.find(".btn-default").trigger "click"
  679. it "should invoke the callback", ->
  680. expect(@callback).to.have.been.called
  681. it "should pass the dialog as `this`", ->
  682. expect(@callback.thisValues[0]).to.equal @dialog
  683. it "with the correct value", ->
  684. expect(@callback).to.have.been.calledWithExactly null
  685. describe "with a default value", ->
  686. beforeEach ->
  687. @callback = sinon.spy()
  688. @dialog = bootbox.prompt
  689. title: "What is your IDE?"
  690. callback: @callback
  691. value: 1
  692. inputType: "select"
  693. inputOptions: [
  694. {value: '#', text: 'Choose one'},
  695. {value: 1, text: 'Vim'},
  696. {value: 2, text: 'Sublime Text'},
  697. {value: 3, text: 'WebStorm/PhpStorm'},
  698. {value: 4, text: 'Komodo IDE'},
  699. ]
  700. @hidden = sinon.spy @dialog, "modal"
  701. it "specified option is selected", ->
  702. expect(@dialog.find(".bootbox-input-select").val()).to.equal "1"
  703. describe "when dismissing the dialog by clicking OK", ->
  704. beforeEach ->
  705. @dialog.find(".btn-primary").trigger "click"
  706. it "should invoke the callback", ->
  707. expect(@callback).to.have.been.called
  708. it "should pass the dialog as `this`", ->
  709. expect(@callback.thisValues[0]).to.equal @dialog
  710. it "with the correct value", ->
  711. expect(@callback).to.have.been.calledWithExactly "1"
  712. describe "when dismissing the dialog by clicking Cancel", ->
  713. beforeEach ->
  714. @dialog.find(".btn-default").trigger "click"
  715. it "should invoke the callback", ->
  716. expect(@callback).to.have.been.called
  717. it "should pass the dialog as `this`", ->
  718. expect(@callback.thisValues[0]).to.equal @dialog
  719. it "with the correct value", ->
  720. expect(@callback).to.have.been.calledWithExactly null
  721. describe "when changing the selected option and dismissing the dialog by clicking OK", ->
  722. beforeEach ->
  723. @dialog.find(".bootbox-input-select").val(3)
  724. @dialog.find(".btn-primary").trigger "click"
  725. it "should invoke the callback", ->
  726. expect(@callback).to.have.been.called
  727. it "with the correct value", ->
  728. expect(@callback).to.have.been.calledWithExactly "3"
  729. describe "with inputType email", ->
  730. describe "without a default value", ->
  731. beforeEach ->
  732. @callback = sinon.spy()
  733. @dialog = bootbox.prompt
  734. title: "What is your email?"
  735. inputType: "email"
  736. callback: @callback
  737. @hidden = sinon.spy @dialog, "modal"
  738. describe "when dismissing the dialog by clicking OK", ->
  739. beforeEach ->
  740. @dialog.find(".btn-primary").trigger "click"
  741. it "should invoke the callback", ->
  742. expect(@callback).to.have.been.called
  743. it "should pass the dialog as `this`", ->
  744. expect(@callback.thisValues[0]).to.equal @dialog
  745. it "with the correct value", ->
  746. expect(@callback).to.have.been.calledWithExactly ""
  747. it "should hide the modal", ->
  748. expect(@hidden).to.have.been.calledWithExactly "hide"
  749. describe "when submitting the form", ->
  750. beforeEach ->
  751. @dialog.find(".bootbox-form").trigger "submit"
  752. it "invokes the callback with the correct value", ->
  753. expect(@callback).to.have.been.calledWithExactly ""
  754. it "should hide the modal", ->
  755. expect(@hidden).to.have.been.calledWithExactly "hide"
  756. describe "when entering a value in the email input", ->
  757. beforeEach ->
  758. @dialog.find(".bootbox-input-email").val "john@smith.com"
  759. describe "when dismissing the dialog by clicking OK", ->
  760. beforeEach ->
  761. @dialog.find(".btn-primary").trigger "click"
  762. it "should invoke the callback", ->
  763. expect(@callback).to.have.been.called
  764. it "should pass the dialog as `this`", ->
  765. expect(@callback.thisValues[0]).to.equal @dialog
  766. it "with the correct value", ->
  767. expect(@callback).to.have.been.calledWithExactly "john@smith.com"
  768. describe "when dismissing the dialog by clicking Cancel", ->
  769. beforeEach ->
  770. @dialog.find(".btn-default").trigger "click"
  771. it "should invoke the callback", ->
  772. expect(@callback).to.have.been.called
  773. it "with the correct value", ->
  774. expect(@callback).to.have.been.calledWithExactly null
  775. describe "with a default value", ->
  776. beforeEach ->
  777. @callback = sinon.spy()
  778. @dialog = bootbox.prompt
  779. title: "What is your email?"
  780. inputType: "email"
  781. value: "john@smith.com"
  782. callback: @callback
  783. @hidden = sinon.spy @dialog, "modal"
  784. describe "when dismissing the dialog by clicking OK", ->
  785. beforeEach ->
  786. @dialog.find(".btn-primary").trigger "click"
  787. it "should invoke the callback", ->
  788. expect(@callback).to.have.been.called
  789. it "with the correct value", ->
  790. expect(@callback).to.have.been.calledWithExactly "john@smith.com"
  791. it "should hide the modal", ->
  792. expect(@hidden).to.have.been.calledWithExactly "hide"
  793. describe "when submitting the form", ->
  794. beforeEach ->
  795. @dialog.find(".bootbox-form").trigger "submit"
  796. it "invokes the callback with the correct value", ->
  797. expect(@callback).to.have.been.calledWithExactly "john@smith.com"
  798. it "should hide the modal", ->
  799. expect(@hidden).to.have.been.calledWithExactly "hide"
  800. describe "when changing a value in the email input", ->
  801. beforeEach ->
  802. @dialog.find(".bootbox-input-email").val "smith@john.com"
  803. describe "when dismissing the dialog by clicking OK", ->
  804. beforeEach ->
  805. @dialog.find(".btn-primary").trigger "click"
  806. it "should invoke the callback", ->
  807. expect(@callback).to.have.been.called
  808. it "with the correct value", ->
  809. expect(@callback).to.have.been.calledWithExactly "smith@john.com"
  810. describe "when dismissing the dialog by clicking Cancel", ->
  811. beforeEach ->
  812. @dialog.find(".btn-default").trigger "click"
  813. it "should invoke the callback", ->
  814. expect(@callback).to.have.been.called
  815. it "with the correct value", ->
  816. expect(@callback).to.have.been.calledWithExactly null
  817. describe "with input type checkbox", ->
  818. describe "without a default value", ->
  819. beforeEach ->
  820. @callback = sinon.spy()
  821. @dialog = bootbox.prompt
  822. title: "What is your IDE?"
  823. inputType: 'checkbox'
  824. inputOptions: [
  825. {value: 1, text: 'Vim'},
  826. {value: 2, text: 'Sublime Text'},
  827. {value: 3, text: 'WebStorm/PhpStorm'},
  828. {value: 4, text: 'Komodo IDE'},
  829. ]
  830. callback: @callback
  831. @hidden = sinon.spy @dialog, "modal"
  832. describe "when dismissing the dialog by clicking OK", ->
  833. beforeEach ->
  834. @dialog.find(".btn-primary").trigger "click"
  835. it "should invoke the callback", ->
  836. expect(@callback).to.have.been.called
  837. it "with an undefined value", ->
  838. expect(@callback).to.have.been.calledWithExactly []
  839. it "should hide the modal", ->
  840. expect(@hidden).to.have.been.calledWithExactly "hide"
  841. describe "when dismissing the dialog by clicking Cancel", ->
  842. beforeEach ->
  843. @dialog.find(".btn-default").trigger "click"
  844. it "should invoke the callback", ->
  845. expect(@callback).to.have.been.called
  846. it "with the correct value", ->
  847. expect(@callback).to.have.been.calledWithExactly null
  848. describe "with default value", ->
  849. describe "one value checked", ->
  850. beforeEach ->
  851. @callback = sinon.spy()
  852. @dialog = bootbox.prompt
  853. title: "What is your IDE?"
  854. callback: @callback
  855. value: 2
  856. inputType: "checkbox"
  857. inputOptions: [
  858. {value: 1, text: 'Vim'},
  859. {value: 2, text: 'Sublime Text'},
  860. {value: 3, text: 'WebStorm/PhpStorm'},
  861. {value: 4, text: 'Komodo IDE'},
  862. ]
  863. @hidden = sinon.spy @dialog, "modal"
  864. it "specified checkbox is checked", ->
  865. expect(@dialog.find("input:checkbox:checked").val()).to.equal "2"
  866. describe "when dismissing the dialog by clicking OK", ->
  867. beforeEach ->
  868. @dialog.find(".btn-primary").trigger "click"
  869. it "should invoke the callback", ->
  870. expect(@callback).to.have.been.called
  871. it "with the correct value", ->
  872. expect(@callback).to.have.been.calledWithExactly ["2"]
  873. describe "when dismissing the dialog by clicking Cancel", ->
  874. beforeEach ->
  875. @dialog.find(".btn-default").trigger "click"
  876. it "should invoke the callback", ->
  877. expect(@callback).to.have.been.called
  878. it "with the correct value", ->
  879. expect(@callback).to.have.been.calledWithExactly null
  880. describe "when changing the checked option and dismissing the dialog by clicking Cancel", ->
  881. beforeEach ->
  882. @dialog.find("input:checkbox:checked").prop('checked', false)
  883. @dialog.find("input:checkbox[value=3]").prop('checked', true)
  884. @dialog.find(".btn-default").trigger "click"
  885. it "should invoke the callback", ->
  886. expect(@callback).to.have.been.called
  887. it "with the correct value", ->
  888. expect(@callback).to.have.been.calledWithExactly null
  889. describe "when changing the selected option and dismissing the dialog by clicking OK", ->
  890. beforeEach ->
  891. @dialog.find("input:checkbox:checked").prop('checked', false)
  892. @dialog.find("input:checkbox[value=3]").prop('checked', true)
  893. @dialog.find(".btn-primary").trigger "click"
  894. it "should invoke the callback", ->
  895. expect(@callback).to.have.been.called
  896. it "with the correct value", ->
  897. expect(@callback).to.have.been.calledWithExactly ["3"]
  898. describe "multiple value checked", ->
  899. beforeEach ->
  900. @callback = sinon.spy()
  901. @dialog = bootbox.prompt
  902. title: "What is your IDE?"
  903. callback: @callback
  904. value: [2, 3]
  905. inputType: "checkbox"
  906. inputOptions: [
  907. {value: 1, text: 'Vim'}
  908. {value: 2, text: 'Sublime Text'}
  909. {value: 3, text: 'WebStorm/PhpStorm'}
  910. {value: 4, text: 'Komodo IDE'}
  911. ]
  912. @hidden = sinon.spy @dialog, "modal"
  913. it "specified checkboxes are checked", ->
  914. checked = []
  915. @dialog.find("input:checkbox:checked").each (foo, bar) =>
  916. checked.push $(bar).val()
  917. expect(checked).to.deep.equal ["2", "3"]
  918. describe "when dismissing the dialog by clicking OK", ->
  919. beforeEach ->
  920. @dialog.find(".btn-primary").trigger "click"
  921. it "should invoke the callback", ->
  922. expect(@callback).to.have.been.called
  923. it "with the correct value", ->
  924. expect(@callback).to.have.been.calledWithExactly ["2", "3"]
  925. describe "when dismissing the dialog by clicking Cancel", ->
  926. beforeEach ->
  927. @dialog.find(".btn-default").trigger "click"
  928. it "should invoke the callback", ->
  929. expect(@callback).to.have.been.called
  930. it "with the correct value", ->
  931. expect(@callback).to.have.been.calledWithExactly null
  932. describe "when changing the checked options and dismissing the dialog by clicking Cancel", ->
  933. beforeEach ->
  934. @dialog.find("input:checkbox:checked").prop('checked', false)
  935. @dialog.find("input:checkbox[value=1]").prop('checked', true)
  936. @dialog.find("input:checkbox[value=4]").prop('checked', true)
  937. @dialog.find(".btn-default").trigger "click"
  938. it "should invoke the callback", ->
  939. expect(@callback).to.have.been.called
  940. it "with the correct value", ->
  941. expect(@callback).to.have.been.calledWithExactly null
  942. describe "when changing the checked options and dismissing the dialog by clicking OK", ->
  943. beforeEach ->
  944. @dialog.find("input:checkbox:checked").prop('checked', false)
  945. @dialog.find("input:checkbox[value=1]").prop('checked', true)
  946. @dialog.find("input:checkbox[value=4]").prop('checked', true)
  947. @dialog.find(".btn-primary").trigger "click"
  948. it "should invoke the callback", ->
  949. expect(@callback).to.have.been.called
  950. it "with the correct value", ->
  951. expect(@callback).to.have.been.calledWithExactly ["1", "4"]