@@ -9,6 +9,10 @@ $(function () {
9
9
} )
10
10
11
11
QUnit . module ( 'modal' , {
12
+ before : function ( ) {
13
+ // Enable the scrollbar measurer
14
+ $ ( '<style type="text/css"> .modal-scrollbar-measure { position: absolute; top: -9999px; width: 50px; height: 50px; overflow: scroll; } </style>' ) . appendTo ( 'head' )
15
+ } ,
12
16
beforeEach : function ( ) {
13
17
// Run all tests in noConflict mode -- it's the only way to ensure that the plugin works in noConflict mode
14
18
$ . fn . bootstrapModal = $ . fn . modal . noConflict ( )
@@ -336,81 +340,144 @@ $(function () {
336
340
$toggleBtn . trigger ( 'click' )
337
341
} )
338
342
339
- QUnit . test ( 'should restore inline body padding after closing' , function ( assert ) {
343
+ QUnit . test ( 'should adjust the inline body padding when opening and restore when closing' , function ( assert ) {
340
344
assert . expect ( 2 )
341
345
var done = assert . async ( )
342
- var originalBodyPad = 0
343
346
var $body = $ ( document . body )
344
-
345
- $body . css ( 'padding-right' , originalBodyPad )
347
+ var originalPadding = $body . css ( 'padding-right' )
346
348
347
349
$ ( '<div id="modal-test"/>' )
348
350
. 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' )
351
+ var currentPadding = $body . css ( 'padding-right' )
352
+ assert . strictEqual ( currentPadding , originalPadding , 'body padding should be reset after closing' )
352
353
$body . removeAttr ( 'style' )
353
354
done ( )
354
355
} )
355
356
. on ( 'shown.bs.modal' , function ( ) {
357
+ var currentPadding = $body . css ( 'padding-right' )
358
+ assert . notStrictEqual ( currentPadding , originalPadding , 'body padding should be adjusted while opening' )
356
359
$ ( this ) . bootstrapModal ( 'hide' )
357
360
} )
358
361
. bootstrapModal ( 'show' )
359
362
} )
360
363
361
- QUnit . test ( 'should ignore values set via CSS when trying to restore body padding after closing ' , function ( assert ) {
362
- assert . expect ( 1 )
364
+ QUnit . test ( 'should store the original body padding in data- padding-right before showing ' , function ( assert ) {
365
+ assert . expect ( 2 )
363
366
var done = assert . async ( )
364
367
var $body = $ ( document . body )
365
- var $style = $ ( '<style>body { padding-right: 42px; }</style>' ) . appendTo ( 'head' )
368
+ var originalPadding = '0px'
369
+ $body . css ( 'padding-right' , originalPadding )
366
370
367
371
$ ( '<div id="modal-test"/>' )
368
372
. on ( 'hidden.bs.modal' , function ( ) {
369
- assert . ok ( ! $body . attr ( 'style ') , 'body does not have inline padding set ')
370
- $style . remove ( )
373
+ assert . strictEqual ( $body . data ( 'padding-right ') , undefined , 'data-padding-right should be cleared after closing ')
374
+ $body . removeAttr ( 'style' )
371
375
done ( )
372
376
} )
373
377
. on ( 'shown.bs.modal' , function ( ) {
378
+ assert . strictEqual ( $body . data ( 'padding-right' ) , originalPadding , 'original body padding should be stored in data-padding-right' )
374
379
$ ( this ) . bootstrapModal ( 'hide' )
375
380
} )
376
381
. bootstrapModal ( 'show' )
377
382
} )
378
383
379
- QUnit . test ( 'should have a paddingRight when the modal is taller than the viewport ' , function ( assert ) {
384
+ QUnit . test ( 'should adjust the inline padding of fixed elements when opening and restore when closing ' , function ( assert ) {
380
385
assert . expect ( 2 )
381
386
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 ')
387
+ var $element = $ ( '<div class="fixed-top"> </div>' ) . appendTo ( '#qunit-fixture' )
388
+ var originalPadding = $element . css ( 'padding-right' )
384
389
385
390
$ ( '<div id="modal-test"/>' )
391
+ . on ( 'hidden.bs.modal' , function ( ) {
392
+ var currentPadding = $element . css ( 'padding-right' )
393
+ assert . strictEqual ( currentPadding , originalPadding , 'fixed element padding should be reset after closing' )
394
+ $element . remove ( )
395
+ done ( )
396
+ } )
386
397
. 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
398
+ var currentPadding = $element . css ( 'padding-right' )
399
+ assert . notStrictEqual ( currentPadding , originalPadding , 'fixed element padding should be adjusted while opening' )
400
+ $ ( this ) . bootstrapModal ( 'hide' )
401
+ } )
402
+ . bootstrapModal ( 'show' )
403
+ } )
404
+
405
+ QUnit . test ( 'should store the original padding of fixed elements in data-padding-right before showing' , function ( assert ) {
406
+ assert . expect ( 2 )
407
+ var done = assert . async ( )
408
+ var $element = $ ( '<div class="fixed-top"></div>' ) . appendTo ( '#qunit-fixture' )
409
+ var originalPadding = '0px'
410
+ $element . css ( 'padding-right' , originalPadding )
411
+
412
+ $ ( '<div id="modal-test"/>' )
413
+ . on ( 'hidden.bs.modal' , function ( ) {
414
+ assert . strictEqual ( $element . data ( 'padding-right' ) , undefined , 'data-padding-right should be cleared after closing' )
415
+ $element . remove ( )
391
416
done ( )
392
417
} )
418
+ . on ( 'shown.bs.modal' , function ( ) {
419
+ assert . strictEqual ( $element . data ( 'padding-right' ) , originalPadding , 'original fixed element padding should be stored in data-padding-right' )
420
+ $ ( this ) . bootstrapModal ( 'hide' )
421
+ } )
393
422
. bootstrapModal ( 'show' )
394
423
} )
395
424
396
- QUnit . test ( 'should remove padding-right on modal after closing' , function ( assert ) {
397
- assert . expect ( 3 )
425
+ QUnit . test ( 'should adjust the inline margin of the navbar-toggler when opening and restore when closing' , function ( assert ) {
426
+ assert . expect ( 2 )
427
+ var done = assert . async ( )
428
+ var $element = $ ( '<div class="navbar-toggler"></div>' ) . appendTo ( '#qunit-fixture' )
429
+ var originalMargin = $element . css ( 'margin-right' )
430
+
431
+ $ ( '<div id="modal-test"/>' )
432
+ . on ( 'hidden.bs.modal' , function ( ) {
433
+ var currentMargin = $element . css ( 'margin-right' )
434
+ assert . strictEqual ( currentMargin , originalMargin , 'navbar-toggler margin should be reset after closing' )
435
+ $element . remove ( )
436
+ done ( )
437
+ } )
438
+ . on ( 'shown.bs.modal' , function ( ) {
439
+ var currentMargin = $element . css ( 'margin-right' )
440
+ assert . notStrictEqual ( currentMargin , originalMargin , 'navbar-toggler margin should be adjusted while opening' )
441
+ $ ( this ) . bootstrapModal ( 'hide' )
442
+ } )
443
+ . bootstrapModal ( 'show' )
444
+ } )
445
+
446
+ QUnit . test ( 'should store the original margin of the navbar-toggler in data-margin-right before showing' , function ( assert ) {
447
+ assert . expect ( 2 )
398
448
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' )
449
+ var $element = $ ( '<div class="navbar-toggler"></div>' ) . appendTo ( '#qunit-fixture' )
450
+ var originalMargin = '0px'
451
+ $element . css ( 'margin-right' , originalMargin )
401
452
402
453
$ ( '<div id="modal-test"/>' )
454
+ . on ( 'hidden.bs.modal' , function ( ) {
455
+ assert . strictEqual ( $element . data ( 'margin-right' ) , undefined , 'data-margin-right should be cleared after closing' )
456
+ $element . remove ( )
457
+ done ( )
458
+ } )
403
459
. 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 )
460
+ assert . strictEqual ( $element . data ( 'margin-right' ) , originalMargin , 'original navbar-toggler margin should be stored in data-margin-right' )
407
461
$ ( this ) . bootstrapModal ( 'hide' )
408
462
} )
463
+ . bootstrapModal ( 'show' )
464
+ } )
465
+
466
+ QUnit . test ( 'should ignore values set via CSS when trying to restore body padding after closing' , function ( assert ) {
467
+ assert . expect ( 1 )
468
+ var done = assert . async ( )
469
+ var $body = $ ( document . body )
470
+ var $style = $ ( '<style>body { padding-right: 42px; }</style>' ) . appendTo ( 'head' )
471
+
472
+ $ ( '<div id="modal-test"/>' )
409
473
. on ( 'hidden.bs.modal' , function ( ) {
410
- var paddingRight = parseInt ( $ ( document . body ) . css ( 'padding-right ') , 10 )
411
- assert . strictEqual ( paddingRight , 0 )
474
+ assert . ok ( ! $ body. attr ( 'style ') , 'body does not have inline padding set' )
475
+ $style . remove ( )
412
476
done ( )
413
477
} )
478
+ . on ( 'shown.bs.modal' , function ( ) {
479
+ $ ( this ) . bootstrapModal ( 'hide' )
480
+ } )
414
481
. bootstrapModal ( 'show' )
415
482
} )
416
483
0 commit comments