Skip to content

Commit 0b78f46

Browse files
Andrew LucaJohann-S
authored andcommitted
test: ensure .navbar-fixed-* padding on modal open
1 parent 5003e6c commit 0b78f46

File tree

1 file changed

+76
-0
lines changed

1 file changed

+76
-0
lines changed

js/tests/unit/modal.js

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,4 +387,80 @@ $(function () {
387387
})
388388
.bootstrapModal('show')
389389
})
390+
391+
QUnit.test('should add padding-right of scrollbar width to .navbar-fixed-top and .navbar-fixed-bottom before open', function (assert) {
392+
assert.expect(2)
393+
var Modal = $.fn.bootstrapModal.Constructor
394+
var done = assert.async()
395+
var $body = $(document.body)
396+
var scrollbarWidth
397+
398+
// simulate overflow scroll
399+
$body.css({ height: '2000px' })
400+
401+
var $fixedTop = $('<nav class="navbar navbar-fixed-top" />').appendTo($body)
402+
var $fixedBottom = $('<nav class="navbar navbar-fixed-bottom" />').appendTo($body)
403+
404+
// patch setScrollbar function to get scrollbar width
405+
var setScrollbar = Modal.prototype.setScrollbar
406+
Modal.prototype.setScrollbar = function () {
407+
setScrollbar.call(this)
408+
scrollbarWidth = this.scrollbarWidth + 'px'
409+
}
410+
411+
$('<div id="modal-test"/>')
412+
.on('hidden.bs.modal', function () {
413+
$fixedTop.remove()
414+
$fixedBottom.remove()
415+
$body.removeAttr('style')
416+
// restore original setScrollbar
417+
Modal.prototype.setScrollbar = setScrollbar
418+
done()
419+
})
420+
.on('shown.bs.modal', function () {
421+
var fixedTopPaddingRight = $fixedTop.css('padding-right')
422+
var fixedBottomPaddingRight = $fixedBottom.css('padding-right')
423+
424+
assert.strictEqual(scrollbarWidth, fixedTopPaddingRight,
425+
'.navbar-fixed-top has padding-right (' + fixedTopPaddingRight + ') equal to scrollbar width (' + scrollbarWidth + ')')
426+
427+
assert.strictEqual(scrollbarWidth, fixedBottomPaddingRight,
428+
'.navbar-fixed-bottom has padding-right (' + fixedBottomPaddingRight + ') equal to scrollbar width (' + scrollbarWidth + ')')
429+
430+
$(this).bootstrapModal('hide')
431+
})
432+
.bootstrapModal('show')
433+
})
434+
435+
QUnit.test('should restore inline padding-right for .navbar-fixed-top and .navbar-fixed-bottom after close', function (assert) {
436+
assert.expect(2)
437+
var done = assert.async()
438+
var $body = $(document.body)
439+
440+
var $styleshhet = $('<link rel="stylesheet" href="../../dist/css/bootstrap.css" />').appendTo(document.head)
441+
var $fixedTop = $('<nav class="navbar navbar-fixed-top" style="padding-right: 10px;" />').appendTo($body)
442+
var $fixedBottom = $('<nav class="navbar navbar-fixed-bottom" style="padding-right: 5%;" />').appendTo($body)
443+
444+
// simulate overflow scroll
445+
$body.css({ height: '2000px' })
446+
447+
$('<div id="modal-test"/>')
448+
.on('hidden.bs.modal', function () {
449+
assert.strictEqual($fixedTop.css('padding-right'), '10px',
450+
'.navbar-fixed-top has inline padding-right restored')
451+
452+
assert.strictEqual($fixedBottom[0].style.paddingRight, '5%',
453+
'.navbar-fixed-bottom has right padding-right restored')
454+
455+
$fixedTop.remove()
456+
$fixedBottom.remove()
457+
$body.removeAttr('style')
458+
$styleshhet.remove()
459+
done()
460+
})
461+
.on('shown.bs.modal', function () {
462+
$(this).bootstrapModal('hide')
463+
})
464+
.bootstrapModal('show')
465+
})
390466
})

0 commit comments

Comments
 (0)