2021-04-14 22:28:50 +02:00
|
|
|
import Backdrop from '../../../src/util/backdrop'
|
|
|
|
import { getTransitionDurationFromElement } from '../../../src/util/index'
|
|
|
|
import { clearFixture, getFixture } from '../../helpers/fixture'
|
|
|
|
|
|
|
|
const CLASS_BACKDROP = '.modal-backdrop'
|
|
|
|
const CLASS_NAME_FADE = 'fade'
|
|
|
|
const CLASS_NAME_SHOW = 'show'
|
|
|
|
|
|
|
|
describe('Backdrop', () => {
|
|
|
|
let fixtureEl
|
|
|
|
|
|
|
|
beforeAll(() => {
|
|
|
|
fixtureEl = getFixture()
|
|
|
|
})
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
clearFixture()
|
|
|
|
const list = document.querySelectorAll(CLASS_BACKDROP)
|
|
|
|
|
2021-07-30 08:28:51 +02:00
|
|
|
for (const el of list) {
|
2021-07-30 00:23:00 +02:00
|
|
|
el.remove()
|
2021-07-30 08:28:51 +02:00
|
|
|
}
|
2021-04-14 22:28:50 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
describe('show', () => {
|
2021-11-26 09:09:12 +01:00
|
|
|
it('should append the backdrop html once on show and include the "show" class if it is "shown"', done => {
|
2021-04-14 22:28:50 +02:00
|
|
|
const instance = new Backdrop({
|
|
|
|
isVisible: true,
|
|
|
|
isAnimated: false
|
|
|
|
})
|
|
|
|
const getElements = () => document.querySelectorAll(CLASS_BACKDROP)
|
|
|
|
|
2021-10-14 17:16:54 +02:00
|
|
|
expect(getElements()).toHaveSize(0)
|
2021-04-14 22:28:50 +02:00
|
|
|
|
|
|
|
instance.show()
|
|
|
|
instance.show(() => {
|
2021-10-14 17:16:54 +02:00
|
|
|
expect(getElements()).toHaveSize(1)
|
2021-07-30 08:28:51 +02:00
|
|
|
for (const el of getElements()) {
|
2021-10-14 17:16:54 +02:00
|
|
|
expect(el).toHaveClass(CLASS_NAME_SHOW)
|
2021-07-30 08:28:51 +02:00
|
|
|
}
|
|
|
|
|
2021-04-14 22:28:50 +02:00
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-11-26 09:09:12 +01:00
|
|
|
it('should not append the backdrop html if it is not "shown"', done => {
|
2021-04-14 22:28:50 +02:00
|
|
|
const instance = new Backdrop({
|
|
|
|
isVisible: false,
|
|
|
|
isAnimated: true
|
|
|
|
})
|
|
|
|
const getElements = () => document.querySelectorAll(CLASS_BACKDROP)
|
|
|
|
|
2021-10-14 17:16:54 +02:00
|
|
|
expect(getElements()).toHaveSize(0)
|
2021-04-14 22:28:50 +02:00
|
|
|
instance.show(() => {
|
2021-10-14 17:16:54 +02:00
|
|
|
expect(getElements()).toHaveSize(0)
|
2021-04-14 22:28:50 +02:00
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-11-26 09:09:12 +01:00
|
|
|
it('should append the backdrop html once and include the "fade" class if it is "shown" and "animated"', done => {
|
2021-04-14 22:28:50 +02:00
|
|
|
const instance = new Backdrop({
|
|
|
|
isVisible: true,
|
|
|
|
isAnimated: true
|
|
|
|
})
|
|
|
|
const getElements = () => document.querySelectorAll(CLASS_BACKDROP)
|
|
|
|
|
2021-10-14 17:16:54 +02:00
|
|
|
expect(getElements()).toHaveSize(0)
|
2021-04-14 22:28:50 +02:00
|
|
|
|
|
|
|
instance.show(() => {
|
2021-10-14 17:16:54 +02:00
|
|
|
expect(getElements()).toHaveSize(1)
|
2021-07-30 08:28:51 +02:00
|
|
|
for (const el of getElements()) {
|
2021-10-14 17:16:54 +02:00
|
|
|
expect(el).toHaveClass(CLASS_NAME_FADE)
|
2021-07-30 08:28:51 +02:00
|
|
|
}
|
|
|
|
|
2021-04-14 22:28:50 +02:00
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('hide', () => {
|
|
|
|
it('should remove the backdrop html', done => {
|
|
|
|
const instance = new Backdrop({
|
|
|
|
isVisible: true,
|
|
|
|
isAnimated: true
|
|
|
|
})
|
|
|
|
|
|
|
|
const getElements = () => document.body.querySelectorAll(CLASS_BACKDROP)
|
|
|
|
|
2021-10-14 17:16:54 +02:00
|
|
|
expect(getElements()).toHaveSize(0)
|
2021-04-14 22:28:50 +02:00
|
|
|
instance.show(() => {
|
2021-10-14 17:16:54 +02:00
|
|
|
expect(getElements()).toHaveSize(1)
|
2021-04-14 22:28:50 +02:00
|
|
|
instance.hide(() => {
|
2021-10-14 17:16:54 +02:00
|
|
|
expect(getElements()).toHaveSize(0)
|
2021-04-14 22:28:50 +02:00
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-11-26 09:09:12 +01:00
|
|
|
it('should remove the "show" class', done => {
|
2021-04-14 22:28:50 +02:00
|
|
|
const instance = new Backdrop({
|
|
|
|
isVisible: true,
|
|
|
|
isAnimated: true
|
|
|
|
})
|
|
|
|
const elem = instance._getElement()
|
|
|
|
|
|
|
|
instance.show()
|
|
|
|
instance.hide(() => {
|
2021-10-14 17:16:54 +02:00
|
|
|
expect(elem).not.toHaveClass(CLASS_NAME_SHOW)
|
2021-04-14 22:28:50 +02:00
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-11-26 09:09:12 +01:00
|
|
|
it('should not try to remove Node on remove method if it is not "shown"', done => {
|
2021-04-14 22:28:50 +02:00
|
|
|
const instance = new Backdrop({
|
|
|
|
isVisible: false,
|
|
|
|
isAnimated: true
|
|
|
|
})
|
|
|
|
const getElements = () => document.querySelectorAll(CLASS_BACKDROP)
|
|
|
|
const spy = spyOn(instance, 'dispose').and.callThrough()
|
|
|
|
|
2021-10-14 17:16:54 +02:00
|
|
|
expect(getElements()).toHaveSize(0)
|
|
|
|
expect(instance._isAppended).toBeFalse()
|
2021-04-14 22:28:50 +02:00
|
|
|
instance.show(() => {
|
|
|
|
instance.hide(() => {
|
2021-10-14 17:16:54 +02:00
|
|
|
expect(getElements()).toHaveSize(0)
|
2021-04-14 22:28:50 +02:00
|
|
|
expect(spy).not.toHaveBeenCalled()
|
2021-10-14 17:16:54 +02:00
|
|
|
expect(instance._isAppended).toBeFalse()
|
2021-04-14 22:28:50 +02:00
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-05-22 02:54:32 +02:00
|
|
|
it('should not error if the backdrop no longer has a parent', done => {
|
|
|
|
fixtureEl.innerHTML = '<div id="wrapper"></div>'
|
2021-05-22 00:16:05 +02:00
|
|
|
|
2021-05-22 02:54:32 +02:00
|
|
|
const wrapper = fixtureEl.querySelector('#wrapper')
|
|
|
|
const instance = new Backdrop({
|
|
|
|
isVisible: true,
|
|
|
|
isAnimated: true,
|
|
|
|
rootElement: wrapper
|
|
|
|
})
|
2021-05-22 00:16:05 +02:00
|
|
|
|
2021-05-22 02:54:32 +02:00
|
|
|
const getElements = () => document.querySelectorAll(CLASS_BACKDROP)
|
|
|
|
|
|
|
|
instance.show(() => {
|
2021-07-30 00:23:00 +02:00
|
|
|
wrapper.remove()
|
2021-05-22 02:54:32 +02:00
|
|
|
instance.hide(() => {
|
2021-10-14 17:16:54 +02:00
|
|
|
expect(getElements()).toHaveSize(0)
|
2021-05-22 02:54:32 +02:00
|
|
|
done()
|
|
|
|
})
|
2021-05-22 00:16:05 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-04-19 07:20:25 +02:00
|
|
|
describe('click callback', () => {
|
2021-11-26 09:09:12 +01:00
|
|
|
it('should execute callback on click', done => {
|
2021-04-19 07:20:25 +02:00
|
|
|
const spy = jasmine.createSpy('spy')
|
|
|
|
|
|
|
|
const instance = new Backdrop({
|
|
|
|
isVisible: true,
|
|
|
|
isAnimated: false,
|
|
|
|
clickCallback: () => spy()
|
|
|
|
})
|
|
|
|
const endTest = () => {
|
|
|
|
setTimeout(() => {
|
|
|
|
expect(spy).toHaveBeenCalled()
|
|
|
|
done()
|
|
|
|
}, 10)
|
|
|
|
}
|
|
|
|
|
|
|
|
instance.show(() => {
|
|
|
|
const clickEvent = document.createEvent('MouseEvents')
|
|
|
|
clickEvent.initEvent('mousedown', true, true)
|
|
|
|
document.querySelector(CLASS_BACKDROP).dispatchEvent(clickEvent)
|
|
|
|
endTest()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-04-14 22:28:50 +02:00
|
|
|
describe('animation callbacks', () => {
|
2021-11-26 09:09:12 +01:00
|
|
|
it('should show and hide backdrop after counting transition duration if it is animated', done => {
|
2021-04-14 22:28:50 +02:00
|
|
|
const instance = new Backdrop({
|
|
|
|
isVisible: true,
|
|
|
|
isAnimated: true
|
|
|
|
})
|
|
|
|
const spy2 = jasmine.createSpy('spy2')
|
|
|
|
|
|
|
|
const execDone = () => {
|
|
|
|
setTimeout(() => {
|
|
|
|
expect(spy2).toHaveBeenCalledTimes(2)
|
|
|
|
done()
|
|
|
|
}, 10)
|
|
|
|
}
|
|
|
|
|
|
|
|
instance.show(spy2)
|
|
|
|
instance.hide(() => {
|
|
|
|
spy2()
|
|
|
|
execDone()
|
|
|
|
})
|
|
|
|
expect(spy2).not.toHaveBeenCalled()
|
|
|
|
})
|
|
|
|
|
2021-11-26 09:09:12 +01:00
|
|
|
it('should show and hide backdrop without a delay if it is not animated', done => {
|
2021-04-14 22:28:50 +02:00
|
|
|
const spy = jasmine.createSpy('spy', getTransitionDurationFromElement)
|
|
|
|
const instance = new Backdrop({
|
|
|
|
isVisible: true,
|
|
|
|
isAnimated: false
|
|
|
|
})
|
|
|
|
const spy2 = jasmine.createSpy('spy2')
|
|
|
|
|
|
|
|
instance.show(spy2)
|
|
|
|
instance.hide(spy2)
|
|
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
expect(spy2).toHaveBeenCalled()
|
|
|
|
expect(spy).not.toHaveBeenCalled()
|
|
|
|
done()
|
|
|
|
}, 10)
|
|
|
|
})
|
|
|
|
|
2021-11-26 09:09:12 +01:00
|
|
|
it('should not call delay callbacks if it is not "shown"', done => {
|
2021-04-14 22:28:50 +02:00
|
|
|
const instance = new Backdrop({
|
|
|
|
isVisible: false,
|
|
|
|
isAnimated: true
|
|
|
|
})
|
|
|
|
const spy = jasmine.createSpy('spy', getTransitionDurationFromElement)
|
|
|
|
|
|
|
|
instance.show()
|
|
|
|
instance.hide(() => {
|
|
|
|
expect(spy).not.toHaveBeenCalled()
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2021-11-26 08:16:59 +01:00
|
|
|
|
2021-06-25 22:41:15 +02:00
|
|
|
describe('Config', () => {
|
|
|
|
describe('rootElement initialization', () => {
|
2021-11-26 09:09:12 +01:00
|
|
|
it('should be appended on "document.body" by default', done => {
|
2021-06-25 22:41:15 +02:00
|
|
|
const instance = new Backdrop({
|
|
|
|
isVisible: true
|
|
|
|
})
|
|
|
|
const getElement = () => document.querySelector(CLASS_BACKDROP)
|
|
|
|
instance.show(() => {
|
|
|
|
expect(getElement().parentElement).toEqual(document.body)
|
|
|
|
done()
|
|
|
|
})
|
2021-05-10 20:17:53 +02:00
|
|
|
})
|
|
|
|
|
2021-11-26 09:09:12 +01:00
|
|
|
it('should find the rootElement if passed as a string', done => {
|
2021-06-25 22:41:15 +02:00
|
|
|
const instance = new Backdrop({
|
|
|
|
isVisible: true,
|
|
|
|
rootElement: 'body'
|
|
|
|
})
|
|
|
|
const getElement = () => document.querySelector(CLASS_BACKDROP)
|
|
|
|
instance.show(() => {
|
|
|
|
expect(getElement().parentElement).toEqual(document.body)
|
|
|
|
done()
|
|
|
|
})
|
2021-05-10 20:17:53 +02:00
|
|
|
})
|
|
|
|
|
2021-11-26 09:09:12 +01:00
|
|
|
it('should be appended on any element given by the proper config', done => {
|
2021-11-26 08:16:59 +01:00
|
|
|
fixtureEl.innerHTML = '<div id="wrapper"></div>'
|
2021-05-10 20:17:53 +02:00
|
|
|
|
2021-06-25 22:41:15 +02:00
|
|
|
const wrapper = fixtureEl.querySelector('#wrapper')
|
|
|
|
const instance = new Backdrop({
|
|
|
|
isVisible: true,
|
|
|
|
rootElement: wrapper
|
|
|
|
})
|
|
|
|
const getElement = () => document.querySelector(CLASS_BACKDROP)
|
|
|
|
instance.show(() => {
|
|
|
|
expect(getElement().parentElement).toEqual(wrapper)
|
|
|
|
done()
|
|
|
|
})
|
2021-05-10 20:17:53 +02:00
|
|
|
})
|
2021-06-25 22:41:15 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
describe('ClassName', () => {
|
2021-11-26 09:09:12 +01:00
|
|
|
it('should allow configuring className', done => {
|
2021-06-25 22:41:15 +02:00
|
|
|
const instance = new Backdrop({
|
|
|
|
isVisible: true,
|
|
|
|
className: 'foo'
|
|
|
|
})
|
|
|
|
const getElement = () => document.querySelector('.foo')
|
|
|
|
instance.show(() => {
|
|
|
|
expect(getElement()).toEqual(instance._getElement())
|
|
|
|
instance.dispose()
|
|
|
|
done()
|
|
|
|
})
|
2021-05-10 20:17:53 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2021-04-14 22:28:50 +02:00
|
|
|
})
|