$(function () {
'use strict'
window.Util = typeof bootstrap !== 'undefined' ? bootstrap.Util : Util
var Modal = typeof window.bootstrap !== 'undefined' ? window.bootstrap.Modal : window.Modal
QUnit.module('modal plugin')
QUnit.test('should be defined on jquery object', function (assert) {
assert.expect(1)
assert.ok($(document.body).modal, 'modal method is defined')
})
QUnit.module('modal', {
before: function () {
// Enable the scrollbar measurer
$('').appendTo('head')
// Function to calculate the scrollbar width which is then compared to the padding or margin changes
$.fn.getScrollbarWidth = $.fn.modal.Constructor.prototype._getScrollbarWidth
// Simulate scrollbars
$('html').css('padding-right', '16px')
},
beforeEach: function () {
// Run all tests in noConflict mode -- it's the only way to ensure that the plugin works in noConflict mode
$.fn.bootstrapModal = $.fn.modal.noConflict()
},
afterEach: function () {
$('.modal-backdrop, #modal-test').remove()
$(document.body).removeClass('modal-open')
$.fn.modal = $.fn.bootstrapModal
delete $.fn.bootstrapModal
$('#qunit-fixture').html('')
}
})
QUnit.test('should provide no conflict', function (assert) {
assert.expect(1)
assert.strictEqual(typeof $.fn.modal, 'undefined', 'modal was set back to undefined (orig value)')
})
QUnit.test('should throw explicit error on undefined method', function (assert) {
assert.expect(1)
var $el = $('
')
.one('show.bs.modal', function (e) {
e.preventDefault()
$otherBtn.trigger('focus')
setTimeout($.proxy(function () {
$(this).bootstrapModal('show')
}, this), 0)
})
.on('hidden.bs.modal', function () {
setTimeout(function () {
assert.ok($(document.activeElement).is($otherBtn), 'focus returned to toggling element')
done()
}, 0)
})
.on('shown.bs.modal', function () {
$('#close').trigger('click')
})
.appendTo('#qunit-fixture')
$toggleBtn.trigger('click')
})
QUnit.test('should adjust the inline padding of the modal when opening', function (assert) {
assert.expect(1)
var done = assert.async()
$('
')
.on('shown.bs.modal', function () {
var expectedPadding = $(this).getScrollbarWidth() + 'px'
var currentPadding = $(this).css('padding-right')
assert.strictEqual(currentPadding, expectedPadding, 'modal padding should be adjusted while opening')
done()
})
.bootstrapModal('show')
})
QUnit.test('should adjust the inline body padding when opening and restore when closing', function (assert) {
assert.expect(2)
var done = assert.async()
var $body = $(document.body)
var originalPadding = $body.css('padding-right')
$('
')
.on('hidden.bs.modal', function () {
var currentPadding = $body.css('padding-right')
assert.strictEqual(currentPadding, originalPadding, 'body padding should be reset after closing')
$body.removeAttr('style')
done()
})
.on('shown.bs.modal', function () {
var expectedPadding = parseFloat(originalPadding) + $(this).getScrollbarWidth() + 'px'
var currentPadding = $body.css('padding-right')
assert.strictEqual(currentPadding, expectedPadding, 'body padding should be adjusted while opening')
$(this).bootstrapModal('hide')
})
.bootstrapModal('show')
})
QUnit.test('should store the original body padding in data-padding-right before showing', function (assert) {
assert.expect(2)
var done = assert.async()
var $body = $(document.body)
var originalPadding = '0px'
$body.css('padding-right', originalPadding)
$('
')
.on('hidden.bs.modal', function () {
assert.strictEqual(document.body.getAttribute('data-padding-right'), null, 'data-padding-right should be cleared after closing')
$body.removeAttr('style')
done()
})
.on('shown.bs.modal', function () {
assert.strictEqual($body.data('padding-right'), originalPadding, 'original body padding should be stored in data-padding-right')
$(this).bootstrapModal('hide')
})
.bootstrapModal('show')
})
QUnit.test('should not adjust the inline body padding when it does not overflow', function (assert) {
assert.expect(1)
var done = assert.async()
var $body = $(document.body)
var originalPadding = $body.css('padding-right')
// Hide scrollbars to prevent the body overflowing
$body.css('overflow', 'hidden') // Real scrollbar (for in-browser testing)
$('html').css('padding-right', '0px') // Simulated scrollbar (for PhantomJS)
$('
')
.on('shown.bs.modal', function () {
var currentPadding = $body.css('padding-right')
assert.strictEqual(currentPadding, originalPadding, 'body padding should not be adjusted')
$(this).bootstrapModal('hide')
// Restore scrollbars
$body.css('overflow', 'auto')
$('html').css('padding-right', '16px')
done()
})
.bootstrapModal('show')
})
QUnit.test('should adjust the inline padding of fixed elements when opening and restore when closing', function (assert) {
assert.expect(2)
var done = assert.async()
var $element = $('
').appendTo('#qunit-fixture')
var originalPadding = $element.css('padding-right')
$('
')
.on('hidden.bs.modal', function () {
var currentPadding = $element.css('padding-right')
assert.strictEqual(currentPadding, originalPadding, 'fixed element padding should be reset after closing')
$element.remove()
done()
})
.on('shown.bs.modal', function () {
var expectedPadding = parseFloat(originalPadding) + $(this).getScrollbarWidth() + 'px'
var currentPadding = $element.css('padding-right')
assert.strictEqual(currentPadding, expectedPadding, 'fixed element padding should be adjusted while opening')
$(this).bootstrapModal('hide')
})
.bootstrapModal('show')
})
QUnit.test('should store the original padding of fixed elements in data-padding-right before showing', function (assert) {
assert.expect(2)
var done = assert.async()
var $element = $('
').appendTo('#qunit-fixture')
var originalPadding = '0px'
$element.css('padding-right', originalPadding)
$('
')
.on('hidden.bs.modal', function () {
assert.strictEqual($element[0].getAttribute('data-padding-right'), null, 'data-padding-right should be cleared after closing')
$element.remove()
done()
})
.on('shown.bs.modal', function () {
assert.strictEqual($element.data('padding-right'), originalPadding, 'original fixed element padding should be stored in data-padding-right')
$(this).bootstrapModal('hide')
})
.bootstrapModal('show')
})
QUnit.test('should adjust the inline margin of sticky elements when opening and restore when closing', function (assert) {
assert.expect(2)
var done = assert.async()
var $element = $('
').appendTo('#qunit-fixture')
var originalPadding = $element.css('margin-right')
$('
')
.on('hidden.bs.modal', function () {
var currentPadding = $element.css('margin-right')
assert.strictEqual(currentPadding, originalPadding, 'sticky element margin should be reset after closing')
$element.remove()
done()
})
.on('shown.bs.modal', function () {
var expectedPadding = parseFloat(originalPadding) - $(this).getScrollbarWidth() + 'px'
var currentPadding = $element.css('margin-right')
assert.strictEqual(currentPadding, expectedPadding, 'sticky element margin should be adjusted while opening')
$(this).bootstrapModal('hide')
})
.bootstrapModal('show')
})
QUnit.test('should store the original margin of sticky elements in data-margin-right before showing', function (assert) {
assert.expect(2)
var done = assert.async()
var $element = $('
').appendTo('#qunit-fixture')
var originalPadding = '0px'
$element.css('margin-right', originalPadding)
$('
')
.on('hidden.bs.modal', function () {
assert.strictEqual($element[0].getAttribute('data-margin-right'), null, 'data-margin-right should be cleared after closing')
$element.remove()
done()
})
.on('shown.bs.modal', function () {
assert.strictEqual($element.data('margin-right'), originalPadding, 'original sticky element margin should be stored in data-margin-right')
$(this).bootstrapModal('hide')
})
.bootstrapModal('show')
})
QUnit.test('should ignore values set via CSS when trying to restore body padding after closing', function (assert) {
assert.expect(1)
var done = assert.async()
var $body = $(document.body)
var $style = $('').appendTo('head')
$('
')
.on('hidden.bs.modal', function () {
assert.strictEqual($body.css('padding-left'), '0px', 'body does not have inline padding set')
$style.remove()
done()
})
.on('shown.bs.modal', function () {
$(this).bootstrapModal('hide')
})
.bootstrapModal('show')
})
QUnit.test('should ignore other inline styles when trying to restore body padding after closing', function (assert) {
assert.expect(2)
var done = assert.async()
var $body = $(document.body)
var $style = $('').appendTo('head')
$body.css('color', 'red')
$('
')
.on('hidden.bs.modal', function () {
assert.strictEqual($body[0].style.paddingRight, '', 'body does not have inline padding set')
assert.strictEqual($body[0].style.color, 'red', 'body still has other inline styles set')
$body.removeAttr('style')
$style.remove()
done()
})
.on('shown.bs.modal', function () {
$(this).bootstrapModal('hide')
})
.bootstrapModal('show')
})
QUnit.test('should properly restore non-pixel inline body padding after closing', function (assert) {
assert.expect(1)
var done = assert.async()
var $body = $(document.body)
$body.css('padding-right', '5%')
$('
')
.on('hidden.bs.modal', function () {
assert.strictEqual($body[0].style.paddingRight, '5%', 'body does not have inline padding set')
$body.removeAttr('style')
done()
})
.on('shown.bs.modal', function () {
$(this).bootstrapModal('hide')
})
.bootstrapModal('show')
})
QUnit.test('should not follow link in area tag', function (assert) {
assert.expect(2)
$('
')
.appendTo('#qunit-fixture')
$('
')
.appendTo('#qunit-fixture')
// We need to use CustomEvent here to have a working preventDefault in IE tests.
var evt = new CustomEvent('click', {
bubbles: true,
cancelable: true
})
$('#test')
.on('click.bs.modal.data-api', function (event) {
assert.notOk(event.defaultPrevented, 'navigating to href will happen')
})
$('#test')[0].dispatchEvent(evt)
assert.ok(evt.defaultPrevented, 'model shown instead of navigating to href')
})
QUnit.test('should not try to open a modal which is already visible', function (assert) {
assert.expect(1)
var done = assert.async()
var count = 0
$('
').on('shown.bs.modal', function () {
count++
}).on('hidden.bs.modal', function () {
assert.strictEqual(count, 1, 'show() runs only once')
done()
})
.bootstrapModal('show')
.bootstrapModal('show')
.bootstrapModal('hide')
})
QUnit.test('transition duration should be the modal-dialog duration before triggering shown event', function (assert) {
assert.expect(1)
var done = assert.async()
var style = [
''
].join('')
var $style = $(style).appendTo('head')
var modalHTML = [
'
'
].join('')
var $modal = $(modalHTML).appendTo('#qunit-fixture')
var expectedTransitionDuration = 300
var spy = sinon.spy(Util, 'getTransitionDurationFromElement')
$modal.on('shown.bs.modal', function () {
assert.ok(spy.returned(expectedTransitionDuration))
$style.remove()
spy.restore()
done()
})
.bootstrapModal('show')
})
QUnit.test('should dispose modal', function (assert) {
assert.expect(2)
var done = assert.async()
var $modal = $([
'
'
].join('')).appendTo('#qunit-fixture')
$modal.on('shown.bs.modal', function () {
var modal = Modal._getInstance($modal[0])
var spy = sinon.spy($modal[0], 'removeEventListener')
modal.dispose()
assert.ok(!Modal._getInstance($modal[0]), 'modal data object was disposed')
assert.ok(spy.called)
done()
}).bootstrapModal('show')
})
QUnit.test('should enforce focus', function (assert) {
assert.expect(2)
var done = assert.async()
var $modal = $([
'
'
].join(''))
.bootstrapModal()
.appendTo('#qunit-fixture')
var modal = Modal._getInstance($modal[0])
var spy = sinon.spy(modal, '_enforceFocus')
$modal.one('shown.bs.modal', function () {
assert.ok(spy.called, '_enforceFocus called')
var spyFocus = sinon.spy(modal._element, 'focus')
function focusInListener() {
assert.ok(spyFocus.called)
document.removeEventListener('focusin', focusInListener)
done()
}
document.addEventListener('focusin', focusInListener)
var focusInEvent = new Event('focusin')
Object.defineProperty(focusInEvent, 'target', {
value: $('#qunit-fixture')[0]
})
document.dispatchEvent(focusInEvent)
})
.bootstrapModal('show')
})
QUnit.test('should scroll to top of the modal body if the modal has .modal-dialog-scrollable class', function (assert) {
assert.expect(2)
var done = assert.async()
var $modal = $([
'
'
].join('')).appendTo('#qunit-fixture')
var $modalBody = $('.modal-body')
$modalBody.scrollTop(100)
assert.strictEqual($modalBody.scrollTop(), 100)
$modal.on('shown.bs.modal', function () {
assert.strictEqual($modalBody.scrollTop(), 0, 'modal body scrollTop should be 0 when opened')
done()
})
.bootstrapModal('show')
})
})