modal.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. $(function () {
  2. 'use strict';
  3. QUnit.module('modal plugin')
  4. QUnit.test('should be defined on jquery object', function (assert) {
  5. assert.expect(1)
  6. assert.ok($(document.body).modal, 'modal method is defined')
  7. })
  8. QUnit.module('modal', {
  9. beforeEach: function () {
  10. // Run all tests in noConflict mode -- it's the only way to ensure that the plugin works in noConflict mode
  11. $.fn.bootstrapModal = $.fn.modal.noConflict()
  12. },
  13. afterEach: function () {
  14. $.fn.modal = $.fn.bootstrapModal
  15. delete $.fn.bootstrapModal
  16. }
  17. })
  18. QUnit.test('should provide no conflict', function (assert) {
  19. assert.expect(1)
  20. assert.strictEqual($.fn.modal, undefined, 'modal was set back to undefined (orig value)')
  21. })
  22. QUnit.test('should return jquery collection containing the element', function (assert) {
  23. assert.expect(2)
  24. var $el = $('<div id="modal-test"/>')
  25. var $modal = $el.bootstrapModal()
  26. assert.ok($modal instanceof $, 'returns jquery collection')
  27. assert.strictEqual($modal[0], $el[0], 'collection contains element')
  28. })
  29. QUnit.test('should expose defaults var for settings', function (assert) {
  30. assert.expect(1)
  31. assert.ok($.fn.bootstrapModal.Constructor.DEFAULTS, 'default object exposed')
  32. })
  33. QUnit.test('should insert into dom when show method is called', function (assert) {
  34. assert.expect(1)
  35. var done = assert.async()
  36. $('<div id="modal-test"/>')
  37. .on('shown.bs.modal', function () {
  38. assert.notEqual($('#modal-test').length, 0, 'modal inserted into dom')
  39. done()
  40. })
  41. .bootstrapModal('show')
  42. })
  43. QUnit.test('should set aria-hidden to false when show method is called', function (assert) {
  44. assert.expect(1)
  45. var done = assert.async()
  46. $('<div id="modal-test"/>')
  47. .on('shown.bs.modal', function () {
  48. assert.strictEqual($('#modal-test').attr('aria-hidden'), 'false', 'aria-hidden is set to string "false" when modal shown')
  49. done()
  50. })
  51. .bootstrapModal('show')
  52. })
  53. QUnit.test('should fire show event', function (assert) {
  54. assert.expect(1)
  55. var done = assert.async()
  56. $('<div id="modal-test"/>')
  57. .on('show.bs.modal', function () {
  58. assert.ok(true, 'show event fired')
  59. done()
  60. })
  61. .bootstrapModal('show')
  62. })
  63. QUnit.test('should not fire shown when show was prevented', function (assert) {
  64. assert.expect(1)
  65. var done = assert.async()
  66. $('<div id="modal-test"/>')
  67. .on('show.bs.modal', function (e) {
  68. e.preventDefault()
  69. assert.ok(true, 'show event fired')
  70. done()
  71. })
  72. .on('shown.bs.modal', function () {
  73. assert.ok(false, 'shown event fired')
  74. })
  75. .bootstrapModal('show')
  76. })
  77. QUnit.test('should hide modal when hide is called', function (assert) {
  78. assert.expect(3)
  79. var done = assert.async()
  80. $('<div id="modal-test"/>')
  81. .on('shown.bs.modal', function () {
  82. assert.ok($('#modal-test').is(':visible'), 'modal visible')
  83. assert.notEqual($('#modal-test').length, 0, 'modal inserted into dom')
  84. $(this).bootstrapModal('hide')
  85. })
  86. .on('hidden.bs.modal', function () {
  87. assert.ok(!$('#modal-test').is(':visible'), 'modal hidden')
  88. done()
  89. })
  90. .bootstrapModal('show')
  91. })
  92. QUnit.test('should set aria-hidden to true when hide is called', function (assert) {
  93. assert.expect(2)
  94. var done = assert.async()
  95. $('<div id="modal-test"/>')
  96. .on('shown.bs.modal', function () {
  97. assert.strictEqual($('#modal-test').length, 1, 'modal has been inserted into the dom')
  98. $(this).bootstrapModal('hide')
  99. })
  100. .on('hidden.bs.modal', function () {
  101. assert.strictEqual($('#modal-test').attr('aria-hidden'), 'true', 'aria-hidden is set to string "true" when modal shown')
  102. done()
  103. })
  104. .bootstrapModal('show')
  105. })
  106. QUnit.test('should toggle when toggle is called', function (assert) {
  107. assert.expect(3)
  108. var done = assert.async()
  109. $('<div id="modal-test"/>')
  110. .on('shown.bs.modal', function () {
  111. assert.ok($('#modal-test').is(':visible'), 'modal visible')
  112. assert.notEqual($('#modal-test').length, 0, 'modal inserted into dom')
  113. $(this).bootstrapModal('toggle')
  114. })
  115. .on('hidden.bs.modal', function () {
  116. assert.ok(!$('#modal-test').is(':visible'), 'modal hidden')
  117. done()
  118. })
  119. .bootstrapModal('toggle')
  120. })
  121. QUnit.test('should remove from dom when click [data-dismiss="modal"]', function (assert) {
  122. assert.expect(3)
  123. var done = assert.async()
  124. $('<div id="modal-test"><span class="close" data-dismiss="modal"/></div>')
  125. .on('shown.bs.modal', function () {
  126. assert.ok($('#modal-test').is(':visible'), 'modal visible')
  127. assert.notEqual($('#modal-test').length, 0, 'modal inserted into dom')
  128. $(this).find('.close').trigger('click')
  129. })
  130. .on('hidden.bs.modal', function () {
  131. assert.ok(!$('#modal-test').is(':visible'), 'modal hidden')
  132. done()
  133. })
  134. .bootstrapModal('toggle')
  135. })
  136. QUnit.test('should allow modal close with "backdrop:false"', function (assert) {
  137. assert.expect(2)
  138. var done = assert.async()
  139. $('<div id="modal-test" data-backdrop="false"/>')
  140. .on('shown.bs.modal', function () {
  141. assert.ok($('#modal-test').is(':visible'), 'modal visible')
  142. $(this).bootstrapModal('hide')
  143. })
  144. .on('hidden.bs.modal', function () {
  145. assert.ok(!$('#modal-test').is(':visible'), 'modal hidden')
  146. done()
  147. })
  148. .bootstrapModal('show')
  149. })
  150. QUnit.test('should close modal when clicking outside of modal-content', function (assert) {
  151. assert.expect(3)
  152. var done = assert.async()
  153. $('<div id="modal-test"><div class="contents"/></div>')
  154. .on('shown.bs.modal', function () {
  155. assert.notEqual($('#modal-test').length, 0, 'modal inserted into dom')
  156. $('.contents').trigger('click')
  157. assert.ok($('#modal-test').is(':visible'), 'modal visible')
  158. $('#modal-test').trigger('click')
  159. })
  160. .on('hidden.bs.modal', function () {
  161. assert.ok(!$('#modal-test').is(':visible'), 'modal hidden')
  162. done()
  163. })
  164. .bootstrapModal('show')
  165. })
  166. QUnit.test('should close modal when escape key is pressed via keydown', function (assert) {
  167. assert.expect(3)
  168. var done = assert.async()
  169. var div = $('<div id="modal-test"/>')
  170. div
  171. .on('shown.bs.modal', function () {
  172. assert.ok($('#modal-test').length, 'modal insterted into dom')
  173. assert.ok($('#modal-test').is(':visible'), 'modal visible')
  174. div.trigger($.Event('keydown', { which: 27 }))
  175. setTimeout(function () {
  176. assert.ok(!$('#modal-test').is(':visible'), 'modal hidden')
  177. div.remove()
  178. done()
  179. }, 0)
  180. })
  181. .bootstrapModal('show')
  182. })
  183. QUnit.test('should not close modal when escape key is pressed via keyup', function (assert) {
  184. assert.expect(3)
  185. var done = assert.async()
  186. var div = $('<div id="modal-test"/>')
  187. div
  188. .on('shown.bs.modal', function () {
  189. assert.ok($('#modal-test').length, 'modal inserted into dom')
  190. assert.ok($('#modal-test').is(':visible'), 'modal visible')
  191. div.trigger($.Event('keyup', { which: 27 }))
  192. setTimeout(function () {
  193. assert.ok($('#modal-test').is(':visible'), 'modal still visible')
  194. div.remove()
  195. done()
  196. }, 0)
  197. })
  198. .bootstrapModal('show')
  199. })
  200. QUnit.test('should trigger hide event once when clicking outside of modal-content', function (assert) {
  201. assert.expect(1)
  202. var done = assert.async()
  203. var triggered
  204. $('<div id="modal-test"><div class="contents"/></div>')
  205. .on('shown.bs.modal', function () {
  206. triggered = 0
  207. $('#modal-test').trigger('click')
  208. })
  209. .on('hide.bs.modal', function () {
  210. triggered += 1
  211. assert.strictEqual(triggered, 1, 'modal hide triggered once')
  212. done()
  213. })
  214. .bootstrapModal('show')
  215. })
  216. QUnit.test('should close reopened modal with [data-dismiss="modal"] click', function (assert) {
  217. assert.expect(2)
  218. var done = assert.async()
  219. $('<div id="modal-test"><div class="contents"><div id="close" data-dismiss="modal"/></div></div>')
  220. .one('shown.bs.modal', function () {
  221. $('#close').trigger('click')
  222. })
  223. .one('hidden.bs.modal', function () {
  224. // after one open-close cycle
  225. assert.ok(!$('#modal-test').is(':visible'), 'modal hidden')
  226. $(this)
  227. .one('shown.bs.modal', function () {
  228. $('#close').trigger('click')
  229. })
  230. .one('hidden.bs.modal', function () {
  231. assert.ok(!$('#modal-test').is(':visible'), 'modal hidden')
  232. done()
  233. })
  234. .bootstrapModal('show')
  235. })
  236. .bootstrapModal('show')
  237. })
  238. QUnit.test('should restore focus to toggling element when modal is hidden after having been opened via data-api', function (assert) {
  239. assert.expect(1)
  240. var done = assert.async()
  241. var $toggleBtn = $('<button data-toggle="modal" data-target="#modal-test"/>').appendTo('#qunit-fixture')
  242. $('<div id="modal-test"><div class="contents"><div id="close" data-dismiss="modal"/></div></div>')
  243. .on('hidden.bs.modal', function () {
  244. setTimeout(function () {
  245. assert.ok($(document.activeElement).is($toggleBtn), 'toggling element is once again focused')
  246. done()
  247. }, 0)
  248. })
  249. .on('shown.bs.modal', function () {
  250. $('#close').trigger('click')
  251. })
  252. .appendTo('#qunit-fixture')
  253. $toggleBtn.trigger('click')
  254. })
  255. QUnit.test('should not restore focus to toggling element if the associated show event gets prevented', function (assert) {
  256. assert.expect(1)
  257. var done = assert.async()
  258. var $toggleBtn = $('<button data-toggle="modal" data-target="#modal-test"/>').appendTo('#qunit-fixture')
  259. var $otherBtn = $('<button id="other-btn"/>').appendTo('#qunit-fixture')
  260. $('<div id="modal-test"><div class="contents"><div id="close" data-dismiss="modal"/></div>')
  261. .one('show.bs.modal', function (e) {
  262. e.preventDefault()
  263. $otherBtn.trigger('focus')
  264. setTimeout($.proxy(function () {
  265. $(this).bootstrapModal('show')
  266. }, this), 0)
  267. })
  268. .on('hidden.bs.modal', function () {
  269. setTimeout(function () {
  270. assert.ok($(document.activeElement).is($otherBtn), 'focus returned to toggling element')
  271. done()
  272. }, 0)
  273. })
  274. .on('shown.bs.modal', function () {
  275. $('#close').trigger('click')
  276. })
  277. .appendTo('#qunit-fixture')
  278. $toggleBtn.trigger('click')
  279. })
  280. QUnit.test('should restore inline body padding after closing', function (assert) {
  281. assert.expect(2)
  282. var done = assert.async()
  283. var originalBodyPad = 0
  284. var $body = $(document.body)
  285. $body.css('padding-right', originalBodyPad)
  286. $('<div id="modal-test"/>')
  287. .on('hidden.bs.modal', function () {
  288. var currentBodyPad = parseInt($body.css('padding-right'), 10)
  289. assert.notStrictEqual($body.attr('style'), '', 'body has non-empty style attribute')
  290. assert.strictEqual(currentBodyPad, originalBodyPad, 'original body padding was not changed')
  291. $body.removeAttr('style')
  292. done()
  293. })
  294. .on('shown.bs.modal', function () {
  295. $(this).bootstrapModal('hide')
  296. })
  297. .bootstrapModal('show')
  298. })
  299. QUnit.test('should ignore values set via CSS when trying to restore body padding after closing', function (assert) {
  300. assert.expect(1)
  301. var done = assert.async()
  302. var $body = $(document.body)
  303. var $style = $('<style>body { padding-right: 42px; }</style>').appendTo('head')
  304. $('<div id="modal-test"/>')
  305. .on('hidden.bs.modal', function () {
  306. assert.ok(!$body.attr('style'), 'body does not have inline padding set')
  307. $style.remove()
  308. done()
  309. })
  310. .on('shown.bs.modal', function () {
  311. $(this).bootstrapModal('hide')
  312. })
  313. .bootstrapModal('show')
  314. })
  315. QUnit.test('should ignore other inline styles when trying to restore body padding after closing', function (assert) {
  316. assert.expect(2)
  317. var done = assert.async()
  318. var $body = $(document.body)
  319. var $style = $('<style>body { padding-right: 42px; }</style>').appendTo('head')
  320. $body.css('color', 'red')
  321. $('<div id="modal-test"/>')
  322. .on('hidden.bs.modal', function () {
  323. assert.strictEqual($body[0].style.paddingRight, '', 'body does not have inline padding set')
  324. assert.strictEqual($body[0].style.color, 'red', 'body still has other inline styles set')
  325. $body.removeAttr('style')
  326. $style.remove()
  327. done()
  328. })
  329. .on('shown.bs.modal', function () {
  330. $(this).bootstrapModal('hide')
  331. })
  332. .bootstrapModal('show')
  333. })
  334. QUnit.test('should properly restore non-pixel inline body padding after closing', function (assert) {
  335. assert.expect(1)
  336. var done = assert.async()
  337. var $body = $(document.body)
  338. $body.css('padding-right', '5%')
  339. $('<div id="modal-test"/>')
  340. .on('hidden.bs.modal', function () {
  341. assert.strictEqual($body[0].style.paddingRight, '5%', 'body does not have inline padding set')
  342. $body.removeAttr('style')
  343. done()
  344. })
  345. .on('shown.bs.modal', function () {
  346. $(this).bootstrapModal('hide')
  347. })
  348. .bootstrapModal('show')
  349. })
  350. })