2019-10-02 11:43:54 +02:00
import Modal from '../../src/modal'
import EventHandler from '../../src/dom/event-handler'
2021-04-11 08:37:59 +02:00
import { getWidth as getScrollBarWidth } from '../../src/util/scrollbar'
2019-05-01 15:43:40 +02:00
/** Test helpers */
2021-04-25 05:50:16 +02:00
import { clearBodyAndDocument , clearFixture , createEvent , getFixture , jQueryMock } from '../helpers/fixture'
2019-05-01 15:43:40 +02:00
describe ( 'Modal' , ( ) => {
let fixtureEl
beforeAll ( ( ) => {
fixtureEl = getFixture ( )
} )
afterEach ( ( ) => {
clearFixture ( )
2021-04-25 05:50:16 +02:00
clearBodyAndDocument ( )
2019-05-01 15:43:40 +02:00
document . body . classList . remove ( 'modal-open' )
2020-03-25 15:35:02 +01:00
document . querySelectorAll ( '.modal-backdrop' )
. forEach ( backdrop => {
document . body . removeChild ( backdrop )
} )
2019-05-01 15:43:40 +02:00
} )
2021-04-12 01:57:53 +02:00
beforeEach ( ( ) => {
2021-04-25 05:50:16 +02:00
clearBodyAndDocument ( )
2019-05-01 15:43:40 +02:00
} )
describe ( 'VERSION' , ( ) => {
it ( 'should return plugin version' , ( ) => {
expect ( Modal . VERSION ) . toEqual ( jasmine . any ( String ) )
} )
} )
describe ( 'Default' , ( ) => {
it ( 'should return plugin default config' , ( ) => {
expect ( Modal . Default ) . toEqual ( jasmine . any ( Object ) )
} )
} )
2021-02-16 07:58:08 +01:00
describe ( 'DATA_KEY' , ( ) => {
it ( 'should return plugin data key' , ( ) => {
expect ( Modal . DATA _KEY ) . toEqual ( 'bs.modal' )
} )
} )
2021-02-22 08:01:04 +01:00
describe ( 'constructor' , ( ) => {
it ( 'should take care of element either passed as a CSS selector or DOM element' , ( ) => {
fixtureEl . innerHTML = '<div class="modal"><div class="modal-dialog"></div></div>'
const modalEl = fixtureEl . querySelector ( '.modal' )
const modalBySelector = new Modal ( '.modal' )
const modalByElement = new Modal ( modalEl )
expect ( modalBySelector . _element ) . toEqual ( modalEl )
expect ( modalByElement . _element ) . toEqual ( modalEl )
} )
} )
2019-05-01 15:43:40 +02:00
describe ( 'toggle' , ( ) => {
it ( 'should toggle a modal' , done => {
2020-05-11 15:54:56 +02:00
fixtureEl . innerHTML = '<div class="modal"><div class="modal-dialog"></div></div>'
2019-05-01 15:43:40 +02:00
2021-04-11 08:37:59 +02:00
document . documentElement . style . overflowY = 'scroll'
2019-05-01 15:43:40 +02:00
const modalEl = fixtureEl . querySelector ( '.modal' )
const modal = new Modal ( modalEl )
const originalPadding = '0px'
document . body . style . paddingRight = originalPadding
modalEl . addEventListener ( 'shown.bs.modal' , ( ) => {
2020-07-22 21:33:11 +02:00
expect ( document . body . getAttribute ( 'data-bs-padding-right' ) ) . toEqual ( originalPadding , 'original body padding should be stored in data-bs-padding-right' )
2019-05-01 15:43:40 +02:00
modal . toggle ( )
} )
modalEl . addEventListener ( 'hidden.bs.modal' , ( ) => {
2020-07-22 21:33:11 +02:00
expect ( document . body . getAttribute ( 'data-bs-padding-right' ) ) . toBeNull ( )
2019-05-01 15:43:40 +02:00
expect ( ) . nothing ( )
2021-04-11 08:37:59 +02:00
document . documentElement . style . overflowY = 'auto'
2019-05-01 15:43:40 +02:00
done ( )
} )
modal . toggle ( )
} )
it ( 'should adjust the inline padding of fixed elements when opening and restore when closing' , done => {
fixtureEl . innerHTML = [
'<div class="fixed-top" style="padding-right: 0px"></div>' ,
2020-05-11 15:54:56 +02:00
'<div class="modal"><div class="modal-dialog"></div></div>'
2019-05-01 15:43:40 +02:00
] . join ( '' )
2021-04-11 08:37:59 +02:00
document . documentElement . style . overflowY = 'scroll'
2019-05-01 15:43:40 +02:00
const fixedEl = fixtureEl . querySelector ( '.fixed-top' )
2020-05-02 15:49:33 +02:00
const originalPadding = Number . parseInt ( window . getComputedStyle ( fixedEl ) . paddingRight , 10 )
2019-05-01 15:43:40 +02:00
const modalEl = fixtureEl . querySelector ( '.modal' )
const modal = new Modal ( modalEl )
2021-04-19 05:55:22 +02:00
const scrollBarWidth = getScrollBarWidth ( )
2019-05-01 15:43:40 +02:00
modalEl . addEventListener ( 'shown.bs.modal' , ( ) => {
2021-04-19 05:55:22 +02:00
const expectedPadding = originalPadding + scrollBarWidth
const currentPadding = Number . parseInt ( window . getComputedStyle ( fixedEl ) . paddingRight , 10 )
2019-05-01 15:43:40 +02:00
2021-04-12 01:57:53 +02:00
expect ( fixedEl . getAttribute ( 'data-bs-padding-right' ) ) . toEqual ( ` ${ originalPadding } px ` , 'original fixed element padding should be stored in data-bs-padding-right' )
2019-05-01 15:43:40 +02:00
expect ( currentPadding ) . toEqual ( expectedPadding , 'fixed element padding should be adjusted while opening' )
modal . toggle ( )
} )
modalEl . addEventListener ( 'hidden.bs.modal' , ( ) => {
2021-04-19 05:55:22 +02:00
const currentPadding = Number . parseInt ( window . getComputedStyle ( fixedEl ) . paddingRight , 10 )
2019-05-01 15:43:40 +02:00
2021-04-12 01:57:53 +02:00
expect ( fixedEl . hasAttribute ( 'data-bs-padding-right' ) ) . toEqual ( false , 'data-bs-padding-right should be cleared after closing' )
2019-05-01 15:43:40 +02:00
expect ( currentPadding ) . toEqual ( originalPadding , 'fixed element padding should be reset after closing' )
2021-04-11 08:37:59 +02:00
document . documentElement . style . overflowY = 'auto'
2019-05-01 15:43:40 +02:00
done ( )
} )
modal . toggle ( )
} )
it ( 'should adjust the inline margin of sticky elements when opening and restore when closing' , done => {
fixtureEl . innerHTML = [
'<div class="sticky-top" style="margin-right: 0px;"></div>' ,
2020-05-11 15:54:56 +02:00
'<div class="modal"><div class="modal-dialog"></div></div>'
2019-05-01 15:43:40 +02:00
] . join ( '' )
2021-04-11 08:37:59 +02:00
document . documentElement . style . overflowY = 'scroll'
2019-05-01 15:43:40 +02:00
const stickyTopEl = fixtureEl . querySelector ( '.sticky-top' )
2020-05-02 15:49:33 +02:00
const originalMargin = Number . parseInt ( window . getComputedStyle ( stickyTopEl ) . marginRight , 10 )
2019-05-01 15:43:40 +02:00
const modalEl = fixtureEl . querySelector ( '.modal' )
const modal = new Modal ( modalEl )
2021-04-19 05:55:22 +02:00
const scrollBarWidth = getScrollBarWidth ( )
2019-05-01 15:43:40 +02:00
modalEl . addEventListener ( 'shown.bs.modal' , ( ) => {
2021-04-19 05:55:22 +02:00
const expectedMargin = originalMargin - scrollBarWidth
2020-05-02 15:49:33 +02:00
const currentMargin = Number . parseInt ( window . getComputedStyle ( stickyTopEl ) . marginRight , 10 )
2019-05-01 15:43:40 +02:00
2021-04-12 01:57:53 +02:00
expect ( stickyTopEl . getAttribute ( 'data-bs-margin-right' ) ) . toEqual ( ` ${ originalMargin } px ` , 'original sticky element margin should be stored in data-bs-margin-right' )
2019-05-01 15:43:40 +02:00
expect ( currentMargin ) . toEqual ( expectedMargin , 'sticky element margin should be adjusted while opening' )
modal . toggle ( )
} )
modalEl . addEventListener ( 'hidden.bs.modal' , ( ) => {
2020-05-02 15:49:33 +02:00
const currentMargin = Number . parseInt ( window . getComputedStyle ( stickyTopEl ) . marginRight , 10 )
2019-05-01 15:43:40 +02:00
2021-04-12 01:57:53 +02:00
expect ( stickyTopEl . hasAttribute ( 'data-bs-margin-right' ) ) . toEqual ( false , 'data-bs-margin-right should be cleared after closing' )
2019-05-01 15:43:40 +02:00
expect ( currentMargin ) . toEqual ( originalMargin , 'sticky element margin should be reset after closing' )
2021-04-11 08:37:59 +02:00
document . documentElement . style . overflowY = 'auto'
2019-05-01 15:43:40 +02:00
done ( )
} )
modal . toggle ( )
} )
2021-02-23 13:52:09 +01:00
it ( 'should not adjust the inline margin and padding of sticky and fixed elements when element do not have full width' , done => {
fixtureEl . innerHTML = [
'<div class="sticky-top" style="margin-right: 0px; padding-right: 0px; width: calc(100vw - 50%)"></div>' ,
'<div class="modal"><div class="modal-dialog"></div></div>'
] . join ( '' )
const stickyTopEl = fixtureEl . querySelector ( '.sticky-top' )
const originalMargin = Number . parseInt ( window . getComputedStyle ( stickyTopEl ) . marginRight , 10 )
const originalPadding = Number . parseInt ( window . getComputedStyle ( stickyTopEl ) . paddingRight , 10 )
const modalEl = fixtureEl . querySelector ( '.modal' )
const modal = new Modal ( modalEl )
modalEl . addEventListener ( 'shown.bs.modal' , ( ) => {
const currentMargin = Number . parseInt ( window . getComputedStyle ( stickyTopEl ) . marginRight , 10 )
const currentPadding = Number . parseInt ( window . getComputedStyle ( stickyTopEl ) . paddingRight , 10 )
expect ( currentMargin ) . toEqual ( originalMargin , 'sticky element\'s margin should not be adjusted while opening' )
expect ( currentPadding ) . toEqual ( originalPadding , 'sticky element\'s padding should not be adjusted while opening' )
done ( )
} )
modal . show ( )
} )
2019-05-01 15:43:40 +02:00
it ( 'should ignore values set via CSS when trying to restore body padding after closing' , done => {
2020-05-11 15:54:56 +02:00
fixtureEl . innerHTML = '<div class="modal"><div class="modal-dialog"></div></div>'
2019-05-01 15:43:40 +02:00
const styleTest = document . createElement ( 'style' )
styleTest . type = 'text/css'
styleTest . appendChild ( document . createTextNode ( 'body { padding-right: 7px; }' ) )
document . head . appendChild ( styleTest )
const modalEl = fixtureEl . querySelector ( '.modal' )
const modal = new Modal ( modalEl )
modalEl . addEventListener ( 'shown.bs.modal' , ( ) => {
modal . toggle ( )
} )
modalEl . addEventListener ( 'hidden.bs.modal' , ( ) => {
expect ( window . getComputedStyle ( document . body ) . paddingLeft ) . toEqual ( '0px' , 'body does not have inline padding set' )
document . head . removeChild ( styleTest )
done ( )
} )
modal . toggle ( )
} )
it ( 'should ignore other inline styles when trying to restore body padding after closing' , done => {
2020-05-11 15:54:56 +02:00
fixtureEl . innerHTML = '<div class="modal"><div class="modal-dialog"></div></div>'
2019-05-01 15:43:40 +02:00
const styleTest = document . createElement ( 'style' )
styleTest . type = 'text/css'
styleTest . appendChild ( document . createTextNode ( 'body { padding-right: 7px; }' ) )
document . head . appendChild ( styleTest )
document . body . style . color = 'red'
const modalEl = fixtureEl . querySelector ( '.modal' )
const modal = new Modal ( modalEl )
modalEl . addEventListener ( 'shown.bs.modal' , ( ) => {
modal . toggle ( )
} )
modalEl . addEventListener ( 'hidden.bs.modal' , ( ) => {
const bodyPaddingRight = document . body . style . paddingRight
expect ( bodyPaddingRight === '0px' || bodyPaddingRight === '' ) . toEqual ( true , 'body does not have inline padding set' )
expect ( document . body . style . color ) . toEqual ( 'red' , 'body still has other inline styles set' )
document . head . removeChild ( styleTest )
document . body . removeAttribute ( 'style' )
done ( )
} )
modal . toggle ( )
} )
} )
describe ( 'show' , ( ) => {
it ( 'should show a modal' , done => {
2020-05-11 15:54:56 +02:00
fixtureEl . innerHTML = '<div class="modal"><div class="modal-dialog"></div></div>'
2019-05-01 15:43:40 +02:00
const modalEl = fixtureEl . querySelector ( '.modal' )
const modal = new Modal ( modalEl )
modalEl . addEventListener ( 'show.bs.modal' , e => {
expect ( e ) . toBeDefined ( )
} )
modalEl . addEventListener ( 'shown.bs.modal' , ( ) => {
expect ( modalEl . getAttribute ( 'aria-modal' ) ) . toEqual ( 'true' )
2020-06-04 09:35:09 +02:00
expect ( modalEl . getAttribute ( 'role' ) ) . toEqual ( 'dialog' )
2021-05-11 07:45:57 +02:00
expect ( modalEl . getAttribute ( 'aria-hidden' ) ) . toBeNull ( )
2019-05-01 15:43:40 +02:00
expect ( modalEl . style . display ) . toEqual ( 'block' )
2021-05-11 07:45:57 +02:00
expect ( document . querySelector ( '.modal-backdrop' ) ) . not . toBeNull ( )
2019-05-01 15:43:40 +02:00
done ( )
} )
modal . show ( )
} )
it ( 'should show a modal without backdrop' , done => {
2020-05-11 15:54:56 +02:00
fixtureEl . innerHTML = '<div class="modal"><div class="modal-dialog"></div></div>'
2019-05-01 15:43:40 +02:00
const modalEl = fixtureEl . querySelector ( '.modal' )
const modal = new Modal ( modalEl , {
backdrop : false
} )
modalEl . addEventListener ( 'show.bs.modal' , e => {
expect ( e ) . toBeDefined ( )
} )
modalEl . addEventListener ( 'shown.bs.modal' , ( ) => {
expect ( modalEl . getAttribute ( 'aria-modal' ) ) . toEqual ( 'true' )
2020-06-04 09:35:09 +02:00
expect ( modalEl . getAttribute ( 'role' ) ) . toEqual ( 'dialog' )
2021-05-11 07:45:57 +02:00
expect ( modalEl . getAttribute ( 'aria-hidden' ) ) . toBeNull ( )
2019-05-01 15:43:40 +02:00
expect ( modalEl . style . display ) . toEqual ( 'block' )
expect ( document . querySelector ( '.modal-backdrop' ) ) . toBeNull ( )
done ( )
} )
modal . show ( )
} )
it ( 'should show a modal and append the element' , done => {
const modalEl = document . createElement ( 'div' )
const id = 'dynamicModal'
modalEl . setAttribute ( 'id' , id )
modalEl . classList . add ( 'modal' )
modalEl . innerHTML = '<div class="modal-dialog"></div>'
const modal = new Modal ( modalEl )
modalEl . addEventListener ( 'shown.bs.modal' , ( ) => {
const dynamicModal = document . getElementById ( id )
2021-05-11 07:45:57 +02:00
expect ( dynamicModal ) . not . toBeNull ( )
2019-05-01 15:43:40 +02:00
dynamicModal . parentNode . removeChild ( dynamicModal )
done ( )
} )
modal . show ( )
} )
it ( 'should do nothing if a modal is shown' , ( ) => {
2020-05-11 15:54:56 +02:00
fixtureEl . innerHTML = '<div class="modal"><div class="modal-dialog"></div></div>'
2019-05-01 15:43:40 +02:00
const modalEl = fixtureEl . querySelector ( '.modal' )
const modal = new Modal ( modalEl )
spyOn ( EventHandler , 'trigger' )
modal . _isShown = true
modal . show ( )
expect ( EventHandler . trigger ) . not . toHaveBeenCalled ( )
} )
it ( 'should do nothing if a modal is transitioning' , ( ) => {
2020-05-11 15:54:56 +02:00
fixtureEl . innerHTML = '<div class="modal"><div class="modal-dialog"></div></div>'
2019-05-01 15:43:40 +02:00
const modalEl = fixtureEl . querySelector ( '.modal' )
const modal = new Modal ( modalEl )
spyOn ( EventHandler , 'trigger' )
modal . _isTransitioning = true
modal . show ( )
expect ( EventHandler . trigger ) . not . toHaveBeenCalled ( )
} )
it ( 'should not fire shown event when show is prevented' , done => {
2020-05-11 15:54:56 +02:00
fixtureEl . innerHTML = '<div class="modal"><div class="modal-dialog"></div></div>'
2019-05-01 15:43:40 +02:00
const modalEl = fixtureEl . querySelector ( '.modal' )
const modal = new Modal ( modalEl )
modalEl . addEventListener ( 'show.bs.modal' , e => {
e . preventDefault ( )
const expectedDone = ( ) => {
expect ( ) . nothing ( )
done ( )
}
setTimeout ( expectedDone , 10 )
} )
modalEl . addEventListener ( 'shown.bs.modal' , ( ) => {
throw new Error ( 'shown event triggered' )
} )
modal . show ( )
} )
it ( 'should set is transitioning if fade class is present' , done => {
2020-11-20 19:10:15 +01:00
fixtureEl . innerHTML = '<div class="modal fade"><div class="modal-dialog"></div></div>'
2019-05-01 15:43:40 +02:00
const modalEl = fixtureEl . querySelector ( '.modal' )
const modal = new Modal ( modalEl )
modalEl . addEventListener ( 'show.bs.modal' , ( ) => {
expect ( modal . _isTransitioning ) . toEqual ( true )
} )
modalEl . addEventListener ( 'shown.bs.modal' , ( ) => {
expect ( modal . _isTransitioning ) . toEqual ( false )
done ( )
} )
modal . show ( )
} )
2020-07-22 21:33:11 +02:00
it ( 'should close modal when a click occurred on data-bs-dismiss="modal"' , done => {
2019-05-01 15:43:40 +02:00
fixtureEl . innerHTML = [
'<div class="modal fade">' ,
' <div class="modal-dialog">' ,
' <div class="modal-header">' ,
2020-07-22 21:33:11 +02:00
' <button type="button" data-bs-dismiss="modal"></button>' ,
2019-05-01 15:43:40 +02:00
' </div>' ,
' </div>' ,
'</div>'
] . join ( '' )
const modalEl = fixtureEl . querySelector ( '.modal' )
2020-07-22 21:33:11 +02:00
const btnClose = fixtureEl . querySelector ( '[data-bs-dismiss="modal"]' )
2019-05-01 15:43:40 +02:00
const modal = new Modal ( modalEl )
spyOn ( modal , 'hide' ) . and . callThrough ( )
modalEl . addEventListener ( 'shown.bs.modal' , ( ) => {
btnClose . click ( )
} )
modalEl . addEventListener ( 'hidden.bs.modal' , ( ) => {
expect ( modal . hide ) . toHaveBeenCalled ( )
done ( )
} )
modal . show ( )
} )
2020-03-04 15:51:15 +01:00
it ( 'should set .modal\'s scroll top to 0' , done => {
2019-05-01 15:43:40 +02:00
fixtureEl . innerHTML = [
'<div class="modal fade">' ,
2020-03-04 15:51:15 +01:00
' <div class="modal-dialog">' ,
2019-05-01 15:43:40 +02:00
' </div>' ,
'</div>'
] . join ( '' )
const modalEl = fixtureEl . querySelector ( '.modal' )
const modal = new Modal ( modalEl )
modalEl . addEventListener ( 'shown.bs.modal' , ( ) => {
2020-03-04 15:51:15 +01:00
expect ( modalEl . scrollTop ) . toEqual ( 0 )
2019-05-01 15:43:40 +02:00
done ( )
} )
modal . show ( )
} )
2020-03-04 15:51:15 +01:00
it ( 'should set modal body scroll top to 0 if modal body do not exists' , done => {
2019-07-28 11:49:44 +02:00
fixtureEl . innerHTML = [
'<div class="modal fade">' ,
2020-03-04 15:51:15 +01:00
' <div class="modal-dialog">' ,
' <div class="modal-body"></div>' ,
2019-07-28 11:49:44 +02:00
' </div>' ,
'</div>'
] . join ( '' )
const modalEl = fixtureEl . querySelector ( '.modal' )
2020-03-04 15:51:15 +01:00
const modalBody = modalEl . querySelector ( '.modal-body' )
2019-07-28 11:49:44 +02:00
const modal = new Modal ( modalEl )
modalEl . addEventListener ( 'shown.bs.modal' , ( ) => {
2020-03-04 15:51:15 +01:00
expect ( modalBody . scrollTop ) . toEqual ( 0 )
2019-07-28 11:49:44 +02:00
done ( )
} )
modal . show ( )
} )
2019-05-01 15:43:40 +02:00
it ( 'should not enforce focus if focus equal to false' , done => {
2020-11-20 19:10:15 +01:00
fixtureEl . innerHTML = '<div class="modal fade"><div class="modal-dialog"></div></div>'
2019-05-01 15:43:40 +02:00
const modalEl = fixtureEl . querySelector ( '.modal' )
const modal = new Modal ( modalEl , {
focus : false
} )
spyOn ( modal , '_enforceFocus' )
modalEl . addEventListener ( 'shown.bs.modal' , ( ) => {
expect ( modal . _enforceFocus ) . not . toHaveBeenCalled ( )
done ( )
} )
modal . show ( )
} )
it ( 'should add listener when escape touch is pressed' , done => {
2020-05-11 15:54:56 +02:00
fixtureEl . innerHTML = '<div class="modal"><div class="modal-dialog"></div></div>'
2019-05-01 15:43:40 +02:00
const modalEl = fixtureEl . querySelector ( '.modal' )
const modal = new Modal ( modalEl )
spyOn ( modal , 'hide' ) . and . callThrough ( )
modalEl . addEventListener ( 'shown.bs.modal' , ( ) => {
const keydownEscape = createEvent ( 'keydown' )
2020-04-15 16:52:18 +02:00
keydownEscape . key = 'Escape'
2019-05-01 15:43:40 +02:00
modalEl . dispatchEvent ( keydownEscape )
} )
modalEl . addEventListener ( 'hidden.bs.modal' , ( ) => {
expect ( modal . hide ) . toHaveBeenCalled ( )
done ( )
} )
modal . show ( )
} )
it ( 'should do nothing when the pressed key is not escape' , done => {
2020-05-11 15:54:56 +02:00
fixtureEl . innerHTML = '<div class="modal"><div class="modal-dialog"></div></div>'
2019-05-01 15:43:40 +02:00
const modalEl = fixtureEl . querySelector ( '.modal' )
const modal = new Modal ( modalEl )
spyOn ( modal , 'hide' )
const expectDone = ( ) => {
expect ( modal . hide ) . not . toHaveBeenCalled ( )
done ( )
}
modalEl . addEventListener ( 'shown.bs.modal' , ( ) => {
const keydownTab = createEvent ( 'keydown' )
2020-04-15 16:52:18 +02:00
keydownTab . key = 'Tab'
2019-05-01 15:43:40 +02:00
modalEl . dispatchEvent ( keydownTab )
setTimeout ( expectDone , 30 )
} )
modal . show ( )
} )
it ( 'should adjust dialog on resize' , done => {
2020-05-11 15:54:56 +02:00
fixtureEl . innerHTML = '<div class="modal"><div class="modal-dialog"></div></div>'
2019-05-01 15:43:40 +02:00
const modalEl = fixtureEl . querySelector ( '.modal' )
const modal = new Modal ( modalEl )
spyOn ( modal , '_adjustDialog' ) . and . callThrough ( )
const expectDone = ( ) => {
expect ( modal . _adjustDialog ) . toHaveBeenCalled ( )
done ( )
}
modalEl . addEventListener ( 'shown.bs.modal' , ( ) => {
const resizeEvent = createEvent ( 'resize' )
window . dispatchEvent ( resizeEvent )
setTimeout ( expectDone , 10 )
} )
modal . show ( )
} )
it ( 'should not close modal when clicking outside of modal-content if backdrop = false' , done => {
2020-05-11 15:54:56 +02:00
fixtureEl . innerHTML = '<div class="modal"><div class="modal-dialog"></div></div>'
2019-05-01 15:43:40 +02:00
const modalEl = fixtureEl . querySelector ( '.modal' )
const modal = new Modal ( modalEl , {
backdrop : false
} )
const shownCallback = ( ) => {
setTimeout ( ( ) => {
expect ( modal . _isShown ) . toEqual ( true )
done ( )
} , 10 )
}
modalEl . addEventListener ( 'shown.bs.modal' , ( ) => {
modalEl . click ( )
shownCallback ( )
} )
modalEl . addEventListener ( 'hidden.bs.modal' , ( ) => {
throw new Error ( 'Should not hide a modal' )
} )
modal . show ( )
} )
2019-10-25 20:12:09 +02:00
it ( 'should not close modal when clicking outside of modal-content if backdrop = static' , done => {
2021-04-08 07:20:21 +02:00
fixtureEl . innerHTML = '<div class="modal"><div class="modal-dialog"></div></div>'
2019-10-25 20:12:09 +02:00
const modalEl = fixtureEl . querySelector ( '.modal' )
const modal = new Modal ( modalEl , {
backdrop : 'static'
} )
const shownCallback = ( ) => {
setTimeout ( ( ) => {
expect ( modal . _isShown ) . toEqual ( true )
done ( )
} , 10 )
}
modalEl . addEventListener ( 'shown.bs.modal' , ( ) => {
modalEl . click ( )
shownCallback ( )
} )
modalEl . addEventListener ( 'hidden.bs.modal' , ( ) => {
throw new Error ( 'Should not hide a modal' )
} )
modal . show ( )
} )
2020-01-10 10:06:12 +01:00
it ( 'should close modal when escape key is pressed with keyboard = true and backdrop is static' , done => {
2021-04-08 07:20:21 +02:00
fixtureEl . innerHTML = '<div class="modal"><div class="modal-dialog"></div></div>'
2020-01-10 10:06:12 +01:00
const modalEl = fixtureEl . querySelector ( '.modal' )
const modal = new Modal ( modalEl , {
backdrop : 'static' ,
keyboard : true
} )
const shownCallback = ( ) => {
setTimeout ( ( ) => {
expect ( modal . _isShown ) . toEqual ( false )
done ( )
} , 10 )
}
modalEl . addEventListener ( 'shown.bs.modal' , ( ) => {
const keydownEscape = createEvent ( 'keydown' )
2020-04-15 16:52:18 +02:00
keydownEscape . key = 'Escape'
2020-01-10 10:06:12 +01:00
modalEl . dispatchEvent ( keydownEscape )
shownCallback ( )
} )
modal . show ( )
} )
2020-11-20 14:36:24 +01:00
it ( 'should not close modal when escape key is pressed with keyboard = false' , done => {
fixtureEl . innerHTML = '<div class="modal"><div class="modal-dialog"></div></div>'
2020-01-10 10:06:12 +01:00
const modalEl = fixtureEl . querySelector ( '.modal' )
const modal = new Modal ( modalEl , {
keyboard : false
} )
const shownCallback = ( ) => {
setTimeout ( ( ) => {
expect ( modal . _isShown ) . toEqual ( true )
done ( )
} , 10 )
}
modalEl . addEventListener ( 'shown.bs.modal' , ( ) => {
const keydownEscape = createEvent ( 'keydown' )
2020-04-15 16:52:18 +02:00
keydownEscape . key = 'Escape'
2020-01-10 10:06:12 +01:00
modalEl . dispatchEvent ( keydownEscape )
shownCallback ( )
} )
modalEl . addEventListener ( 'hidden.bs.modal' , ( ) => {
throw new Error ( 'Should not hide a modal' )
} )
modal . show ( )
} )
2020-06-25 10:35:53 +02:00
it ( 'should not overflow when clicking outside of modal-content if backdrop = static' , done => {
2021-04-08 07:20:21 +02:00
fixtureEl . innerHTML = '<div class="modal"><div class="modal-dialog" style="transition-duration: 20ms;"></div></div>'
2020-06-25 10:35:53 +02:00
const modalEl = fixtureEl . querySelector ( '.modal' )
const modal = new Modal ( modalEl , {
backdrop : 'static'
} )
modalEl . addEventListener ( 'shown.bs.modal' , ( ) => {
modalEl . click ( )
setTimeout ( ( ) => {
2020-11-02 15:13:24 +01:00
expect ( modalEl . clientHeight ) . toEqual ( modalEl . scrollHeight )
2020-06-25 10:35:53 +02:00
done ( )
} , 20 )
} )
modal . show ( )
} )
2019-05-01 15:43:40 +02:00
it ( 'should not adjust the inline body padding when it does not overflow' , done => {
2020-05-11 15:54:56 +02:00
fixtureEl . innerHTML = '<div class="modal"><div class="modal-dialog"></div></div>'
2019-05-01 15:43:40 +02:00
const modalEl = fixtureEl . querySelector ( '.modal' )
const modal = new Modal ( modalEl )
const originalPadding = window . getComputedStyle ( document . body ) . paddingRight
// Hide scrollbars to prevent the body overflowing
document . body . style . overflow = 'hidden'
document . documentElement . style . paddingRight = '0px'
modalEl . addEventListener ( 'shown.bs.modal' , ( ) => {
const currentPadding = window . getComputedStyle ( document . body ) . paddingRight
expect ( currentPadding ) . toEqual ( originalPadding , 'body padding should not be adjusted' )
// Restore scrollbars
document . body . style . overflow = 'auto'
done ( )
} )
modal . show ( )
} )
2020-05-10 15:59:22 +02:00
it ( 'should not adjust the inline body padding when it does not overflow, even on a scaled display' , done => {
fixtureEl . innerHTML = '<div class="modal"><div class="modal-dialog"></div></div>'
const modalEl = fixtureEl . querySelector ( '.modal' )
const modal = new Modal ( modalEl )
const originalPadding = window . getComputedStyle ( document . body ) . paddingRight
// Remove body margins as would be done by Bootstrap css
document . body . style . margin = '0'
// Hide scrollbars to prevent the body overflowing
document . body . style . overflow = 'hidden'
// Simulate a discrepancy between exact, i.e. floating point body width, and rounded body width
// as it can occur when zooming or scaling the display to something else than 100%
document . documentElement . style . paddingRight = '.48px'
modalEl . addEventListener ( 'shown.bs.modal' , ( ) => {
const currentPadding = window . getComputedStyle ( document . body ) . paddingRight
expect ( currentPadding ) . toEqual ( originalPadding , 'body padding should not be adjusted' )
// Restore overridden css
document . body . style . removeProperty ( 'margin' )
document . body . style . removeProperty ( 'overflow' )
done ( )
} )
modal . show ( )
} )
2019-05-01 15:43:40 +02:00
it ( 'should enforce focus' , done => {
2020-05-11 15:54:56 +02:00
fixtureEl . innerHTML = '<div class="modal"><div class="modal-dialog"></div></div>'
2019-05-01 15:43:40 +02:00
const modalEl = fixtureEl . querySelector ( '.modal' )
const modal = new Modal ( modalEl )
spyOn ( modal , '_enforceFocus' ) . and . callThrough ( )
const focusInListener = ( ) => {
expect ( modal . _element . focus ) . toHaveBeenCalled ( )
document . removeEventListener ( 'focusin' , focusInListener )
done ( )
}
modalEl . addEventListener ( 'shown.bs.modal' , ( ) => {
expect ( modal . _enforceFocus ) . toHaveBeenCalled ( )
spyOn ( modal . _element , 'focus' )
document . addEventListener ( 'focusin' , focusInListener )
const focusInEvent = createEvent ( 'focusin' , { bubbles : true } )
Object . defineProperty ( focusInEvent , 'target' , {
value : fixtureEl
} )
document . dispatchEvent ( focusInEvent )
} )
modal . show ( )
} )
} )
describe ( 'hide' , ( ) => {
it ( 'should hide a modal' , done => {
2020-05-11 15:54:56 +02:00
fixtureEl . innerHTML = '<div class="modal"><div class="modal-dialog"></div></div>'
2019-05-01 15:43:40 +02:00
const modalEl = fixtureEl . querySelector ( '.modal' )
const modal = new Modal ( modalEl )
modalEl . addEventListener ( 'shown.bs.modal' , ( ) => {
modal . hide ( )
} )
modalEl . addEventListener ( 'hide.bs.modal' , e => {
expect ( e ) . toBeDefined ( )
} )
modalEl . addEventListener ( 'hidden.bs.modal' , ( ) => {
2021-05-11 07:45:57 +02:00
expect ( modalEl . getAttribute ( 'aria-modal' ) ) . toBeNull ( )
expect ( modalEl . getAttribute ( 'role' ) ) . toBeNull ( )
2019-05-01 15:43:40 +02:00
expect ( modalEl . getAttribute ( 'aria-hidden' ) ) . toEqual ( 'true' )
expect ( modalEl . style . display ) . toEqual ( 'none' )
expect ( document . querySelector ( '.modal-backdrop' ) ) . toBeNull ( )
done ( )
} )
modal . show ( )
} )
it ( 'should close modal when clicking outside of modal-content' , done => {
2020-05-11 15:54:56 +02:00
fixtureEl . innerHTML = '<div class="modal"><div class="modal-dialog"></div></div>'
2019-05-01 15:43:40 +02:00
const modalEl = fixtureEl . querySelector ( '.modal' )
const modal = new Modal ( modalEl )
modalEl . addEventListener ( 'shown.bs.modal' , ( ) => {
modalEl . click ( )
} )
modalEl . addEventListener ( 'hidden.bs.modal' , ( ) => {
2021-05-11 07:45:57 +02:00
expect ( modalEl . getAttribute ( 'aria-modal' ) ) . toBeNull ( )
expect ( modalEl . getAttribute ( 'role' ) ) . toBeNull ( )
2019-05-01 15:43:40 +02:00
expect ( modalEl . getAttribute ( 'aria-hidden' ) ) . toEqual ( 'true' )
expect ( modalEl . style . display ) . toEqual ( 'none' )
expect ( document . querySelector ( '.modal-backdrop' ) ) . toBeNull ( )
done ( )
} )
modal . show ( )
} )
it ( 'should do nothing is the modal is not shown' , ( ) => {
2020-05-11 15:54:56 +02:00
fixtureEl . innerHTML = '<div class="modal"><div class="modal-dialog"></div></div>'
2019-05-01 15:43:40 +02:00
const modalEl = fixtureEl . querySelector ( '.modal' )
const modal = new Modal ( modalEl )
modal . hide ( )
expect ( ) . nothing ( )
} )
it ( 'should do nothing is the modal is transitioning' , ( ) => {
2020-05-11 15:54:56 +02:00
fixtureEl . innerHTML = '<div class="modal"><div class="modal-dialog"></div></div>'
2019-05-01 15:43:40 +02:00
const modalEl = fixtureEl . querySelector ( '.modal' )
const modal = new Modal ( modalEl )
modal . _isTransitioning = true
modal . hide ( )
expect ( ) . nothing ( )
} )
it ( 'should not hide a modal if hide is prevented' , done => {
2020-05-11 15:54:56 +02:00
fixtureEl . innerHTML = '<div class="modal"><div class="modal-dialog"></div></div>'
2019-05-01 15:43:40 +02:00
const modalEl = fixtureEl . querySelector ( '.modal' )
const modal = new Modal ( modalEl )
modalEl . addEventListener ( 'shown.bs.modal' , ( ) => {
modal . hide ( )
} )
const hideCallback = ( ) => {
setTimeout ( ( ) => {
expect ( modal . _isShown ) . toEqual ( true )
done ( )
} , 10 )
}
modalEl . addEventListener ( 'hide.bs.modal' , e => {
e . preventDefault ( )
hideCallback ( )
} )
modalEl . addEventListener ( 'hidden.bs.modal' , ( ) => {
throw new Error ( 'should not trigger hidden' )
} )
modal . show ( )
} )
} )
describe ( 'dispose' , ( ) => {
it ( 'should dispose a modal' , ( ) => {
2020-11-20 19:10:15 +01:00
fixtureEl . innerHTML = '<div id="exampleModal" class="modal"><div class="modal-dialog"></div></div>'
2019-05-01 15:43:40 +02:00
const modalEl = fixtureEl . querySelector ( '.modal' )
const modal = new Modal ( modalEl )
2019-07-28 15:24:46 +02:00
expect ( Modal . getInstance ( modalEl ) ) . toEqual ( modal )
2019-05-01 15:43:40 +02:00
spyOn ( EventHandler , 'off' )
modal . dispose ( )
2021-05-11 07:45:57 +02:00
expect ( Modal . getInstance ( modalEl ) ) . toBeNull ( )
2019-05-01 15:43:40 +02:00
expect ( EventHandler . off ) . toHaveBeenCalledTimes ( 4 )
} )
} )
describe ( 'handleUpdate' , ( ) => {
it ( 'should call adjust dialog' , ( ) => {
2020-11-20 19:10:15 +01:00
fixtureEl . innerHTML = '<div id="exampleModal" class="modal"><div class="modal-dialog"></div></div>'
2019-05-01 15:43:40 +02:00
const modalEl = fixtureEl . querySelector ( '.modal' )
const modal = new Modal ( modalEl )
spyOn ( modal , '_adjustDialog' )
modal . handleUpdate ( )
expect ( modal . _adjustDialog ) . toHaveBeenCalled ( )
} )
} )
describe ( 'data-api' , ( ) => {
2021-01-13 20:59:47 +01:00
it ( 'should toggle modal' , done => {
2019-05-01 15:43:40 +02:00
fixtureEl . innerHTML = [
2020-07-22 21:33:11 +02:00
'<button type="button" data-bs-toggle="modal" data-bs-target="#exampleModal"></button>' ,
2020-11-20 19:10:15 +01:00
'<div id="exampleModal" class="modal"><div class="modal-dialog"></div></div>'
2019-05-01 15:43:40 +02:00
] . join ( '' )
const modalEl = fixtureEl . querySelector ( '.modal' )
2020-07-22 21:33:11 +02:00
const trigger = fixtureEl . querySelector ( '[data-bs-toggle="modal"]' )
2019-05-01 15:43:40 +02:00
modalEl . addEventListener ( 'shown.bs.modal' , ( ) => {
expect ( modalEl . getAttribute ( 'aria-modal' ) ) . toEqual ( 'true' )
2020-06-04 09:35:09 +02:00
expect ( modalEl . getAttribute ( 'role' ) ) . toEqual ( 'dialog' )
2021-05-11 07:45:57 +02:00
expect ( modalEl . getAttribute ( 'aria-hidden' ) ) . toBeNull ( )
2019-05-01 15:43:40 +02:00
expect ( modalEl . style . display ) . toEqual ( 'block' )
2021-05-11 07:45:57 +02:00
expect ( document . querySelector ( '.modal-backdrop' ) ) . not . toBeNull ( )
2021-01-13 20:59:47 +01:00
setTimeout ( ( ) => trigger . click ( ) , 10 )
} )
modalEl . addEventListener ( 'hidden.bs.modal' , ( ) => {
2021-05-11 07:45:57 +02:00
expect ( modalEl . getAttribute ( 'aria-modal' ) ) . toBeNull ( )
expect ( modalEl . getAttribute ( 'role' ) ) . toBeNull ( )
2021-01-13 20:59:47 +01:00
expect ( modalEl . getAttribute ( 'aria-hidden' ) ) . toEqual ( 'true' )
expect ( modalEl . style . display ) . toEqual ( 'none' )
2021-05-11 07:45:57 +02:00
expect ( document . querySelector ( '.modal-backdrop' ) ) . toBeNull ( )
2019-05-01 15:43:40 +02:00
done ( )
} )
trigger . click ( )
} )
it ( 'should not recreate a new modal' , done => {
fixtureEl . innerHTML = [
2020-07-22 21:33:11 +02:00
'<button type="button" data-bs-toggle="modal" data-bs-target="#exampleModal"></button>' ,
2020-11-20 19:10:15 +01:00
'<div id="exampleModal" class="modal"><div class="modal-dialog"></div></div>'
2019-05-01 15:43:40 +02:00
] . join ( '' )
const modalEl = fixtureEl . querySelector ( '.modal' )
const modal = new Modal ( modalEl )
2020-07-22 21:33:11 +02:00
const trigger = fixtureEl . querySelector ( '[data-bs-toggle="modal"]' )
2019-05-01 15:43:40 +02:00
spyOn ( modal , 'show' ) . and . callThrough ( )
modalEl . addEventListener ( 'shown.bs.modal' , ( ) => {
expect ( modal . show ) . toHaveBeenCalled ( )
done ( )
} )
trigger . click ( )
} )
it ( 'should prevent default when the trigger is <a> or <area>' , done => {
fixtureEl . innerHTML = [
2020-07-22 21:33:11 +02:00
'<a data-bs-toggle="modal" href="#" data-bs-target="#exampleModal"></a>' ,
2020-11-20 19:10:15 +01:00
'<div id="exampleModal" class="modal"><div class="modal-dialog"></div></div>'
2019-05-01 15:43:40 +02:00
] . join ( '' )
const modalEl = fixtureEl . querySelector ( '.modal' )
2020-07-22 21:33:11 +02:00
const trigger = fixtureEl . querySelector ( '[data-bs-toggle="modal"]' )
2019-05-01 15:43:40 +02:00
spyOn ( Event . prototype , 'preventDefault' ) . and . callThrough ( )
modalEl . addEventListener ( 'shown.bs.modal' , ( ) => {
expect ( modalEl . getAttribute ( 'aria-modal' ) ) . toEqual ( 'true' )
2020-06-04 09:35:09 +02:00
expect ( modalEl . getAttribute ( 'role' ) ) . toEqual ( 'dialog' )
2021-05-11 07:45:57 +02:00
expect ( modalEl . getAttribute ( 'aria-hidden' ) ) . toBeNull ( )
2019-05-01 15:43:40 +02:00
expect ( modalEl . style . display ) . toEqual ( 'block' )
2021-05-11 07:45:57 +02:00
expect ( document . querySelector ( '.modal-backdrop' ) ) . not . toBeNull ( )
2019-05-01 15:43:40 +02:00
expect ( Event . prototype . preventDefault ) . toHaveBeenCalled ( )
done ( )
} )
trigger . click ( )
} )
it ( 'should focus the trigger on hide' , done => {
fixtureEl . innerHTML = [
2020-07-22 21:33:11 +02:00
'<a data-bs-toggle="modal" href="#" data-bs-target="#exampleModal"></a>' ,
2020-11-20 19:10:15 +01:00
'<div id="exampleModal" class="modal"><div class="modal-dialog"></div></div>'
2019-05-01 15:43:40 +02:00
] . join ( '' )
const modalEl = fixtureEl . querySelector ( '.modal' )
2020-07-22 21:33:11 +02:00
const trigger = fixtureEl . querySelector ( '[data-bs-toggle="modal"]' )
2019-05-01 15:43:40 +02:00
spyOn ( trigger , 'focus' )
modalEl . addEventListener ( 'shown.bs.modal' , ( ) => {
2019-07-28 15:24:46 +02:00
const modal = Modal . getInstance ( modalEl )
2019-05-01 15:43:40 +02:00
modal . hide ( )
} )
const hideListener = ( ) => {
setTimeout ( ( ) => {
expect ( trigger . focus ) . toHaveBeenCalled ( )
done ( )
} , 20 )
}
modalEl . addEventListener ( 'hidden.bs.modal' , ( ) => {
hideListener ( )
} )
trigger . click ( )
} )
it ( 'should not focus the trigger if the modal is not visible' , done => {
fixtureEl . innerHTML = [
2020-07-22 21:33:11 +02:00
'<a data-bs-toggle="modal" href="#" data-bs-target="#exampleModal" style="display: none;"></a>' ,
2020-11-20 19:10:15 +01:00
'<div id="exampleModal" class="modal" style="display: none;"><div class="modal-dialog"></div></div>'
2019-05-01 15:43:40 +02:00
] . join ( '' )
const modalEl = fixtureEl . querySelector ( '.modal' )
2020-07-22 21:33:11 +02:00
const trigger = fixtureEl . querySelector ( '[data-bs-toggle="modal"]' )
2019-05-01 15:43:40 +02:00
spyOn ( trigger , 'focus' )
modalEl . addEventListener ( 'shown.bs.modal' , ( ) => {
2019-07-28 15:24:46 +02:00
const modal = Modal . getInstance ( modalEl )
2019-05-01 15:43:40 +02:00
modal . hide ( )
} )
const hideListener = ( ) => {
setTimeout ( ( ) => {
expect ( trigger . focus ) . not . toHaveBeenCalled ( )
done ( )
} , 20 )
}
modalEl . addEventListener ( 'hidden.bs.modal' , ( ) => {
hideListener ( )
} )
trigger . click ( )
} )
it ( 'should not focus the trigger if the modal is not shown' , done => {
fixtureEl . innerHTML = [
2020-07-22 21:33:11 +02:00
'<a data-bs-toggle="modal" href="#" data-bs-target="#exampleModal"></a>' ,
2020-11-20 19:10:15 +01:00
'<div id="exampleModal" class="modal"><div class="modal-dialog"></div></div>'
2019-05-01 15:43:40 +02:00
] . join ( '' )
const modalEl = fixtureEl . querySelector ( '.modal' )
2020-07-22 21:33:11 +02:00
const trigger = fixtureEl . querySelector ( '[data-bs-toggle="modal"]' )
2019-05-01 15:43:40 +02:00
spyOn ( trigger , 'focus' )
const showListener = ( ) => {
setTimeout ( ( ) => {
expect ( trigger . focus ) . not . toHaveBeenCalled ( )
done ( )
} , 10 )
}
modalEl . addEventListener ( 'show.bs.modal' , e => {
e . preventDefault ( )
showListener ( )
} )
trigger . click ( )
} )
} )
2019-07-28 15:24:46 +02:00
describe ( 'jQueryInterface' , ( ) => {
2019-05-01 15:43:40 +02:00
it ( 'should create a modal' , ( ) => {
2020-05-11 15:54:56 +02:00
fixtureEl . innerHTML = '<div class="modal"><div class="modal-dialog"></div></div>'
2019-05-01 15:43:40 +02:00
const div = fixtureEl . querySelector ( 'div' )
2019-07-28 15:24:46 +02:00
jQueryMock . fn . modal = Modal . jQueryInterface
2019-05-01 15:43:40 +02:00
jQueryMock . elements = [ div ]
jQueryMock . fn . modal . call ( jQueryMock )
2021-05-11 07:45:57 +02:00
expect ( Modal . getInstance ( div ) ) . not . toBeNull ( )
2019-05-01 15:43:40 +02:00
} )
2021-03-03 01:17:48 +01:00
it ( 'should create a modal with given config' , ( ) => {
fixtureEl . innerHTML = '<div class="modal"><div class="modal-dialog"></div></div>'
const div = fixtureEl . querySelector ( 'div' )
jQueryMock . fn . modal = Modal . jQueryInterface
jQueryMock . elements = [ div ]
jQueryMock . fn . modal . call ( jQueryMock , { keyboard : false } )
spyOn ( Modal . prototype , 'constructor' )
expect ( Modal . prototype . constructor ) . not . toHaveBeenCalledWith ( div , { keyboard : false } )
const modal = Modal . getInstance ( div )
2021-05-11 07:45:57 +02:00
expect ( modal ) . not . toBeNull ( )
2021-03-03 01:17:48 +01:00
expect ( modal . _config . keyboard ) . toBe ( false )
} )
2019-05-01 15:43:40 +02:00
it ( 'should not re create a modal' , ( ) => {
2020-05-11 15:54:56 +02:00
fixtureEl . innerHTML = '<div class="modal"><div class="modal-dialog"></div></div>'
2019-05-01 15:43:40 +02:00
const div = fixtureEl . querySelector ( 'div' )
const modal = new Modal ( div )
2019-07-28 15:24:46 +02:00
jQueryMock . fn . modal = Modal . jQueryInterface
2019-05-01 15:43:40 +02:00
jQueryMock . elements = [ div ]
jQueryMock . fn . modal . call ( jQueryMock )
2019-07-28 15:24:46 +02:00
expect ( Modal . getInstance ( div ) ) . toEqual ( modal )
2019-05-01 15:43:40 +02:00
} )
it ( 'should throw error on undefined method' , ( ) => {
2020-05-11 15:54:56 +02:00
fixtureEl . innerHTML = '<div class="modal"><div class="modal-dialog"></div></div>'
2019-05-01 15:43:40 +02:00
const div = fixtureEl . querySelector ( 'div' )
const action = 'undefinedMethod'
2019-07-28 15:24:46 +02:00
jQueryMock . fn . modal = Modal . jQueryInterface
2019-05-01 15:43:40 +02:00
jQueryMock . elements = [ div ]
2021-01-13 21:13:30 +01:00
expect ( ( ) => {
2019-05-01 15:43:40 +02:00
jQueryMock . fn . modal . call ( jQueryMock , action )
2021-01-13 21:13:30 +01:00
} ) . toThrowError ( TypeError , ` No method named " ${ action } " ` )
2019-05-01 15:43:40 +02:00
} )
2020-11-20 19:10:15 +01:00
it ( 'should call show method' , ( ) => {
2020-05-11 15:54:56 +02:00
fixtureEl . innerHTML = '<div class="modal"><div class="modal-dialog"></div></div>'
2019-05-01 15:43:40 +02:00
const div = fixtureEl . querySelector ( 'div' )
const modal = new Modal ( div )
2019-07-28 15:24:46 +02:00
jQueryMock . fn . modal = Modal . jQueryInterface
2019-05-01 15:43:40 +02:00
jQueryMock . elements = [ div ]
spyOn ( modal , 'show' )
jQueryMock . fn . modal . call ( jQueryMock , 'show' )
expect ( modal . show ) . toHaveBeenCalled ( )
} )
2020-11-20 19:10:15 +01:00
it ( 'should not call show method' , ( ) => {
fixtureEl . innerHTML = '<div class="modal" data-bs-show="false"><div class="modal-dialog"></div></div>'
2019-05-01 15:43:40 +02:00
const div = fixtureEl . querySelector ( 'div' )
2019-07-28 15:24:46 +02:00
jQueryMock . fn . modal = Modal . jQueryInterface
2019-05-01 15:43:40 +02:00
jQueryMock . elements = [ div ]
spyOn ( Modal . prototype , 'show' )
jQueryMock . fn . modal . call ( jQueryMock )
expect ( Modal . prototype . show ) . not . toHaveBeenCalled ( )
} )
} )
2019-07-28 15:24:46 +02:00
describe ( 'getInstance' , ( ) => {
2019-05-01 15:43:40 +02:00
it ( 'should return modal instance' , ( ) => {
2020-05-11 15:54:56 +02:00
fixtureEl . innerHTML = '<div class="modal"><div class="modal-dialog"></div></div>'
2019-05-01 15:43:40 +02:00
const div = fixtureEl . querySelector ( 'div' )
const modal = new Modal ( div )
2019-07-28 15:24:46 +02:00
expect ( Modal . getInstance ( div ) ) . toEqual ( modal )
2020-11-16 16:23:09 +01:00
expect ( Modal . getInstance ( div ) ) . toBeInstanceOf ( Modal )
2019-05-01 15:43:40 +02:00
} )
it ( 'should return null when there is no modal instance' , ( ) => {
2020-05-11 15:54:56 +02:00
fixtureEl . innerHTML = '<div class="modal"><div class="modal-dialog"></div></div>'
2019-05-01 15:43:40 +02:00
const div = fixtureEl . querySelector ( 'div' )
2021-05-11 07:45:57 +02:00
expect ( Modal . getInstance ( div ) ) . toBeNull ( )
2019-05-01 15:43:40 +02:00
} )
} )
} )