4 QUnit.module('modal plugin')
6 QUnit.test('should be defined on jquery object', function (assert) {
8 assert.ok($(document.body).modal, 'modal method is defined')
11 QUnit.module('modal', {
12 beforeEach: function () {
13 // Run all tests in noConflict mode -- it's the only way to ensure that the plugin works in noConflict mode
14 $.fn.bootstrapModal = $.fn.modal.noConflict()
16 afterEach: function () {
17 $.fn.modal = $.fn.bootstrapModal
18 delete $.fn.bootstrapModal
22 QUnit.test('should provide no conflict', function (assert) {
24 assert.strictEqual($.fn.modal, undefined, 'modal was set back to undefined (orig value)')
27 QUnit.test('should return jquery collection containing the element', function (assert) {
29 var $el = $('<div id="modal-test"/>')
30 var $modal = $el.bootstrapModal()
31 assert.ok($modal instanceof $, 'returns jquery collection')
32 assert.strictEqual($modal[0], $el[0], 'collection contains element')
35 QUnit.test('should expose defaults var for settings', function (assert) {
37 assert.ok($.fn.bootstrapModal.Constructor.DEFAULTS, 'default object exposed')
40 QUnit.test('should insert into dom when show method is called', function (assert) {
42 var done = assert.async()
44 $('<div id="modal-test"/>')
45 .on('shown.bs.modal', function () {
46 assert.notEqual($('#modal-test').length, 0, 'modal inserted into dom')
49 .bootstrapModal('show')
52 QUnit.test('should set aria-hidden to false when show method is called', function (assert) {
54 var done = assert.async()
56 $('<div id="modal-test"/>')
57 .on('shown.bs.modal', function () {
58 assert.strictEqual($('#modal-test').attr('aria-hidden'), 'false', 'aria-hidden is set to string "false" when modal shown')
61 .bootstrapModal('show')
64 QUnit.test('should fire show event', function (assert) {
66 var done = assert.async()
68 $('<div id="modal-test"/>')
69 .on('show.bs.modal', function () {
70 assert.ok(true, 'show event fired')
73 .bootstrapModal('show')
76 QUnit.test('should not fire shown when show was prevented', function (assert) {
78 var done = assert.async()
80 $('<div id="modal-test"/>')
81 .on('show.bs.modal', function (e) {
83 assert.ok(true, 'show event fired')
86 .on('shown.bs.modal', function () {
87 assert.ok(false, 'shown event fired')
89 .bootstrapModal('show')
92 QUnit.test('should hide modal when hide is called', function (assert) {
94 var done = assert.async()
96 $('<div id="modal-test"/>')
97 .on('shown.bs.modal', function () {
98 assert.ok($('#modal-test').is(':visible'), 'modal visible')
99 assert.notEqual($('#modal-test').length, 0, 'modal inserted into dom')
100 $(this).bootstrapModal('hide')
102 .on('hidden.bs.modal', function () {
103 assert.ok(!$('#modal-test').is(':visible'), 'modal hidden')
106 .bootstrapModal('show')
109 QUnit.test('should set aria-hidden to true when hide is called', function (assert) {
111 var done = assert.async()
113 $('<div id="modal-test"/>')
114 .on('shown.bs.modal', function () {
115 assert.strictEqual($('#modal-test').length, 1, 'modal has been inserted into the dom')
116 $(this).bootstrapModal('hide')
118 .on('hidden.bs.modal', function () {
119 assert.strictEqual($('#modal-test').attr('aria-hidden'), 'true', 'aria-hidden is set to string "true" when modal shown')
122 .bootstrapModal('show')
125 QUnit.test('should toggle when toggle is called', function (assert) {
127 var done = assert.async()
129 $('<div id="modal-test"/>')
130 .on('shown.bs.modal', function () {
131 assert.ok($('#modal-test').is(':visible'), 'modal visible')
132 assert.notEqual($('#modal-test').length, 0, 'modal inserted into dom')
133 $(this).bootstrapModal('toggle')
135 .on('hidden.bs.modal', function () {
136 assert.ok(!$('#modal-test').is(':visible'), 'modal hidden')
139 .bootstrapModal('toggle')
142 QUnit.test('should remove from dom when click [data-dismiss="modal"]', function (assert) {
144 var done = assert.async()
146 $('<div id="modal-test"><span class="close" data-dismiss="modal"/></div>')
147 .on('shown.bs.modal', function () {
148 assert.ok($('#modal-test').is(':visible'), 'modal visible')
149 assert.notEqual($('#modal-test').length, 0, 'modal inserted into dom')
150 $(this).find('.close').trigger('click')
152 .on('hidden.bs.modal', function () {
153 assert.ok(!$('#modal-test').is(':visible'), 'modal hidden')
156 .bootstrapModal('toggle')
159 QUnit.test('should allow modal close with "backdrop:false"', function (assert) {
161 var done = assert.async()
163 $('<div id="modal-test" data-backdrop="false"/>')
164 .on('shown.bs.modal', function () {
165 assert.ok($('#modal-test').is(':visible'), 'modal visible')
166 $(this).bootstrapModal('hide')
168 .on('hidden.bs.modal', function () {
169 assert.ok(!$('#modal-test').is(':visible'), 'modal hidden')
172 .bootstrapModal('show')
175 QUnit.test('should close modal when clicking outside of modal-content', function (assert) {
177 var done = assert.async()
179 $('<div id="modal-test"><div class="contents"/></div>')
180 .on('shown.bs.modal', function () {
181 assert.notEqual($('#modal-test').length, 0, 'modal inserted into dom')
182 $('.contents').trigger('click')
183 assert.ok($('#modal-test').is(':visible'), 'modal visible')
184 $('#modal-test').trigger('click')
186 .on('hidden.bs.modal', function () {
187 assert.ok(!$('#modal-test').is(':visible'), 'modal hidden')
190 .bootstrapModal('show')
193 QUnit.test('should close modal when escape key is pressed via keydown', function (assert) {
195 var done = assert.async()
197 var div = $('<div id="modal-test"/>')
199 .on('shown.bs.modal', function () {
200 assert.ok($('#modal-test').length, 'modal insterted into dom')
201 assert.ok($('#modal-test').is(':visible'), 'modal visible')
202 div.trigger($.Event('keydown', { which: 27 }))
204 setTimeout(function () {
205 assert.ok(!$('#modal-test').is(':visible'), 'modal hidden')
210 .bootstrapModal('show')
213 QUnit.test('should not close modal when escape key is pressed via keyup', function (assert) {
215 var done = assert.async()
217 var div = $('<div id="modal-test"/>')
219 .on('shown.bs.modal', function () {
220 assert.ok($('#modal-test').length, 'modal inserted into dom')
221 assert.ok($('#modal-test').is(':visible'), 'modal visible')
222 div.trigger($.Event('keyup', { which: 27 }))
224 setTimeout(function () {
225 assert.ok($('#modal-test').is(':visible'), 'modal still visible')
230 .bootstrapModal('show')
233 QUnit.test('should trigger hide event once when clicking outside of modal-content', function (assert) {
235 var done = assert.async()
239 $('<div id="modal-test"><div class="contents"/></div>')
240 .on('shown.bs.modal', function () {
242 $('#modal-test').trigger('click')
244 .on('hide.bs.modal', function () {
246 assert.strictEqual(triggered, 1, 'modal hide triggered once')
249 .bootstrapModal('show')
252 QUnit.test('should close reopened modal with [data-dismiss="modal"] click', function (assert) {
254 var done = assert.async()
256 $('<div id="modal-test"><div class="contents"><div id="close" data-dismiss="modal"/></div></div>')
257 .one('shown.bs.modal', function () {
258 $('#close').trigger('click')
260 .one('hidden.bs.modal', function () {
261 // after one open-close cycle
262 assert.ok(!$('#modal-test').is(':visible'), 'modal hidden')
264 .one('shown.bs.modal', function () {
265 $('#close').trigger('click')
267 .one('hidden.bs.modal', function () {
268 assert.ok(!$('#modal-test').is(':visible'), 'modal hidden')
271 .bootstrapModal('show')
273 .bootstrapModal('show')
276 QUnit.test('should restore focus to toggling element when modal is hidden after having been opened via data-api', function (assert) {
278 var done = assert.async()
280 var $toggleBtn = $('<button data-toggle="modal" data-target="#modal-test"/>').appendTo('#qunit-fixture')
282 $('<div id="modal-test"><div class="contents"><div id="close" data-dismiss="modal"/></div></div>')
283 .on('hidden.bs.modal', function () {
284 setTimeout(function () {
285 assert.ok($(document.activeElement).is($toggleBtn), 'toggling element is once again focused')
289 .on('shown.bs.modal', function () {
290 $('#close').trigger('click')
292 .appendTo('#qunit-fixture')
294 $toggleBtn.trigger('click')
297 QUnit.test('should not restore focus to toggling element if the associated show event gets prevented', function (assert) {
299 var done = assert.async()
300 var $toggleBtn = $('<button data-toggle="modal" data-target="#modal-test"/>').appendTo('#qunit-fixture')
301 var $otherBtn = $('<button id="other-btn"/>').appendTo('#qunit-fixture')
303 $('<div id="modal-test"><div class="contents"><div id="close" data-dismiss="modal"/></div>')
304 .one('show.bs.modal', function (e) {
306 $otherBtn.trigger('focus')
307 setTimeout($.proxy(function () {
308 $(this).bootstrapModal('show')
311 .on('hidden.bs.modal', function () {
312 setTimeout(function () {
313 assert.ok($(document.activeElement).is($otherBtn), 'focus returned to toggling element')
317 .on('shown.bs.modal', function () {
318 $('#close').trigger('click')
320 .appendTo('#qunit-fixture')
322 $toggleBtn.trigger('click')
325 QUnit.test('should restore inline body padding after closing', function (assert) {
327 var done = assert.async()
328 var originalBodyPad = 0
329 var $body = $(document.body)
331 $body.css('padding-right', originalBodyPad)
333 $('<div id="modal-test"/>')
334 .on('hidden.bs.modal', function () {
335 var currentBodyPad = parseInt($body.css('padding-right'), 10)
336 assert.notStrictEqual($body.attr('style'), '', 'body has non-empty style attribute')
337 assert.strictEqual(currentBodyPad, originalBodyPad, 'original body padding was not changed')
338 $body.removeAttr('style')
341 .on('shown.bs.modal', function () {
342 $(this).bootstrapModal('hide')
344 .bootstrapModal('show')
347 QUnit.test('should ignore values set via CSS when trying to restore body padding after closing', function (assert) {
349 var done = assert.async()
350 var $body = $(document.body)
351 var $style = $('<style>body { padding-right: 42px; }</style>').appendTo('head')
353 $('<div id="modal-test"/>')
354 .on('hidden.bs.modal', function () {
355 assert.ok(!$body.attr('style'), 'body does not have inline padding set')
359 .on('shown.bs.modal', function () {
360 $(this).bootstrapModal('hide')
362 .bootstrapModal('show')
365 QUnit.test('should ignore other inline styles when trying to restore body padding after closing', function (assert) {
367 var done = assert.async()
368 var $body = $(document.body)
369 var $style = $('<style>body { padding-right: 42px; }</style>').appendTo('head')
371 $body.css('color', 'red')
373 $('<div id="modal-test"/>')
374 .on('hidden.bs.modal', function () {
375 assert.strictEqual($body[0].style.paddingRight, '', 'body does not have inline padding set')
376 assert.strictEqual($body[0].style.color, 'red', 'body still has other inline styles set')
377 $body.removeAttr('style')
381 .on('shown.bs.modal', function () {
382 $(this).bootstrapModal('hide')
384 .bootstrapModal('show')
387 QUnit.test('should properly restore non-pixel inline body padding after closing', function (assert) {
389 var done = assert.async()
390 var $body = $(document.body)
392 $body.css('padding-right', '5%')
394 $('<div id="modal-test"/>')
395 .on('hidden.bs.modal', function () {
396 assert.strictEqual($body[0].style.paddingRight, '5%', 'body does not have inline padding set')
397 $body.removeAttr('style')
400 .on('shown.bs.modal', function () {
401 $(this).bootstrapModal('hide')
403 .bootstrapModal('show')