2019-10-02 11:43:54 +02:00
|
|
|
import Alert from '../../src/alert'
|
2020-03-25 15:35:02 +01:00
|
|
|
import { getTransitionDurationFromElement } from '../../src/util/index'
|
2019-03-13 15:23:50 +01:00
|
|
|
|
|
|
|
/** Test helpers */
|
2021-06-28 15:34:47 +02:00
|
|
|
import { clearFixture, getFixture, jQueryMock } from '../helpers/fixture'
|
2019-03-13 15:23:50 +01:00
|
|
|
|
|
|
|
describe('Alert', () => {
|
|
|
|
let fixtureEl
|
|
|
|
|
|
|
|
beforeAll(() => {
|
|
|
|
fixtureEl = getFixture()
|
|
|
|
})
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
clearFixture()
|
|
|
|
})
|
|
|
|
|
2021-02-22 08:01:04 +01:00
|
|
|
it('should take care of element either passed as a CSS selector or DOM element', () => {
|
|
|
|
fixtureEl.innerHTML = '<div class="alert"></div>'
|
|
|
|
|
|
|
|
const alertEl = fixtureEl.querySelector('.alert')
|
|
|
|
const alertBySelector = new Alert('.alert')
|
|
|
|
const alertByElement = new Alert(alertEl)
|
|
|
|
|
|
|
|
expect(alertBySelector._element).toEqual(alertEl)
|
|
|
|
expect(alertByElement._element).toEqual(alertEl)
|
|
|
|
})
|
|
|
|
|
2019-03-13 15:23:50 +01:00
|
|
|
it('should return version', () => {
|
|
|
|
expect(typeof Alert.VERSION).toEqual('string')
|
|
|
|
})
|
|
|
|
|
2021-02-16 07:58:08 +01:00
|
|
|
describe('DATA_KEY', () => {
|
|
|
|
it('should return plugin data key', () => {
|
|
|
|
expect(Alert.DATA_KEY).toEqual('bs.alert')
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2019-03-13 15:23:50 +01:00
|
|
|
describe('data-api', () => {
|
2020-02-10 08:35:13 +01:00
|
|
|
it('should close an alert without instantiating it manually', () => {
|
2019-03-13 15:23:50 +01:00
|
|
|
fixtureEl.innerHTML = [
|
|
|
|
'<div class="alert">',
|
2020-07-22 21:33:11 +02:00
|
|
|
' <button type="button" data-bs-dismiss="alert">x</button>',
|
2019-03-13 15:23:50 +01:00
|
|
|
'</div>'
|
|
|
|
].join('')
|
|
|
|
|
|
|
|
const button = document.querySelector('button')
|
|
|
|
|
|
|
|
button.click()
|
2020-03-25 15:35:02 +01:00
|
|
|
expect(document.querySelectorAll('.alert').length).toEqual(0)
|
2019-03-13 15:23:50 +01:00
|
|
|
})
|
|
|
|
|
2020-02-10 08:35:13 +01:00
|
|
|
it('should close an alert without instantiating it manually with the parent selector', () => {
|
2019-03-13 15:23:50 +01:00
|
|
|
fixtureEl.innerHTML = [
|
|
|
|
'<div class="alert">',
|
2020-07-22 21:33:11 +02:00
|
|
|
' <button type="button" data-bs-target=".alert" data-bs-dismiss="alert">x</button>',
|
2019-03-13 15:23:50 +01:00
|
|
|
'</div>'
|
|
|
|
].join('')
|
|
|
|
|
|
|
|
const button = document.querySelector('button')
|
|
|
|
|
|
|
|
button.click()
|
2020-03-25 15:35:02 +01:00
|
|
|
expect(document.querySelectorAll('.alert').length).toEqual(0)
|
2019-03-13 15:23:50 +01:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('close', () => {
|
|
|
|
it('should close an alert', done => {
|
|
|
|
const spy = jasmine.createSpy('spy', getTransitionDurationFromElement)
|
|
|
|
fixtureEl.innerHTML = '<div class="alert"></div>'
|
|
|
|
|
|
|
|
const alertEl = document.querySelector('.alert')
|
|
|
|
const alert = new Alert(alertEl)
|
|
|
|
|
|
|
|
alertEl.addEventListener('closed.bs.alert', () => {
|
2020-03-25 15:35:02 +01:00
|
|
|
expect(document.querySelectorAll('.alert').length).toEqual(0)
|
2019-03-13 15:23:50 +01:00
|
|
|
expect(spy).not.toHaveBeenCalled()
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
|
|
|
|
alert.close()
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should close alert with fade class', done => {
|
|
|
|
fixtureEl.innerHTML = '<div class="alert fade"></div>'
|
|
|
|
|
|
|
|
const alertEl = document.querySelector('.alert')
|
|
|
|
const alert = new Alert(alertEl)
|
|
|
|
|
|
|
|
alertEl.addEventListener('transitionend', () => {
|
|
|
|
expect().nothing()
|
|
|
|
})
|
|
|
|
|
|
|
|
alertEl.addEventListener('closed.bs.alert', () => {
|
2020-03-25 15:35:02 +01:00
|
|
|
expect(document.querySelectorAll('.alert').length).toEqual(0)
|
2019-03-13 15:23:50 +01:00
|
|
|
done()
|
|
|
|
})
|
|
|
|
|
|
|
|
alert.close()
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should not remove alert if close event is prevented', done => {
|
|
|
|
fixtureEl.innerHTML = '<div class="alert"></div>'
|
|
|
|
|
2021-06-28 15:34:47 +02:00
|
|
|
const getAlert = () => document.querySelector('.alert')
|
|
|
|
const alertEl = getAlert()
|
2019-03-13 15:23:50 +01:00
|
|
|
const alert = new Alert(alertEl)
|
|
|
|
|
2021-06-28 15:34:47 +02:00
|
|
|
alertEl.addEventListener('close.bs.alert', event => {
|
|
|
|
event.preventDefault()
|
2019-03-13 15:23:50 +01:00
|
|
|
setTimeout(() => {
|
2021-06-28 15:34:47 +02:00
|
|
|
expect(getAlert()).not.toBeNull()
|
2019-03-13 15:23:50 +01:00
|
|
|
done()
|
|
|
|
}, 10)
|
|
|
|
})
|
|
|
|
|
|
|
|
alertEl.addEventListener('closed.bs.alert', () => {
|
2021-06-28 15:34:47 +02:00
|
|
|
throw new Error('should not fire closed event')
|
2019-03-13 15:23:50 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
alert.close()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('dispose', () => {
|
|
|
|
it('should dispose an alert', () => {
|
|
|
|
fixtureEl.innerHTML = '<div class="alert"></div>'
|
|
|
|
|
|
|
|
const alertEl = document.querySelector('.alert')
|
|
|
|
const alert = new Alert(alertEl)
|
|
|
|
|
2021-05-11 07:45:57 +02:00
|
|
|
expect(Alert.getInstance(alertEl)).not.toBeNull()
|
2019-03-13 15:23:50 +01:00
|
|
|
|
|
|
|
alert.dispose()
|
|
|
|
|
2019-07-28 15:24:46 +02:00
|
|
|
expect(Alert.getInstance(alertEl)).toBeNull()
|
2019-03-13 15:23:50 +01:00
|
|
|
})
|
|
|
|
})
|
2019-03-27 09:47:57 +01:00
|
|
|
|
2019-07-28 15:24:46 +02:00
|
|
|
describe('jQueryInterface', () => {
|
2019-03-27 09:47:57 +01:00
|
|
|
it('should handle config passed and toggle existing alert', () => {
|
|
|
|
fixtureEl.innerHTML = '<div class="alert"></div>'
|
|
|
|
|
|
|
|
const alertEl = fixtureEl.querySelector('.alert')
|
|
|
|
const alert = new Alert(alertEl)
|
|
|
|
|
|
|
|
spyOn(alert, 'close')
|
|
|
|
|
2019-07-28 15:24:46 +02:00
|
|
|
jQueryMock.fn.alert = Alert.jQueryInterface
|
2019-03-27 09:47:57 +01:00
|
|
|
jQueryMock.elements = [alertEl]
|
|
|
|
|
|
|
|
jQueryMock.fn.alert.call(jQueryMock, 'close')
|
|
|
|
|
|
|
|
expect(alert.close).toHaveBeenCalled()
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should create new alert instance and call close', () => {
|
|
|
|
fixtureEl.innerHTML = '<div class="alert"></div>'
|
|
|
|
|
|
|
|
const alertEl = fixtureEl.querySelector('.alert')
|
|
|
|
|
2019-07-28 15:24:46 +02:00
|
|
|
jQueryMock.fn.alert = Alert.jQueryInterface
|
2019-03-27 09:47:57 +01:00
|
|
|
jQueryMock.elements = [alertEl]
|
|
|
|
|
2021-06-28 15:34:47 +02:00
|
|
|
expect(Alert.getInstance(alertEl)).toBeNull()
|
2019-03-27 09:47:57 +01:00
|
|
|
jQueryMock.fn.alert.call(jQueryMock, 'close')
|
|
|
|
|
|
|
|
expect(fixtureEl.querySelector('.alert')).toBeNull()
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should just create an alert instance without calling close', () => {
|
|
|
|
fixtureEl.innerHTML = '<div class="alert"></div>'
|
|
|
|
|
|
|
|
const alertEl = fixtureEl.querySelector('.alert')
|
|
|
|
|
2019-07-28 15:24:46 +02:00
|
|
|
jQueryMock.fn.alert = Alert.jQueryInterface
|
2019-03-27 09:47:57 +01:00
|
|
|
jQueryMock.elements = [alertEl]
|
|
|
|
|
|
|
|
jQueryMock.fn.alert.call(jQueryMock)
|
|
|
|
|
2021-05-11 07:45:57 +02:00
|
|
|
expect(Alert.getInstance(alertEl)).not.toBeNull()
|
2019-03-27 09:47:57 +01:00
|
|
|
expect(fixtureEl.querySelector('.alert')).not.toBeNull()
|
|
|
|
})
|
|
|
|
})
|
2019-09-04 16:58:29 +02:00
|
|
|
|
|
|
|
describe('getInstance', () => {
|
|
|
|
it('should return alert instance', () => {
|
|
|
|
fixtureEl.innerHTML = '<div></div>'
|
|
|
|
|
|
|
|
const div = fixtureEl.querySelector('div')
|
|
|
|
const alert = new Alert(div)
|
|
|
|
|
|
|
|
expect(Alert.getInstance(div)).toEqual(alert)
|
2020-11-16 16:23:09 +01:00
|
|
|
expect(Alert.getInstance(div)).toBeInstanceOf(Alert)
|
2019-09-04 16:58:29 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
it('should return null when there is no alert instance', () => {
|
|
|
|
fixtureEl.innerHTML = '<div></div>'
|
|
|
|
|
|
|
|
const div = fixtureEl.querySelector('div')
|
|
|
|
|
|
|
|
expect(Alert.getInstance(div)).toEqual(null)
|
|
|
|
})
|
|
|
|
})
|
2021-06-03 17:53:27 +02:00
|
|
|
|
|
|
|
describe('getOrCreateInstance', () => {
|
|
|
|
it('should return alert instance', () => {
|
|
|
|
fixtureEl.innerHTML = '<div></div>'
|
|
|
|
|
|
|
|
const div = fixtureEl.querySelector('div')
|
|
|
|
const alert = new Alert(div)
|
|
|
|
|
|
|
|
expect(Alert.getOrCreateInstance(div)).toEqual(alert)
|
|
|
|
expect(Alert.getInstance(div)).toEqual(Alert.getOrCreateInstance(div, {}))
|
|
|
|
expect(Alert.getOrCreateInstance(div)).toBeInstanceOf(Alert)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should return new instance when there is no alert instance', () => {
|
|
|
|
fixtureEl.innerHTML = '<div></div>'
|
|
|
|
|
|
|
|
const div = fixtureEl.querySelector('div')
|
|
|
|
|
|
|
|
expect(Alert.getInstance(div)).toEqual(null)
|
|
|
|
expect(Alert.getOrCreateInstance(div)).toBeInstanceOf(Alert)
|
|
|
|
})
|
|
|
|
})
|
2019-03-13 15:23:50 +01:00
|
|
|
})
|