modal.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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 fire show event', function (assert) {
  44. assert.expect(1)
  45. var done = assert.async()
  46. $('<div id="modal-test"/>')
  47. .on('show.bs.modal', function () {
  48. assert.ok(true, 'show event fired')
  49. done()
  50. })
  51. .bootstrapModal('show')
  52. })
  53. QUnit.test('should not fire shown when show was prevented', function (assert) {
  54. assert.expect(1)
  55. var done = assert.async()
  56. $('<div id="modal-test"/>')
  57. .on('show.bs.modal', function (e) {
  58. e.preventDefault()
  59. assert.ok(true, 'show event fired')
  60. done()
  61. })
  62. .on('shown.bs.modal', function () {
  63. assert.ok(false, 'shown event fired')
  64. })
  65. .bootstrapModal('show')
  66. })
  67. QUnit.test('should hide modal when hide is called', function (assert) {
  68. assert.expect(3)
  69. var done = assert.async()
  70. $('<div id="modal-test"/>')
  71. .on('shown.bs.modal', function () {
  72. assert.ok($('#modal-test').is(':visible'), 'modal visible')
  73. assert.notEqual($('#modal-test').length, 0, 'modal inserted into dom')
  74. $(this).bootstrapModal('hide')
  75. })
  76. .on('hidden.bs.modal', function () {
  77. assert.ok(!$('#modal-test').is(':visible'), 'modal hidden')
  78. done()
  79. })
  80. .bootstrapModal('show')
  81. })
  82. QUnit.test('should toggle when toggle is called', function (assert) {
  83. assert.expect(3)
  84. var done = assert.async()
  85. $('<div id="modal-test"/>')
  86. .on('shown.bs.modal', function () {
  87. assert.ok($('#modal-test').is(':visible'), 'modal visible')
  88. assert.notEqual($('#modal-test').length, 0, 'modal inserted into dom')
  89. $(this).bootstrapModal('toggle')
  90. })
  91. .on('hidden.bs.modal', function () {
  92. assert.ok(!$('#modal-test').is(':visible'), 'modal hidden')
  93. done()
  94. })
  95. .bootstrapModal('toggle')
  96. })
  97. QUnit.test('should remove from dom when click [data-dismiss="modal"]', function (assert) {
  98. assert.expect(3)
  99. var done = assert.async()
  100. $('<div id="modal-test"><span class="close" data-dismiss="modal"/></div>')
  101. .on('shown.bs.modal', function () {
  102. assert.ok($('#modal-test').is(':visible'), 'modal visible')
  103. assert.notEqual($('#modal-test').length, 0, 'modal inserted into dom')
  104. $(this).find('.close').trigger('click')
  105. })
  106. .on('hidden.bs.modal', function () {
  107. assert.ok(!$('#modal-test').is(':visible'), 'modal hidden')
  108. done()
  109. })
  110. .bootstrapModal('toggle')
  111. })
  112. QUnit.test('should allow modal close with "backdrop:false"', function (assert) {
  113. assert.expect(2)
  114. var done = assert.async()
  115. $('<div id="modal-test" data-backdrop="false"/>')
  116. .on('shown.bs.modal', function () {
  117. assert.ok($('#modal-test').is(':visible'), 'modal visible')
  118. $(this).bootstrapModal('hide')
  119. })
  120. .on('hidden.bs.modal', function () {
  121. assert.ok(!$('#modal-test').is(':visible'), 'modal hidden')
  122. done()
  123. })
  124. .bootstrapModal('show')
  125. })
  126. QUnit.test('should close modal when clicking outside of modal-content', function (assert) {
  127. assert.expect(3)
  128. var done = assert.async()
  129. $('<div id="modal-test"><div class="contents"/></div>')
  130. .on('shown.bs.modal', function () {
  131. assert.notEqual($('#modal-test').length, 0, 'modal inserted into dom')
  132. $('.contents').trigger('click')
  133. assert.ok($('#modal-test').is(':visible'), 'modal visible')
  134. $('#modal-test').trigger('click')
  135. })
  136. .on('hidden.bs.modal', function () {
  137. assert.ok(!$('#modal-test').is(':visible'), 'modal hidden')
  138. done()
  139. })
  140. .bootstrapModal('show')
  141. })
  142. QUnit.test('should close modal when escape key is pressed via keydown', function (assert) {
  143. assert.expect(3)
  144. var done = assert.async()
  145. var $div = $('<div id="modal-test"/>')
  146. $div
  147. .on('shown.bs.modal', function () {
  148. assert.ok($('#modal-test').length, 'modal inserted into dom')
  149. assert.ok($('#modal-test').is(':visible'), 'modal visible')
  150. $div.trigger($.Event('keydown', { which: 27 }))
  151. setTimeout(function () {
  152. assert.ok(!$('#modal-test').is(':visible'), 'modal hidden')
  153. $div.remove()
  154. done()
  155. }, 0)
  156. })
  157. .bootstrapModal('show')
  158. })
  159. QUnit.test('should not close modal when escape key is pressed via keyup', function (assert) {
  160. assert.expect(3)
  161. var done = assert.async()
  162. var $div = $('<div id="modal-test"/>')
  163. $div
  164. .on('shown.bs.modal', function () {
  165. assert.ok($('#modal-test').length, 'modal inserted into dom')
  166. assert.ok($('#modal-test').is(':visible'), 'modal visible')
  167. $div.trigger($.Event('keyup', { which: 27 }))
  168. setTimeout(function () {
  169. assert.ok($div.is(':visible'), 'modal still visible')
  170. $div.remove()
  171. done()
  172. }, 0)
  173. })
  174. .bootstrapModal('show')
  175. })
  176. QUnit.test('should trigger hide event once when clicking outside of modal-content', function (assert) {
  177. assert.expect(1)
  178. var done = assert.async()
  179. var triggered
  180. $('<div id="modal-test"><div class="contents"/></div>')
  181. .on('shown.bs.modal', function () {
  182. triggered = 0
  183. $('#modal-test').trigger('click')
  184. })
  185. .on('hide.bs.modal', function () {
  186. triggered += 1
  187. assert.strictEqual(triggered, 1, 'modal hide triggered once')
  188. done()
  189. })
  190. .bootstrapModal('show')
  191. })
  192. QUnit.test('should close reopened modal with [data-dismiss="modal"] click', function (assert) {
  193. assert.expect(2)
  194. var done = assert.async()
  195. $('<div id="modal-test"><div class="contents"><div id="close" data-dismiss="modal"/></div></div>')
  196. .one('shown.bs.modal', function () {
  197. $('#close').trigger('click')
  198. })
  199. .one('hidden.bs.modal', function () {
  200. // after one open-close cycle
  201. assert.ok(!$('#modal-test').is(':visible'), 'modal hidden')
  202. $(this)
  203. .one('shown.bs.modal', function () {
  204. $('#close').trigger('click')
  205. })
  206. .one('hidden.bs.modal', function () {
  207. assert.ok(!$('#modal-test').is(':visible'), 'modal hidden')
  208. done()
  209. })
  210. .bootstrapModal('show')
  211. })
  212. .bootstrapModal('show')
  213. })
  214. QUnit.test('should restore focus to toggling element when modal is hidden after having been opened via data-api', function (assert) {
  215. assert.expect(1)
  216. var done = assert.async()
  217. var $toggleBtn = $('<button data-toggle="modal" data-target="#modal-test"/>').appendTo('#qunit-fixture')
  218. $('<div id="modal-test"><div class="contents"><div id="close" data-dismiss="modal"/></div></div>')
  219. .on('hidden.bs.modal', function () {
  220. setTimeout(function () {
  221. assert.ok($(document.activeElement).is($toggleBtn), 'toggling element is once again focused')
  222. done()
  223. }, 0)
  224. })
  225. .on('shown.bs.modal', function () {
  226. $('#close').trigger('click')
  227. })
  228. .appendTo('#qunit-fixture')
  229. $toggleBtn.trigger('click')
  230. })
  231. QUnit.test('should not restore focus to toggling element if the associated show event gets prevented', function (assert) {
  232. assert.expect(1)
  233. var done = assert.async()
  234. var $toggleBtn = $('<button data-toggle="modal" data-target="#modal-test"/>').appendTo('#qunit-fixture')
  235. var $otherBtn = $('<button id="other-btn"/>').appendTo('#qunit-fixture')
  236. $('<div id="modal-test"><div class="contents"><div id="close" data-dismiss="modal"/></div>')
  237. .one('show.bs.modal', function (e) {
  238. e.preventDefault()
  239. $otherBtn.trigger('focus')
  240. setTimeout($.proxy(function () {
  241. $(this).bootstrapModal('show')
  242. }, this), 0)
  243. })
  244. .on('hidden.bs.modal', function () {
  245. setTimeout(function () {
  246. assert.ok($(document.activeElement).is($otherBtn), 'focus returned to toggling element')
  247. done()
  248. }, 0)
  249. })
  250. .on('shown.bs.modal', function () {
  251. $('#close').trigger('click')
  252. })
  253. .appendTo('#qunit-fixture')
  254. $toggleBtn.trigger('click')
  255. })
  256. QUnit.test('should restore inline body padding after closing', function (assert) {
  257. assert.expect(2)
  258. var done = assert.async()
  259. var originalBodyPad = 0
  260. var $body = $(document.body)
  261. $body.css('padding-right', originalBodyPad)
  262. $('<div id="modal-test"/>')
  263. .on('hidden.bs.modal', function () {
  264. var currentBodyPad = parseInt($body.css('padding-right'), 10)
  265. assert.notStrictEqual($body.attr('style'), '', 'body has non-empty style attribute')
  266. assert.strictEqual(currentBodyPad, originalBodyPad, 'original body padding was not changed')
  267. $body.removeAttr('style')
  268. done()
  269. })
  270. .on('shown.bs.modal', function () {
  271. $(this).bootstrapModal('hide')
  272. })
  273. .bootstrapModal('show')
  274. })
  275. QUnit.test('should ignore values set via CSS when trying to restore body padding after closing', function (assert) {
  276. assert.expect(1)
  277. var done = assert.async()
  278. var $body = $(document.body)
  279. var $style = $('<style>body { padding-right: 42px; }</style>').appendTo('head')
  280. $('<div id="modal-test"/>')
  281. .on('hidden.bs.modal', function () {
  282. assert.ok(!$body.attr('style'), 'body does not have inline padding set')
  283. $style.remove()
  284. done()
  285. })
  286. .on('shown.bs.modal', function () {
  287. $(this).bootstrapModal('hide')
  288. })
  289. .bootstrapModal('show')
  290. })
  291. QUnit.test('should ignore other inline styles when trying to restore body padding after closing', function (assert) {
  292. assert.expect(2)
  293. var done = assert.async()
  294. var $body = $(document.body)
  295. var $style = $('<style>body { padding-right: 42px; }</style>').appendTo('head')
  296. $body.css('color', 'red')
  297. $('<div id="modal-test"/>')
  298. .on('hidden.bs.modal', function () {
  299. assert.strictEqual($body[0].style.paddingRight, '', 'body does not have inline padding set')
  300. assert.strictEqual($body[0].style.color, 'red', 'body still has other inline styles set')
  301. $body.removeAttr('style')
  302. $style.remove()
  303. done()
  304. })
  305. .on('shown.bs.modal', function () {
  306. $(this).bootstrapModal('hide')
  307. })
  308. .bootstrapModal('show')
  309. })
  310. QUnit.test('should properly restore non-pixel inline body padding after closing', function (assert) {
  311. assert.expect(1)
  312. var done = assert.async()
  313. var $body = $(document.body)
  314. $body.css('padding-right', '5%')
  315. $('<div id="modal-test"/>')
  316. .on('hidden.bs.modal', function () {
  317. assert.strictEqual($body[0].style.paddingRight, '5%', 'body does not have inline padding set')
  318. $body.removeAttr('style')
  319. done()
  320. })
  321. .on('shown.bs.modal', function () {
  322. $(this).bootstrapModal('hide')
  323. })
  324. .bootstrapModal('show')
  325. })
  326. })