]>
git.ipfire.org Git - ipfire.org.git/blob - src/scss/bootstrap-4.0.0-alpha.6/js/tests/unit/modal.js
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 throw explicit error on undefined method' , function ( assert
) {
29 var $ el
= $( '<div id="modal-test"/>' )
32 $ el
. bootstrapModal ( 'noMethod' )
35 assert
. strictEqual ( err
. message
, 'No method named "noMethod"' )
39 QUnit
. test ( 'should return jquery collection containing the element' , function ( assert
) {
41 var $ el
= $( '<div id="modal-test"/>' )
42 var $ modal
= $ el
. bootstrapModal ()
43 assert
. ok ($ modal
instanceof $, 'returns jquery collection' )
44 assert
. strictEqual ($ modal
[ 0 ], $ el
[ 0 ], 'collection contains element' )
47 QUnit
. test ( 'should expose defaults var for settings' , function ( assert
) {
49 assert
. ok ($. fn
. bootstrapModal
. Constructor
. Default
, 'default object exposed' )
52 QUnit
. test ( 'should insert into dom 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
. notEqual ($( '#modal-test' ). length
, 0 , 'modal inserted into dom' )
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 toggle when toggle is called' , function ( assert
) {
111 var done
= assert
. async ()
113 $( '<div id="modal-test"/>' )
114 . on ( 'shown.bs.modal' , function () {
115 assert
. ok ($( '#modal-test' ). is ( ':visible' ), 'modal visible' )
116 assert
. notEqual ($( '#modal-test' ). length
, 0 , 'modal inserted into dom' )
117 $( this ). bootstrapModal ( 'toggle' )
119 . on ( 'hidden.bs.modal' , function () {
120 assert
. ok (!$( '#modal-test' ). is ( ':visible' ), 'modal hidden' )
123 . bootstrapModal ( 'toggle' )
126 QUnit
. test ( 'should remove from dom when click [data-dismiss="modal"]' , function ( assert
) {
128 var done
= assert
. async ()
130 $( '<div id="modal-test"><span class="close" data-dismiss="modal"/></div>' )
131 . on ( 'shown.bs.modal' , function () {
132 assert
. ok ($( '#modal-test' ). is ( ':visible' ), 'modal visible' )
133 assert
. notEqual ($( '#modal-test' ). length
, 0 , 'modal inserted into dom' )
134 $( this ). find ( '.close' ). trigger ( 'click' )
136 . on ( 'hidden.bs.modal' , function () {
137 assert
. ok (!$( '#modal-test' ). is ( ':visible' ), 'modal hidden' )
140 . bootstrapModal ( 'toggle' )
143 QUnit
. test ( 'should allow modal close with "backdrop:false"' , function ( assert
) {
145 var done
= assert
. async ()
147 $( '<div id="modal-test" data-backdrop="false"/>' )
148 . on ( 'shown.bs.modal' , function () {
149 assert
. ok ($( '#modal-test' ). is ( ':visible' ), 'modal visible' )
150 $( this ). bootstrapModal ( 'hide' )
152 . on ( 'hidden.bs.modal' , function () {
153 assert
. ok (!$( '#modal-test' ). is ( ':visible' ), 'modal hidden' )
156 . bootstrapModal ( 'show' )
159 QUnit
. test ( 'should close modal when clicking outside of modal-content' , function ( assert
) {
161 var done
= assert
. async ()
163 $( '<div id="modal-test"><div class="contents"/></div>' )
164 . on ( 'shown.bs.modal' , function () {
165 assert
. notEqual ($( '#modal-test' ). length
, 0 , 'modal inserted into dom' )
166 $( '.contents' ). trigger ( 'click' )
167 assert
. ok ($( '#modal-test' ). is ( ':visible' ), 'modal visible' )
168 $( '#modal-test' ). trigger ( 'click' )
170 . on ( 'hidden.bs.modal' , function () {
171 assert
. ok (!$( '#modal-test' ). is ( ':visible' ), 'modal hidden' )
174 . bootstrapModal ( 'show' )
177 QUnit
. test ( 'should not close modal when clicking outside of modal-content if data-backdrop="true"' , function ( assert
) {
179 var done
= assert
. async ()
181 $( '<div id="modal-test" data-backdrop="false"><div class="contents"/></div>' )
182 . on ( 'shown.bs.modal' , function () {
183 $( '#modal-test' ). trigger ( 'click' )
184 assert
. ok ($( '#modal-test' ). is ( ':visible' ), 'modal not hidden' )
187 . bootstrapModal ( 'show' )
190 QUnit
. test ( 'should close modal when escape key is pressed via keydown' , function ( assert
) {
192 var done
= assert
. async ()
194 var $ div
= $( '<div id="modal-test"/>' )
196 . on ( 'shown.bs.modal' , function () {
197 assert
. ok ($( '#modal-test' ). length
, 'modal inserted into dom' )
198 assert
. ok ($( '#modal-test' ). is ( ':visible' ), 'modal visible' )
199 $ div
. trigger ($. Event ( 'keydown' , { which
: 27 }))
201 setTimeout ( function () {
202 assert
. ok (!$( '#modal-test' ). is ( ':visible' ), 'modal hidden' )
207 . bootstrapModal ( 'show' )
210 QUnit
. test ( 'should not close modal when escape key is pressed via keyup' , function ( assert
) {
212 var done
= assert
. async ()
214 var $ div
= $( '<div id="modal-test"/>' )
216 . on ( 'shown.bs.modal' , function () {
217 assert
. ok ($( '#modal-test' ). length
, 'modal inserted into dom' )
218 assert
. ok ($( '#modal-test' ). is ( ':visible' ), 'modal visible' )
219 $ div
. trigger ($. Event ( 'keyup' , { which
: 27 }))
221 setTimeout ( function () {
222 assert
. ok ($ div
. is ( ':visible' ), 'modal still visible' )
227 . bootstrapModal ( 'show' )
230 QUnit
. test ( 'should trigger hide event once when clicking outside of modal-content' , function ( assert
) {
232 var done
= assert
. async ()
236 $( '<div id="modal-test"><div class="contents"/></div>' )
237 . on ( 'shown.bs.modal' , function () {
239 $( '#modal-test' ). trigger ( 'click' )
241 . on ( 'hide.bs.modal' , function () {
243 assert
. strictEqual ( triggered
, 1 , 'modal hide triggered once' )
246 . bootstrapModal ( 'show' )
249 QUnit
. test ( 'should remove aria-hidden attribute when shown, add it back when hidden' , function ( assert
) {
251 var done
= assert
. async ()
253 $( '<div id="modal-test" aria-hidden="true"/>' )
254 . on ( 'shown.bs.modal' , function () {
255 assert
. notOk ($( '#modal-test' ). is ( '[aria-hidden]' ), 'aria-hidden attribute removed' )
256 $( this ). bootstrapModal ( 'hide' )
258 . on ( 'hidden.bs.modal' , function () {
259 assert
. ok ($( '#modal-test' ). is ( '[aria-hidden]' ), 'aria-hidden attribute added' )
260 assert
. strictEqual ($( '#modal-test' ). attr ( 'aria-hidden' ), 'true' , 'correct aria-hidden="true" added' )
263 . bootstrapModal ( 'show' )
266 QUnit
. test ( 'should close reopened modal with [data-dismiss="modal"] click' , function ( assert
) {
268 var done
= assert
. async ()
270 $( '<div id="modal-test"><div class="contents"><div id="close" data-dismiss="modal"/></div></div>' )
271 . one ( 'shown.bs.modal' , function () {
272 $( '#close' ). trigger ( 'click' )
274 . one ( 'hidden.bs.modal' , function () {
275 // after one open-close cycle
276 assert
. ok (!$( '#modal-test' ). is ( ':visible' ), 'modal hidden' )
278 . one ( 'shown.bs.modal' , function () {
279 $( '#close' ). trigger ( 'click' )
281 . one ( 'hidden.bs.modal' , function () {
282 assert
. ok (!$( '#modal-test' ). is ( ':visible' ), 'modal hidden' )
285 . bootstrapModal ( 'show' )
287 . bootstrapModal ( 'show' )
290 QUnit
. test ( 'should restore focus to toggling element when modal is hidden after having been opened via data-api' , function ( assert
) {
292 var done
= assert
. async ()
294 var $ toggleBtn
= $( '<button data-toggle="modal" data-target="#modal-test"/>' ). appendTo ( '#qunit-fixture' )
296 $( '<div id="modal-test"><div class="contents"><div id="close" data-dismiss="modal"/></div></div>' )
297 . on ( 'hidden.bs.modal' , function () {
298 setTimeout ( function () {
299 assert
. ok ($( document
. activeElement
). is ($ toggleBtn
), 'toggling element is once again focused' )
303 . on ( 'shown.bs.modal' , function () {
304 $( '#close' ). trigger ( 'click' )
306 . appendTo ( '#qunit-fixture' )
308 $ toggleBtn
. trigger ( 'click' )
311 QUnit
. test ( 'should not restore focus to toggling element if the associated show event gets prevented' , function ( assert
) {
313 var done
= assert
. async ()
314 var $ toggleBtn
= $( '<button data-toggle="modal" data-target="#modal-test"/>' ). appendTo ( '#qunit-fixture' )
315 var $ otherBtn
= $( '<button id="other-btn"/>' ). appendTo ( '#qunit-fixture' )
317 $( '<div id="modal-test"><div class="contents"><div id="close" data-dismiss="modal"/></div>' )
318 . one ( 'show.bs.modal' , function ( e
) {
320 $ otherBtn
. trigger ( 'focus' )
321 setTimeout ($. proxy ( function () {
322 $( this ). bootstrapModal ( 'show' )
325 . on ( 'hidden.bs.modal' , function () {
326 setTimeout ( function () {
327 assert
. ok ($( document
. activeElement
). is ($ otherBtn
), 'focus returned to toggling element' )
331 . on ( 'shown.bs.modal' , function () {
332 $( '#close' ). trigger ( 'click' )
334 . appendTo ( '#qunit-fixture' )
336 $ toggleBtn
. trigger ( 'click' )
339 QUnit
. test ( 'should restore inline body padding after closing' , function ( assert
) {
341 var done
= assert
. async ()
342 var originalBodyPad
= 0
343 var $ body
= $( document
. body
)
345 $ body
. css ( 'padding-right' , originalBodyPad
)
347 $( '<div id="modal-test"/>' )
348 . on ( 'hidden.bs.modal' , function () {
349 var currentBodyPad
= parseInt ($ body
. css ( 'padding-right' ), 10 )
350 assert
. notStrictEqual ($ body
. attr ( 'style' ), '' , 'body has non-empty style attribute' )
351 assert
. strictEqual ( currentBodyPad
, originalBodyPad
, 'original body padding was not changed' )
352 $ body
. removeAttr ( 'style' )
355 . on ( 'shown.bs.modal' , function () {
356 $( this ). bootstrapModal ( 'hide' )
358 . bootstrapModal ( 'show' )
361 QUnit
. test ( 'should ignore values set via CSS when trying to restore body padding after closing' , function ( assert
) {
363 var done
= assert
. async ()
364 var $ body
= $( document
. body
)
365 var $ style
= $( '<style>body { padding-right: 42px; }</style>' ). appendTo ( 'head' )
367 $( '<div id="modal-test"/>' )
368 . on ( 'hidden.bs.modal' , function () {
369 assert
. ok (!$ body
. attr ( 'style' ), 'body does not have inline padding set' )
373 . on ( 'shown.bs.modal' , function () {
374 $( this ). bootstrapModal ( 'hide' )
376 . bootstrapModal ( 'show' )
379 QUnit
. test ( 'should have a paddingRight when the modal is taller than the viewport' , function ( assert
) {
381 var done
= assert
. async ()
382 $( '<div class="fixed-top fixed-bottom sticky-top is-fixed">@Johann-S</div>' ). appendTo ( '#qunit-fixture' )
383 $( '.fixed-top, .fixed-bottom, .is-fixed, .sticky-top' ). css ( 'padding-right' , '10px' )
385 $( '<div id="modal-test"/>' )
386 . on ( 'shown.bs.modal' , function () {
387 var paddingRight
= parseInt ($( document
. body
). css ( 'padding-right' ), 10 )
388 assert
. strictEqual ( isNaN ( paddingRight
), false )
389 assert
. strictEqual ( paddingRight
!== 0 , true )
390 $( document
. body
). css ( 'padding-right' , '' ) // Because test case "should ignore other inline styles when trying to restore body padding after closing" fail if not
393 . bootstrapModal ( 'show' )
396 QUnit
. test ( 'should remove padding-right on modal after closing' , function ( assert
) {
398 var done
= assert
. async ()
399 $( '<div class="fixed-top fixed-bottom is-fixed sticky-top">@Johann-S</div>' ). appendTo ( '#qunit-fixture' )
400 $( '.fixed-top, .fixed-bottom, .is-fixed, .sticky-top' ). css ( 'padding-right' , '10px' )
402 $( '<div id="modal-test"/>' )
403 . on ( 'shown.bs.modal' , function () {
404 var paddingRight
= parseInt ($( document
. body
). css ( 'padding-right' ), 10 )
405 assert
. strictEqual ( isNaN ( paddingRight
), false )
406 assert
. strictEqual ( paddingRight
!== 0 , true )
407 $( this ). bootstrapModal ( 'hide' )
409 . on ( 'hidden.bs.modal' , function () {
410 var paddingRight
= parseInt ($( document
. body
). css ( 'padding-right' ), 10 )
411 assert
. strictEqual ( paddingRight
, 0 )
414 . bootstrapModal ( 'show' )
417 QUnit
. test ( 'should ignore other inline styles when trying to restore body padding after closing' , function ( assert
) {
419 var done
= assert
. async ()
420 var $ body
= $( document
. body
)
421 var $ style
= $( '<style>body { padding-right: 42px; }</style>' ). appendTo ( 'head' )
423 $ body
. css ( 'color' , 'red' )
425 $( '<div id="modal-test"/>' )
426 . on ( 'hidden.bs.modal' , function () {
427 assert
. strictEqual ($ body
[ 0 ]. style
. paddingRight
, '' , 'body does not have inline padding set' )
428 assert
. strictEqual ($ body
[ 0 ]. style
. color
, 'red' , 'body still has other inline styles set' )
429 $ body
. removeAttr ( 'style' )
433 . on ( 'shown.bs.modal' , function () {
434 $( this ). bootstrapModal ( 'hide' )
436 . bootstrapModal ( 'show' )
439 QUnit
. test ( 'should properly restore non-pixel inline body padding after closing' , function ( assert
) {
441 var done
= assert
. async ()
442 var $ body
= $( document
. body
)
444 $ body
. css ( 'padding-right' , '5%' )
446 $( '<div id="modal-test"/>' )
447 . on ( 'hidden.bs.modal' , function () {
448 assert
. strictEqual ($ body
[ 0 ]. style
. paddingRight
, '5%' , 'body does not have inline padding set' )
449 $ body
. removeAttr ( 'style' )
452 . on ( 'shown.bs.modal' , function () {
453 $( this ). bootstrapModal ( 'hide' )
455 . bootstrapModal ( 'show' )
458 QUnit
. test ( 'should not follow link in area tag' , function ( assert
) {
460 var done
= assert
. async ()
462 $( '<map><area id="test" shape="default" data-toggle="modal" data-target="#modal-test" href="demo.html"/></map>' )
463 . appendTo ( '#qunit-fixture' )
465 $( '<div id="modal-test"><div class="contents"><div id="close" data-dismiss="modal"/></div></div>' )
466 . appendTo ( '#qunit-fixture' )
469 . on ( 'click.bs.modal.data-api' , function ( event
) {
470 assert
. notOk ( event
. isDefaultPrevented (), 'navigating to href will happen' )
472 setTimeout ( function () {
473 assert
. ok ( event
. isDefaultPrevented (), 'model shown instead of navigating to href' )