0
0
mirror of https://github.com/twbs/bootstrap.git synced 2024-11-29 11:24:18 +01:00

tests: replace 'done' callback with 'Promise' to fix deprecation errors (#35659)

Reference:

https://jasmine.github.io/tutorials/async

'DEPRECATION: An asynchronous function called its 'done' callback more than once. This is a bug in the spec, beforeAll, beforeEach, afterAll, or afterEach function in question. This will be treated as an error in a future version. See<https://jasmine.github.io/tutorials/upgrading_to_Jasmine_4.0#deprecations-due-to-calling-done-multiple-times> for more information.
This commit is contained in:
GeoSot 2022-01-30 14:30:04 +02:00 committed by GitHub
parent d092817059
commit aa650f0f1e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
20 changed files with 7154 additions and 6571 deletions

View File

@ -50,22 +50,24 @@ describe('getInstance', () => {
}) })
// Asynchronous test // Asynchronous test
it('should show a tooltip without the animation', done => { it('should show a tooltip without the animation', () => {
fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip"></a>' return new Promise(resolve => {
fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip"></a>'
const tooltipEl = fixtureEl.querySelector('a') const tooltipEl = fixtureEl.querySelector('a')
const tooltip = new Tooltip(tooltipEl, { const tooltip = new Tooltip(tooltipEl, {
animation: false animation: false
})
tooltipEl.addEventListener('shown.bs.tooltip', () => {
const tip = document.querySelector('.tooltip')
expect(tip).not.toBeNull()
expect(tip.classList.contains('fade')).toEqual(false)
resolve()
})
tooltip.show()
}) })
tooltipEl.addEventListener('shown.bs.tooltip', () => {
const tip = document.querySelector('.tooltip')
expect(tip).not.toBeNull()
expect(tip.classList.contains('fade')).toEqual(false)
done()
})
tooltip.show()
}) })
``` ```

View File

@ -63,60 +63,66 @@ describe('Alert', () => {
}) })
describe('close', () => { describe('close', () => {
it('should close an alert', done => { it('should close an alert', () => {
const spy = jasmine.createSpy('spy', getTransitionDurationFromElement) return new Promise(resolve => {
fixtureEl.innerHTML = '<div class="alert"></div>' const spy = jasmine.createSpy('spy', getTransitionDurationFromElement)
fixtureEl.innerHTML = '<div class="alert"></div>'
const alertEl = document.querySelector('.alert') const alertEl = document.querySelector('.alert')
const alert = new Alert(alertEl) const alert = new Alert(alertEl)
alertEl.addEventListener('closed.bs.alert', () => { alertEl.addEventListener('closed.bs.alert', () => {
expect(document.querySelectorAll('.alert')).toHaveSize(0) expect(document.querySelectorAll('.alert')).toHaveSize(0)
expect(spy).not.toHaveBeenCalled() expect(spy).not.toHaveBeenCalled()
done() resolve()
})
alert.close()
}) })
alert.close()
}) })
it('should close alert with fade class', done => { it('should close alert with fade class', () => {
fixtureEl.innerHTML = '<div class="alert fade"></div>' return new Promise(resolve => {
fixtureEl.innerHTML = '<div class="alert fade"></div>'
const alertEl = document.querySelector('.alert') const alertEl = document.querySelector('.alert')
const alert = new Alert(alertEl) const alert = new Alert(alertEl)
alertEl.addEventListener('transitionend', () => { alertEl.addEventListener('transitionend', () => {
expect().nothing() expect().nothing()
})
alertEl.addEventListener('closed.bs.alert', () => {
expect(document.querySelectorAll('.alert')).toHaveSize(0)
resolve()
})
alert.close()
}) })
alertEl.addEventListener('closed.bs.alert', () => {
expect(document.querySelectorAll('.alert')).toHaveSize(0)
done()
})
alert.close()
}) })
it('should not remove alert if close event is prevented', done => { it('should not remove alert if close event is prevented', () => {
fixtureEl.innerHTML = '<div class="alert"></div>' return new Promise(resolve => {
fixtureEl.innerHTML = '<div class="alert"></div>'
const getAlert = () => document.querySelector('.alert') const getAlert = () => document.querySelector('.alert')
const alertEl = getAlert() const alertEl = getAlert()
const alert = new Alert(alertEl) const alert = new Alert(alertEl)
alertEl.addEventListener('close.bs.alert', event => { alertEl.addEventListener('close.bs.alert', event => {
event.preventDefault() event.preventDefault()
setTimeout(() => { setTimeout(() => {
expect(getAlert()).not.toBeNull() expect(getAlert()).not.toBeNull()
done() resolve()
}, 10) }, 10)
})
alertEl.addEventListener('closed.bs.alert', () => {
throw new Error('should not fire closed event')
})
alert.close()
}) })
alertEl.addEventListener('closed.bs.alert', () => {
throw new Error('should not fire closed event')
})
alert.close()
}) })
}) })

View File

@ -63,94 +63,100 @@ describe('Carousel', () => {
expect(carouselByElement._element).toEqual(carouselEl) expect(carouselByElement._element).toEqual(carouselEl)
}) })
it('should go to next item if right arrow key is pressed', done => { it('should go to next item if right arrow key is pressed', () => {
fixtureEl.innerHTML = [ return new Promise(resolve => {
'<div id="myCarousel" class="carousel slide">', fixtureEl.innerHTML = [
' <div class="carousel-inner">', '<div id="myCarousel" class="carousel slide">',
' <div class="carousel-item active">item 1</div>', ' <div class="carousel-inner">',
' <div id="item2" class="carousel-item">item 2</div>', ' <div class="carousel-item active">item 1</div>',
' <div class="carousel-item">item 3</div>', ' <div id="item2" class="carousel-item">item 2</div>',
' </div>', ' <div class="carousel-item">item 3</div>',
'</div>' ' </div>',
].join('') '</div>'
].join('')
const carouselEl = fixtureEl.querySelector('#myCarousel') const carouselEl = fixtureEl.querySelector('#myCarousel')
const carousel = new Carousel(carouselEl, { const carousel = new Carousel(carouselEl, {
keyboard: true keyboard: true
})
spyOn(carousel, '_keydown').and.callThrough()
carouselEl.addEventListener('slid.bs.carousel', () => {
expect(fixtureEl.querySelector('.active')).toEqual(fixtureEl.querySelector('#item2'))
expect(carousel._keydown).toHaveBeenCalled()
resolve()
})
const keydown = createEvent('keydown')
keydown.key = 'ArrowRight'
carouselEl.dispatchEvent(keydown)
}) })
spyOn(carousel, '_keydown').and.callThrough()
carouselEl.addEventListener('slid.bs.carousel', () => {
expect(fixtureEl.querySelector('.active')).toEqual(fixtureEl.querySelector('#item2'))
expect(carousel._keydown).toHaveBeenCalled()
done()
})
const keydown = createEvent('keydown')
keydown.key = 'ArrowRight'
carouselEl.dispatchEvent(keydown)
}) })
it('should go to previous item if left arrow key is pressed', done => { it('should go to previous item if left arrow key is pressed', () => {
fixtureEl.innerHTML = [ return new Promise(resolve => {
'<div id="myCarousel" class="carousel slide">', fixtureEl.innerHTML = [
' <div class="carousel-inner">', '<div id="myCarousel" class="carousel slide">',
' <div id="item1" class="carousel-item">item 1</div>', ' <div class="carousel-inner">',
' <div class="carousel-item active">item 2</div>', ' <div id="item1" class="carousel-item">item 1</div>',
' <div class="carousel-item">item 3</div>', ' <div class="carousel-item active">item 2</div>',
' </div>', ' <div class="carousel-item">item 3</div>',
'</div>' ' </div>',
].join('') '</div>'
].join('')
const carouselEl = fixtureEl.querySelector('#myCarousel') const carouselEl = fixtureEl.querySelector('#myCarousel')
const carousel = new Carousel(carouselEl, { const carousel = new Carousel(carouselEl, {
keyboard: true keyboard: true
})
spyOn(carousel, '_keydown').and.callThrough()
carouselEl.addEventListener('slid.bs.carousel', () => {
expect(fixtureEl.querySelector('.active')).toEqual(fixtureEl.querySelector('#item1'))
expect(carousel._keydown).toHaveBeenCalled()
resolve()
})
const keydown = createEvent('keydown')
keydown.key = 'ArrowLeft'
carouselEl.dispatchEvent(keydown)
}) })
spyOn(carousel, '_keydown').and.callThrough()
carouselEl.addEventListener('slid.bs.carousel', () => {
expect(fixtureEl.querySelector('.active')).toEqual(fixtureEl.querySelector('#item1'))
expect(carousel._keydown).toHaveBeenCalled()
done()
})
const keydown = createEvent('keydown')
keydown.key = 'ArrowLeft'
carouselEl.dispatchEvent(keydown)
}) })
it('should not prevent keydown if key is not ARROW_LEFT or ARROW_RIGHT', done => { it('should not prevent keydown if key is not ARROW_LEFT or ARROW_RIGHT', () => {
fixtureEl.innerHTML = [ return new Promise(resolve => {
'<div id="myCarousel" class="carousel slide">', fixtureEl.innerHTML = [
' <div class="carousel-inner">', '<div id="myCarousel" class="carousel slide">',
' <div class="carousel-item active">item 1</div>', ' <div class="carousel-inner">',
' <div class="carousel-item">item 2</div>', ' <div class="carousel-item active">item 1</div>',
' <div class="carousel-item">item 3</div>', ' <div class="carousel-item">item 2</div>',
' </div>', ' <div class="carousel-item">item 3</div>',
'</div>' ' </div>',
].join('') '</div>'
].join('')
const carouselEl = fixtureEl.querySelector('#myCarousel') const carouselEl = fixtureEl.querySelector('#myCarousel')
const carousel = new Carousel(carouselEl, { const carousel = new Carousel(carouselEl, {
keyboard: true keyboard: true
})
spyOn(carousel, '_keydown').and.callThrough()
carouselEl.addEventListener('keydown', event => {
expect(carousel._keydown).toHaveBeenCalled()
expect(event.defaultPrevented).toBeFalse()
resolve()
})
const keydown = createEvent('keydown')
keydown.key = 'ArrowDown'
carouselEl.dispatchEvent(keydown)
}) })
spyOn(carousel, '_keydown').and.callThrough()
carouselEl.addEventListener('keydown', event => {
expect(carousel._keydown).toHaveBeenCalled()
expect(event.defaultPrevented).toBeFalse()
done()
})
const keydown = createEvent('keydown')
keydown.key = 'ArrowDown'
carouselEl.dispatchEvent(keydown)
}) })
it('should ignore keyboard events within <input>s and <textarea>s', () => { it('should ignore keyboard events within <input>s and <textarea>s', () => {
@ -222,70 +228,74 @@ describe('Carousel', () => {
expect(carousel._triggerSlideEvent).not.toHaveBeenCalled() expect(carousel._triggerSlideEvent).not.toHaveBeenCalled()
}) })
it('should wrap around from end to start when wrap option is true', done => { it('should wrap around from end to start when wrap option is true', () => {
fixtureEl.innerHTML = [ return new Promise(resolve => {
'<div id="myCarousel" class="carousel slide">', fixtureEl.innerHTML = [
' <div class="carousel-inner">', '<div id="myCarousel" class="carousel slide">',
' <div id="one" class="carousel-item active"></div>', ' <div class="carousel-inner">',
' <div id="two" class="carousel-item"></div>', ' <div id="one" class="carousel-item active"></div>',
' <div id="three" class="carousel-item">item 3</div>', ' <div id="two" class="carousel-item"></div>',
' </div>', ' <div id="three" class="carousel-item">item 3</div>',
'</div>' ' </div>',
].join('') '</div>'
].join('')
const carouselEl = fixtureEl.querySelector('#myCarousel') const carouselEl = fixtureEl.querySelector('#myCarousel')
const carousel = new Carousel(carouselEl, { wrap: true }) const carousel = new Carousel(carouselEl, { wrap: true })
const getActiveId = () => carouselEl.querySelector('.carousel-item.active').getAttribute('id') const getActiveId = () => carouselEl.querySelector('.carousel-item.active').getAttribute('id')
carouselEl.addEventListener('slid.bs.carousel', event => { carouselEl.addEventListener('slid.bs.carousel', event => {
const activeId = getActiveId() const activeId = getActiveId()
if (activeId === 'two') { if (activeId === 'two') {
carousel.next() carousel.next()
return return
} }
if (activeId === 'three') { if (activeId === 'three') {
carousel.next() carousel.next()
return return
} }
if (activeId === 'one') { if (activeId === 'one') {
// carousel wrapped around and slid from 3rd to 1st slide // carousel wrapped around and slid from 3rd to 1st slide
expect(activeId).toEqual('one') expect(activeId).toEqual('one')
expect(event.from + 1).toEqual(3) expect(event.from + 1).toEqual(3)
done() resolve()
} }
})
carousel.next()
}) })
carousel.next()
}) })
it('should stay at the start when the prev method is called and wrap is false', done => { it('should stay at the start when the prev method is called and wrap is false', () => {
fixtureEl.innerHTML = [ return new Promise(resolve => {
'<div id="myCarousel" class="carousel slide">', fixtureEl.innerHTML = [
' <div class="carousel-inner">', '<div id="myCarousel" class="carousel slide">',
' <div id="one" class="carousel-item active"></div>', ' <div class="carousel-inner">',
' <div id="two" class="carousel-item"></div>', ' <div id="one" class="carousel-item active"></div>',
' <div id="three" class="carousel-item">item 3</div>', ' <div id="two" class="carousel-item"></div>',
' </div>', ' <div id="three" class="carousel-item">item 3</div>',
'</div>' ' </div>',
].join('') '</div>'
].join('')
const carouselEl = fixtureEl.querySelector('#myCarousel') const carouselEl = fixtureEl.querySelector('#myCarousel')
const firstElement = fixtureEl.querySelector('#one') const firstElement = fixtureEl.querySelector('#one')
const carousel = new Carousel(carouselEl, { wrap: false }) const carousel = new Carousel(carouselEl, { wrap: false })
carouselEl.addEventListener('slid.bs.carousel', () => { carouselEl.addEventListener('slid.bs.carousel', () => {
throw new Error('carousel slid when it should not have slid') throw new Error('carousel slid when it should not have slid')
})
carousel.prev()
setTimeout(() => {
expect(firstElement).toHaveClass('active')
resolve()
}, 10)
}) })
carousel.prev()
setTimeout(() => {
expect(firstElement).toHaveClass('active')
done()
}, 10)
}) })
it('should not add touch event listeners if touch = false', () => { it('should not add touch event listeners if touch = false', () => {
@ -335,274 +345,290 @@ describe('Carousel', () => {
expect(carousel._addTouchEventListeners).toHaveBeenCalled() expect(carousel._addTouchEventListeners).toHaveBeenCalled()
}) })
it('should allow swiperight and call _slide (prev) with pointer events', done => { it('should allow swiperight and call _slide (prev) with pointer events', () => {
if (!supportPointerEvent) { return new Promise(resolve => {
expect().nothing() if (!supportPointerEvent) {
done() expect().nothing()
return resolve()
} return
}
document.documentElement.ontouchstart = noop document.documentElement.ontouchstart = noop
document.head.append(stylesCarousel) document.head.append(stylesCarousel)
Simulator.setType('pointer') Simulator.setType('pointer')
fixtureEl.innerHTML = [ fixtureEl.innerHTML = [
'<div class="carousel" data-bs-interval="false">', '<div class="carousel" data-bs-interval="false">',
' <div class="carousel-inner">', ' <div class="carousel-inner">',
' <div id="item" class="carousel-item">', ' <div id="item" class="carousel-item">',
' <img alt="">', ' <img alt="">',
' </div>', ' </div>',
' <div class="carousel-item active">', ' <div class="carousel-item active">',
' <img alt="">', ' <img alt="">',
' </div>', ' </div>',
' </div>', ' </div>',
'</div>' '</div>'
].join('') ].join('')
const carouselEl = fixtureEl.querySelector('.carousel') const carouselEl = fixtureEl.querySelector('.carousel')
const item = fixtureEl.querySelector('#item') const item = fixtureEl.querySelector('#item')
const carousel = new Carousel(carouselEl) const carousel = new Carousel(carouselEl)
spyOn(carousel, '_slide').and.callThrough() spyOn(carousel, '_slide').and.callThrough()
carouselEl.addEventListener('slid.bs.carousel', event => { carouselEl.addEventListener('slid.bs.carousel', event => {
expect(item).toHaveClass('active') expect(item).toHaveClass('active')
expect(carousel._slide).toHaveBeenCalledWith('right') expect(carousel._slide).toHaveBeenCalledWith('right')
expect(event.direction).toEqual('right') expect(event.direction).toEqual('right')
stylesCarousel.remove() stylesCarousel.remove()
delete document.documentElement.ontouchstart delete document.documentElement.ontouchstart
done() resolve()
}) })
Simulator.gestures.swipe(carouselEl, { Simulator.gestures.swipe(carouselEl, {
deltaX: 300, deltaX: 300,
deltaY: 0 deltaY: 0
})
}) })
}) })
it('should allow swipeleft and call next with pointer events', done => { it('should allow swipeleft and call next with pointer events', () => {
if (!supportPointerEvent) { return new Promise(resolve => {
expect().nothing() if (!supportPointerEvent) {
done() expect().nothing()
return resolve()
} return
}
document.documentElement.ontouchstart = noop document.documentElement.ontouchstart = noop
document.head.append(stylesCarousel) document.head.append(stylesCarousel)
Simulator.setType('pointer') Simulator.setType('pointer')
fixtureEl.innerHTML = [ fixtureEl.innerHTML = [
'<div class="carousel" data-bs-interval="false">', '<div class="carousel" data-bs-interval="false">',
' <div class="carousel-inner">', ' <div class="carousel-inner">',
' <div id="item" class="carousel-item active">', ' <div id="item" class="carousel-item active">',
' <img alt="">', ' <img alt="">',
' </div>', ' </div>',
' <div class="carousel-item">', ' <div class="carousel-item">',
' <img alt="">', ' <img alt="">',
' </div>', ' </div>',
' </div>', ' </div>',
'</div>' '</div>'
].join('') ].join('')
const carouselEl = fixtureEl.querySelector('.carousel') const carouselEl = fixtureEl.querySelector('.carousel')
const item = fixtureEl.querySelector('#item') const item = fixtureEl.querySelector('#item')
const carousel = new Carousel(carouselEl) const carousel = new Carousel(carouselEl)
spyOn(carousel, '_slide').and.callThrough() spyOn(carousel, '_slide').and.callThrough()
carouselEl.addEventListener('slid.bs.carousel', event => { carouselEl.addEventListener('slid.bs.carousel', event => {
expect(item).not.toHaveClass('active') expect(item).not.toHaveClass('active')
expect(carousel._slide).toHaveBeenCalledWith('left') expect(carousel._slide).toHaveBeenCalledWith('left')
expect(event.direction).toEqual('left') expect(event.direction).toEqual('left')
stylesCarousel.remove() stylesCarousel.remove()
delete document.documentElement.ontouchstart delete document.documentElement.ontouchstart
done() resolve()
}) })
Simulator.gestures.swipe(carouselEl, { Simulator.gestures.swipe(carouselEl, {
pos: [300, 10], pos: [300, 10],
deltaX: -300, deltaX: -300,
deltaY: 0 deltaY: 0
})
}) })
}) })
it('should allow swiperight and call _slide (prev) with touch events', done => { it('should allow swiperight and call _slide (prev) with touch events', () => {
Simulator.setType('touch') return new Promise(resolve => {
clearPointerEvents() Simulator.setType('touch')
document.documentElement.ontouchstart = noop clearPointerEvents()
document.documentElement.ontouchstart = noop
fixtureEl.innerHTML = [ fixtureEl.innerHTML = [
'<div class="carousel" data-bs-interval="false">', '<div class="carousel" data-bs-interval="false">',
' <div class="carousel-inner">', ' <div class="carousel-inner">',
' <div id="item" class="carousel-item">', ' <div id="item" class="carousel-item">',
' <img alt="">', ' <img alt="">',
' </div>', ' </div>',
' <div class="carousel-item active">', ' <div class="carousel-item active">',
' <img alt="">', ' <img alt="">',
' </div>', ' </div>',
' </div>', ' </div>',
'</div>' '</div>'
].join('') ].join('')
const carouselEl = fixtureEl.querySelector('.carousel') const carouselEl = fixtureEl.querySelector('.carousel')
const item = fixtureEl.querySelector('#item') const item = fixtureEl.querySelector('#item')
const carousel = new Carousel(carouselEl) const carousel = new Carousel(carouselEl)
spyOn(carousel, '_slide').and.callThrough() spyOn(carousel, '_slide').and.callThrough()
carouselEl.addEventListener('slid.bs.carousel', event => { carouselEl.addEventListener('slid.bs.carousel', event => {
expect(item).toHaveClass('active') expect(item).toHaveClass('active')
expect(carousel._slide).toHaveBeenCalledWith('right') expect(carousel._slide).toHaveBeenCalledWith('right')
expect(event.direction).toEqual('right') expect(event.direction).toEqual('right')
delete document.documentElement.ontouchstart delete document.documentElement.ontouchstart
restorePointerEvents() restorePointerEvents()
done() resolve()
}) })
Simulator.gestures.swipe(carouselEl, { Simulator.gestures.swipe(carouselEl, {
deltaX: 300, deltaX: 300,
deltaY: 0 deltaY: 0
})
}) })
}) })
it('should allow swipeleft and call _slide (next) with touch events', done => { it('should allow swipeleft and call _slide (next) with touch events', () => {
Simulator.setType('touch') return new Promise(resolve => {
clearPointerEvents() Simulator.setType('touch')
document.documentElement.ontouchstart = noop clearPointerEvents()
document.documentElement.ontouchstart = noop
fixtureEl.innerHTML = [ fixtureEl.innerHTML = [
'<div class="carousel" data-bs-interval="false">', '<div class="carousel" data-bs-interval="false">',
' <div class="carousel-inner">', ' <div class="carousel-inner">',
' <div id="item" class="carousel-item active">', ' <div id="item" class="carousel-item active">',
' <img alt="">', ' <img alt="">',
' </div>', ' </div>',
' <div class="carousel-item">', ' <div class="carousel-item">',
' <img alt="">', ' <img alt="">',
' </div>', ' </div>',
' </div>', ' </div>',
'</div>' '</div>'
].join('') ].join('')
const carouselEl = fixtureEl.querySelector('.carousel') const carouselEl = fixtureEl.querySelector('.carousel')
const item = fixtureEl.querySelector('#item') const item = fixtureEl.querySelector('#item')
const carousel = new Carousel(carouselEl) const carousel = new Carousel(carouselEl)
spyOn(carousel, '_slide').and.callThrough() spyOn(carousel, '_slide').and.callThrough()
carouselEl.addEventListener('slid.bs.carousel', event => { carouselEl.addEventListener('slid.bs.carousel', event => {
expect(item).not.toHaveClass('active') expect(item).not.toHaveClass('active')
expect(carousel._slide).toHaveBeenCalledWith('left') expect(carousel._slide).toHaveBeenCalledWith('left')
expect(event.direction).toEqual('left') expect(event.direction).toEqual('left')
delete document.documentElement.ontouchstart delete document.documentElement.ontouchstart
restorePointerEvents() restorePointerEvents()
done() resolve()
}) })
Simulator.gestures.swipe(carouselEl, { Simulator.gestures.swipe(carouselEl, {
pos: [300, 10], pos: [300, 10],
deltaX: -300, deltaX: -300,
deltaY: 0 deltaY: 0
})
}) })
}) })
it('should not slide when swiping and carousel is sliding', done => { it('should not slide when swiping and carousel is sliding', () => {
Simulator.setType('touch') return new Promise(resolve => {
clearPointerEvents() Simulator.setType('touch')
document.documentElement.ontouchstart = noop clearPointerEvents()
document.documentElement.ontouchstart = noop
fixtureEl.innerHTML = [ fixtureEl.innerHTML = [
'<div class="carousel" data-bs-interval="false">', '<div class="carousel" data-bs-interval="false">',
' <div class="carousel-inner">', ' <div class="carousel-inner">',
' <div id="item" class="carousel-item active">', ' <div id="item" class="carousel-item active">',
' <img alt="">', ' <img alt="">',
' </div>', ' </div>',
' <div class="carousel-item">', ' <div class="carousel-item">',
' <img alt="">', ' <img alt="">',
' </div>', ' </div>',
' </div>', ' </div>',
'</div>' '</div>'
].join('') ].join('')
const carouselEl = fixtureEl.querySelector('.carousel') const carouselEl = fixtureEl.querySelector('.carousel')
const carousel = new Carousel(carouselEl) const carousel = new Carousel(carouselEl)
carousel._isSliding = true carousel._isSliding = true
spyOn(carousel, '_triggerSlideEvent') spyOn(carousel, '_triggerSlideEvent')
Simulator.gestures.swipe(carouselEl, { Simulator.gestures.swipe(carouselEl, {
deltaX: 300, deltaX: 300,
deltaY: 0 deltaY: 0
}) })
Simulator.gestures.swipe(carouselEl, { Simulator.gestures.swipe(carouselEl, {
pos: [300, 10], pos: [300, 10],
deltaX: -300, deltaX: -300,
deltaY: 0 deltaY: 0
}) })
setTimeout(() => { setTimeout(() => {
expect(carousel._triggerSlideEvent).not.toHaveBeenCalled() expect(carousel._triggerSlideEvent).not.toHaveBeenCalled()
delete document.documentElement.ontouchstart delete document.documentElement.ontouchstart
restorePointerEvents() restorePointerEvents()
done() resolve()
}, 300) }, 300)
})
it('should not allow pinch with touch events', done => {
Simulator.setType('touch')
clearPointerEvents()
document.documentElement.ontouchstart = noop
fixtureEl.innerHTML = '<div class="carousel" data-bs-interval="false"></div>'
const carouselEl = fixtureEl.querySelector('.carousel')
const carousel = new Carousel(carouselEl)
Simulator.gestures.swipe(carouselEl, {
pos: [300, 10],
deltaX: -300,
deltaY: 0,
touches: 2
}, () => {
restorePointerEvents()
delete document.documentElement.ontouchstart
expect(carousel._swipeHelper._deltaX).toEqual(0)
done()
}) })
}) })
it('should call pause method on mouse over with pause equal to hover', done => { it('should not allow pinch with touch events', () => {
fixtureEl.innerHTML = '<div class="carousel"></div>' return new Promise(resolve => {
Simulator.setType('touch')
clearPointerEvents()
document.documentElement.ontouchstart = noop
const carouselEl = fixtureEl.querySelector('.carousel') fixtureEl.innerHTML = '<div class="carousel" data-bs-interval="false"></div>'
const carousel = new Carousel(carouselEl)
spyOn(carousel, 'pause') const carouselEl = fixtureEl.querySelector('.carousel')
const carousel = new Carousel(carouselEl)
const mouseOverEvent = createEvent('mouseover') Simulator.gestures.swipe(carouselEl, {
carouselEl.dispatchEvent(mouseOverEvent) pos: [300, 10],
deltaX: -300,
setTimeout(() => { deltaY: 0,
expect(carousel.pause).toHaveBeenCalled() touches: 2
done() }, () => {
}, 10) restorePointerEvents()
delete document.documentElement.ontouchstart
expect(carousel._swipeHelper._deltaX).toEqual(0)
resolve()
})
})
}) })
it('should call cycle on mouse out with pause equal to hover', done => { it('should call pause method on mouse over with pause equal to hover', () => {
fixtureEl.innerHTML = '<div class="carousel"></div>' return new Promise(resolve => {
fixtureEl.innerHTML = '<div class="carousel"></div>'
const carouselEl = fixtureEl.querySelector('.carousel') const carouselEl = fixtureEl.querySelector('.carousel')
const carousel = new Carousel(carouselEl) const carousel = new Carousel(carouselEl)
spyOn(carousel, 'cycle') spyOn(carousel, 'pause')
const mouseOutEvent = createEvent('mouseout') const mouseOverEvent = createEvent('mouseover')
carouselEl.dispatchEvent(mouseOutEvent) carouselEl.dispatchEvent(mouseOverEvent)
setTimeout(() => { setTimeout(() => {
expect(carousel.cycle).toHaveBeenCalled() expect(carousel.pause).toHaveBeenCalled()
done() resolve()
}, 10) }, 10)
})
})
it('should call cycle on mouse out with pause equal to hover', () => {
return new Promise(resolve => {
fixtureEl.innerHTML = '<div class="carousel"></div>'
const carouselEl = fixtureEl.querySelector('.carousel')
const carousel = new Carousel(carouselEl)
spyOn(carousel, 'cycle')
const mouseOutEvent = createEvent('mouseout')
carouselEl.dispatchEvent(mouseOutEvent)
setTimeout(() => {
expect(carousel.cycle).toHaveBeenCalled()
resolve()
}, 10)
})
}) })
}) })
@ -621,100 +647,106 @@ describe('Carousel', () => {
expect(carousel._triggerSlideEvent).not.toHaveBeenCalled() expect(carousel._triggerSlideEvent).not.toHaveBeenCalled()
}) })
it('should not fire slid when slide is prevented', done => { it('should not fire slid when slide is prevented', () => {
fixtureEl.innerHTML = '<div></div>' return new Promise(resolve => {
fixtureEl.innerHTML = '<div></div>'
const carouselEl = fixtureEl.querySelector('div') const carouselEl = fixtureEl.querySelector('div')
const carousel = new Carousel(carouselEl, {}) const carousel = new Carousel(carouselEl, {})
let slidEvent = false let slidEvent = false
const doneTest = () => { const doneTest = () => {
setTimeout(() => { setTimeout(() => {
expect(slidEvent).toBeFalse() expect(slidEvent).toBeFalse()
done() resolve()
}, 20) }, 20)
} }
carouselEl.addEventListener('slide.bs.carousel', event => { carouselEl.addEventListener('slide.bs.carousel', event => {
event.preventDefault() event.preventDefault()
doneTest() doneTest()
})
carouselEl.addEventListener('slid.bs.carousel', () => {
slidEvent = true
})
carousel.next()
}) })
carouselEl.addEventListener('slid.bs.carousel', () => {
slidEvent = true
})
carousel.next()
}) })
it('should fire slide event with: direction, relatedTarget, from and to', done => { it('should fire slide event with: direction, relatedTarget, from and to', () => {
fixtureEl.innerHTML = [ return new Promise(resolve => {
'<div id="myCarousel" class="carousel slide">', fixtureEl.innerHTML = [
' <div class="carousel-inner">', '<div id="myCarousel" class="carousel slide">',
' <div class="carousel-item active">item 1</div>', ' <div class="carousel-inner">',
' <div class="carousel-item">item 2</div>', ' <div class="carousel-item active">item 1</div>',
' <div class="carousel-item">item 3</div>', ' <div class="carousel-item">item 2</div>',
' </div>', ' <div class="carousel-item">item 3</div>',
'</div>' ' </div>',
].join('') '</div>'
].join('')
const carouselEl = fixtureEl.querySelector('#myCarousel') const carouselEl = fixtureEl.querySelector('#myCarousel')
const carousel = new Carousel(carouselEl, {}) const carousel = new Carousel(carouselEl, {})
const onSlide = event => { const onSlide = event => {
expect(event.direction).toEqual('left') expect(event.direction).toEqual('left')
expect(event.relatedTarget).toHaveClass('carousel-item') expect(event.relatedTarget).toHaveClass('carousel-item')
expect(event.from).toEqual(0) expect(event.from).toEqual(0)
expect(event.to).toEqual(1) expect(event.to).toEqual(1)
carouselEl.removeEventListener('slide.bs.carousel', onSlide) carouselEl.removeEventListener('slide.bs.carousel', onSlide)
carouselEl.addEventListener('slide.bs.carousel', onSlide2) carouselEl.addEventListener('slide.bs.carousel', onSlide2)
carousel.prev() carousel.prev()
} }
const onSlide2 = event => { const onSlide2 = event => {
expect(event.direction).toEqual('right') expect(event.direction).toEqual('right')
done() resolve()
} }
carouselEl.addEventListener('slide.bs.carousel', onSlide) carouselEl.addEventListener('slide.bs.carousel', onSlide)
carousel.next() carousel.next()
})
}) })
it('should fire slid event with: direction, relatedTarget, from and to', done => { it('should fire slid event with: direction, relatedTarget, from and to', () => {
fixtureEl.innerHTML = [ return new Promise(resolve => {
'<div id="myCarousel" class="carousel slide">', fixtureEl.innerHTML = [
' <div class="carousel-inner">', '<div id="myCarousel" class="carousel slide">',
' <div class="carousel-item active">item 1</div>', ' <div class="carousel-inner">',
' <div class="carousel-item">item 2</div>', ' <div class="carousel-item active">item 1</div>',
' <div class="carousel-item">item 3</div>', ' <div class="carousel-item">item 2</div>',
' </div>', ' <div class="carousel-item">item 3</div>',
'</div>' ' </div>',
].join('') '</div>'
].join('')
const carouselEl = fixtureEl.querySelector('#myCarousel') const carouselEl = fixtureEl.querySelector('#myCarousel')
const carousel = new Carousel(carouselEl, {}) const carousel = new Carousel(carouselEl, {})
const onSlid = event => { const onSlid = event => {
expect(event.direction).toEqual('left') expect(event.direction).toEqual('left')
expect(event.relatedTarget).toHaveClass('carousel-item') expect(event.relatedTarget).toHaveClass('carousel-item')
expect(event.from).toEqual(0) expect(event.from).toEqual(0)
expect(event.to).toEqual(1) expect(event.to).toEqual(1)
carouselEl.removeEventListener('slid.bs.carousel', onSlid) carouselEl.removeEventListener('slid.bs.carousel', onSlid)
carouselEl.addEventListener('slid.bs.carousel', onSlid2) carouselEl.addEventListener('slid.bs.carousel', onSlid2)
carousel.prev() carousel.prev()
} }
const onSlid2 = event => { const onSlid2 = event => {
expect(event.direction).toEqual('right') expect(event.direction).toEqual('right')
done() resolve()
} }
carouselEl.addEventListener('slid.bs.carousel', onSlid) carouselEl.addEventListener('slid.bs.carousel', onSlid)
carousel.next() carousel.next()
})
}) })
it('should update the active element to the next item before sliding', () => { it('should update the active element to the next item before sliding', () => {
@ -737,36 +769,38 @@ describe('Carousel', () => {
expect(carousel._activeElement).toEqual(secondItemEl) expect(carousel._activeElement).toEqual(secondItemEl)
}) })
it('should update indicators if present', done => { it('should update indicators if present', () => {
fixtureEl.innerHTML = [ return new Promise(resolve => {
'<div id="myCarousel" class="carousel slide">', fixtureEl.innerHTML = [
' <div class="carousel-indicators">', '<div id="myCarousel" class="carousel slide">',
' <button type="button" id="firstIndicator" data-bs-target="myCarousel" data-bs-slide-to="0" class="active" aria-current="true" aria-label="Slide 1"></button>', ' <div class="carousel-indicators">',
' <button type="button" id="secondIndicator" data-bs-target="myCarousel" data-bs-slide-to="1" aria-label="Slide 2"></button>', ' <button type="button" id="firstIndicator" data-bs-target="myCarousel" data-bs-slide-to="0" class="active" aria-current="true" aria-label="Slide 1"></button>',
' <button type="button" data-bs-target="myCarousel" data-bs-slide-to="2" aria-label="Slide 3"></button>', ' <button type="button" id="secondIndicator" data-bs-target="myCarousel" data-bs-slide-to="1" aria-label="Slide 2"></button>',
' </div>', ' <button type="button" data-bs-target="myCarousel" data-bs-slide-to="2" aria-label="Slide 3"></button>',
' <div class="carousel-inner">', ' </div>',
' <div class="carousel-item active">item 1</div>', ' <div class="carousel-inner">',
' <div class="carousel-item" data-bs-interval="7">item 2</div>', ' <div class="carousel-item active">item 1</div>',
' <div class="carousel-item">item 3</div>', ' <div class="carousel-item" data-bs-interval="7">item 2</div>',
' </div>', ' <div class="carousel-item">item 3</div>',
'</div>' ' </div>',
].join('') '</div>'
].join('')
const carouselEl = fixtureEl.querySelector('#myCarousel') const carouselEl = fixtureEl.querySelector('#myCarousel')
const firstIndicator = fixtureEl.querySelector('#firstIndicator') const firstIndicator = fixtureEl.querySelector('#firstIndicator')
const secondIndicator = fixtureEl.querySelector('#secondIndicator') const secondIndicator = fixtureEl.querySelector('#secondIndicator')
const carousel = new Carousel(carouselEl) const carousel = new Carousel(carouselEl)
carouselEl.addEventListener('slid.bs.carousel', () => { carouselEl.addEventListener('slid.bs.carousel', () => {
expect(firstIndicator).not.toHaveClass('active') expect(firstIndicator).not.toHaveClass('active')
expect(firstIndicator.hasAttribute('aria-current')).toBeFalse() expect(firstIndicator.hasAttribute('aria-current')).toBeFalse()
expect(secondIndicator).toHaveClass('active') expect(secondIndicator).toHaveClass('active')
expect(secondIndicator.getAttribute('aria-current')).toEqual('true') expect(secondIndicator.getAttribute('aria-current')).toEqual('true')
done() resolve()
})
carousel.next()
}) })
carousel.next()
}) })
it('should call next()/prev() instance methods when clicking the respective direction buttons', () => { it('should call next()/prev() instance methods when clicking the respective direction buttons', () => {
@ -1018,51 +1052,55 @@ describe('Carousel', () => {
}) })
describe('to', () => { describe('to', () => {
it('should go directly to the provided index', done => { it('should go directly to the provided index', () => {
fixtureEl.innerHTML = [ return new Promise(resolve => {
'<div id="myCarousel" class="carousel slide">', fixtureEl.innerHTML = [
' <div class="carousel-inner">', '<div id="myCarousel" class="carousel slide">',
' <div id="item1" class="carousel-item active">item 1</div>', ' <div class="carousel-inner">',
' <div class="carousel-item">item 2</div>', ' <div id="item1" class="carousel-item active">item 1</div>',
' <div id="item3" class="carousel-item">item 3</div>', ' <div class="carousel-item">item 2</div>',
' </div>', ' <div id="item3" class="carousel-item">item 3</div>',
'</div>' ' </div>',
].join('') '</div>'
].join('')
const carouselEl = fixtureEl.querySelector('#myCarousel') const carouselEl = fixtureEl.querySelector('#myCarousel')
const carousel = new Carousel(carouselEl, {}) const carousel = new Carousel(carouselEl, {})
expect(fixtureEl.querySelector('.active')).toEqual(fixtureEl.querySelector('#item1')) expect(fixtureEl.querySelector('.active')).toEqual(fixtureEl.querySelector('#item1'))
carousel.to(2) carousel.to(2)
carouselEl.addEventListener('slid.bs.carousel', () => { carouselEl.addEventListener('slid.bs.carousel', () => {
expect(fixtureEl.querySelector('.active')).toEqual(fixtureEl.querySelector('#item3')) expect(fixtureEl.querySelector('.active')).toEqual(fixtureEl.querySelector('#item3'))
done() resolve()
})
}) })
}) })
it('should return to a previous slide if the provided index is lower than the current', done => { it('should return to a previous slide if the provided index is lower than the current', () => {
fixtureEl.innerHTML = [ return new Promise(resolve => {
'<div id="myCarousel" class="carousel slide">', fixtureEl.innerHTML = [
' <div class="carousel-inner">', '<div id="myCarousel" class="carousel slide">',
' <div class="carousel-item">item 1</div>', ' <div class="carousel-inner">',
' <div id="item2" class="carousel-item">item 2</div>', ' <div class="carousel-item">item 1</div>',
' <div id="item3" class="carousel-item active">item 3</div>', ' <div id="item2" class="carousel-item">item 2</div>',
' </div>', ' <div id="item3" class="carousel-item active">item 3</div>',
'</div>' ' </div>',
].join('') '</div>'
].join('')
const carouselEl = fixtureEl.querySelector('#myCarousel') const carouselEl = fixtureEl.querySelector('#myCarousel')
const carousel = new Carousel(carouselEl, {}) const carousel = new Carousel(carouselEl, {})
expect(fixtureEl.querySelector('.active')).toEqual(fixtureEl.querySelector('#item3')) expect(fixtureEl.querySelector('.active')).toEqual(fixtureEl.querySelector('#item3'))
carousel.to(1) carousel.to(1)
carouselEl.addEventListener('slid.bs.carousel', () => { carouselEl.addEventListener('slid.bs.carousel', () => {
expect(fixtureEl.querySelector('.active')).toEqual(fixtureEl.querySelector('#item2')) expect(fixtureEl.querySelector('.active')).toEqual(fixtureEl.querySelector('#item2'))
done() resolve()
})
}) })
}) })
@ -1118,36 +1156,38 @@ describe('Carousel', () => {
expect(carousel.cycle).toHaveBeenCalled() expect(carousel.cycle).toHaveBeenCalled()
}) })
it('should wait before performing to if a slide is sliding', done => { it('should wait before performing to if a slide is sliding', () => {
fixtureEl.innerHTML = [ return new Promise(resolve => {
'<div id="myCarousel" class="carousel slide">', fixtureEl.innerHTML = [
' <div class="carousel-inner">', '<div id="myCarousel" class="carousel slide">',
' <div class="carousel-item active">item 1</div>', ' <div class="carousel-inner">',
' <div class="carousel-item" data-bs-interval="7">item 2</div>', ' <div class="carousel-item active">item 1</div>',
' <div class="carousel-item">item 3</div>', ' <div class="carousel-item" data-bs-interval="7">item 2</div>',
' </div>', ' <div class="carousel-item">item 3</div>',
'</div>' ' </div>',
].join('') '</div>'
].join('')
const carouselEl = fixtureEl.querySelector('#myCarousel') const carouselEl = fixtureEl.querySelector('#myCarousel')
const carousel = new Carousel(carouselEl, {}) const carousel = new Carousel(carouselEl, {})
spyOn(EventHandler, 'one').and.callThrough() spyOn(EventHandler, 'one').and.callThrough()
spyOn(carousel, '_slide') spyOn(carousel, '_slide')
carousel._isSliding = true carousel._isSliding = true
carousel.to(1) carousel.to(1)
expect(carousel._slide).not.toHaveBeenCalled() expect(carousel._slide).not.toHaveBeenCalled()
expect(EventHandler.one).toHaveBeenCalled() expect(EventHandler.one).toHaveBeenCalled()
spyOn(carousel, 'to') spyOn(carousel, 'to')
EventHandler.trigger(carouselEl, 'slid.bs.carousel') EventHandler.trigger(carouselEl, 'slid.bs.carousel')
setTimeout(() => { setTimeout(() => {
expect(carousel.to).toHaveBeenCalledWith(1) expect(carousel.to).toHaveBeenCalledWith(1)
done() resolve()
})
}) })
}) })
}) })
@ -1421,75 +1461,81 @@ describe('Carousel', () => {
expect(Carousel.getInstance(carouselEl)).not.toBeNull() expect(Carousel.getInstance(carouselEl)).not.toBeNull()
}) })
it('should create carousel and go to the next slide on click (with real button controls)', done => { it('should create carousel and go to the next slide on click (with real button controls)', () => {
fixtureEl.innerHTML = [ return new Promise(resolve => {
'<div id="myCarousel" class="carousel slide">', fixtureEl.innerHTML = [
' <div class="carousel-inner">', '<div id="myCarousel" class="carousel slide">',
' <div class="carousel-item active">item 1</div>', ' <div class="carousel-inner">',
' <div id="item2" class="carousel-item">item 2</div>', ' <div class="carousel-item active">item 1</div>',
' <div class="carousel-item">item 3</div>', ' <div id="item2" class="carousel-item">item 2</div>',
' </div>', ' <div class="carousel-item">item 3</div>',
' <button class="carousel-control-prev" data-bs-target="#myCarousel" type="button" data-bs-slide="prev"></button>', ' </div>',
' <button id="next" class="carousel-control-next" data-bs-target="#myCarousel" type="button" data-bs-slide="next"></button>', ' <button class="carousel-control-prev" data-bs-target="#myCarousel" type="button" data-bs-slide="prev"></button>',
'</div>' ' <button id="next" class="carousel-control-next" data-bs-target="#myCarousel" type="button" data-bs-slide="next"></button>',
].join('') '</div>'
].join('')
const next = fixtureEl.querySelector('#next') const next = fixtureEl.querySelector('#next')
const item2 = fixtureEl.querySelector('#item2') const item2 = fixtureEl.querySelector('#item2')
next.click() next.click()
setTimeout(() => { setTimeout(() => {
expect(item2).toHaveClass('active') expect(item2).toHaveClass('active')
done() resolve()
}, 10) }, 10)
})
}) })
it('should create carousel and go to the next slide on click (using links as controls)', done => { it('should create carousel and go to the next slide on click (using links as controls)', () => {
fixtureEl.innerHTML = [ return new Promise(resolve => {
'<div id="myCarousel" class="carousel slide">', fixtureEl.innerHTML = [
' <div class="carousel-inner">', '<div id="myCarousel" class="carousel slide">',
' <div class="carousel-item active">item 1</div>', ' <div class="carousel-inner">',
' <div id="item2" class="carousel-item">item 2</div>', ' <div class="carousel-item active">item 1</div>',
' <div class="carousel-item">item 3</div>', ' <div id="item2" class="carousel-item">item 2</div>',
' </div>', ' <div class="carousel-item">item 3</div>',
' <a class="carousel-control-prev" href="#myCarousel" role="button" data-bs-slide="prev"></a>', ' </div>',
' <a id="next" class="carousel-control-next" href="#myCarousel" role="button" data-bs-slide="next"></a>', ' <a class="carousel-control-prev" href="#myCarousel" role="button" data-bs-slide="prev"></a>',
'</div>' ' <a id="next" class="carousel-control-next" href="#myCarousel" role="button" data-bs-slide="next"></a>',
].join('') '</div>'
].join('')
const next = fixtureEl.querySelector('#next') const next = fixtureEl.querySelector('#next')
const item2 = fixtureEl.querySelector('#item2') const item2 = fixtureEl.querySelector('#item2')
next.click() next.click()
setTimeout(() => { setTimeout(() => {
expect(item2).toHaveClass('active') expect(item2).toHaveClass('active')
done() resolve()
}, 10) }, 10)
})
}) })
it('should create carousel and go to the next slide on click with data-bs-slide-to', done => { it('should create carousel and go to the next slide on click with data-bs-slide-to', () => {
fixtureEl.innerHTML = [ return new Promise(resolve => {
'<div id="myCarousel" class="carousel slide">', fixtureEl.innerHTML = [
' <div class="carousel-inner">', '<div id="myCarousel" class="carousel slide">',
' <div class="carousel-item active">item 1</div>', ' <div class="carousel-inner">',
' <div id="item2" class="carousel-item">item 2</div>', ' <div class="carousel-item active">item 1</div>',
' <div class="carousel-item">item 3</div>', ' <div id="item2" class="carousel-item">item 2</div>',
' </div>', ' <div class="carousel-item">item 3</div>',
' <div id="next" data-bs-target="#myCarousel" data-bs-slide-to="1"></div>', ' </div>',
'</div>' ' <div id="next" data-bs-target="#myCarousel" data-bs-slide-to="1"></div>',
].join('') '</div>'
].join('')
const next = fixtureEl.querySelector('#next') const next = fixtureEl.querySelector('#next')
const item2 = fixtureEl.querySelector('#item2') const item2 = fixtureEl.querySelector('#item2')
next.click() next.click()
setTimeout(() => { setTimeout(() => {
expect(item2).toHaveClass('active') expect(item2).toHaveClass('active')
done() resolve()
}, 10) }, 10)
})
}) })
it('should do nothing if no selector on click on arrows', () => { it('should do nothing if no selector on click on arrows', () => {

View File

@ -134,37 +134,39 @@ describe('Collapse', () => {
expect(collapse.hide).toHaveBeenCalled() expect(collapse.hide).toHaveBeenCalled()
}) })
it('should find collapse children if they have collapse class too not only data-bs-parent', done => { it('should find collapse children if they have collapse class too not only data-bs-parent', () => {
fixtureEl.innerHTML = [ return new Promise(resolve => {
'<div class="my-collapse">', fixtureEl.innerHTML = [
' <div class="item">', '<div class="my-collapse">',
' <a data-bs-toggle="collapse" href="#">Toggle item 1</a>', ' <div class="item">',
' <div id="collapse1" class="collapse show">Lorem ipsum 1</div>', ' <a data-bs-toggle="collapse" href="#">Toggle item 1</a>',
' </div>', ' <div id="collapse1" class="collapse show">Lorem ipsum 1</div>',
' <div class="item">', ' </div>',
' <a id="triggerCollapse2" data-bs-toggle="collapse" href="#">Toggle item 2</a>', ' <div class="item">',
' <div id="collapse2" class="collapse">Lorem ipsum 2</div>', ' <a id="triggerCollapse2" data-bs-toggle="collapse" href="#">Toggle item 2</a>',
' </div>', ' <div id="collapse2" class="collapse">Lorem ipsum 2</div>',
'</div>' ' </div>',
].join('') '</div>'
].join('')
const parent = fixtureEl.querySelector('.my-collapse') const parent = fixtureEl.querySelector('.my-collapse')
const collapseEl1 = fixtureEl.querySelector('#collapse1') const collapseEl1 = fixtureEl.querySelector('#collapse1')
const collapseEl2 = fixtureEl.querySelector('#collapse2') const collapseEl2 = fixtureEl.querySelector('#collapse2')
const collapseList = [].concat(...fixtureEl.querySelectorAll('.collapse')) const collapseList = [].concat(...fixtureEl.querySelectorAll('.collapse'))
.map(el => new Collapse(el, { .map(el => new Collapse(el, {
parent, parent,
toggle: false toggle: false
})) }))
collapseEl2.addEventListener('shown.bs.collapse', () => { collapseEl2.addEventListener('shown.bs.collapse', () => {
expect(collapseEl2).toHaveClass('show') expect(collapseEl2).toHaveClass('show')
expect(collapseEl1).not.toHaveClass('show') expect(collapseEl1).not.toHaveClass('show')
done() resolve()
})
collapseList[1].toggle()
}) })
collapseList[1].toggle()
}) })
}) })
@ -200,203 +202,215 @@ describe('Collapse', () => {
expect(EventHandler.trigger).not.toHaveBeenCalled() expect(EventHandler.trigger).not.toHaveBeenCalled()
}) })
it('should show a collapsed element', done => { it('should show a collapsed element', () => {
fixtureEl.innerHTML = '<div class="collapse" style="height: 0px;"></div>' return new Promise(resolve => {
fixtureEl.innerHTML = '<div class="collapse" style="height: 0px;"></div>'
const collapseEl = fixtureEl.querySelector('div') const collapseEl = fixtureEl.querySelector('div')
const collapse = new Collapse(collapseEl, { const collapse = new Collapse(collapseEl, {
toggle: false toggle: false
}) })
collapseEl.addEventListener('show.bs.collapse', () => { collapseEl.addEventListener('show.bs.collapse', () => {
expect(collapseEl.style.height).toEqual('0px') expect(collapseEl.style.height).toEqual('0px')
}) })
collapseEl.addEventListener('shown.bs.collapse', () => { collapseEl.addEventListener('shown.bs.collapse', () => {
expect(collapseEl).toHaveClass('show') expect(collapseEl).toHaveClass('show')
expect(collapseEl.style.height).toEqual('') expect(collapseEl.style.height).toEqual('')
done() resolve()
}) })
collapse.show()
})
it('should show a collapsed element on width', done => {
fixtureEl.innerHTML = '<div class="collapse collapse-horizontal" style="width: 0px;"></div>'
const collapseEl = fixtureEl.querySelector('div')
const collapse = new Collapse(collapseEl, {
toggle: false
})
collapseEl.addEventListener('show.bs.collapse', () => {
expect(collapseEl.style.width).toEqual('0px')
})
collapseEl.addEventListener('shown.bs.collapse', () => {
expect(collapseEl).toHaveClass('show')
expect(collapseEl.style.width).toEqual('')
done()
})
collapse.show()
})
it('should collapse only the first collapse', done => {
fixtureEl.innerHTML = [
'<div class="card" id="accordion1">',
' <div id="collapse1" class="collapse"></div>',
'</div>',
'<div class="card" id="accordion2">',
' <div id="collapse2" class="collapse show"></div>',
'</div>'
].join('')
const el1 = fixtureEl.querySelector('#collapse1')
const el2 = fixtureEl.querySelector('#collapse2')
const collapse = new Collapse(el1, {
toggle: false
})
el1.addEventListener('shown.bs.collapse', () => {
expect(el1).toHaveClass('show')
expect(el2).toHaveClass('show')
done()
})
collapse.show()
})
it('should be able to handle toggling of other children siblings', done => {
fixtureEl.innerHTML = [
'<div id="parentGroup" class="accordion">',
' <div id="parentHeader" class="accordion-header">',
' <button data-bs-target="#parentContent" data-bs-toggle="collapse" role="button" class="accordion-toggle">Parent</button>',
' </div>',
' <div id="parentContent" class="accordion-collapse collapse" aria-labelledby="parentHeader" data-bs-parent="#parentGroup">',
' <div class="accordion-body">',
' <div id="childGroup" class="accordion">',
' <div class="accordion-item">',
' <div id="childHeader1" class="accordion-header">',
' <button data-bs-target="#childContent1" data-bs-toggle="collapse" role="button" class="accordion-toggle">Child 1</button>',
' </div>',
' <div id="childContent1" class="accordion-collapse collapse" aria-labelledby="childHeader1" data-bs-parent="#childGroup">',
' <div>content</div>',
' </div>',
' </div>',
' <div class="accordion-item">',
' <div id="childHeader2" class="accordion-header">',
' <button data-bs-target="#childContent2" data-bs-toggle="collapse" role="button" class="accordion-toggle">Child 2</button>',
' </div>',
' <div id="childContent2" class="accordion-collapse collapse" aria-labelledby="childHeader2" data-bs-parent="#childGroup">',
' <div>content</div>',
' </div>',
' </div>',
' </div>',
' </div>',
' </div>',
'</div>'
].join('')
const el = selector => fixtureEl.querySelector(selector)
const parentBtn = el('[data-bs-target="#parentContent"]')
const childBtn1 = el('[data-bs-target="#childContent1"]')
const childBtn2 = el('[data-bs-target="#childContent2"]')
const parentCollapseEl = el('#parentContent')
const childCollapseEl1 = el('#childContent1')
const childCollapseEl2 = el('#childContent2')
parentCollapseEl.addEventListener('shown.bs.collapse', () => {
expect(parentCollapseEl).toHaveClass('show')
childBtn1.click()
})
childCollapseEl1.addEventListener('shown.bs.collapse', () => {
expect(childCollapseEl1).toHaveClass('show')
childBtn2.click()
})
childCollapseEl2.addEventListener('shown.bs.collapse', () => {
expect(childCollapseEl2).toHaveClass('show')
expect(childCollapseEl1).not.toHaveClass('show')
done()
})
parentBtn.click()
})
it('should not change tab tabpanels descendants on accordion', done => {
fixtureEl.innerHTML = [
'<div class="accordion" id="accordionExample">',
' <div class="accordion-item">',
' <h2 class="accordion-header" id="headingOne">',
' <button class="accordion-button" type="button" data-bs-toggle="collapse" data-bs-target="#collapseOne" aria-expanded="true" aria-controls="collapseOne">',
' Accordion Item #1',
' </button>',
' </h2>',
' <div id="collapseOne" class="accordion-collapse collapse show" aria-labelledby="headingOne" data-bs-parent="#accordionExample">',
' <div class="accordion-body">',
' <nav>',
' <div class="nav nav-tabs" id="nav-tab" role="tablist">',
' <button class="nav-link active" id="nav-home-tab" data-bs-toggle="tab" data-bs-target="#nav-home" type="button" role="tab" aria-controls="nav-home" aria-selected="true">Home</button>',
' <button class="nav-link" id="nav-profile-tab" data-bs-toggle="tab" data-bs-target="#nav-profile" type="button" role="tab" aria-controls="nav-profile" aria-selected="false">Profile</button>',
' </div>',
' </nav>',
' <div class="tab-content" id="nav-tabContent">',
' <div class="tab-pane fade show active" id="nav-home" role="tabpanel" aria-labelledby="nav-home-tab">Home</div>',
' <div class="tab-pane fade" id="nav-profile" role="tabpanel" aria-labelledby="nav-profile-tab">Profile</div>',
' </div>',
' </div>',
' </div>',
' </div>',
'</div>'
].join('')
const el = fixtureEl.querySelector('#collapseOne')
const activeTabPane = fixtureEl.querySelector('#nav-home')
const collapse = new Collapse(el)
let times = 1
el.addEventListener('hidden.bs.collapse', () => {
collapse.show() collapse.show()
}) })
el.addEventListener('shown.bs.collapse', () => {
expect(activeTabPane).toHaveClass('show')
times++
if (times === 2) {
done()
}
collapse.hide()
})
collapse.show()
}) })
it('should not fire shown when show is prevented', done => { it('should show a collapsed element on width', () => {
fixtureEl.innerHTML = '<div class="collapse"></div>' return new Promise(resolve => {
fixtureEl.innerHTML = '<div class="collapse collapse-horizontal" style="width: 0px;"></div>'
const collapseEl = fixtureEl.querySelector('div') const collapseEl = fixtureEl.querySelector('div')
const collapse = new Collapse(collapseEl, { const collapse = new Collapse(collapseEl, {
toggle: false toggle: false
})
collapseEl.addEventListener('show.bs.collapse', () => {
expect(collapseEl.style.width).toEqual('0px')
})
collapseEl.addEventListener('shown.bs.collapse', () => {
expect(collapseEl).toHaveClass('show')
expect(collapseEl.style.width).toEqual('')
resolve()
})
collapse.show()
}) })
})
const expectEnd = () => { it('should collapse only the first collapse', () => {
setTimeout(() => { return new Promise(resolve => {
expect().nothing() fixtureEl.innerHTML = [
done() '<div class="card" id="accordion1">',
}, 10) ' <div id="collapse1" class="collapse"></div>',
} '</div>',
'<div class="card" id="accordion2">',
' <div id="collapse2" class="collapse show"></div>',
'</div>'
].join('')
collapseEl.addEventListener('show.bs.collapse', event => { const el1 = fixtureEl.querySelector('#collapse1')
event.preventDefault() const el2 = fixtureEl.querySelector('#collapse2')
expectEnd() const collapse = new Collapse(el1, {
toggle: false
})
el1.addEventListener('shown.bs.collapse', () => {
expect(el1).toHaveClass('show')
expect(el2).toHaveClass('show')
resolve()
})
collapse.show()
}) })
})
collapseEl.addEventListener('shown.bs.collapse', () => { it('should be able to handle toggling of other children siblings', () => {
throw new Error('should not fire shown event') return new Promise(resolve => {
fixtureEl.innerHTML = [
'<div id="parentGroup" class="accordion">',
' <div id="parentHeader" class="accordion-header">',
' <button data-bs-target="#parentContent" data-bs-toggle="collapse" role="button" class="accordion-toggle">Parent</button>',
' </div>',
' <div id="parentContent" class="accordion-collapse collapse" aria-labelledby="parentHeader" data-bs-parent="#parentGroup">',
' <div class="accordion-body">',
' <div id="childGroup" class="accordion">',
' <div class="accordion-item">',
' <div id="childHeader1" class="accordion-header">',
' <button data-bs-target="#childContent1" data-bs-toggle="collapse" role="button" class="accordion-toggle">Child 1</button>',
' </div>',
' <div id="childContent1" class="accordion-collapse collapse" aria-labelledby="childHeader1" data-bs-parent="#childGroup">',
' <div>content</div>',
' </div>',
' </div>',
' <div class="accordion-item">',
' <div id="childHeader2" class="accordion-header">',
' <button data-bs-target="#childContent2" data-bs-toggle="collapse" role="button" class="accordion-toggle">Child 2</button>',
' </div>',
' <div id="childContent2" class="accordion-collapse collapse" aria-labelledby="childHeader2" data-bs-parent="#childGroup">',
' <div>content</div>',
' </div>',
' </div>',
' </div>',
' </div>',
' </div>',
'</div>'
].join('')
const el = selector => fixtureEl.querySelector(selector)
const parentBtn = el('[data-bs-target="#parentContent"]')
const childBtn1 = el('[data-bs-target="#childContent1"]')
const childBtn2 = el('[data-bs-target="#childContent2"]')
const parentCollapseEl = el('#parentContent')
const childCollapseEl1 = el('#childContent1')
const childCollapseEl2 = el('#childContent2')
parentCollapseEl.addEventListener('shown.bs.collapse', () => {
expect(parentCollapseEl).toHaveClass('show')
childBtn1.click()
})
childCollapseEl1.addEventListener('shown.bs.collapse', () => {
expect(childCollapseEl1).toHaveClass('show')
childBtn2.click()
})
childCollapseEl2.addEventListener('shown.bs.collapse', () => {
expect(childCollapseEl2).toHaveClass('show')
expect(childCollapseEl1).not.toHaveClass('show')
resolve()
})
parentBtn.click()
}) })
})
collapse.show() it('should not change tab tabpanels descendants on accordion', () => {
return new Promise(resolve => {
fixtureEl.innerHTML = [
'<div class="accordion" id="accordionExample">',
' <div class="accordion-item">',
' <h2 class="accordion-header" id="headingOne">',
' <button class="accordion-button" type="button" data-bs-toggle="collapse" data-bs-target="#collapseOne" aria-expanded="true" aria-controls="collapseOne">',
' Accordion Item #1',
' </button>',
' </h2>',
' <div id="collapseOne" class="accordion-collapse collapse show" aria-labelledby="headingOne" data-bs-parent="#accordionExample">',
' <div class="accordion-body">',
' <nav>',
' <div class="nav nav-tabs" id="nav-tab" role="tablist">',
' <button class="nav-link active" id="nav-home-tab" data-bs-toggle="tab" data-bs-target="#nav-home" type="button" role="tab" aria-controls="nav-home" aria-selected="true">Home</button>',
' <button class="nav-link" id="nav-profile-tab" data-bs-toggle="tab" data-bs-target="#nav-profile" type="button" role="tab" aria-controls="nav-profile" aria-selected="false">Profile</button>',
' </div>',
' </nav>',
' <div class="tab-content" id="nav-tabContent">',
' <div class="tab-pane fade show active" id="nav-home" role="tabpanel" aria-labelledby="nav-home-tab">Home</div>',
' <div class="tab-pane fade" id="nav-profile" role="tabpanel" aria-labelledby="nav-profile-tab">Profile</div>',
' </div>',
' </div>',
' </div>',
' </div>',
'</div>'
].join('')
const el = fixtureEl.querySelector('#collapseOne')
const activeTabPane = fixtureEl.querySelector('#nav-home')
const collapse = new Collapse(el)
let times = 1
el.addEventListener('hidden.bs.collapse', () => {
collapse.show()
})
el.addEventListener('shown.bs.collapse', () => {
expect(activeTabPane).toHaveClass('show')
times++
if (times === 2) {
resolve()
}
collapse.hide()
})
collapse.show()
})
})
it('should not fire shown when show is prevented', () => {
return new Promise(resolve => {
fixtureEl.innerHTML = '<div class="collapse"></div>'
const collapseEl = fixtureEl.querySelector('div')
const collapse = new Collapse(collapseEl, {
toggle: false
})
const expectEnd = () => {
setTimeout(() => {
expect().nothing()
resolve()
}, 10)
}
collapseEl.addEventListener('show.bs.collapse', event => {
event.preventDefault()
expectEnd()
})
collapseEl.addEventListener('shown.bs.collapse', () => {
throw new Error('should not fire shown event')
})
collapse.show()
})
}) })
}) })
@ -432,48 +446,52 @@ describe('Collapse', () => {
expect(EventHandler.trigger).not.toHaveBeenCalled() expect(EventHandler.trigger).not.toHaveBeenCalled()
}) })
it('should hide a collapsed element', done => { it('should hide a collapsed element', () => {
fixtureEl.innerHTML = '<div class="collapse show"></div>' return new Promise(resolve => {
fixtureEl.innerHTML = '<div class="collapse show"></div>'
const collapseEl = fixtureEl.querySelector('div') const collapseEl = fixtureEl.querySelector('div')
const collapse = new Collapse(collapseEl, { const collapse = new Collapse(collapseEl, {
toggle: false toggle: false
})
collapseEl.addEventListener('hidden.bs.collapse', () => {
expect(collapseEl).not.toHaveClass('show')
expect(collapseEl.style.height).toEqual('')
resolve()
})
collapse.hide()
}) })
collapseEl.addEventListener('hidden.bs.collapse', () => {
expect(collapseEl).not.toHaveClass('show')
expect(collapseEl.style.height).toEqual('')
done()
})
collapse.hide()
}) })
it('should not fire hidden when hide is prevented', done => { it('should not fire hidden when hide is prevented', () => {
fixtureEl.innerHTML = '<div class="collapse show"></div>' return new Promise(resolve => {
fixtureEl.innerHTML = '<div class="collapse show"></div>'
const collapseEl = fixtureEl.querySelector('div') const collapseEl = fixtureEl.querySelector('div')
const collapse = new Collapse(collapseEl, { const collapse = new Collapse(collapseEl, {
toggle: false toggle: false
})
const expectEnd = () => {
setTimeout(() => {
expect().nothing()
resolve()
}, 10)
}
collapseEl.addEventListener('hide.bs.collapse', event => {
event.preventDefault()
expectEnd()
})
collapseEl.addEventListener('hidden.bs.collapse', () => {
throw new Error('should not fire hidden event')
})
collapse.hide()
}) })
const expectEnd = () => {
setTimeout(() => {
expect().nothing()
done()
}, 10)
}
collapseEl.addEventListener('hide.bs.collapse', event => {
event.preventDefault()
expectEnd()
})
collapseEl.addEventListener('hidden.bs.collapse', () => {
throw new Error('should not fire hidden event')
})
collapse.hide()
}) })
}) })
@ -495,411 +513,433 @@ describe('Collapse', () => {
}) })
describe('data-api', () => { describe('data-api', () => {
it('should prevent url change if click on nested elements', done => { it('should prevent url change if click on nested elements', () => {
fixtureEl.innerHTML = [ return new Promise(resolve => {
'<a role="button" data-bs-toggle="collapse" class="collapsed" href="#collapse">', fixtureEl.innerHTML = [
' <span id="nested"></span>', '<a role="button" data-bs-toggle="collapse" class="collapsed" href="#collapse">',
'</a>', ' <span id="nested"></span>',
'<div id="collapse" class="collapse"></div>' '</a>',
].join('') '<div id="collapse" class="collapse"></div>'
].join('')
const triggerEl = fixtureEl.querySelector('a') const triggerEl = fixtureEl.querySelector('a')
const nestedTriggerEl = fixtureEl.querySelector('#nested') const nestedTriggerEl = fixtureEl.querySelector('#nested')
spyOn(Event.prototype, 'preventDefault').and.callThrough() spyOn(Event.prototype, 'preventDefault').and.callThrough()
triggerEl.addEventListener('click', event => { triggerEl.addEventListener('click', event => {
expect(event.target.isEqualNode(nestedTriggerEl)).toBeTrue() expect(event.target.isEqualNode(nestedTriggerEl)).toBeTrue()
expect(event.delegateTarget.isEqualNode(triggerEl)).toBeTrue() expect(event.delegateTarget.isEqualNode(triggerEl)).toBeTrue()
expect(Event.prototype.preventDefault).toHaveBeenCalled() expect(Event.prototype.preventDefault).toHaveBeenCalled()
done() resolve()
})
nestedTriggerEl.click()
})
it('should show multiple collapsed elements', done => {
fixtureEl.innerHTML = [
'<a role="button" data-bs-toggle="collapse" class="collapsed" href=".multi"></a>',
'<div id="collapse1" class="collapse multi"></div>',
'<div id="collapse2" class="collapse multi"></div>'
].join('')
const trigger = fixtureEl.querySelector('a')
const collapse1 = fixtureEl.querySelector('#collapse1')
const collapse2 = fixtureEl.querySelector('#collapse2')
collapse2.addEventListener('shown.bs.collapse', () => {
expect(trigger.getAttribute('aria-expanded')).toEqual('true')
expect(trigger).not.toHaveClass('collapsed')
expect(collapse1).toHaveClass('show')
expect(collapse1).toHaveClass('show')
done()
})
trigger.click()
})
it('should hide multiple collapsed elements', done => {
fixtureEl.innerHTML = [
'<a role="button" data-bs-toggle="collapse" href=".multi"></a>',
'<div id="collapse1" class="collapse multi show"></div>',
'<div id="collapse2" class="collapse multi show"></div>'
].join('')
const trigger = fixtureEl.querySelector('a')
const collapse1 = fixtureEl.querySelector('#collapse1')
const collapse2 = fixtureEl.querySelector('#collapse2')
collapse2.addEventListener('hidden.bs.collapse', () => {
expect(trigger.getAttribute('aria-expanded')).toEqual('false')
expect(trigger).toHaveClass('collapsed')
expect(collapse1).not.toHaveClass('show')
expect(collapse1).not.toHaveClass('show')
done()
})
trigger.click()
})
it('should remove "collapsed" class from target when collapse is shown', done => {
fixtureEl.innerHTML = [
'<a id="link1" role="button" data-bs-toggle="collapse" class="collapsed" href="#" data-bs-target="#test1"></a>',
'<a id="link2" role="button" data-bs-toggle="collapse" class="collapsed" href="#" data-bs-target="#test1"></a>',
'<div id="test1"></div>'
].join('')
const link1 = fixtureEl.querySelector('#link1')
const link2 = fixtureEl.querySelector('#link2')
const collapseTest1 = fixtureEl.querySelector('#test1')
collapseTest1.addEventListener('shown.bs.collapse', () => {
expect(link1.getAttribute('aria-expanded')).toEqual('true')
expect(link2.getAttribute('aria-expanded')).toEqual('true')
expect(link1).not.toHaveClass('collapsed')
expect(link2).not.toHaveClass('collapsed')
done()
})
link1.click()
})
it('should add "collapsed" class to target when collapse is hidden', done => {
fixtureEl.innerHTML = [
'<a id="link1" role="button" data-bs-toggle="collapse" href="#" data-bs-target="#test1"></a>',
'<a id="link2" role="button" data-bs-toggle="collapse" href="#" data-bs-target="#test1"></a>',
'<div id="test1" class="show"></div>'
].join('')
const link1 = fixtureEl.querySelector('#link1')
const link2 = fixtureEl.querySelector('#link2')
const collapseTest1 = fixtureEl.querySelector('#test1')
collapseTest1.addEventListener('hidden.bs.collapse', () => {
expect(link1.getAttribute('aria-expanded')).toEqual('false')
expect(link2.getAttribute('aria-expanded')).toEqual('false')
expect(link1).toHaveClass('collapsed')
expect(link2).toHaveClass('collapsed')
done()
})
link1.click()
})
it('should allow accordion to use children other than card', done => {
fixtureEl.innerHTML = [
'<div id="accordion">',
' <div class="item">',
' <a id="linkTrigger" data-bs-toggle="collapse" href="#collapseOne" aria-expanded="false" aria-controls="collapseOne"></a>',
' <div id="collapseOne" class="collapse" role="tabpanel" aria-labelledby="headingThree" data-bs-parent="#accordion"></div>',
' </div>',
' <div class="item">',
' <a id="linkTriggerTwo" data-bs-toggle="collapse" href="#collapseTwo" aria-expanded="false" aria-controls="collapseTwo"></a>',
' <div id="collapseTwo" class="collapse show" role="tabpanel" aria-labelledby="headingTwo" data-bs-parent="#accordion"></div>',
' </div>',
'</div>'
].join('')
const trigger = fixtureEl.querySelector('#linkTrigger')
const triggerTwo = fixtureEl.querySelector('#linkTriggerTwo')
const collapseOne = fixtureEl.querySelector('#collapseOne')
const collapseTwo = fixtureEl.querySelector('#collapseTwo')
collapseOne.addEventListener('shown.bs.collapse', () => {
expect(collapseOne).toHaveClass('show')
expect(collapseTwo).not.toHaveClass('show')
collapseTwo.addEventListener('shown.bs.collapse', () => {
expect(collapseOne).not.toHaveClass('show')
expect(collapseTwo).toHaveClass('show')
done()
}) })
triggerTwo.click() nestedTriggerEl.click()
}) })
trigger.click()
}) })
it('should not prevent event for input', done => { it('should show multiple collapsed elements', () => {
fixtureEl.innerHTML = [ return new Promise(resolve => {
'<input type="checkbox" data-bs-toggle="collapse" data-bs-target="#collapsediv1">', fixtureEl.innerHTML = [
'<div id="collapsediv1"></div>' '<a role="button" data-bs-toggle="collapse" class="collapsed" href=".multi"></a>',
].join('') '<div id="collapse1" class="collapse multi"></div>',
'<div id="collapse2" class="collapse multi"></div>'
].join('')
const target = fixtureEl.querySelector('input') const trigger = fixtureEl.querySelector('a')
const collapseEl = fixtureEl.querySelector('#collapsediv1') const collapse1 = fixtureEl.querySelector('#collapse1')
const collapse2 = fixtureEl.querySelector('#collapse2')
collapseEl.addEventListener('shown.bs.collapse', () => { collapse2.addEventListener('shown.bs.collapse', () => {
expect(collapseEl).toHaveClass('show') expect(trigger.getAttribute('aria-expanded')).toEqual('true')
expect(target.checked).toBeTrue() expect(trigger).not.toHaveClass('collapsed')
done() expect(collapse1).toHaveClass('show')
}) expect(collapse1).toHaveClass('show')
resolve()
target.click()
})
it('should allow accordion to contain nested elements', done => {
fixtureEl.innerHTML = [
'<div id="accordion">',
' <div class="row">',
' <div class="col-lg-6">',
' <div class="item">',
' <a id="linkTrigger" data-bs-toggle="collapse" href="#collapseOne" aria-expanded="false" aria-controls="collapseOne"></a>',
' <div id="collapseOne" class="collapse" role="tabpanel" aria-labelledby="headingThree" data-bs-parent="#accordion"></div>',
' </div>',
' </div>',
' <div class="col-lg-6">',
' <div class="item">',
' <a id="linkTriggerTwo" data-bs-toggle="collapse" href="#collapseTwo" aria-expanded="false" aria-controls="collapseTwo"></a>',
' <div id="collapseTwo" class="collapse show" role="tabpanel" aria-labelledby="headingTwo" data-bs-parent="#accordion"></div>',
' </div>',
' </div>',
' </div>',
'</div>'
].join('')
const triggerEl = fixtureEl.querySelector('#linkTrigger')
const triggerTwoEl = fixtureEl.querySelector('#linkTriggerTwo')
const collapseOneEl = fixtureEl.querySelector('#collapseOne')
const collapseTwoEl = fixtureEl.querySelector('#collapseTwo')
collapseOneEl.addEventListener('shown.bs.collapse', () => {
expect(collapseOneEl).toHaveClass('show')
expect(triggerEl).not.toHaveClass('collapsed')
expect(triggerEl.getAttribute('aria-expanded')).toEqual('true')
expect(collapseTwoEl).not.toHaveClass('show')
expect(triggerTwoEl).toHaveClass('collapsed')
expect(triggerTwoEl.getAttribute('aria-expanded')).toEqual('false')
collapseTwoEl.addEventListener('shown.bs.collapse', () => {
expect(collapseOneEl).not.toHaveClass('show')
expect(triggerEl).toHaveClass('collapsed')
expect(triggerEl.getAttribute('aria-expanded')).toEqual('false')
expect(collapseTwoEl).toHaveClass('show')
expect(triggerTwoEl).not.toHaveClass('collapsed')
expect(triggerTwoEl.getAttribute('aria-expanded')).toEqual('true')
done()
}) })
triggerTwoEl.click() trigger.click()
}) })
triggerEl.click()
}) })
it('should allow accordion to target multiple elements', done => { it('should hide multiple collapsed elements', () => {
fixtureEl.innerHTML = [ return new Promise(resolve => {
'<div id="accordion">', fixtureEl.innerHTML = [
' <a id="linkTriggerOne" data-bs-toggle="collapse" data-bs-target=".collapseOne" href="#" aria-expanded="false" aria-controls="collapseOne"></a>', '<a role="button" data-bs-toggle="collapse" href=".multi"></a>',
' <a id="linkTriggerTwo" data-bs-toggle="collapse" data-bs-target=".collapseTwo" href="#" aria-expanded="false" aria-controls="collapseTwo"></a>', '<div id="collapse1" class="collapse multi show"></div>',
' <div id="collapseOneOne" class="collapse collapseOne" role="tabpanel" data-bs-parent="#accordion"></div>', '<div id="collapse2" class="collapse multi show"></div>'
' <div id="collapseOneTwo" class="collapse collapseOne" role="tabpanel" data-bs-parent="#accordion"></div>', ].join('')
' <div id="collapseTwoOne" class="collapse collapseTwo" role="tabpanel" data-bs-parent="#accordion"></div>',
' <div id="collapseTwoTwo" class="collapse collapseTwo" role="tabpanel" data-bs-parent="#accordion"></div>',
'</div>'
].join('')
const trigger = fixtureEl.querySelector('#linkTriggerOne') const trigger = fixtureEl.querySelector('a')
const triggerTwo = fixtureEl.querySelector('#linkTriggerTwo') const collapse1 = fixtureEl.querySelector('#collapse1')
const collapseOneOne = fixtureEl.querySelector('#collapseOneOne') const collapse2 = fixtureEl.querySelector('#collapse2')
const collapseOneTwo = fixtureEl.querySelector('#collapseOneTwo')
const collapseTwoOne = fixtureEl.querySelector('#collapseTwoOne')
const collapseTwoTwo = fixtureEl.querySelector('#collapseTwoTwo')
const collapsedElements = {
one: false,
two: false
}
function firstTest() { collapse2.addEventListener('hidden.bs.collapse', () => {
expect(collapseOneOne).toHaveClass('show') expect(trigger.getAttribute('aria-expanded')).toEqual('false')
expect(collapseOneTwo).toHaveClass('show') expect(trigger).toHaveClass('collapsed')
expect(collapse1).not.toHaveClass('show')
expect(collapse1).not.toHaveClass('show')
resolve()
})
expect(collapseTwoOne).not.toHaveClass('show') trigger.click()
expect(collapseTwoTwo).not.toHaveClass('show')
triggerTwo.click()
}
function secondTest() {
expect(collapseOneOne).not.toHaveClass('show')
expect(collapseOneTwo).not.toHaveClass('show')
expect(collapseTwoOne).toHaveClass('show')
expect(collapseTwoTwo).toHaveClass('show')
done()
}
collapseOneOne.addEventListener('shown.bs.collapse', () => {
if (collapsedElements.one) {
firstTest()
} else {
collapsedElements.one = true
}
}) })
collapseOneTwo.addEventListener('shown.bs.collapse', () => {
if (collapsedElements.one) {
firstTest()
} else {
collapsedElements.one = true
}
})
collapseTwoOne.addEventListener('shown.bs.collapse', () => {
if (collapsedElements.two) {
secondTest()
} else {
collapsedElements.two = true
}
})
collapseTwoTwo.addEventListener('shown.bs.collapse', () => {
if (collapsedElements.two) {
secondTest()
} else {
collapsedElements.two = true
}
})
trigger.click()
}) })
it('should collapse accordion children but not nested accordion children', done => { it('should remove "collapsed" class from target when collapse is shown', () => {
fixtureEl.innerHTML = [ return new Promise(resolve => {
'<div id="accordion">', fixtureEl.innerHTML = [
' <div class="item">', '<a id="link1" role="button" data-bs-toggle="collapse" class="collapsed" href="#" data-bs-target="#test1"></a>',
' <a id="linkTrigger" data-bs-toggle="collapse" href="#collapseOne" aria-expanded="false" aria-controls="collapseOne"></a>', '<a id="link2" role="button" data-bs-toggle="collapse" class="collapsed" href="#" data-bs-target="#test1"></a>',
' <div id="collapseOne" data-bs-parent="#accordion" class="collapse" role="tabpanel" aria-labelledby="headingThree">', '<div id="test1"></div>'
' <div id="nestedAccordion">', ].join('')
' <div class="item">',
' <a id="nestedLinkTrigger" data-bs-toggle="collapse" href="#nestedCollapseOne" aria-expanded="false" aria-controls="nestedCollapseOne"></a>',
' <div id="nestedCollapseOne" data-bs-parent="#nestedAccordion" class="collapse" role="tabpanel" aria-labelledby="headingThree"></div>',
' </div>',
' </div>',
' </div>',
' </div>',
' <div class="item">',
' <a id="linkTriggerTwo" data-bs-toggle="collapse" href="#collapseTwo" aria-expanded="false" aria-controls="collapseTwo"></a>',
' <div id="collapseTwo" data-bs-parent="#accordion" class="collapse show" role="tabpanel" aria-labelledby="headingTwo"></div>',
' </div>',
'</div>'
].join('')
const trigger = fixtureEl.querySelector('#linkTrigger') const link1 = fixtureEl.querySelector('#link1')
const triggerTwo = fixtureEl.querySelector('#linkTriggerTwo') const link2 = fixtureEl.querySelector('#link2')
const nestedTrigger = fixtureEl.querySelector('#nestedLinkTrigger') const collapseTest1 = fixtureEl.querySelector('#test1')
const collapseOne = fixtureEl.querySelector('#collapseOne')
const collapseTwo = fixtureEl.querySelector('#collapseTwo')
const nestedCollapseOne = fixtureEl.querySelector('#nestedCollapseOne')
function handlerCollapseOne() { collapseTest1.addEventListener('shown.bs.collapse', () => {
expect(collapseOne).toHaveClass('show') expect(link1.getAttribute('aria-expanded')).toEqual('true')
expect(collapseTwo).not.toHaveClass('show') expect(link2.getAttribute('aria-expanded')).toEqual('true')
expect(nestedCollapseOne).not.toHaveClass('show') expect(link1).not.toHaveClass('collapsed')
expect(link2).not.toHaveClass('collapsed')
resolve()
})
nestedCollapseOne.addEventListener('shown.bs.collapse', handlerNestedCollapseOne) link1.click()
nestedTrigger.click() })
collapseOne.removeEventListener('shown.bs.collapse', handlerCollapseOne) })
}
function handlerNestedCollapseOne() { it('should add "collapsed" class to target when collapse is hidden', () => {
expect(collapseOne).toHaveClass('show') return new Promise(resolve => {
expect(collapseTwo).not.toHaveClass('show') fixtureEl.innerHTML = [
expect(nestedCollapseOne).toHaveClass('show') '<a id="link1" role="button" data-bs-toggle="collapse" href="#" data-bs-target="#test1"></a>',
'<a id="link2" role="button" data-bs-toggle="collapse" href="#" data-bs-target="#test1"></a>',
'<div id="test1" class="show"></div>'
].join('')
collapseTwo.addEventListener('shown.bs.collapse', () => { const link1 = fixtureEl.querySelector('#link1')
expect(collapseOne).not.toHaveClass('show') const link2 = fixtureEl.querySelector('#link2')
expect(collapseTwo).toHaveClass('show') const collapseTest1 = fixtureEl.querySelector('#test1')
collapseTest1.addEventListener('hidden.bs.collapse', () => {
expect(link1.getAttribute('aria-expanded')).toEqual('false')
expect(link2.getAttribute('aria-expanded')).toEqual('false')
expect(link1).toHaveClass('collapsed')
expect(link2).toHaveClass('collapsed')
resolve()
})
link1.click()
})
})
it('should allow accordion to use children other than card', () => {
return new Promise(resolve => {
fixtureEl.innerHTML = [
'<div id="accordion">',
' <div class="item">',
' <a id="linkTrigger" data-bs-toggle="collapse" href="#collapseOne" aria-expanded="false" aria-controls="collapseOne"></a>',
' <div id="collapseOne" class="collapse" role="tabpanel" aria-labelledby="headingThree" data-bs-parent="#accordion"></div>',
' </div>',
' <div class="item">',
' <a id="linkTriggerTwo" data-bs-toggle="collapse" href="#collapseTwo" aria-expanded="false" aria-controls="collapseTwo"></a>',
' <div id="collapseTwo" class="collapse show" role="tabpanel" aria-labelledby="headingTwo" data-bs-parent="#accordion"></div>',
' </div>',
'</div>'
].join('')
const trigger = fixtureEl.querySelector('#linkTrigger')
const triggerTwo = fixtureEl.querySelector('#linkTriggerTwo')
const collapseOne = fixtureEl.querySelector('#collapseOne')
const collapseTwo = fixtureEl.querySelector('#collapseTwo')
collapseOne.addEventListener('shown.bs.collapse', () => {
expect(collapseOne).toHaveClass('show')
expect(collapseTwo).not.toHaveClass('show')
collapseTwo.addEventListener('shown.bs.collapse', () => {
expect(collapseOne).not.toHaveClass('show')
expect(collapseTwo).toHaveClass('show')
resolve()
})
triggerTwo.click()
})
trigger.click()
})
})
it('should not prevent event for input', () => {
return new Promise(resolve => {
fixtureEl.innerHTML = [
'<input type="checkbox" data-bs-toggle="collapse" data-bs-target="#collapsediv1">',
'<div id="collapsediv1"></div>'
].join('')
const target = fixtureEl.querySelector('input')
const collapseEl = fixtureEl.querySelector('#collapsediv1')
collapseEl.addEventListener('shown.bs.collapse', () => {
expect(collapseEl).toHaveClass('show')
expect(target.checked).toBeTrue()
resolve()
})
target.click()
})
})
it('should allow accordion to contain nested elements', () => {
return new Promise(resolve => {
fixtureEl.innerHTML = [
'<div id="accordion">',
' <div class="row">',
' <div class="col-lg-6">',
' <div class="item">',
' <a id="linkTrigger" data-bs-toggle="collapse" href="#collapseOne" aria-expanded="false" aria-controls="collapseOne"></a>',
' <div id="collapseOne" class="collapse" role="tabpanel" aria-labelledby="headingThree" data-bs-parent="#accordion"></div>',
' </div>',
' </div>',
' <div class="col-lg-6">',
' <div class="item">',
' <a id="linkTriggerTwo" data-bs-toggle="collapse" href="#collapseTwo" aria-expanded="false" aria-controls="collapseTwo"></a>',
' <div id="collapseTwo" class="collapse show" role="tabpanel" aria-labelledby="headingTwo" data-bs-parent="#accordion"></div>',
' </div>',
' </div>',
' </div>',
'</div>'
].join('')
const triggerEl = fixtureEl.querySelector('#linkTrigger')
const triggerTwoEl = fixtureEl.querySelector('#linkTriggerTwo')
const collapseOneEl = fixtureEl.querySelector('#collapseOne')
const collapseTwoEl = fixtureEl.querySelector('#collapseTwo')
collapseOneEl.addEventListener('shown.bs.collapse', () => {
expect(collapseOneEl).toHaveClass('show')
expect(triggerEl).not.toHaveClass('collapsed')
expect(triggerEl.getAttribute('aria-expanded')).toEqual('true')
expect(collapseTwoEl).not.toHaveClass('show')
expect(triggerTwoEl).toHaveClass('collapsed')
expect(triggerTwoEl.getAttribute('aria-expanded')).toEqual('false')
collapseTwoEl.addEventListener('shown.bs.collapse', () => {
expect(collapseOneEl).not.toHaveClass('show')
expect(triggerEl).toHaveClass('collapsed')
expect(triggerEl.getAttribute('aria-expanded')).toEqual('false')
expect(collapseTwoEl).toHaveClass('show')
expect(triggerTwoEl).not.toHaveClass('collapsed')
expect(triggerTwoEl.getAttribute('aria-expanded')).toEqual('true')
resolve()
})
triggerTwoEl.click()
})
triggerEl.click()
})
})
it('should allow accordion to target multiple elements', () => {
return new Promise(resolve => {
fixtureEl.innerHTML = [
'<div id="accordion">',
' <a id="linkTriggerOne" data-bs-toggle="collapse" data-bs-target=".collapseOne" href="#" aria-expanded="false" aria-controls="collapseOne"></a>',
' <a id="linkTriggerTwo" data-bs-toggle="collapse" data-bs-target=".collapseTwo" href="#" aria-expanded="false" aria-controls="collapseTwo"></a>',
' <div id="collapseOneOne" class="collapse collapseOne" role="tabpanel" data-bs-parent="#accordion"></div>',
' <div id="collapseOneTwo" class="collapse collapseOne" role="tabpanel" data-bs-parent="#accordion"></div>',
' <div id="collapseTwoOne" class="collapse collapseTwo" role="tabpanel" data-bs-parent="#accordion"></div>',
' <div id="collapseTwoTwo" class="collapse collapseTwo" role="tabpanel" data-bs-parent="#accordion"></div>',
'</div>'
].join('')
const trigger = fixtureEl.querySelector('#linkTriggerOne')
const triggerTwo = fixtureEl.querySelector('#linkTriggerTwo')
const collapseOneOne = fixtureEl.querySelector('#collapseOneOne')
const collapseOneTwo = fixtureEl.querySelector('#collapseOneTwo')
const collapseTwoOne = fixtureEl.querySelector('#collapseTwoOne')
const collapseTwoTwo = fixtureEl.querySelector('#collapseTwoTwo')
const collapsedElements = {
one: false,
two: false
}
function firstTest() {
expect(collapseOneOne).toHaveClass('show')
expect(collapseOneTwo).toHaveClass('show')
expect(collapseTwoOne).not.toHaveClass('show')
expect(collapseTwoTwo).not.toHaveClass('show')
triggerTwo.click()
}
function secondTest() {
expect(collapseOneOne).not.toHaveClass('show')
expect(collapseOneTwo).not.toHaveClass('show')
expect(collapseTwoOne).toHaveClass('show')
expect(collapseTwoTwo).toHaveClass('show')
resolve()
}
collapseOneOne.addEventListener('shown.bs.collapse', () => {
if (collapsedElements.one) {
firstTest()
} else {
collapsedElements.one = true
}
})
collapseOneTwo.addEventListener('shown.bs.collapse', () => {
if (collapsedElements.one) {
firstTest()
} else {
collapsedElements.one = true
}
})
collapseTwoOne.addEventListener('shown.bs.collapse', () => {
if (collapsedElements.two) {
secondTest()
} else {
collapsedElements.two = true
}
})
collapseTwoTwo.addEventListener('shown.bs.collapse', () => {
if (collapsedElements.two) {
secondTest()
} else {
collapsedElements.two = true
}
})
trigger.click()
})
})
it('should collapse accordion children but not nested accordion children', () => {
return new Promise(resolve => {
fixtureEl.innerHTML = [
'<div id="accordion">',
' <div class="item">',
' <a id="linkTrigger" data-bs-toggle="collapse" href="#collapseOne" aria-expanded="false" aria-controls="collapseOne"></a>',
' <div id="collapseOne" data-bs-parent="#accordion" class="collapse" role="tabpanel" aria-labelledby="headingThree">',
' <div id="nestedAccordion">',
' <div class="item">',
' <a id="nestedLinkTrigger" data-bs-toggle="collapse" href="#nestedCollapseOne" aria-expanded="false" aria-controls="nestedCollapseOne"></a>',
' <div id="nestedCollapseOne" data-bs-parent="#nestedAccordion" class="collapse" role="tabpanel" aria-labelledby="headingThree"></div>',
' </div>',
' </div>',
' </div>',
' </div>',
' <div class="item">',
' <a id="linkTriggerTwo" data-bs-toggle="collapse" href="#collapseTwo" aria-expanded="false" aria-controls="collapseTwo"></a>',
' <div id="collapseTwo" data-bs-parent="#accordion" class="collapse show" role="tabpanel" aria-labelledby="headingTwo"></div>',
' </div>',
'</div>'
].join('')
const trigger = fixtureEl.querySelector('#linkTrigger')
const triggerTwo = fixtureEl.querySelector('#linkTriggerTwo')
const nestedTrigger = fixtureEl.querySelector('#nestedLinkTrigger')
const collapseOne = fixtureEl.querySelector('#collapseOne')
const collapseTwo = fixtureEl.querySelector('#collapseTwo')
const nestedCollapseOne = fixtureEl.querySelector('#nestedCollapseOne')
function handlerCollapseOne() {
expect(collapseOne).toHaveClass('show')
expect(collapseTwo).not.toHaveClass('show')
expect(nestedCollapseOne).not.toHaveClass('show')
nestedCollapseOne.addEventListener('shown.bs.collapse', handlerNestedCollapseOne)
nestedTrigger.click()
collapseOne.removeEventListener('shown.bs.collapse', handlerCollapseOne)
}
function handlerNestedCollapseOne() {
expect(collapseOne).toHaveClass('show')
expect(collapseTwo).not.toHaveClass('show')
expect(nestedCollapseOne).toHaveClass('show') expect(nestedCollapseOne).toHaveClass('show')
done()
})
triggerTwo.click() collapseTwo.addEventListener('shown.bs.collapse', () => {
nestedCollapseOne.removeEventListener('shown.bs.collapse', handlerNestedCollapseOne) expect(collapseOne).not.toHaveClass('show')
} expect(collapseTwo).toHaveClass('show')
expect(nestedCollapseOne).toHaveClass('show')
resolve()
})
collapseOne.addEventListener('shown.bs.collapse', handlerCollapseOne) triggerTwo.click()
trigger.click() nestedCollapseOne.removeEventListener('shown.bs.collapse', handlerNestedCollapseOne)
}
collapseOne.addEventListener('shown.bs.collapse', handlerCollapseOne)
trigger.click()
})
}) })
it('should add "collapsed" class and set aria-expanded to triggers only when all the targeted collapse are hidden', done => { it('should add "collapsed" class and set aria-expanded to triggers only when all the targeted collapse are hidden', () => {
fixtureEl.innerHTML = [ return new Promise(resolve => {
'<a id="trigger1" role="button" data-bs-toggle="collapse" href="#test1"></a>', fixtureEl.innerHTML = [
'<a id="trigger2" role="button" data-bs-toggle="collapse" href="#test2"></a>', '<a id="trigger1" role="button" data-bs-toggle="collapse" href="#test1"></a>',
'<a id="trigger3" role="button" data-bs-toggle="collapse" href=".multi"></a>', '<a id="trigger2" role="button" data-bs-toggle="collapse" href="#test2"></a>',
'<div id="test1" class="multi"></div>', '<a id="trigger3" role="button" data-bs-toggle="collapse" href=".multi"></a>',
'<div id="test2" class="multi"></div>' '<div id="test1" class="multi"></div>',
].join('') '<div id="test2" class="multi"></div>'
].join('')
const trigger1 = fixtureEl.querySelector('#trigger1') const trigger1 = fixtureEl.querySelector('#trigger1')
const trigger2 = fixtureEl.querySelector('#trigger2') const trigger2 = fixtureEl.querySelector('#trigger2')
const trigger3 = fixtureEl.querySelector('#trigger3') const trigger3 = fixtureEl.querySelector('#trigger3')
const target1 = fixtureEl.querySelector('#test1') const target1 = fixtureEl.querySelector('#test1')
const target2 = fixtureEl.querySelector('#test2') const target2 = fixtureEl.querySelector('#test2')
const target2Shown = () => { const target2Shown = () => {
expect(trigger1).not.toHaveClass('collapsed')
expect(trigger1.getAttribute('aria-expanded')).toEqual('true')
expect(trigger2).not.toHaveClass('collapsed')
expect(trigger2.getAttribute('aria-expanded')).toEqual('true')
expect(trigger3).not.toHaveClass('collapsed')
expect(trigger3.getAttribute('aria-expanded')).toEqual('true')
target2.addEventListener('hidden.bs.collapse', () => {
expect(trigger1).not.toHaveClass('collapsed') expect(trigger1).not.toHaveClass('collapsed')
expect(trigger1.getAttribute('aria-expanded')).toEqual('true') expect(trigger1.getAttribute('aria-expanded')).toEqual('true')
expect(trigger2).toHaveClass('collapsed') expect(trigger2).not.toHaveClass('collapsed')
expect(trigger2.getAttribute('aria-expanded')).toEqual('false') expect(trigger2.getAttribute('aria-expanded')).toEqual('true')
expect(trigger3).not.toHaveClass('collapsed') expect(trigger3).not.toHaveClass('collapsed')
expect(trigger3.getAttribute('aria-expanded')).toEqual('true') expect(trigger3.getAttribute('aria-expanded')).toEqual('true')
target1.addEventListener('hidden.bs.collapse', () => { target2.addEventListener('hidden.bs.collapse', () => {
expect(trigger1).toHaveClass('collapsed') expect(trigger1).not.toHaveClass('collapsed')
expect(trigger1.getAttribute('aria-expanded')).toEqual('false') expect(trigger1.getAttribute('aria-expanded')).toEqual('true')
expect(trigger2).toHaveClass('collapsed') expect(trigger2).toHaveClass('collapsed')
expect(trigger2.getAttribute('aria-expanded')).toEqual('false') expect(trigger2.getAttribute('aria-expanded')).toEqual('false')
expect(trigger3).toHaveClass('collapsed') expect(trigger3).not.toHaveClass('collapsed')
expect(trigger3.getAttribute('aria-expanded')).toEqual('false') expect(trigger3.getAttribute('aria-expanded')).toEqual('true')
done()
target1.addEventListener('hidden.bs.collapse', () => {
expect(trigger1).toHaveClass('collapsed')
expect(trigger1.getAttribute('aria-expanded')).toEqual('false')
expect(trigger2).toHaveClass('collapsed')
expect(trigger2.getAttribute('aria-expanded')).toEqual('false')
expect(trigger3).toHaveClass('collapsed')
expect(trigger3.getAttribute('aria-expanded')).toEqual('false')
resolve()
})
trigger1.click()
}) })
trigger1.click() trigger2.click()
}) }
trigger2.click() target2.addEventListener('shown.bs.collapse', target2Shown)
} trigger3.click()
})
target2.addEventListener('shown.bs.collapse', target2Shown)
trigger3.click()
}) })
}) })

View File

@ -1,5 +1,6 @@
import EventHandler from '../../../src/dom/event-handler' import EventHandler from '../../../src/dom/event-handler'
import { getFixture, clearFixture } from '../../helpers/fixture' import { clearFixture, getFixture } from '../../helpers/fixture'
import { noop } from '../../../src/util'
describe('EventHandler', () => { describe('EventHandler', () => {
let fixtureEl let fixtureEl
@ -18,176 +19,190 @@ describe('EventHandler', () => {
const div = fixtureEl.querySelector('div') const div = fixtureEl.querySelector('div')
EventHandler.on(div, null, () => {}) EventHandler.on(div, null, noop)
EventHandler.on(null, 'click', () => {}) EventHandler.on(null, 'click', noop)
expect().nothing() expect().nothing()
}) })
it('should add event listener', done => { it('should add event listener', () => {
fixtureEl.innerHTML = '<div></div>' return new Promise(resolve => {
fixtureEl.innerHTML = '<div></div>'
const div = fixtureEl.querySelector('div') const div = fixtureEl.querySelector('div')
EventHandler.on(div, 'click', () => { EventHandler.on(div, 'click', () => {
expect().nothing() expect().nothing()
done() resolve()
})
div.click()
}) })
div.click()
}) })
it('should add namespaced event listener', done => { it('should add namespaced event listener', () => {
fixtureEl.innerHTML = '<div></div>' return new Promise(resolve => {
fixtureEl.innerHTML = '<div></div>'
const div = fixtureEl.querySelector('div') const div = fixtureEl.querySelector('div')
EventHandler.on(div, 'bs.namespace', () => { EventHandler.on(div, 'bs.namespace', () => {
expect().nothing() expect().nothing()
done() resolve()
})
EventHandler.trigger(div, 'bs.namespace')
}) })
EventHandler.trigger(div, 'bs.namespace')
}) })
it('should add native namespaced event listener', done => { it('should add native namespaced event listener', () => {
fixtureEl.innerHTML = '<div></div>' return new Promise(resolve => {
fixtureEl.innerHTML = '<div></div>'
const div = fixtureEl.querySelector('div') const div = fixtureEl.querySelector('div')
EventHandler.on(div, 'click.namespace', () => { EventHandler.on(div, 'click.namespace', () => {
expect().nothing() expect().nothing()
done() resolve()
})
EventHandler.trigger(div, 'click')
}) })
EventHandler.trigger(div, 'click')
}) })
it('should handle event delegation', done => { it('should handle event delegation', () => {
EventHandler.on(document, 'click', '.test', () => { return new Promise(resolve => {
expect().nothing() EventHandler.on(document, 'click', '.test', () => {
done() expect().nothing()
resolve()
})
fixtureEl.innerHTML = '<div class="test"></div>'
const div = fixtureEl.querySelector('div')
div.click()
}) })
fixtureEl.innerHTML = '<div class="test"></div>'
const div = fixtureEl.querySelector('div')
div.click()
}) })
it('should handle mouseenter/mouseleave like the native counterpart', done => { it('should handle mouseenter/mouseleave like the native counterpart', () => {
fixtureEl.innerHTML = [ return new Promise(resolve => {
'<div class="outer">', fixtureEl.innerHTML = [
'<div class="inner">', '<div class="outer">',
'<div class="nested">', '<div class="inner">',
'<div class="deep"></div>', '<div class="nested">',
'</div>', '<div class="deep"></div>',
'</div>', '</div>',
'<div class="sibling"></div>', '</div>',
'</div>' '<div class="sibling"></div>',
].join('') '</div>'
].join('')
const outer = fixtureEl.querySelector('.outer') const outer = fixtureEl.querySelector('.outer')
const inner = fixtureEl.querySelector('.inner') const inner = fixtureEl.querySelector('.inner')
const nested = fixtureEl.querySelector('.nested') const nested = fixtureEl.querySelector('.nested')
const deep = fixtureEl.querySelector('.deep') const deep = fixtureEl.querySelector('.deep')
const sibling = fixtureEl.querySelector('.sibling') const sibling = fixtureEl.querySelector('.sibling')
const enterSpy = jasmine.createSpy('mouseenter') const enterSpy = jasmine.createSpy('mouseenter')
const leaveSpy = jasmine.createSpy('mouseleave') const leaveSpy = jasmine.createSpy('mouseleave')
const delegateEnterSpy = jasmine.createSpy('mouseenter') const delegateEnterSpy = jasmine.createSpy('mouseenter')
const delegateLeaveSpy = jasmine.createSpy('mouseleave') const delegateLeaveSpy = jasmine.createSpy('mouseleave')
EventHandler.on(inner, 'mouseenter', enterSpy) EventHandler.on(inner, 'mouseenter', enterSpy)
EventHandler.on(inner, 'mouseleave', leaveSpy) EventHandler.on(inner, 'mouseleave', leaveSpy)
EventHandler.on(outer, 'mouseenter', '.inner', delegateEnterSpy) EventHandler.on(outer, 'mouseenter', '.inner', delegateEnterSpy)
EventHandler.on(outer, 'mouseleave', '.inner', delegateLeaveSpy) EventHandler.on(outer, 'mouseleave', '.inner', delegateLeaveSpy)
EventHandler.on(sibling, 'mouseenter', () => { EventHandler.on(sibling, 'mouseenter', () => {
expect(enterSpy.calls.count()).toEqual(2) expect(enterSpy.calls.count()).toEqual(2)
expect(leaveSpy.calls.count()).toEqual(2) expect(leaveSpy.calls.count()).toEqual(2)
expect(delegateEnterSpy.calls.count()).toEqual(2) expect(delegateEnterSpy.calls.count()).toEqual(2)
expect(delegateLeaveSpy.calls.count()).toEqual(2) expect(delegateLeaveSpy.calls.count()).toEqual(2)
done() resolve()
}) })
const moveMouse = (from, to) => { const moveMouse = (from, to) => {
from.dispatchEvent(new MouseEvent('mouseout', { from.dispatchEvent(new MouseEvent('mouseout', {
bubbles: true, bubbles: true,
relatedTarget: to relatedTarget: to
})) }))
to.dispatchEvent(new MouseEvent('mouseover', { to.dispatchEvent(new MouseEvent('mouseover', {
bubbles: true, bubbles: true,
relatedTarget: from relatedTarget: from
})) }))
} }
// from outer to deep and back to outer (nested) // from outer to deep and back to outer (nested)
moveMouse(outer, inner)
moveMouse(inner, nested)
moveMouse(nested, deep)
moveMouse(deep, nested)
moveMouse(nested, inner)
moveMouse(inner, outer)
setTimeout(() => {
expect(enterSpy.calls.count()).toEqual(1)
expect(leaveSpy.calls.count()).toEqual(1)
expect(delegateEnterSpy.calls.count()).toEqual(1)
expect(delegateLeaveSpy.calls.count()).toEqual(1)
// from outer to inner to sibling (adjacent)
moveMouse(outer, inner) moveMouse(outer, inner)
moveMouse(inner, sibling) moveMouse(inner, nested)
}, 20) moveMouse(nested, deep)
moveMouse(deep, nested)
moveMouse(nested, inner)
moveMouse(inner, outer)
setTimeout(() => {
expect(enterSpy.calls.count()).toEqual(1)
expect(leaveSpy.calls.count()).toEqual(1)
expect(delegateEnterSpy.calls.count()).toEqual(1)
expect(delegateLeaveSpy.calls.count()).toEqual(1)
// from outer to inner to sibling (adjacent)
moveMouse(outer, inner)
moveMouse(inner, sibling)
}, 20)
})
}) })
}) })
describe('one', () => { describe('one', () => {
it('should call listener just once', done => { it('should call listener just once', () => {
fixtureEl.innerHTML = '<div></div>' return new Promise(resolve => {
fixtureEl.innerHTML = '<div></div>'
let called = 0 let called = 0
const div = fixtureEl.querySelector('div') const div = fixtureEl.querySelector('div')
const obj = { const obj = {
oneListener() { oneListener() {
called++ called++
}
} }
}
EventHandler.one(div, 'bootstrap', obj.oneListener) EventHandler.one(div, 'bootstrap', obj.oneListener)
EventHandler.trigger(div, 'bootstrap') EventHandler.trigger(div, 'bootstrap')
EventHandler.trigger(div, 'bootstrap') EventHandler.trigger(div, 'bootstrap')
setTimeout(() => { setTimeout(() => {
expect(called).toEqual(1) expect(called).toEqual(1)
done() resolve()
}, 20) }, 20)
})
}) })
it('should call delegated listener just once', done => { it('should call delegated listener just once', () => {
fixtureEl.innerHTML = '<div></div>' return new Promise(resolve => {
fixtureEl.innerHTML = '<div></div>'
let called = 0 let called = 0
const div = fixtureEl.querySelector('div') const div = fixtureEl.querySelector('div')
const obj = { const obj = {
oneListener() { oneListener() {
called++ called++
}
} }
}
EventHandler.one(fixtureEl, 'bootstrap', 'div', obj.oneListener) EventHandler.one(fixtureEl, 'bootstrap', 'div', obj.oneListener)
EventHandler.trigger(div, 'bootstrap') EventHandler.trigger(div, 'bootstrap')
EventHandler.trigger(div, 'bootstrap') EventHandler.trigger(div, 'bootstrap')
setTimeout(() => { setTimeout(() => {
expect(called).toEqual(1) expect(called).toEqual(1)
done() resolve()
}, 20) }, 20)
})
}) })
}) })
@ -196,171 +211,185 @@ describe('EventHandler', () => {
fixtureEl.innerHTML = '<div></div>' fixtureEl.innerHTML = '<div></div>'
const div = fixtureEl.querySelector('div') const div = fixtureEl.querySelector('div')
EventHandler.off(div, null, () => {}) EventHandler.off(div, null, noop)
EventHandler.off(null, 'click', () => {}) EventHandler.off(null, 'click', noop)
expect().nothing() expect().nothing()
}) })
it('should remove a listener', done => { it('should remove a listener', () => {
fixtureEl.innerHTML = '<div></div>' return new Promise(resolve => {
const div = fixtureEl.querySelector('div') fixtureEl.innerHTML = '<div></div>'
const div = fixtureEl.querySelector('div')
let called = 0 let called = 0
const handler = () => { const handler = () => {
called++ called++
} }
EventHandler.on(div, 'foobar', handler) EventHandler.on(div, 'foobar', handler)
EventHandler.trigger(div, 'foobar') EventHandler.trigger(div, 'foobar')
EventHandler.off(div, 'foobar', handler) EventHandler.off(div, 'foobar', handler)
EventHandler.trigger(div, 'foobar') EventHandler.trigger(div, 'foobar')
setTimeout(() => { setTimeout(() => {
expect(called).toEqual(1) expect(called).toEqual(1)
done() resolve()
}, 20) }, 20)
})
}) })
it('should remove all the events', done => { it('should remove all the events', () => {
fixtureEl.innerHTML = '<div></div>' return new Promise(resolve => {
const div = fixtureEl.querySelector('div') fixtureEl.innerHTML = '<div></div>'
const div = fixtureEl.querySelector('div')
let called = 0 let called = 0
EventHandler.on(div, 'foobar', () => { EventHandler.on(div, 'foobar', () => {
called++ called++
})
EventHandler.on(div, 'foobar', () => {
called++
})
EventHandler.trigger(div, 'foobar')
EventHandler.off(div, 'foobar')
EventHandler.trigger(div, 'foobar')
setTimeout(() => {
expect(called).toEqual(2)
resolve()
}, 20)
}) })
EventHandler.on(div, 'foobar', () => {
called++
})
EventHandler.trigger(div, 'foobar')
EventHandler.off(div, 'foobar')
EventHandler.trigger(div, 'foobar')
setTimeout(() => {
expect(called).toEqual(2)
done()
}, 20)
}) })
it('should remove all the namespaced listeners if namespace is passed', done => { it('should remove all the namespaced listeners if namespace is passed', () => {
fixtureEl.innerHTML = '<div></div>' return new Promise(resolve => {
const div = fixtureEl.querySelector('div') fixtureEl.innerHTML = '<div></div>'
const div = fixtureEl.querySelector('div')
let called = 0 let called = 0
EventHandler.on(div, 'foobar.namespace', () => { EventHandler.on(div, 'foobar.namespace', () => {
called++ called++
})
EventHandler.on(div, 'foofoo.namespace', () => {
called++
})
EventHandler.trigger(div, 'foobar.namespace')
EventHandler.trigger(div, 'foofoo.namespace')
EventHandler.off(div, '.namespace')
EventHandler.trigger(div, 'foobar.namespace')
EventHandler.trigger(div, 'foofoo.namespace')
setTimeout(() => {
expect(called).toEqual(2)
resolve()
}, 20)
}) })
EventHandler.on(div, 'foofoo.namespace', () => {
called++
})
EventHandler.trigger(div, 'foobar.namespace')
EventHandler.trigger(div, 'foofoo.namespace')
EventHandler.off(div, '.namespace')
EventHandler.trigger(div, 'foobar.namespace')
EventHandler.trigger(div, 'foofoo.namespace')
setTimeout(() => {
expect(called).toEqual(2)
done()
}, 20)
}) })
it('should remove the namespaced listeners', done => { it('should remove the namespaced listeners', () => {
fixtureEl.innerHTML = '<div></div>' return new Promise(resolve => {
const div = fixtureEl.querySelector('div') fixtureEl.innerHTML = '<div></div>'
const div = fixtureEl.querySelector('div')
let calledCallback1 = 0 let calledCallback1 = 0
let calledCallback2 = 0 let calledCallback2 = 0
EventHandler.on(div, 'foobar.namespace', () => { EventHandler.on(div, 'foobar.namespace', () => {
calledCallback1++ calledCallback1++
})
EventHandler.on(div, 'foofoo.namespace', () => {
calledCallback2++
})
EventHandler.trigger(div, 'foobar.namespace')
EventHandler.off(div, 'foobar.namespace')
EventHandler.trigger(div, 'foobar.namespace')
EventHandler.trigger(div, 'foofoo.namespace')
setTimeout(() => {
expect(calledCallback1).toEqual(1)
expect(calledCallback2).toEqual(1)
resolve()
}, 20)
}) })
EventHandler.on(div, 'foofoo.namespace', () => {
calledCallback2++
})
EventHandler.trigger(div, 'foobar.namespace')
EventHandler.off(div, 'foobar.namespace')
EventHandler.trigger(div, 'foobar.namespace')
EventHandler.trigger(div, 'foofoo.namespace')
setTimeout(() => {
expect(calledCallback1).toEqual(1)
expect(calledCallback2).toEqual(1)
done()
}, 20)
}) })
it('should remove the all the namespaced listeners for native events', done => { it('should remove the all the namespaced listeners for native events', () => {
fixtureEl.innerHTML = '<div></div>' return new Promise(resolve => {
const div = fixtureEl.querySelector('div') fixtureEl.innerHTML = '<div></div>'
const div = fixtureEl.querySelector('div')
let called = 0 let called = 0
EventHandler.on(div, 'click.namespace', () => { EventHandler.on(div, 'click.namespace', () => {
called++ called++
})
EventHandler.on(div, 'click.namespace2', () => {
called++
})
EventHandler.trigger(div, 'click')
EventHandler.off(div, 'click')
EventHandler.trigger(div, 'click')
setTimeout(() => {
expect(called).toEqual(2)
resolve()
}, 20)
}) })
EventHandler.on(div, 'click.namespace2', () => {
called++
})
EventHandler.trigger(div, 'click')
EventHandler.off(div, 'click')
EventHandler.trigger(div, 'click')
setTimeout(() => {
expect(called).toEqual(2)
done()
}, 20)
}) })
it('should remove the specified namespaced listeners for native events', done => { it('should remove the specified namespaced listeners for native events', () => {
fixtureEl.innerHTML = '<div></div>' return new Promise(resolve => {
const div = fixtureEl.querySelector('div') fixtureEl.innerHTML = '<div></div>'
const div = fixtureEl.querySelector('div')
let called1 = 0 let called1 = 0
let called2 = 0 let called2 = 0
EventHandler.on(div, 'click.namespace', () => { EventHandler.on(div, 'click.namespace', () => {
called1++ called1++
})
EventHandler.on(div, 'click.namespace2', () => {
called2++
})
EventHandler.trigger(div, 'click')
EventHandler.off(div, 'click.namespace')
EventHandler.trigger(div, 'click')
setTimeout(() => {
expect(called1).toEqual(1)
expect(called2).toEqual(2)
resolve()
}, 20)
}) })
EventHandler.on(div, 'click.namespace2', () => {
called2++
})
EventHandler.trigger(div, 'click')
EventHandler.off(div, 'click.namespace')
EventHandler.trigger(div, 'click')
setTimeout(() => {
expect(called1).toEqual(1)
expect(called2).toEqual(2)
done()
}, 20)
}) })
it('should remove a listener registered by .one', done => { it('should remove a listener registered by .one', () => {
fixtureEl.innerHTML = '<div></div>' return new Promise(resolve => {
fixtureEl.innerHTML = '<div></div>'
const div = fixtureEl.querySelector('div') const div = fixtureEl.querySelector('div')
const handler = () => { const handler = () => {
throw new Error('called') throw new Error('called')
} }
EventHandler.one(div, 'foobar', handler) EventHandler.one(div, 'foobar', handler)
EventHandler.off(div, 'foobar', handler) EventHandler.off(div, 'foobar', handler)
EventHandler.trigger(div, 'foobar') EventHandler.trigger(div, 'foobar')
setTimeout(() => { setTimeout(() => {
expect().nothing() expect().nothing()
done() resolve()
}, 20) }, 20)
})
}) })
it('should remove the correct delegated event listener', () => { it('should remove the correct delegated event listener', () => {

View File

@ -1,5 +1,5 @@
import Manipulator from '../../../src/dom/manipulator' import Manipulator from '../../../src/dom/manipulator'
import { getFixture, clearFixture } from '../../helpers/fixture' import { clearFixture, getFixture } from '../../helpers/fixture'
describe('Manipulator', () => { describe('Manipulator', () => {
let fixtureEl let fixtureEl
@ -134,42 +134,44 @@ describe('Manipulator', () => {
}) })
}) })
it('should not change offset when viewport is scrolled', done => { it('should not change offset when viewport is scrolled', () => {
const top = 500 return new Promise(resolve => {
const left = 1000 const top = 500
const scrollY = 200 const left = 1000
const scrollX = 400 const scrollY = 200
const scrollX = 400
fixtureEl.innerHTML = `<div style="position:absolute;top:${top}px;left:${left}px"></div>` fixtureEl.innerHTML = `<div style="position:absolute;top:${top}px;left:${left}px"></div>`
const div = fixtureEl.querySelector('div') const div = fixtureEl.querySelector('div')
const offset = Manipulator.offset(div) const offset = Manipulator.offset(div)
// append an element that forces scrollbars on the window so we can scroll // append an element that forces scrollbars on the window so we can scroll
const { defaultView: win, body } = fixtureEl.ownerDocument const { defaultView: win, body } = fixtureEl.ownerDocument
const forceScrollBars = document.createElement('div') const forceScrollBars = document.createElement('div')
forceScrollBars.style.cssText = 'position:absolute;top:5000px;left:5000px;width:1px;height:1px' forceScrollBars.style.cssText = 'position:absolute;top:5000px;left:5000px;width:1px;height:1px'
body.append(forceScrollBars) body.append(forceScrollBars)
const scrollHandler = () => { const scrollHandler = () => {
expect(window.pageYOffset).toEqual(scrollY) expect(window.pageYOffset).toEqual(scrollY)
expect(window.pageXOffset).toEqual(scrollX) expect(window.pageXOffset).toEqual(scrollX)
const newOffset = Manipulator.offset(div) const newOffset = Manipulator.offset(div)
expect(newOffset).toEqual({ expect(newOffset).toEqual({
top: offset.top, top: offset.top,
left: offset.left left: offset.left
}) })
win.removeEventListener('scroll', scrollHandler) win.removeEventListener('scroll', scrollHandler)
forceScrollBars.remove() forceScrollBars.remove()
win.scrollTo(0, 0) win.scrollTo(0, 0)
done() resolve()
} }
win.addEventListener('scroll', scrollHandler) win.addEventListener('scroll', scrollHandler)
win.scrollTo(scrollX, scrollY) win.scrollTo(scrollX, scrollY)
})
}) })
}) })

View File

@ -57,36 +57,38 @@ describe('Dropdown', () => {
expect(dropdownByElement._element).toEqual(btnDropdown) expect(dropdownByElement._element).toEqual(btnDropdown)
}) })
it('should create offset modifier correctly when offset option is a function', done => { it('should create offset modifier correctly when offset option is a function', () => {
fixtureEl.innerHTML = [ return new Promise(resolve => {
'<div class="dropdown">', fixtureEl.innerHTML = [
' <button class="btn dropdown-toggle" data-bs-toggle="dropdown">Dropdown</button>', '<div class="dropdown">',
' <div class="dropdown-menu">', ' <button class="btn dropdown-toggle" data-bs-toggle="dropdown">Dropdown</button>',
' <a class="dropdown-item" href="#">Secondary link</a>', ' <div class="dropdown-menu">',
' </div>', ' <a class="dropdown-item" href="#">Secondary link</a>',
'</div>' ' </div>',
].join('') '</div>'
].join('')
const getOffset = jasmine.createSpy('getOffset').and.returnValue([10, 20]) const getOffset = jasmine.createSpy('getOffset').and.returnValue([10, 20])
const btnDropdown = fixtureEl.querySelector('[data-bs-toggle="dropdown"]') const btnDropdown = fixtureEl.querySelector('[data-bs-toggle="dropdown"]')
const dropdown = new Dropdown(btnDropdown, { const dropdown = new Dropdown(btnDropdown, {
offset: getOffset, offset: getOffset,
popperConfig: { popperConfig: {
onFirstUpdate: state => { onFirstUpdate: state => {
expect(getOffset).toHaveBeenCalledWith({ expect(getOffset).toHaveBeenCalledWith({
popper: state.rects.popper, popper: state.rects.popper,
reference: state.rects.reference, reference: state.rects.reference,
placement: state.placement placement: state.placement
}, btnDropdown) }, btnDropdown)
done() resolve()
}
} }
} })
const offset = dropdown._getOffset()
expect(typeof offset).toEqual('function')
dropdown.show()
}) })
const offset = dropdown._getOffset()
expect(typeof offset).toEqual('function')
dropdown.show()
}) })
it('should create offset modifier correctly when offset option is a string into data attribute', () => { it('should create offset modifier correctly when offset option is a string into data attribute', () => {
@ -151,761 +153,817 @@ describe('Dropdown', () => {
}) })
describe('toggle', () => { describe('toggle', () => {
it('should toggle a dropdown', done => { it('should toggle a dropdown', () => {
fixtureEl.innerHTML = [ return new Promise(resolve => {
'<div class="dropdown">', fixtureEl.innerHTML = [
' <button class="btn dropdown-toggle" data-bs-toggle="dropdown" aria-expanded="false">Dropdown</button>', '<div class="dropdown">',
' <div class="dropdown-menu">', ' <button class="btn dropdown-toggle" data-bs-toggle="dropdown" aria-expanded="false">Dropdown</button>',
' <a class="dropdown-item" href="#">Secondary link</a>', ' <div class="dropdown-menu">',
' </div>', ' <a class="dropdown-item" href="#">Secondary link</a>',
'</div>' ' </div>',
].join('') '</div>'
].join('')
const btnDropdown = fixtureEl.querySelector('[data-bs-toggle="dropdown"]') const btnDropdown = fixtureEl.querySelector('[data-bs-toggle="dropdown"]')
const dropdown = new Dropdown(btnDropdown) const dropdown = new Dropdown(btnDropdown)
btnDropdown.addEventListener('shown.bs.dropdown', () => { btnDropdown.addEventListener('shown.bs.dropdown', () => {
expect(btnDropdown).toHaveClass('show') expect(btnDropdown).toHaveClass('show')
expect(btnDropdown.getAttribute('aria-expanded')).toEqual('true') expect(btnDropdown.getAttribute('aria-expanded')).toEqual('true')
done() resolve()
}) })
dropdown.toggle()
})
it('should destroy old popper references on toggle', done => {
fixtureEl.innerHTML = [
'<div class="first dropdown">',
' <button class="firstBtn btn" data-bs-toggle="dropdown" aria-expanded="false">Dropdown</button>',
' <div class="dropdown-menu">',
' <a class="dropdown-item" href="#">Secondary link</a>',
' </div>',
'</div>',
'<div class="second dropdown">',
' <button class="secondBtn btn" data-bs-toggle="dropdown" aria-expanded="false">Dropdown</button>',
' <div class="dropdown-menu">',
' <a class="dropdown-item" href="#">Secondary link</a>',
' </div>',
'</div>'
].join('')
const btnDropdown1 = fixtureEl.querySelector('.firstBtn')
const btnDropdown2 = fixtureEl.querySelector('.secondBtn')
const firstDropdownEl = fixtureEl.querySelector('.first')
const secondDropdownEl = fixtureEl.querySelector('.second')
const dropdown1 = new Dropdown(btnDropdown1)
firstDropdownEl.addEventListener('shown.bs.dropdown', () => {
expect(btnDropdown1).toHaveClass('show')
spyOn(dropdown1._popper, 'destroy')
btnDropdown2.click()
})
secondDropdownEl.addEventListener('shown.bs.dropdown', () => setTimeout(() => {
expect(dropdown1._popper.destroy).toHaveBeenCalled()
done()
}))
dropdown1.toggle()
})
it('should toggle a dropdown and add/remove event listener on mobile', done => {
fixtureEl.innerHTML = [
'<div class="dropdown">',
' <button class="btn dropdown-toggle" data-bs-toggle="dropdown" aria-expanded="false">Dropdown</button>',
' <div class="dropdown-menu">',
' <a class="dropdown-item" href="#">Secondary link</a>',
' </div>',
'</div>'
].join('')
const defaultValueOnTouchStart = document.documentElement.ontouchstart
const btnDropdown = fixtureEl.querySelector('[data-bs-toggle="dropdown"]')
const dropdown = new Dropdown(btnDropdown)
document.documentElement.ontouchstart = noop
spyOn(EventHandler, 'on')
spyOn(EventHandler, 'off')
btnDropdown.addEventListener('shown.bs.dropdown', () => {
expect(btnDropdown).toHaveClass('show')
expect(btnDropdown.getAttribute('aria-expanded')).toEqual('true')
expect(EventHandler.on).toHaveBeenCalledWith(jasmine.any(Object), 'mouseover', noop)
dropdown.toggle() dropdown.toggle()
}) })
btnDropdown.addEventListener('hidden.bs.dropdown', () => {
expect(btnDropdown).not.toHaveClass('show')
expect(btnDropdown.getAttribute('aria-expanded')).toEqual('false')
expect(EventHandler.off).toHaveBeenCalledWith(jasmine.any(Object), 'mouseover', noop)
document.documentElement.ontouchstart = defaultValueOnTouchStart
done()
})
dropdown.toggle()
}) })
it('should toggle a dropdown at the right', done => { it('should destroy old popper references on toggle', () => {
fixtureEl.innerHTML = [ return new Promise(resolve => {
'<div class="dropdown">', fixtureEl.innerHTML = [
' <button class="btn dropdown-toggle" data-bs-toggle="dropdown" aria-expanded="false">Dropdown</button>', '<div class="first dropdown">',
' <div class="dropdown-menu dropdown-menu-end">', ' <button class="firstBtn btn" data-bs-toggle="dropdown" aria-expanded="false">Dropdown</button>',
' <a class="dropdown-item" href="#">Secondary link</a>', ' <div class="dropdown-menu">',
' </div>', ' <a class="dropdown-item" href="#">Secondary link</a>',
'</div>' ' </div>',
].join('') '</div>',
'<div class="second dropdown">',
' <button class="secondBtn btn" data-bs-toggle="dropdown" aria-expanded="false">Dropdown</button>',
' <div class="dropdown-menu">',
' <a class="dropdown-item" href="#">Secondary link</a>',
' </div>',
'</div>'
].join('')
const btnDropdown = fixtureEl.querySelector('[data-bs-toggle="dropdown"]') const btnDropdown1 = fixtureEl.querySelector('.firstBtn')
const dropdown = new Dropdown(btnDropdown) const btnDropdown2 = fixtureEl.querySelector('.secondBtn')
const firstDropdownEl = fixtureEl.querySelector('.first')
const secondDropdownEl = fixtureEl.querySelector('.second')
const dropdown1 = new Dropdown(btnDropdown1)
btnDropdown.addEventListener('shown.bs.dropdown', () => { firstDropdownEl.addEventListener('shown.bs.dropdown', () => {
expect(btnDropdown).toHaveClass('show') expect(btnDropdown1).toHaveClass('show')
expect(btnDropdown.getAttribute('aria-expanded')).toEqual('true') spyOn(dropdown1._popper, 'destroy')
done() btnDropdown2.click()
})
secondDropdownEl.addEventListener('shown.bs.dropdown', () => setTimeout(() => {
expect(dropdown1._popper.destroy).toHaveBeenCalled()
resolve()
}))
dropdown1.toggle()
}) })
dropdown.toggle()
}) })
it('should toggle a dropup', done => { it('should toggle a dropdown and add/remove event listener on mobile', () => {
fixtureEl.innerHTML = [ return new Promise(resolve => {
'<div class="dropup">', fixtureEl.innerHTML = [
' <button class="btn dropdown-toggle" data-bs-toggle="dropdown" aria-expanded="false">Dropdown</button>', '<div class="dropdown">',
' <div class="dropdown-menu">', ' <button class="btn dropdown-toggle" data-bs-toggle="dropdown" aria-expanded="false">Dropdown</button>',
' <a class="dropdown-item" href="#">Secondary link</a>', ' <div class="dropdown-menu">',
' </div>', ' <a class="dropdown-item" href="#">Secondary link</a>',
'</div>' ' </div>',
].join('') '</div>'
].join('')
const btnDropdown = fixtureEl.querySelector('[data-bs-toggle="dropdown"]') const defaultValueOnTouchStart = document.documentElement.ontouchstart
const dropupEl = fixtureEl.querySelector('.dropup') const btnDropdown = fixtureEl.querySelector('[data-bs-toggle="dropdown"]')
const dropdown = new Dropdown(btnDropdown) const dropdown = new Dropdown(btnDropdown)
dropupEl.addEventListener('shown.bs.dropdown', () => { document.documentElement.ontouchstart = noop
expect(btnDropdown).toHaveClass('show') spyOn(EventHandler, 'on')
expect(btnDropdown.getAttribute('aria-expanded')).toEqual('true') spyOn(EventHandler, 'off')
done()
btnDropdown.addEventListener('shown.bs.dropdown', () => {
expect(btnDropdown).toHaveClass('show')
expect(btnDropdown.getAttribute('aria-expanded')).toEqual('true')
expect(EventHandler.on).toHaveBeenCalledWith(jasmine.any(Object), 'mouseover', noop)
dropdown.toggle()
})
btnDropdown.addEventListener('hidden.bs.dropdown', () => {
expect(btnDropdown).not.toHaveClass('show')
expect(btnDropdown.getAttribute('aria-expanded')).toEqual('false')
expect(EventHandler.off).toHaveBeenCalledWith(jasmine.any(Object), 'mouseover', noop)
document.documentElement.ontouchstart = defaultValueOnTouchStart
resolve()
})
dropdown.toggle()
}) })
dropdown.toggle()
}) })
it('should toggle a dropup at the right', done => { it('should toggle a dropdown at the right', () => {
fixtureEl.innerHTML = [ return new Promise(resolve => {
'<div class="dropup">', fixtureEl.innerHTML = [
' <button class="btn dropdown-toggle" data-bs-toggle="dropdown" aria-expanded="false">Dropdown</button>', '<div class="dropdown">',
' <div class="dropdown-menu dropdown-menu-end">', ' <button class="btn dropdown-toggle" data-bs-toggle="dropdown" aria-expanded="false">Dropdown</button>',
' <a class="dropdown-item" href="#">Secondary link</a>', ' <div class="dropdown-menu dropdown-menu-end">',
' </div>', ' <a class="dropdown-item" href="#">Secondary link</a>',
'</div>' ' </div>',
].join('') '</div>'
].join('')
const btnDropdown = fixtureEl.querySelector('[data-bs-toggle="dropdown"]') const btnDropdown = fixtureEl.querySelector('[data-bs-toggle="dropdown"]')
const dropupEl = fixtureEl.querySelector('.dropup') const dropdown = new Dropdown(btnDropdown)
const dropdown = new Dropdown(btnDropdown)
dropupEl.addEventListener('shown.bs.dropdown', () => { btnDropdown.addEventListener('shown.bs.dropdown', () => {
expect(btnDropdown).toHaveClass('show') expect(btnDropdown).toHaveClass('show')
expect(btnDropdown.getAttribute('aria-expanded')).toEqual('true') expect(btnDropdown.getAttribute('aria-expanded')).toEqual('true')
done() resolve()
})
dropdown.toggle()
}) })
dropdown.toggle()
}) })
it('should toggle a dropend', done => { it('should toggle a dropup', () => {
fixtureEl.innerHTML = [ return new Promise(resolve => {
'<div class="dropend">', fixtureEl.innerHTML = [
' <button class="btn dropdown-toggle" data-bs-toggle="dropdown" aria-expanded="false">Dropdown</button>', '<div class="dropup">',
' <div class="dropdown-menu">', ' <button class="btn dropdown-toggle" data-bs-toggle="dropdown" aria-expanded="false">Dropdown</button>',
' <a class="dropdown-item" href="#">Secondary link</a>', ' <div class="dropdown-menu">',
' </div>', ' <a class="dropdown-item" href="#">Secondary link</a>',
'</div>' ' </div>',
].join('') '</div>'
].join('')
const btnDropdown = fixtureEl.querySelector('[data-bs-toggle="dropdown"]') const btnDropdown = fixtureEl.querySelector('[data-bs-toggle="dropdown"]')
const dropendEl = fixtureEl.querySelector('.dropend') const dropupEl = fixtureEl.querySelector('.dropup')
const dropdown = new Dropdown(btnDropdown) const dropdown = new Dropdown(btnDropdown)
dropendEl.addEventListener('shown.bs.dropdown', () => { dropupEl.addEventListener('shown.bs.dropdown', () => {
expect(btnDropdown).toHaveClass('show') expect(btnDropdown).toHaveClass('show')
expect(btnDropdown.getAttribute('aria-expanded')).toEqual('true') expect(btnDropdown.getAttribute('aria-expanded')).toEqual('true')
done() resolve()
})
dropdown.toggle()
}) })
dropdown.toggle()
}) })
it('should toggle a dropstart', done => { it('should toggle a dropup at the right', () => {
fixtureEl.innerHTML = [ return new Promise(resolve => {
'<div class="dropstart">', fixtureEl.innerHTML = [
' <button class="btn dropdown-toggle" data-bs-toggle="dropdown" aria-expanded="false">Dropdown</button>', '<div class="dropup">',
' <div class="dropdown-menu">', ' <button class="btn dropdown-toggle" data-bs-toggle="dropdown" aria-expanded="false">Dropdown</button>',
' <a class="dropdown-item" href="#">Secondary link</a>', ' <div class="dropdown-menu dropdown-menu-end">',
' </div>', ' <a class="dropdown-item" href="#">Secondary link</a>',
'</div>' ' </div>',
].join('') '</div>'
].join('')
const btnDropdown = fixtureEl.querySelector('[data-bs-toggle="dropdown"]') const btnDropdown = fixtureEl.querySelector('[data-bs-toggle="dropdown"]')
const dropstartEl = fixtureEl.querySelector('.dropstart') const dropupEl = fixtureEl.querySelector('.dropup')
const dropdown = new Dropdown(btnDropdown) const dropdown = new Dropdown(btnDropdown)
dropstartEl.addEventListener('shown.bs.dropdown', () => { dropupEl.addEventListener('shown.bs.dropdown', () => {
expect(btnDropdown).toHaveClass('show') expect(btnDropdown).toHaveClass('show')
expect(btnDropdown.getAttribute('aria-expanded')).toEqual('true') expect(btnDropdown.getAttribute('aria-expanded')).toEqual('true')
done() resolve()
})
dropdown.toggle()
}) })
dropdown.toggle()
}) })
it('should toggle a dropdown with parent reference', done => { it('should toggle a dropend', () => {
fixtureEl.innerHTML = [ return new Promise(resolve => {
'<div class="dropdown">', fixtureEl.innerHTML = [
' <button class="btn dropdown-toggle" data-bs-toggle="dropdown" aria-expanded="false">Dropdown</button>', '<div class="dropend">',
' <div class="dropdown-menu">', ' <button class="btn dropdown-toggle" data-bs-toggle="dropdown" aria-expanded="false">Dropdown</button>',
' <a class="dropdown-item" href="#">Secondary link</a>', ' <div class="dropdown-menu">',
' </div>', ' <a class="dropdown-item" href="#">Secondary link</a>',
'</div>' ' </div>',
].join('') '</div>'
].join('')
const btnDropdown = fixtureEl.querySelector('[data-bs-toggle="dropdown"]') const btnDropdown = fixtureEl.querySelector('[data-bs-toggle="dropdown"]')
const dropdown = new Dropdown(btnDropdown, { const dropendEl = fixtureEl.querySelector('.dropend')
reference: 'parent' const dropdown = new Dropdown(btnDropdown)
dropendEl.addEventListener('shown.bs.dropdown', () => {
expect(btnDropdown).toHaveClass('show')
expect(btnDropdown.getAttribute('aria-expanded')).toEqual('true')
resolve()
})
dropdown.toggle()
}) })
btnDropdown.addEventListener('shown.bs.dropdown', () => {
expect(btnDropdown).toHaveClass('show')
expect(btnDropdown.getAttribute('aria-expanded')).toEqual('true')
done()
})
dropdown.toggle()
}) })
it('should toggle a dropdown with a dom node reference', done => { it('should toggle a dropstart', () => {
fixtureEl.innerHTML = [ return new Promise(resolve => {
'<div class="dropdown">', fixtureEl.innerHTML = [
' <button class="btn dropdown-toggle" data-bs-toggle="dropdown" aria-expanded="false">Dropdown</button>', '<div class="dropstart">',
' <div class="dropdown-menu">', ' <button class="btn dropdown-toggle" data-bs-toggle="dropdown" aria-expanded="false">Dropdown</button>',
' <a class="dropdown-item" href="#">Secondary link</a>', ' <div class="dropdown-menu">',
' </div>', ' <a class="dropdown-item" href="#">Secondary link</a>',
'</div>' ' </div>',
].join('') '</div>'
].join('')
const btnDropdown = fixtureEl.querySelector('[data-bs-toggle="dropdown"]') const btnDropdown = fixtureEl.querySelector('[data-bs-toggle="dropdown"]')
const dropdown = new Dropdown(btnDropdown, { const dropstartEl = fixtureEl.querySelector('.dropstart')
reference: fixtureEl const dropdown = new Dropdown(btnDropdown)
dropstartEl.addEventListener('shown.bs.dropdown', () => {
expect(btnDropdown).toHaveClass('show')
expect(btnDropdown.getAttribute('aria-expanded')).toEqual('true')
resolve()
})
dropdown.toggle()
}) })
btnDropdown.addEventListener('shown.bs.dropdown', () => {
expect(btnDropdown).toHaveClass('show')
expect(btnDropdown.getAttribute('aria-expanded')).toEqual('true')
done()
})
dropdown.toggle()
}) })
it('should toggle a dropdown with a jquery object reference', done => { it('should toggle a dropdown with parent reference', () => {
fixtureEl.innerHTML = [ return new Promise(resolve => {
'<div class="dropdown">', fixtureEl.innerHTML = [
' <button class="btn dropdown-toggle" data-bs-toggle="dropdown" aria-expanded="false">Dropdown</button>', '<div class="dropdown">',
' <div class="dropdown-menu">', ' <button class="btn dropdown-toggle" data-bs-toggle="dropdown" aria-expanded="false">Dropdown</button>',
' <a class="dropdown-item" href="#">Secondary link</a>', ' <div class="dropdown-menu">',
' </div>', ' <a class="dropdown-item" href="#">Secondary link</a>',
'</div>' ' </div>',
].join('') '</div>'
].join('')
const btnDropdown = fixtureEl.querySelector('[data-bs-toggle="dropdown"]') const btnDropdown = fixtureEl.querySelector('[data-bs-toggle="dropdown"]')
const dropdown = new Dropdown(btnDropdown, { const dropdown = new Dropdown(btnDropdown, {
reference: { 0: fixtureEl, jquery: 'jQuery' } reference: 'parent'
})
btnDropdown.addEventListener('shown.bs.dropdown', () => {
expect(btnDropdown).toHaveClass('show')
expect(btnDropdown.getAttribute('aria-expanded')).toEqual('true')
resolve()
})
dropdown.toggle()
}) })
btnDropdown.addEventListener('shown.bs.dropdown', () => {
expect(btnDropdown).toHaveClass('show')
expect(btnDropdown.getAttribute('aria-expanded')).toEqual('true')
done()
})
dropdown.toggle()
}) })
it('should toggle a dropdown with a valid virtual element reference', done => { it('should toggle a dropdown with a dom node reference', () => {
fixtureEl.innerHTML = [ return new Promise(resolve => {
'<div class="dropdown">', fixtureEl.innerHTML = [
' <button class="btn dropdown-toggle visually-hidden" data-bs-toggle="dropdown" aria-expanded="false">Dropdown</button>', '<div class="dropdown">',
' <div class="dropdown-menu">', ' <button class="btn dropdown-toggle" data-bs-toggle="dropdown" aria-expanded="false">Dropdown</button>',
' <a class="dropdown-item" href="#">Secondary link</a>', ' <div class="dropdown-menu">',
' </div>', ' <a class="dropdown-item" href="#">Secondary link</a>',
'</div>' ' </div>',
].join('') '</div>'
].join('')
const btnDropdown = fixtureEl.querySelector('[data-bs-toggle="dropdown"]') const btnDropdown = fixtureEl.querySelector('[data-bs-toggle="dropdown"]')
const virtualElement = { const dropdown = new Dropdown(btnDropdown, {
nodeType: 1, reference: fixtureEl
getBoundingClientRect() { })
return {
width: 0, btnDropdown.addEventListener('shown.bs.dropdown', () => {
height: 0, expect(btnDropdown).toHaveClass('show')
top: 0, expect(btnDropdown.getAttribute('aria-expanded')).toEqual('true')
right: 0, resolve()
bottom: 0, })
left: 0
dropdown.toggle()
})
})
it('should toggle a dropdown with a jquery object reference', () => {
return new Promise(resolve => {
fixtureEl.innerHTML = [
'<div class="dropdown">',
' <button class="btn dropdown-toggle" data-bs-toggle="dropdown" aria-expanded="false">Dropdown</button>',
' <div class="dropdown-menu">',
' <a class="dropdown-item" href="#">Secondary link</a>',
' </div>',
'</div>'
].join('')
const btnDropdown = fixtureEl.querySelector('[data-bs-toggle="dropdown"]')
const dropdown = new Dropdown(btnDropdown, {
reference: { 0: fixtureEl, jquery: 'jQuery' }
})
btnDropdown.addEventListener('shown.bs.dropdown', () => {
expect(btnDropdown).toHaveClass('show')
expect(btnDropdown.getAttribute('aria-expanded')).toEqual('true')
resolve()
})
dropdown.toggle()
})
})
it('should toggle a dropdown with a valid virtual element reference', () => {
return new Promise(resolve => {
fixtureEl.innerHTML = [
'<div class="dropdown">',
' <button class="btn dropdown-toggle visually-hidden" data-bs-toggle="dropdown" aria-expanded="false">Dropdown</button>',
' <div class="dropdown-menu">',
' <a class="dropdown-item" href="#">Secondary link</a>',
' </div>',
'</div>'
].join('')
const btnDropdown = fixtureEl.querySelector('[data-bs-toggle="dropdown"]')
const virtualElement = {
nodeType: 1,
getBoundingClientRect() {
return {
width: 0,
height: 0,
top: 0,
right: 0,
bottom: 0,
left: 0
}
} }
} }
}
expect(() => new Dropdown(btnDropdown, { expect(() => new Dropdown(btnDropdown, {
reference: {} reference: {}
})).toThrowError(TypeError, 'DROPDOWN: Option "reference" provided type "object" without a required "getBoundingClientRect" method.') })).toThrowError(TypeError, 'DROPDOWN: Option "reference" provided type "object" without a required "getBoundingClientRect" method.')
expect(() => new Dropdown(btnDropdown, { expect(() => new Dropdown(btnDropdown, {
reference: { reference: {
getBoundingClientRect: 'not-a-function' getBoundingClientRect: 'not-a-function'
}
})).toThrowError(TypeError, 'DROPDOWN: Option "reference" provided type "object" without a required "getBoundingClientRect" method.')
// use onFirstUpdate as Poppers internal update is executed async
const dropdown = new Dropdown(btnDropdown, {
reference: virtualElement,
popperConfig: {
onFirstUpdate() {
expect(virtualElement.getBoundingClientRect).toHaveBeenCalled()
expect(btnDropdown).toHaveClass('show')
expect(btnDropdown.getAttribute('aria-expanded')).toEqual('true')
done()
} }
} })).toThrowError(TypeError, 'DROPDOWN: Option "reference" provided type "object" without a required "getBoundingClientRect" method.')
})
spyOn(virtualElement, 'getBoundingClientRect').and.callThrough() // use onFirstUpdate as Poppers internal update is executed async
const dropdown = new Dropdown(btnDropdown, {
reference: virtualElement,
popperConfig: {
onFirstUpdate() {
expect(virtualElement.getBoundingClientRect).toHaveBeenCalled()
expect(btnDropdown).toHaveClass('show')
expect(btnDropdown.getAttribute('aria-expanded')).toEqual('true')
resolve()
}
}
})
dropdown.toggle() spyOn(virtualElement, 'getBoundingClientRect').and.callThrough()
})
it('should not toggle a dropdown if the element is disabled', done => { dropdown.toggle()
fixtureEl.innerHTML = [
'<div class="dropdown">',
' <button disabled class="btn dropdown-toggle" data-bs-toggle="dropdown">Dropdown</button>',
' <div class="dropdown-menu">',
' <a class="dropdown-item" href="#">Secondary link</a>',
' </div>',
'</div>'
].join('')
const btnDropdown = fixtureEl.querySelector('[data-bs-toggle="dropdown"]')
const dropdown = new Dropdown(btnDropdown)
btnDropdown.addEventListener('shown.bs.dropdown', () => {
throw new Error('should not throw shown.bs.dropdown event')
})
dropdown.toggle()
setTimeout(() => {
expect().nothing()
done()
}) })
}) })
it('should not toggle a dropdown if the element contains .disabled', done => { it('should not toggle a dropdown if the element is disabled', () => {
fixtureEl.innerHTML = [ return new Promise(resolve => {
'<div class="dropdown">', fixtureEl.innerHTML = [
' <button class="btn dropdown-toggle disabled" data-bs-toggle="dropdown">Dropdown</button>', '<div class="dropdown">',
' <div class="dropdown-menu">', ' <button disabled class="btn dropdown-toggle" data-bs-toggle="dropdown">Dropdown</button>',
' <a class="dropdown-item" href="#">Secondary link</a>', ' <div class="dropdown-menu">',
' </div>', ' <a class="dropdown-item" href="#">Secondary link</a>',
'</div>' ' </div>',
].join('') '</div>'
].join('')
const btnDropdown = fixtureEl.querySelector('[data-bs-toggle="dropdown"]') const btnDropdown = fixtureEl.querySelector('[data-bs-toggle="dropdown"]')
const dropdown = new Dropdown(btnDropdown) const dropdown = new Dropdown(btnDropdown)
btnDropdown.addEventListener('shown.bs.dropdown', () => { btnDropdown.addEventListener('shown.bs.dropdown', () => {
throw new Error('should not throw shown.bs.dropdown event') throw new Error('should not throw shown.bs.dropdown event')
}) })
dropdown.toggle() dropdown.toggle()
setTimeout(() => { setTimeout(() => {
expect().nothing() expect().nothing()
done() resolve()
})
}) })
}) })
it('should not toggle a dropdown if the menu is shown', done => { it('should not toggle a dropdown if the element contains .disabled', () => {
fixtureEl.innerHTML = [ return new Promise(resolve => {
'<div class="dropdown">', fixtureEl.innerHTML = [
' <button class="btn dropdown-toggle" data-bs-toggle="dropdown">Dropdown</button>', '<div class="dropdown">',
' <div class="dropdown-menu show">', ' <button class="btn dropdown-toggle disabled" data-bs-toggle="dropdown">Dropdown</button>',
' <a class="dropdown-item" href="#">Secondary link</a>', ' <div class="dropdown-menu">',
' </div>', ' <a class="dropdown-item" href="#">Secondary link</a>',
'</div>' ' </div>',
].join('') '</div>'
].join('')
const btnDropdown = fixtureEl.querySelector('[data-bs-toggle="dropdown"]') const btnDropdown = fixtureEl.querySelector('[data-bs-toggle="dropdown"]')
const dropdown = new Dropdown(btnDropdown) const dropdown = new Dropdown(btnDropdown)
btnDropdown.addEventListener('shown.bs.dropdown', () => { btnDropdown.addEventListener('shown.bs.dropdown', () => {
throw new Error('should not throw shown.bs.dropdown event') throw new Error('should not throw shown.bs.dropdown event')
}) })
dropdown.toggle() dropdown.toggle()
setTimeout(() => { setTimeout(() => {
expect().nothing() expect().nothing()
done() resolve()
})
}) })
}) })
it('should not toggle a dropdown if show event is prevented', done => { it('should not toggle a dropdown if the menu is shown', () => {
fixtureEl.innerHTML = [ return new Promise(resolve => {
'<div class="dropdown">', fixtureEl.innerHTML = [
' <button class="btn dropdown-toggle" data-bs-toggle="dropdown">Dropdown</button>', '<div class="dropdown">',
' <div class="dropdown-menu">', ' <button class="btn dropdown-toggle" data-bs-toggle="dropdown">Dropdown</button>',
' <a class="dropdown-item" href="#">Secondary link</a>', ' <div class="dropdown-menu show">',
' </div>', ' <a class="dropdown-item" href="#">Secondary link</a>',
'</div>' ' </div>',
].join('') '</div>'
].join('')
const btnDropdown = fixtureEl.querySelector('[data-bs-toggle="dropdown"]') const btnDropdown = fixtureEl.querySelector('[data-bs-toggle="dropdown"]')
const dropdown = new Dropdown(btnDropdown) const dropdown = new Dropdown(btnDropdown)
btnDropdown.addEventListener('show.bs.dropdown', event => { btnDropdown.addEventListener('shown.bs.dropdown', () => {
event.preventDefault() throw new Error('should not throw shown.bs.dropdown event')
})
dropdown.toggle()
setTimeout(() => {
expect().nothing()
resolve()
})
}) })
})
btnDropdown.addEventListener('shown.bs.dropdown', () => { it('should not toggle a dropdown if show event is prevented', () => {
throw new Error('should not throw shown.bs.dropdown event') return new Promise(resolve => {
}) fixtureEl.innerHTML = [
'<div class="dropdown">',
' <button class="btn dropdown-toggle" data-bs-toggle="dropdown">Dropdown</button>',
' <div class="dropdown-menu">',
' <a class="dropdown-item" href="#">Secondary link</a>',
' </div>',
'</div>'
].join('')
dropdown.toggle() const btnDropdown = fixtureEl.querySelector('[data-bs-toggle="dropdown"]')
const dropdown = new Dropdown(btnDropdown)
setTimeout(() => { btnDropdown.addEventListener('show.bs.dropdown', event => {
expect().nothing() event.preventDefault()
done() })
btnDropdown.addEventListener('shown.bs.dropdown', () => {
throw new Error('should not throw shown.bs.dropdown event')
})
dropdown.toggle()
setTimeout(() => {
expect().nothing()
resolve()
})
}) })
}) })
}) })
describe('show', () => { describe('show', () => {
it('should show a dropdown', done => { it('should show a dropdown', () => {
fixtureEl.innerHTML = [ return new Promise(resolve => {
'<div class="dropdown">', fixtureEl.innerHTML = [
' <button class="btn dropdown-toggle" data-bs-toggle="dropdown">Dropdown</button>', '<div class="dropdown">',
' <div class="dropdown-menu">', ' <button class="btn dropdown-toggle" data-bs-toggle="dropdown">Dropdown</button>',
' <a class="dropdown-item" href="#">Secondary link</a>', ' <div class="dropdown-menu">',
' </div>', ' <a class="dropdown-item" href="#">Secondary link</a>',
'</div>' ' </div>',
].join('') '</div>'
].join('')
const btnDropdown = fixtureEl.querySelector('[data-bs-toggle="dropdown"]') const btnDropdown = fixtureEl.querySelector('[data-bs-toggle="dropdown"]')
const dropdown = new Dropdown(btnDropdown) const dropdown = new Dropdown(btnDropdown)
btnDropdown.addEventListener('shown.bs.dropdown', () => { btnDropdown.addEventListener('shown.bs.dropdown', () => {
expect(btnDropdown).toHaveClass('show') expect(btnDropdown).toHaveClass('show')
done() resolve()
})
dropdown.show()
}) })
dropdown.show()
}) })
it('should not show a dropdown if the element is disabled', done => { it('should not show a dropdown if the element is disabled', () => {
fixtureEl.innerHTML = [ return new Promise(resolve => {
'<div class="dropdown">', fixtureEl.innerHTML = [
' <button disabled class="btn dropdown-toggle" data-bs-toggle="dropdown">Dropdown</button>', '<div class="dropdown">',
' <div class="dropdown-menu">', ' <button disabled class="btn dropdown-toggle" data-bs-toggle="dropdown">Dropdown</button>',
' <a class="dropdown-item" href="#">Secondary link</a>', ' <div class="dropdown-menu">',
' </div>', ' <a class="dropdown-item" href="#">Secondary link</a>',
'</div>' ' </div>',
].join('') '</div>'
].join('')
const btnDropdown = fixtureEl.querySelector('[data-bs-toggle="dropdown"]') const btnDropdown = fixtureEl.querySelector('[data-bs-toggle="dropdown"]')
const dropdown = new Dropdown(btnDropdown) const dropdown = new Dropdown(btnDropdown)
btnDropdown.addEventListener('shown.bs.dropdown', () => { btnDropdown.addEventListener('shown.bs.dropdown', () => {
throw new Error('should not throw shown.bs.dropdown event') throw new Error('should not throw shown.bs.dropdown event')
})
dropdown.show()
setTimeout(() => {
expect().nothing()
resolve()
}, 10)
}) })
dropdown.show()
setTimeout(() => {
expect().nothing()
done()
}, 10)
}) })
it('should not show a dropdown if the element contains .disabled', done => { it('should not show a dropdown if the element contains .disabled', () => {
fixtureEl.innerHTML = [ return new Promise(resolve => {
'<div class="dropdown">', fixtureEl.innerHTML = [
' <button class="btn dropdown-toggle disabled" data-bs-toggle="dropdown">Dropdown</button>', '<div class="dropdown">',
' <div class="dropdown-menu">', ' <button class="btn dropdown-toggle disabled" data-bs-toggle="dropdown">Dropdown</button>',
' <a class="dropdown-item" href="#">Secondary link</a>', ' <div class="dropdown-menu">',
' </div>', ' <a class="dropdown-item" href="#">Secondary link</a>',
'</div>' ' </div>',
].join('') '</div>'
].join('')
const btnDropdown = fixtureEl.querySelector('[data-bs-toggle="dropdown"]') const btnDropdown = fixtureEl.querySelector('[data-bs-toggle="dropdown"]')
const dropdown = new Dropdown(btnDropdown) const dropdown = new Dropdown(btnDropdown)
btnDropdown.addEventListener('shown.bs.dropdown', () => { btnDropdown.addEventListener('shown.bs.dropdown', () => {
throw new Error('should not throw shown.bs.dropdown event') throw new Error('should not throw shown.bs.dropdown event')
})
dropdown.show()
setTimeout(() => {
expect().nothing()
resolve()
}, 10)
}) })
dropdown.show()
setTimeout(() => {
expect().nothing()
done()
}, 10)
}) })
it('should not show a dropdown if the menu is shown', done => { it('should not show a dropdown if the menu is shown', () => {
fixtureEl.innerHTML = [ return new Promise(resolve => {
'<div class="dropdown">', fixtureEl.innerHTML = [
' <button class="btn dropdown-toggle" data-bs-toggle="dropdown">Dropdown</button>', '<div class="dropdown">',
' <div class="dropdown-menu show">', ' <button class="btn dropdown-toggle" data-bs-toggle="dropdown">Dropdown</button>',
' <a class="dropdown-item" href="#">Secondary link</a>', ' <div class="dropdown-menu show">',
' </div>', ' <a class="dropdown-item" href="#">Secondary link</a>',
'</div>' ' </div>',
].join('') '</div>'
].join('')
const btnDropdown = fixtureEl.querySelector('[data-bs-toggle="dropdown"]') const btnDropdown = fixtureEl.querySelector('[data-bs-toggle="dropdown"]')
const dropdown = new Dropdown(btnDropdown) const dropdown = new Dropdown(btnDropdown)
btnDropdown.addEventListener('shown.bs.dropdown', () => { btnDropdown.addEventListener('shown.bs.dropdown', () => {
throw new Error('should not throw shown.bs.dropdown event') throw new Error('should not throw shown.bs.dropdown event')
})
dropdown.show()
setTimeout(() => {
expect().nothing()
resolve()
}, 10)
}) })
dropdown.show()
setTimeout(() => {
expect().nothing()
done()
}, 10)
}) })
it('should not show a dropdown if show event is prevented', done => { it('should not show a dropdown if show event is prevented', () => {
fixtureEl.innerHTML = [ return new Promise(resolve => {
'<div class="dropdown">', fixtureEl.innerHTML = [
' <button class="btn dropdown-toggle" data-bs-toggle="dropdown">Dropdown</button>', '<div class="dropdown">',
' <div class="dropdown-menu">', ' <button class="btn dropdown-toggle" data-bs-toggle="dropdown">Dropdown</button>',
' <a class="dropdown-item" href="#">Secondary link</a>', ' <div class="dropdown-menu">',
' </div>', ' <a class="dropdown-item" href="#">Secondary link</a>',
'</div>' ' </div>',
].join('') '</div>'
].join('')
const btnDropdown = fixtureEl.querySelector('[data-bs-toggle="dropdown"]') const btnDropdown = fixtureEl.querySelector('[data-bs-toggle="dropdown"]')
const dropdown = new Dropdown(btnDropdown) const dropdown = new Dropdown(btnDropdown)
btnDropdown.addEventListener('show.bs.dropdown', event => { btnDropdown.addEventListener('show.bs.dropdown', event => {
event.preventDefault() event.preventDefault()
})
btnDropdown.addEventListener('shown.bs.dropdown', () => {
throw new Error('should not throw shown.bs.dropdown event')
})
dropdown.show()
setTimeout(() => {
expect().nothing()
resolve()
}, 10)
}) })
btnDropdown.addEventListener('shown.bs.dropdown', () => {
throw new Error('should not throw shown.bs.dropdown event')
})
dropdown.show()
setTimeout(() => {
expect().nothing()
done()
}, 10)
}) })
}) })
describe('hide', () => { describe('hide', () => {
it('should hide a dropdown', done => { it('should hide a dropdown', () => {
fixtureEl.innerHTML = [ return new Promise(resolve => {
'<div class="dropdown">', fixtureEl.innerHTML = [
' <button class="btn dropdown-toggle" data-bs-toggle="dropdown" aria-expanded="true">Dropdown</button>', '<div class="dropdown">',
' <div class="dropdown-menu show">', ' <button class="btn dropdown-toggle" data-bs-toggle="dropdown" aria-expanded="true">Dropdown</button>',
' <a class="dropdown-item" href="#">Secondary link</a>', ' <div class="dropdown-menu show">',
' </div>', ' <a class="dropdown-item" href="#">Secondary link</a>',
'</div>' ' </div>',
].join('') '</div>'
].join('')
const btnDropdown = fixtureEl.querySelector('[data-bs-toggle="dropdown"]') const btnDropdown = fixtureEl.querySelector('[data-bs-toggle="dropdown"]')
const dropdownMenu = fixtureEl.querySelector('.dropdown-menu') const dropdownMenu = fixtureEl.querySelector('.dropdown-menu')
const dropdown = new Dropdown(btnDropdown) const dropdown = new Dropdown(btnDropdown)
btnDropdown.addEventListener('hidden.bs.dropdown', () => { btnDropdown.addEventListener('hidden.bs.dropdown', () => {
expect(dropdownMenu).not.toHaveClass('show') expect(dropdownMenu).not.toHaveClass('show')
expect(btnDropdown.getAttribute('aria-expanded')).toEqual('false') expect(btnDropdown.getAttribute('aria-expanded')).toEqual('false')
done() resolve()
}) })
dropdown.hide()
})
it('should hide a dropdown and destroy popper', done => {
fixtureEl.innerHTML = [
'<div class="dropdown">',
' <button class="btn dropdown-toggle" data-bs-toggle="dropdown">Dropdown</button>',
' <div class="dropdown-menu">',
' <a class="dropdown-item" href="#">Secondary link</a>',
' </div>',
'</div>'
].join('')
const btnDropdown = fixtureEl.querySelector('[data-bs-toggle="dropdown"]')
const dropdown = new Dropdown(btnDropdown)
btnDropdown.addEventListener('shown.bs.dropdown', () => {
spyOn(dropdown._popper, 'destroy')
dropdown.hide() dropdown.hide()
}) })
btnDropdown.addEventListener('hidden.bs.dropdown', () => {
expect(dropdown._popper.destroy).toHaveBeenCalled()
done()
})
dropdown.show()
}) })
it('should not hide a dropdown if the element is disabled', done => { it('should hide a dropdown and destroy popper', () => {
fixtureEl.innerHTML = [ return new Promise(resolve => {
'<div class="dropdown">', fixtureEl.innerHTML = [
' <button disabled class="btn dropdown-toggle" data-bs-toggle="dropdown">Dropdown</button>', '<div class="dropdown">',
' <div class="dropdown-menu show">', ' <button class="btn dropdown-toggle" data-bs-toggle="dropdown">Dropdown</button>',
' <a class="dropdown-item" href="#">Secondary link</a>', ' <div class="dropdown-menu">',
' </div>', ' <a class="dropdown-item" href="#">Secondary link</a>',
'</div>' ' </div>',
].join('') '</div>'
].join('')
const btnDropdown = fixtureEl.querySelector('[data-bs-toggle="dropdown"]') const btnDropdown = fixtureEl.querySelector('[data-bs-toggle="dropdown"]')
const dropdownMenu = fixtureEl.querySelector('.dropdown-menu') const dropdown = new Dropdown(btnDropdown)
const dropdown = new Dropdown(btnDropdown)
btnDropdown.addEventListener('hidden.bs.dropdown', () => { btnDropdown.addEventListener('shown.bs.dropdown', () => {
throw new Error('should not throw hidden.bs.dropdown event') spyOn(dropdown._popper, 'destroy')
}) dropdown.hide()
})
dropdown.hide() btnDropdown.addEventListener('hidden.bs.dropdown', () => {
expect(dropdown._popper.destroy).toHaveBeenCalled()
resolve()
})
setTimeout(() => { dropdown.show()
expect(dropdownMenu).toHaveClass('show')
done()
}, 10)
})
it('should not hide a dropdown if the element contains .disabled', done => {
fixtureEl.innerHTML = [
'<div class="dropdown">',
' <button class="btn dropdown-toggle disabled" data-bs-toggle="dropdown">Dropdown</button>',
' <div class="dropdown-menu show">',
' <a class="dropdown-item" href="#">Secondary link</a>',
' </div>',
'</div>'
].join('')
const btnDropdown = fixtureEl.querySelector('[data-bs-toggle="dropdown"]')
const dropdownMenu = fixtureEl.querySelector('.dropdown-menu')
const dropdown = new Dropdown(btnDropdown)
btnDropdown.addEventListener('hidden.bs.dropdown', () => {
throw new Error('should not throw hidden.bs.dropdown event')
})
dropdown.hide()
setTimeout(() => {
expect(dropdownMenu).toHaveClass('show')
done()
}, 10)
})
it('should not hide a dropdown if the menu is not shown', done => {
fixtureEl.innerHTML = [
'<div class="dropdown">',
' <button class="btn dropdown-toggle" data-bs-toggle="dropdown">Dropdown</button>',
' <div class="dropdown-menu">',
' <a class="dropdown-item" href="#">Secondary link</a>',
' </div>',
'</div>'
].join('')
const btnDropdown = fixtureEl.querySelector('[data-bs-toggle="dropdown"]')
const dropdown = new Dropdown(btnDropdown)
btnDropdown.addEventListener('hidden.bs.dropdown', () => {
throw new Error('should not throw hidden.bs.dropdown event')
})
dropdown.hide()
setTimeout(() => {
expect().nothing()
done()
}, 10)
})
it('should not hide a dropdown if hide event is prevented', done => {
fixtureEl.innerHTML = [
'<div class="dropdown">',
' <button class="btn dropdown-toggle" data-bs-toggle="dropdown">Dropdown</button>',
' <div class="dropdown-menu show">',
' <a class="dropdown-item" href="#">Secondary link</a>',
' </div>',
'</div>'
].join('')
const btnDropdown = fixtureEl.querySelector('[data-bs-toggle="dropdown"]')
const dropdownMenu = fixtureEl.querySelector('.dropdown-menu')
const dropdown = new Dropdown(btnDropdown)
btnDropdown.addEventListener('hide.bs.dropdown', event => {
event.preventDefault()
})
btnDropdown.addEventListener('hidden.bs.dropdown', () => {
throw new Error('should not throw hidden.bs.dropdown event')
})
dropdown.hide()
setTimeout(() => {
expect(dropdownMenu).toHaveClass('show')
done()
}) })
}) })
it('should remove event listener on touch-enabled device that was added in show method', done => { it('should not hide a dropdown if the element is disabled', () => {
fixtureEl.innerHTML = [ return new Promise(resolve => {
'<div class="dropdown">', fixtureEl.innerHTML = [
' <button class="btn dropdown-toggle" data-bs-toggle="dropdown">Dropdown</button>', '<div class="dropdown">',
' <div class="dropdown-menu">', ' <button disabled class="btn dropdown-toggle" data-bs-toggle="dropdown">Dropdown</button>',
' <a class="dropdown-item" href="#">Dropdwon item</a>', ' <div class="dropdown-menu show">',
' </div>', ' <a class="dropdown-item" href="#">Secondary link</a>',
'</div>' ' </div>',
].join('') '</div>'
].join('')
const defaultValueOnTouchStart = document.documentElement.ontouchstart const btnDropdown = fixtureEl.querySelector('[data-bs-toggle="dropdown"]')
const btnDropdown = fixtureEl.querySelector('[data-bs-toggle="dropdown"]') const dropdownMenu = fixtureEl.querySelector('.dropdown-menu')
const dropdown = new Dropdown(btnDropdown) const dropdown = new Dropdown(btnDropdown)
document.documentElement.ontouchstart = noop btnDropdown.addEventListener('hidden.bs.dropdown', () => {
spyOn(EventHandler, 'off') throw new Error('should not throw hidden.bs.dropdown event')
})
btnDropdown.addEventListener('shown.bs.dropdown', () => {
dropdown.hide() dropdown.hide()
setTimeout(() => {
expect(dropdownMenu).toHaveClass('show')
resolve()
}, 10)
}) })
})
btnDropdown.addEventListener('hidden.bs.dropdown', () => { it('should not hide a dropdown if the element contains .disabled', () => {
expect(btnDropdown).not.toHaveClass('show') return new Promise(resolve => {
expect(btnDropdown.getAttribute('aria-expanded')).toEqual('false') fixtureEl.innerHTML = [
expect(EventHandler.off).toHaveBeenCalled() '<div class="dropdown">',
' <button class="btn dropdown-toggle disabled" data-bs-toggle="dropdown">Dropdown</button>',
' <div class="dropdown-menu show">',
' <a class="dropdown-item" href="#">Secondary link</a>',
' </div>',
'</div>'
].join('')
document.documentElement.ontouchstart = defaultValueOnTouchStart const btnDropdown = fixtureEl.querySelector('[data-bs-toggle="dropdown"]')
done() const dropdownMenu = fixtureEl.querySelector('.dropdown-menu')
const dropdown = new Dropdown(btnDropdown)
btnDropdown.addEventListener('hidden.bs.dropdown', () => {
throw new Error('should not throw hidden.bs.dropdown event')
})
dropdown.hide()
setTimeout(() => {
expect(dropdownMenu).toHaveClass('show')
resolve()
}, 10)
}) })
})
dropdown.show() it('should not hide a dropdown if the menu is not shown', () => {
return new Promise(resolve => {
fixtureEl.innerHTML = [
'<div class="dropdown">',
' <button class="btn dropdown-toggle" data-bs-toggle="dropdown">Dropdown</button>',
' <div class="dropdown-menu">',
' <a class="dropdown-item" href="#">Secondary link</a>',
' </div>',
'</div>'
].join('')
const btnDropdown = fixtureEl.querySelector('[data-bs-toggle="dropdown"]')
const dropdown = new Dropdown(btnDropdown)
btnDropdown.addEventListener('hidden.bs.dropdown', () => {
throw new Error('should not throw hidden.bs.dropdown event')
})
dropdown.hide()
setTimeout(() => {
expect().nothing()
resolve()
}, 10)
})
})
it('should not hide a dropdown if hide event is prevented', () => {
return new Promise(resolve => {
fixtureEl.innerHTML = [
'<div class="dropdown">',
' <button class="btn dropdown-toggle" data-bs-toggle="dropdown">Dropdown</button>',
' <div class="dropdown-menu show">',
' <a class="dropdown-item" href="#">Secondary link</a>',
' </div>',
'</div>'
].join('')
const btnDropdown = fixtureEl.querySelector('[data-bs-toggle="dropdown"]')
const dropdownMenu = fixtureEl.querySelector('.dropdown-menu')
const dropdown = new Dropdown(btnDropdown)
btnDropdown.addEventListener('hide.bs.dropdown', event => {
event.preventDefault()
})
btnDropdown.addEventListener('hidden.bs.dropdown', () => {
throw new Error('should not throw hidden.bs.dropdown event')
})
dropdown.hide()
setTimeout(() => {
expect(dropdownMenu).toHaveClass('show')
resolve()
})
})
})
it('should remove event listener on touch-enabled device that was added in show method', () => {
return new Promise(resolve => {
fixtureEl.innerHTML = [
'<div class="dropdown">',
' <button class="btn dropdown-toggle" data-bs-toggle="dropdown">Dropdown</button>',
' <div class="dropdown-menu">',
' <a class="dropdown-item" href="#">Dropdwon item</a>',
' </div>',
'</div>'
].join('')
const defaultValueOnTouchStart = document.documentElement.ontouchstart
const btnDropdown = fixtureEl.querySelector('[data-bs-toggle="dropdown"]')
const dropdown = new Dropdown(btnDropdown)
document.documentElement.ontouchstart = noop
spyOn(EventHandler, 'off')
btnDropdown.addEventListener('shown.bs.dropdown', () => {
dropdown.hide()
})
btnDropdown.addEventListener('hidden.bs.dropdown', () => {
expect(btnDropdown).not.toHaveClass('show')
expect(btnDropdown.getAttribute('aria-expanded')).toEqual('false')
expect(EventHandler.off).toHaveBeenCalled()
document.documentElement.ontouchstart = defaultValueOnTouchStart
resolve()
})
dropdown.show()
})
}) })
}) })
@ -1013,903 +1071,957 @@ describe('Dropdown', () => {
}) })
describe('data-api', () => { describe('data-api', () => {
it('should show and hide a dropdown', done => { it('should show and hide a dropdown', () => {
fixtureEl.innerHTML = [ return new Promise(resolve => {
'<div class="dropdown">', fixtureEl.innerHTML = [
' <button class="btn dropdown-toggle" data-bs-toggle="dropdown" aria-expanded="false">Dropdown</button>', '<div class="dropdown">',
' <div class="dropdown-menu">', ' <button class="btn dropdown-toggle" data-bs-toggle="dropdown" aria-expanded="false">Dropdown</button>',
' <a class="dropdown-item" href="#">Secondary link</a>', ' <div class="dropdown-menu">',
' </div>', ' <a class="dropdown-item" href="#">Secondary link</a>',
'</div>' ' </div>',
].join('') '</div>'
].join('')
const btnDropdown = fixtureEl.querySelector('[data-bs-toggle="dropdown"]') const btnDropdown = fixtureEl.querySelector('[data-bs-toggle="dropdown"]')
let showEventTriggered = false let showEventTriggered = false
let hideEventTriggered = false let hideEventTriggered = false
btnDropdown.addEventListener('show.bs.dropdown', () => { btnDropdown.addEventListener('show.bs.dropdown', () => {
showEventTriggered = true showEventTriggered = true
})
btnDropdown.addEventListener('shown.bs.dropdown', event => setTimeout(() => {
expect(btnDropdown).toHaveClass('show')
expect(btnDropdown.getAttribute('aria-expanded')).toEqual('true')
expect(showEventTriggered).toBeTrue()
expect(event.relatedTarget).toEqual(btnDropdown)
document.body.click()
}))
btnDropdown.addEventListener('hide.bs.dropdown', () => {
hideEventTriggered = true
})
btnDropdown.addEventListener('hidden.bs.dropdown', event => {
expect(btnDropdown).not.toHaveClass('show')
expect(btnDropdown.getAttribute('aria-expanded')).toEqual('false')
expect(hideEventTriggered).toBeTrue()
expect(event.relatedTarget).toEqual(btnDropdown)
done()
})
btnDropdown.click()
})
it('should not use "static" Popper in navbar', done => {
fixtureEl.innerHTML = [
'<nav class="navbar navbar-expand-md navbar-light bg-light">',
' <div class="dropdown">',
' <button class="btn dropdown-toggle" data-bs-toggle="dropdown" aria-expanded="false">Dropdown</button>',
' <div class="dropdown-menu">',
' <a class="dropdown-item" href="#">Secondary link</a>',
' </div>',
' </div>',
'</nav>'
].join('')
const btnDropdown = fixtureEl.querySelector('[data-bs-toggle="dropdown"]')
const dropdownMenu = fixtureEl.querySelector('.dropdown-menu')
const dropdown = new Dropdown(btnDropdown)
btnDropdown.addEventListener('shown.bs.dropdown', () => {
expect(dropdown._popper).not.toBeNull()
expect(dropdownMenu.getAttribute('data-bs-popper')).toEqual('static')
done()
})
dropdown.show()
})
it('should not collapse the dropdown when clicking a select option nested in the dropdown', done => {
fixtureEl.innerHTML = [
'<div class="dropdown">',
' <button class="btn dropdown-toggle" data-bs-toggle="dropdown" aria-expanded="false">Dropdown</button>',
' <div class="dropdown-menu">',
' <select>',
' <option selected>Open this select menu</option>',
' <option value="1">One</option>',
' </select>',
' </div>',
'</div>'
].join('')
const btnDropdown = fixtureEl.querySelector('[data-bs-toggle="dropdown"]')
const dropdownMenu = fixtureEl.querySelector('.dropdown-menu')
const dropdown = new Dropdown(btnDropdown)
const hideSpy = spyOn(dropdown, '_completeHide')
btnDropdown.addEventListener('shown.bs.dropdown', () => {
const clickEvent = new MouseEvent('click', {
bubbles: true
}) })
dropdownMenu.querySelector('option').dispatchEvent(clickEvent) btnDropdown.addEventListener('shown.bs.dropdown', event => setTimeout(() => {
}) expect(btnDropdown).toHaveClass('show')
expect(btnDropdown.getAttribute('aria-expanded')).toEqual('true')
dropdownMenu.addEventListener('click', event => { expect(showEventTriggered).toBeTrue()
expect(event.target.tagName).toMatch(/select|option/i) expect(event.relatedTarget).toEqual(btnDropdown)
document.body.click()
Dropdown.clearMenus(event)
setTimeout(() => {
expect(hideSpy).not.toHaveBeenCalled()
done()
}, 10)
})
dropdown.show()
})
it('should manage bs attribute `data-bs-popper`="static" when dropdown is in navbar', done => {
fixtureEl.innerHTML = [
'<nav class="navbar navbar-expand-md navbar-light bg-light">',
' <div class="dropdown">',
' <button class="btn dropdown-toggle" data-bs-toggle="dropdown" aria-expanded="false">Dropdown</button>',
' <div class="dropdown-menu">',
' <a class="dropdown-item" href="#">Secondary link</a>',
' </div>',
' </div>',
'</nav>'
].join('')
const btnDropdown = fixtureEl.querySelector('[data-bs-toggle="dropdown"]')
const dropdownMenu = fixtureEl.querySelector('.dropdown-menu')
const dropdown = new Dropdown(btnDropdown)
btnDropdown.addEventListener('shown.bs.dropdown', () => {
expect(dropdownMenu.getAttribute('data-bs-popper')).toEqual('static')
dropdown.hide()
})
btnDropdown.addEventListener('hidden.bs.dropdown', () => {
expect(dropdownMenu.getAttribute('data-bs-popper')).toBeNull()
done()
})
dropdown.show()
})
it('should not use Popper if display set to static', done => {
fixtureEl.innerHTML = [
'<div class="dropdown">',
' <button class="btn dropdown-toggle" data-bs-toggle="dropdown" data-bs-display="static">Dropdown</button>',
' <div class="dropdown-menu">',
' <a class="dropdown-item" href="#">Secondary link</a>',
' </div>',
'</div>'
].join('')
const btnDropdown = fixtureEl.querySelector('[data-bs-toggle="dropdown"]')
const dropdownMenu = fixtureEl.querySelector('.dropdown-menu')
btnDropdown.addEventListener('shown.bs.dropdown', () => {
// Popper adds this attribute when we use it
expect(dropdownMenu.getAttribute('data-popper-placement')).toBeNull()
done()
})
btnDropdown.click()
})
it('should manage bs attribute `data-bs-popper`="static" when display set to static', done => {
fixtureEl.innerHTML = [
'<div class="dropdown">',
' <button class="btn dropdown-toggle" data-bs-toggle="dropdown" data-bs-display="static">Dropdown</button>',
' <div class="dropdown-menu">',
' <a class="dropdown-item" href="#">Secondary link</a>',
' </div>',
'</div>'
].join('')
const btnDropdown = fixtureEl.querySelector('[data-bs-toggle="dropdown"]')
const dropdownMenu = fixtureEl.querySelector('.dropdown-menu')
const dropdown = new Dropdown(btnDropdown)
btnDropdown.addEventListener('shown.bs.dropdown', () => {
expect(dropdownMenu.getAttribute('data-bs-popper')).toEqual('static')
dropdown.hide()
})
btnDropdown.addEventListener('hidden.bs.dropdown', () => {
expect(dropdownMenu.getAttribute('data-bs-popper')).toBeNull()
done()
})
dropdown.show()
})
it('should remove "show" class if tabbing outside of menu', done => {
fixtureEl.innerHTML = [
'<div class="dropdown">',
' <button class="btn dropdown-toggle" data-bs-toggle="dropdown">Dropdown</button>',
' <div class="dropdown-menu">',
' <a class="dropdown-item" href="#">Secondary link</a>',
' </div>',
'</div>'
].join('')
const btnDropdown = fixtureEl.querySelector('[data-bs-toggle="dropdown"]')
btnDropdown.addEventListener('shown.bs.dropdown', () => {
expect(btnDropdown).toHaveClass('show')
const keyup = createEvent('keyup')
keyup.key = 'Tab'
document.dispatchEvent(keyup)
})
btnDropdown.addEventListener('hidden.bs.dropdown', () => {
expect(btnDropdown).not.toHaveClass('show')
done()
})
btnDropdown.click()
})
it('should remove "show" class if body is clicked, with multiple dropdowns', done => {
fixtureEl.innerHTML = [
'<div class="nav">',
' <div class="dropdown" id="testmenu">',
' <a class="dropdown-toggle" data-bs-toggle="dropdown" href="#testmenu">Test menu</a>',
' <div class="dropdown-menu">',
' <a class="dropdown-item" href="#sub1">Submenu 1</a>',
' </div>',
' </div>',
'</div>',
'<div class="btn-group">',
' <button class="btn">Actions</button>',
' <button class="btn dropdown-toggle" data-bs-toggle="dropdown"></button>',
' <div class="dropdown-menu">',
' <a class="dropdown-item" href="#">Action 1</a>',
' </div>',
'</div>'
].join('')
const triggerDropdownList = fixtureEl.querySelectorAll('[data-bs-toggle="dropdown"]')
expect(triggerDropdownList).toHaveSize(2)
const [triggerDropdownFirst, triggerDropdownLast] = triggerDropdownList
triggerDropdownFirst.addEventListener('shown.bs.dropdown', () => {
expect(triggerDropdownFirst).toHaveClass('show')
expect(fixtureEl.querySelectorAll('.dropdown-menu.show')).toHaveSize(1)
document.body.click()
})
triggerDropdownFirst.addEventListener('hidden.bs.dropdown', () => {
expect(fixtureEl.querySelectorAll('.dropdown-menu.show')).toHaveSize(0)
triggerDropdownLast.click()
})
triggerDropdownLast.addEventListener('shown.bs.dropdown', () => {
expect(triggerDropdownLast).toHaveClass('show')
expect(fixtureEl.querySelectorAll('.dropdown-menu.show')).toHaveSize(1)
document.body.click()
})
triggerDropdownLast.addEventListener('hidden.bs.dropdown', () => {
expect(fixtureEl.querySelectorAll('.dropdown-menu.show')).toHaveSize(0)
done()
})
triggerDropdownFirst.click()
})
it('should remove "show" class if body if tabbing outside of menu, with multiple dropdowns', done => {
fixtureEl.innerHTML = [
'<div class="dropdown">',
' <a class="dropdown-toggle" data-bs-toggle="dropdown" href="#testmenu">Test menu</a>',
' <div class="dropdown-menu">',
' <a class="dropdown-item" href="#sub1">Submenu 1</a>',
' </div>',
'</div>',
'<div class="btn-group">',
' <button class="btn">Actions</button>',
' <button class="btn dropdown-toggle" data-bs-toggle="dropdown"></button>',
' <div class="dropdown-menu">',
' <a class="dropdown-item" href="#">Action 1</a>',
' </div>',
'</div>'
].join('')
const triggerDropdownList = fixtureEl.querySelectorAll('[data-bs-toggle="dropdown"]')
expect(triggerDropdownList).toHaveSize(2)
const [triggerDropdownFirst, triggerDropdownLast] = triggerDropdownList
triggerDropdownFirst.addEventListener('shown.bs.dropdown', () => {
expect(triggerDropdownFirst).toHaveClass('show')
expect(fixtureEl.querySelectorAll('.dropdown-menu.show')).toHaveSize(1)
const keyup = createEvent('keyup')
keyup.key = 'Tab'
document.dispatchEvent(keyup)
})
triggerDropdownFirst.addEventListener('hidden.bs.dropdown', () => {
expect(fixtureEl.querySelectorAll('.dropdown-menu.show')).toHaveSize(0)
triggerDropdownLast.click()
})
triggerDropdownLast.addEventListener('shown.bs.dropdown', () => {
expect(triggerDropdownLast).toHaveClass('show')
expect(fixtureEl.querySelectorAll('.dropdown-menu.show')).toHaveSize(1)
const keyup = createEvent('keyup')
keyup.key = 'Tab'
document.dispatchEvent(keyup)
})
triggerDropdownLast.addEventListener('hidden.bs.dropdown', () => {
expect(fixtureEl.querySelectorAll('.dropdown-menu.show')).toHaveSize(0)
done()
})
triggerDropdownFirst.click()
})
it('should fire hide and hidden event without a clickEvent if event type is not click', done => {
fixtureEl.innerHTML = [
'<div class="dropdown">',
' <button class="btn dropdown-toggle" data-bs-toggle="dropdown">Dropdown</button>',
' <div class="dropdown-menu">',
' <a class="dropdown-item" href="#sub1">Submenu 1</a>',
' </div>',
'</div>'
].join('')
const triggerDropdown = fixtureEl.querySelector('[data-bs-toggle="dropdown"]')
triggerDropdown.addEventListener('hide.bs.dropdown', event => {
expect(event.clickEvent).toBeUndefined()
})
triggerDropdown.addEventListener('hidden.bs.dropdown', event => {
expect(event.clickEvent).toBeUndefined()
done()
})
triggerDropdown.addEventListener('shown.bs.dropdown', () => {
const keydown = createEvent('keydown')
keydown.key = 'Escape'
triggerDropdown.dispatchEvent(keydown)
})
triggerDropdown.click()
})
it('should bubble up the events to the parent elements', done => {
fixtureEl.innerHTML = [
'<div class="dropdown">',
' <button class="btn dropdown-toggle" data-bs-toggle="dropdown">Dropdown</button>',
' <div class="dropdown-menu">',
' <a class="dropdown-item" href="#subMenu">Sub menu</a>',
' </div>',
'</div>'
].join('')
const triggerDropdown = fixtureEl.querySelector('[data-bs-toggle="dropdown"]')
const dropdownParent = fixtureEl.querySelector('.dropdown')
const dropdown = new Dropdown(triggerDropdown)
const showFunction = jasmine.createSpy('showFunction')
dropdownParent.addEventListener('show.bs.dropdown', showFunction)
const shownFunction = jasmine.createSpy('shownFunction')
dropdownParent.addEventListener('shown.bs.dropdown', () => {
shownFunction()
dropdown.hide()
})
const hideFunction = jasmine.createSpy('hideFunction')
dropdownParent.addEventListener('hide.bs.dropdown', hideFunction)
dropdownParent.addEventListener('hidden.bs.dropdown', () => {
expect(showFunction).toHaveBeenCalled()
expect(shownFunction).toHaveBeenCalled()
expect(hideFunction).toHaveBeenCalled()
done()
})
dropdown.show()
})
it('should ignore keyboard events within <input>s and <textarea>s', done => {
fixtureEl.innerHTML = [
'<div class="dropdown">',
' <button class="btn dropdown-toggle" data-bs-toggle="dropdown">Dropdown</button>',
' <div class="dropdown-menu">',
' <a class="dropdown-item" href="#sub1">Submenu 1</a>',
' <input type="text">',
' <textarea></textarea>',
' </div>',
'</div>'
].join('')
const triggerDropdown = fixtureEl.querySelector('[data-bs-toggle="dropdown"]')
const input = fixtureEl.querySelector('input')
const textarea = fixtureEl.querySelector('textarea')
triggerDropdown.addEventListener('shown.bs.dropdown', () => {
input.focus()
const keydown = createEvent('keydown')
keydown.key = 'ArrowUp'
input.dispatchEvent(keydown)
expect(document.activeElement).toEqual(input, 'input still focused')
textarea.focus()
textarea.dispatchEvent(keydown)
expect(document.activeElement).toEqual(textarea, 'textarea still focused')
done()
})
triggerDropdown.click()
})
it('should skip disabled element when using keyboard navigation', done => {
fixtureEl.innerHTML = [
'<div class="dropdown">',
' <button class="btn dropdown-toggle" data-bs-toggle="dropdown">Dropdown</button>',
' <div class="dropdown-menu">',
' <a class="dropdown-item disabled" href="#sub1">Submenu 1</a>',
' <button class="dropdown-item" type="button" disabled>Disabled button</button>',
' <a id="item1" class="dropdown-item" href="#">Another link</a>',
' </div>',
'</div>'
].join('')
const triggerDropdown = fixtureEl.querySelector('[data-bs-toggle="dropdown"]')
triggerDropdown.addEventListener('shown.bs.dropdown', () => {
const keydown = createEvent('keydown')
keydown.key = 'ArrowDown'
triggerDropdown.dispatchEvent(keydown)
triggerDropdown.dispatchEvent(keydown)
expect(document.activeElement).not.toHaveClass('disabled')
expect(document.activeElement.hasAttribute('disabled')).toBeFalse()
done()
})
triggerDropdown.click()
})
it('should skip hidden element when using keyboard navigation', done => {
fixtureEl.innerHTML = [
'<style>',
' .d-none {',
' display: none;',
' }',
'</style>',
'<div class="dropdown">',
' <button class="btn dropdown-toggle" data-bs-toggle="dropdown">Dropdown</button>',
' <div class="dropdown-menu">',
' <button class="dropdown-item d-none" type="button">Hidden button by class</button>',
' <a class="dropdown-item" href="#sub1" style="display: none">Hidden link</a>',
' <a class="dropdown-item" href="#sub1" style="visibility: hidden">Hidden link</a>',
' <a id="item1" class="dropdown-item" href="#">Another link</a>',
' </div>',
'</div>'
].join('')
const triggerDropdown = fixtureEl.querySelector('[data-bs-toggle="dropdown"]')
triggerDropdown.addEventListener('shown.bs.dropdown', () => {
const keydown = createEvent('keydown')
keydown.key = 'ArrowDown'
triggerDropdown.dispatchEvent(keydown)
expect(document.activeElement).not.toHaveClass('d-none')
expect(document.activeElement.style.display).not.toEqual('none')
expect(document.activeElement.style.visibility).not.toEqual('hidden')
done()
})
triggerDropdown.click()
})
it('should focus next/previous element when using keyboard navigation', done => {
fixtureEl.innerHTML = [
'<div class="dropdown">',
' <button class="btn dropdown-toggle" data-bs-toggle="dropdown">Dropdown</button>',
' <div class="dropdown-menu">',
' <a id="item1" class="dropdown-item" href="#">A link</a>',
' <a id="item2" class="dropdown-item" href="#">Another link</a>',
' </div>',
'</div>'
].join('')
const triggerDropdown = fixtureEl.querySelector('[data-bs-toggle="dropdown"]')
const item1 = fixtureEl.querySelector('#item1')
const item2 = fixtureEl.querySelector('#item2')
triggerDropdown.addEventListener('shown.bs.dropdown', () => {
const keydownArrowDown = createEvent('keydown')
keydownArrowDown.key = 'ArrowDown'
triggerDropdown.dispatchEvent(keydownArrowDown)
expect(document.activeElement).toEqual(item1, 'item1 is focused')
document.activeElement.dispatchEvent(keydownArrowDown)
expect(document.activeElement).toEqual(item2, 'item2 is focused')
const keydownArrowUp = createEvent('keydown')
keydownArrowUp.key = 'ArrowUp'
document.activeElement.dispatchEvent(keydownArrowUp)
expect(document.activeElement).toEqual(item1, 'item1 is focused')
done()
})
triggerDropdown.click()
})
it('should open the dropdown and focus on the last item when using ArrowUp for the first time', done => {
fixtureEl.innerHTML = [
'<div class="dropdown">',
' <button class="btn dropdown-toggle" data-bs-toggle="dropdown">Dropdown</button>',
' <div class="dropdown-menu">',
' <a id="item1" class="dropdown-item" href="#">A link</a>',
' <a id="item2" class="dropdown-item" href="#">Another link</a>',
' </div>',
'</div>'
].join('')
const triggerDropdown = fixtureEl.querySelector('[data-bs-toggle="dropdown"]')
const lastItem = fixtureEl.querySelector('#item2')
triggerDropdown.addEventListener('shown.bs.dropdown', () => {
setTimeout(() => {
expect(document.activeElement).toEqual(lastItem, 'item2 is focused')
done()
})
})
const keydown = createEvent('keydown')
keydown.key = 'ArrowUp'
triggerDropdown.dispatchEvent(keydown)
})
it('should open the dropdown and focus on the first item when using ArrowDown for the first time', done => {
fixtureEl.innerHTML = [
'<div class="dropdown">',
' <button class="btn dropdown-toggle" data-bs-toggle="dropdown">Dropdown</button>',
' <div class="dropdown-menu">',
' <a id="item1" class="dropdown-item" href="#">A link</a>',
' <a id="item2" class="dropdown-item" href="#">Another link</a>',
' </div>',
'</div>'
].join('')
const triggerDropdown = fixtureEl.querySelector('[data-bs-toggle="dropdown"]')
const firstItem = fixtureEl.querySelector('#item1')
triggerDropdown.addEventListener('shown.bs.dropdown', () => {
setTimeout(() => {
expect(document.activeElement).toEqual(firstItem, 'item1 is focused')
done()
})
})
const keydown = createEvent('keydown')
keydown.key = 'ArrowDown'
triggerDropdown.dispatchEvent(keydown)
})
it('should not close the dropdown if the user clicks on a text field within dropdown-menu', done => {
fixtureEl.innerHTML = [
'<div class="dropdown">',
' <button class="btn dropdown-toggle" data-bs-toggle="dropdown">Dropdown</button>',
' <div class="dropdown-menu">',
' <input type="text">',
' </div>',
'</div>'
].join('')
const triggerDropdown = fixtureEl.querySelector('[data-bs-toggle="dropdown"]')
const input = fixtureEl.querySelector('input')
input.addEventListener('click', () => {
expect(triggerDropdown).toHaveClass('show')
done()
})
triggerDropdown.addEventListener('shown.bs.dropdown', () => {
expect(triggerDropdown).toHaveClass('show')
input.dispatchEvent(createEvent('click'))
})
triggerDropdown.click()
})
it('should not close the dropdown if the user clicks on a textarea within dropdown-menu', done => {
fixtureEl.innerHTML = [
'<div class="dropdown">',
' <button class="btn dropdown-toggle" data-bs-toggle="dropdown">Dropdown</button>',
' <div class="dropdown-menu">',
' <textarea></textarea>',
' </div>',
'</div>'
].join('')
const triggerDropdown = fixtureEl.querySelector('[data-bs-toggle="dropdown"]')
const textarea = fixtureEl.querySelector('textarea')
textarea.addEventListener('click', () => {
expect(triggerDropdown).toHaveClass('show')
done()
})
triggerDropdown.addEventListener('shown.bs.dropdown', () => {
expect(triggerDropdown).toHaveClass('show')
textarea.dispatchEvent(createEvent('click'))
})
triggerDropdown.click()
})
it('should close the dropdown if the user clicks on a text field that is not contained within dropdown-menu', done => {
fixtureEl.innerHTML = [
'<div class="dropdown">',
' <button class="btn dropdown-toggle" data-bs-toggle="dropdown">Dropdown</button>',
' <div class="dropdown-menu">',
' </div>',
'</div>',
'<input type="text">'
].join('')
const triggerDropdown = fixtureEl.querySelector('[data-bs-toggle="dropdown"]')
const input = fixtureEl.querySelector('input')
triggerDropdown.addEventListener('hidden.bs.dropdown', () => {
expect().nothing()
done()
})
triggerDropdown.addEventListener('shown.bs.dropdown', () => {
input.dispatchEvent(createEvent('click', {
bubbles: true
})) }))
})
triggerDropdown.click() btnDropdown.addEventListener('hide.bs.dropdown', () => {
hideEventTriggered = true
})
btnDropdown.addEventListener('hidden.bs.dropdown', event => {
expect(btnDropdown).not.toHaveClass('show')
expect(btnDropdown.getAttribute('aria-expanded')).toEqual('false')
expect(hideEventTriggered).toBeTrue()
expect(event.relatedTarget).toEqual(btnDropdown)
resolve()
})
btnDropdown.click()
})
}) })
it('should ignore keyboard events for <input>s and <textarea>s within dropdown-menu, except for escape key', done => { it('should not use "static" Popper in navbar', () => {
fixtureEl.innerHTML = [ return new Promise(resolve => {
'<div class="dropdown">', fixtureEl.innerHTML = [
' <button class="btn dropdown-toggle" data-bs-toggle="dropdown">Dropdown</button>', '<nav class="navbar navbar-expand-md navbar-light bg-light">',
' <div class="dropdown-menu">', ' <div class="dropdown">',
' <a class="dropdown-item" href="#sub1">Submenu 1</a>', ' <button class="btn dropdown-toggle" data-bs-toggle="dropdown" aria-expanded="false">Dropdown</button>',
' <input type="text">', ' <div class="dropdown-menu">',
' <textarea></textarea>', ' <a class="dropdown-item" href="#">Secondary link</a>',
' </div>', ' </div>',
'</div>' ' </div>',
].join('') '</nav>'
].join('')
const triggerDropdown = fixtureEl.querySelector('[data-bs-toggle="dropdown"]') const btnDropdown = fixtureEl.querySelector('[data-bs-toggle="dropdown"]')
const input = fixtureEl.querySelector('input') const dropdownMenu = fixtureEl.querySelector('.dropdown-menu')
const textarea = fixtureEl.querySelector('textarea') const dropdown = new Dropdown(btnDropdown)
const test = (eventKey, elementToDispatch) => { btnDropdown.addEventListener('shown.bs.dropdown', () => {
const event = createEvent('keydown') expect(dropdown._popper).not.toBeNull()
event.key = eventKey expect(dropdownMenu.getAttribute('data-bs-popper')).toEqual('static')
elementToDispatch.focus() resolve()
elementToDispatch.dispatchEvent(event) })
expect(document.activeElement).toEqual(elementToDispatch, `${elementToDispatch.tagName} still focused`)
}
const keydownEscape = createEvent('keydown') dropdown.show()
keydownEscape.key = 'Escape'
triggerDropdown.addEventListener('shown.bs.dropdown', () => {
// Key Space
test('Space', input)
test('Space', textarea)
// Key ArrowUp
test('ArrowUp', input)
test('ArrowUp', textarea)
// Key ArrowDown
test('ArrowDown', input)
test('ArrowDown', textarea)
// Key Escape
input.focus()
input.dispatchEvent(keydownEscape)
expect(triggerDropdown).not.toHaveClass('show')
done()
}) })
triggerDropdown.click()
}) })
it('should not open dropdown if escape key was pressed on the toggle', done => { it('should not collapse the dropdown when clicking a select option nested in the dropdown', () => {
fixtureEl.innerHTML = [ return new Promise(resolve => {
'<div class="tabs">', fixtureEl.innerHTML = [
' <div class="dropdown">', '<div class="dropdown">',
' <button disabled class="btn dropdown-toggle" data-bs-toggle="dropdown">Dropdown</button>', ' <button class="btn dropdown-toggle" data-bs-toggle="dropdown" aria-expanded="false">Dropdown</button>',
' <div class="dropdown-menu">', ' <div class="dropdown-menu">',
' <a class="dropdown-item" href="#">Secondary link</a>', ' <select>',
' <a class="dropdown-item" href="#">Something else here</a>', ' <option selected>Open this select menu</option>',
' <div class="divider"></div>', ' <option value="1">One</option>',
' <a class="dropdown-item" href="#">Another link</a>', ' </select>',
' </div>', ' </div>',
' </div>', '</div>'
'</div>' ].join('')
].join('')
const triggerDropdown = fixtureEl.querySelector('[data-bs-toggle="dropdown"]') const btnDropdown = fixtureEl.querySelector('[data-bs-toggle="dropdown"]')
const dropdown = new Dropdown(triggerDropdown) const dropdownMenu = fixtureEl.querySelector('.dropdown-menu')
const button = fixtureEl.querySelector('button[data-bs-toggle="dropdown"]') const dropdown = new Dropdown(btnDropdown)
spyOn(dropdown, 'toggle') const hideSpy = spyOn(dropdown, '_completeHide')
// Key escape btnDropdown.addEventListener('shown.bs.dropdown', () => {
button.focus() const clickEvent = new MouseEvent('click', {
// Key escape bubbles: true
const keydownEscape = createEvent('keydown') })
keydownEscape.key = 'Escape'
button.dispatchEvent(keydownEscape)
setTimeout(() => { dropdownMenu.querySelector('option').dispatchEvent(clickEvent)
expect(dropdown.toggle).not.toHaveBeenCalled() })
expect(triggerDropdown).not.toHaveClass('show')
done() dropdownMenu.addEventListener('click', event => {
}, 20) expect(event.target.tagName).toMatch(/select|option/i)
Dropdown.clearMenus(event)
setTimeout(() => {
expect(hideSpy).not.toHaveBeenCalled()
resolve()
}, 10)
})
dropdown.show()
})
}) })
it('should propagate escape key events if dropdown is closed', done => { it('should manage bs attribute `data-bs-popper`="static" when dropdown is in navbar', () => {
fixtureEl.innerHTML = [ return new Promise(resolve => {
'<div class="parent">', fixtureEl.innerHTML = [
' <div class="dropdown">', '<nav class="navbar navbar-expand-md navbar-light bg-light">',
' <button class="btn dropdown-toggle" data-bs-toggle="dropdown">Dropdown</button>', ' <div class="dropdown">',
' <div class="dropdown-menu">', ' <button class="btn dropdown-toggle" data-bs-toggle="dropdown" aria-expanded="false">Dropdown</button>',
' <a class="dropdown-item" href="#">Some Item</a>', ' <div class="dropdown-menu">',
' </div>', ' <a class="dropdown-item" href="#">Secondary link</a>',
' </div>', ' </div>',
'</div>' ' </div>',
].join('') '</nav>'
].join('')
const parent = fixtureEl.querySelector('.parent') const btnDropdown = fixtureEl.querySelector('[data-bs-toggle="dropdown"]')
const toggle = fixtureEl.querySelector('[data-bs-toggle="dropdown"]') const dropdownMenu = fixtureEl.querySelector('.dropdown-menu')
const dropdown = new Dropdown(btnDropdown)
const parentKeyHandler = jasmine.createSpy('parentKeyHandler') btnDropdown.addEventListener('shown.bs.dropdown', () => {
expect(dropdownMenu.getAttribute('data-bs-popper')).toEqual('static')
dropdown.hide()
})
parent.addEventListener('keydown', parentKeyHandler) btnDropdown.addEventListener('hidden.bs.dropdown', () => {
parent.addEventListener('keyup', () => { expect(dropdownMenu.getAttribute('data-bs-popper')).toBeNull()
expect(parentKeyHandler).toHaveBeenCalled() resolve()
done() })
dropdown.show()
}) })
const keydownEscape = createEvent('keydown', { bubbles: true })
keydownEscape.key = 'Escape'
const keyupEscape = createEvent('keyup', { bubbles: true })
keyupEscape.key = 'Escape'
toggle.focus()
toggle.dispatchEvent(keydownEscape)
toggle.dispatchEvent(keyupEscape)
}) })
it('should close dropdown using `escape` button, and return focus to its trigger', done => { it('should not use Popper if display set to static', () => {
fixtureEl.innerHTML = [ return new Promise(resolve => {
'<div class="dropdown">', fixtureEl.innerHTML = [
' <button class="btn dropdown-toggle" data-bs-toggle="dropdown">Dropdown</button>', '<div class="dropdown">',
' <div class="dropdown-menu">', ' <button class="btn dropdown-toggle" data-bs-toggle="dropdown" data-bs-display="static">Dropdown</button>',
' <a class="dropdown-item" href="#">Some Item</a>', ' <div class="dropdown-menu">',
' </div>', ' <a class="dropdown-item" href="#">Secondary link</a>',
'</div>' ' </div>',
].join('') '</div>'
].join('')
const toggle = fixtureEl.querySelector('[data-bs-toggle="dropdown"]') const btnDropdown = fixtureEl.querySelector('[data-bs-toggle="dropdown"]')
const dropdownMenu = fixtureEl.querySelector('.dropdown-menu')
toggle.addEventListener('shown.bs.dropdown', () => { btnDropdown.addEventListener('shown.bs.dropdown', () => {
const keydownEvent = createEvent('keydown', { bubbles: true }) // Popper adds this attribute when we use it
keydownEvent.key = 'ArrowDown' expect(dropdownMenu.getAttribute('data-popper-placement')).toBeNull()
toggle.dispatchEvent(keydownEvent) resolve()
keydownEvent.key = 'Escape' })
toggle.dispatchEvent(keydownEvent)
btnDropdown.click()
}) })
toggle.addEventListener('hidden.bs.dropdown', () => setTimeout(() => {
expect(document.activeElement).toEqual(toggle)
done()
}))
toggle.click()
}) })
it('should close dropdown (only) by clicking inside the dropdown menu when it has data-attribute `data-bs-auto-close="inside"`', done => { it('should manage bs attribute `data-bs-popper`="static" when display set to static', () => {
fixtureEl.innerHTML = [ return new Promise(resolve => {
'<div class="dropdown">', fixtureEl.innerHTML = [
' <button class="btn dropdown-toggle" data-bs-toggle="dropdown" data-bs-auto-close="inside">Dropdown toggle</button>', '<div class="dropdown">',
' <div class="dropdown-menu">', ' <button class="btn dropdown-toggle" data-bs-toggle="dropdown" data-bs-display="static">Dropdown</button>',
' <a class="dropdown-item" href="#">Dropdown item</a>', ' <div class="dropdown-menu">',
' </div>', ' <a class="dropdown-item" href="#">Secondary link</a>',
'</div>' ' </div>',
].join('') '</div>'
].join('')
const dropdownToggle = fixtureEl.querySelector('[data-bs-toggle="dropdown"]') const btnDropdown = fixtureEl.querySelector('[data-bs-toggle="dropdown"]')
const dropdownMenu = fixtureEl.querySelector('.dropdown-menu') const dropdownMenu = fixtureEl.querySelector('.dropdown-menu')
const dropdown = new Dropdown(btnDropdown)
const expectDropdownToBeOpened = () => setTimeout(() => { btnDropdown.addEventListener('shown.bs.dropdown', () => {
expect(dropdownToggle).toHaveClass('show') expect(dropdownMenu.getAttribute('data-bs-popper')).toEqual('static')
dropdownMenu.click() dropdown.hide()
}, 150) })
dropdownToggle.addEventListener('shown.bs.dropdown', () => { btnDropdown.addEventListener('hidden.bs.dropdown', () => {
document.documentElement.click() expect(dropdownMenu.getAttribute('data-bs-popper')).toBeNull()
expectDropdownToBeOpened() resolve()
})
dropdown.show()
}) })
dropdownToggle.addEventListener('hidden.bs.dropdown', () => setTimeout(() => {
expect(dropdownToggle).not.toHaveClass('show')
done()
}))
dropdownToggle.click()
}) })
it('should close dropdown (only) by clicking outside the dropdown menu when it has data-attribute `data-bs-auto-close="outside"`', done => { it('should remove "show" class if tabbing outside of menu', () => {
fixtureEl.innerHTML = [ return new Promise(resolve => {
'<div class="dropdown">', fixtureEl.innerHTML = [
' <button class="btn dropdown-toggle" data-bs-toggle="dropdown" data-bs-auto-close="outside">Dropdown toggle</button>', '<div class="dropdown">',
' <div class="dropdown-menu">', ' <button class="btn dropdown-toggle" data-bs-toggle="dropdown">Dropdown</button>',
' <a class="dropdown-item" href="#">Dropdown item</a>', ' <div class="dropdown-menu">',
' </div>', ' <a class="dropdown-item" href="#">Secondary link</a>',
'</div>' ' </div>',
].join('') '</div>'
].join('')
const dropdownToggle = fixtureEl.querySelector('[data-bs-toggle="dropdown"]') const btnDropdown = fixtureEl.querySelector('[data-bs-toggle="dropdown"]')
const dropdownMenu = fixtureEl.querySelector('.dropdown-menu')
const expectDropdownToBeOpened = () => setTimeout(() => { btnDropdown.addEventListener('shown.bs.dropdown', () => {
expect(dropdownToggle).toHaveClass('show') expect(btnDropdown).toHaveClass('show')
document.documentElement.click()
}, 150)
dropdownToggle.addEventListener('shown.bs.dropdown', () => { const keyup = createEvent('keyup')
dropdownMenu.click()
expectDropdownToBeOpened() keyup.key = 'Tab'
document.dispatchEvent(keyup)
})
btnDropdown.addEventListener('hidden.bs.dropdown', () => {
expect(btnDropdown).not.toHaveClass('show')
resolve()
})
btnDropdown.click()
}) })
dropdownToggle.addEventListener('hidden.bs.dropdown', () => {
expect(dropdownToggle).not.toHaveClass('show')
done()
})
dropdownToggle.click()
}) })
it('should not close dropdown by clicking inside or outside the dropdown menu when it has data-attribute `data-bs-auto-close="false"`', done => { it('should remove "show" class if body is clicked, with multiple dropdowns', () => {
fixtureEl.innerHTML = [ return new Promise(resolve => {
'<div class="dropdown">', fixtureEl.innerHTML = [
' <button class="btn dropdown-toggle" data-bs-toggle="dropdown" data-bs-auto-close="false">Dropdown toggle</button>', '<div class="nav">',
' <div class="dropdown-menu">', ' <div class="dropdown" id="testmenu">',
' <a class="dropdown-item" href="#">Dropdown item</a>', ' <a class="dropdown-toggle" data-bs-toggle="dropdown" href="#testmenu">Test menu</a>',
' </div>', ' <div class="dropdown-menu">',
'</div>' ' <a class="dropdown-item" href="#sub1">Submenu 1</a>',
].join('') ' </div>',
' </div>',
'</div>',
'<div class="btn-group">',
' <button class="btn">Actions</button>',
' <button class="btn dropdown-toggle" data-bs-toggle="dropdown"></button>',
' <div class="dropdown-menu">',
' <a class="dropdown-item" href="#">Action 1</a>',
' </div>',
'</div>'
].join('')
const dropdownToggle = fixtureEl.querySelector('[data-bs-toggle="dropdown"]') const triggerDropdownList = fixtureEl.querySelectorAll('[data-bs-toggle="dropdown"]')
const dropdownMenu = fixtureEl.querySelector('.dropdown-menu')
const expectDropdownToBeOpened = (shouldTriggerClick = true) => setTimeout(() => { expect(triggerDropdownList).toHaveSize(2)
expect(dropdownToggle).toHaveClass('show')
if (shouldTriggerClick) { const [triggerDropdownFirst, triggerDropdownLast] = triggerDropdownList
document.documentElement.click()
} else { triggerDropdownFirst.addEventListener('shown.bs.dropdown', () => {
done() expect(triggerDropdownFirst).toHaveClass('show')
expect(fixtureEl.querySelectorAll('.dropdown-menu.show')).toHaveSize(1)
document.body.click()
})
triggerDropdownFirst.addEventListener('hidden.bs.dropdown', () => {
expect(fixtureEl.querySelectorAll('.dropdown-menu.show')).toHaveSize(0)
triggerDropdownLast.click()
})
triggerDropdownLast.addEventListener('shown.bs.dropdown', () => {
expect(triggerDropdownLast).toHaveClass('show')
expect(fixtureEl.querySelectorAll('.dropdown-menu.show')).toHaveSize(1)
document.body.click()
})
triggerDropdownLast.addEventListener('hidden.bs.dropdown', () => {
expect(fixtureEl.querySelectorAll('.dropdown-menu.show')).toHaveSize(0)
resolve()
})
triggerDropdownFirst.click()
})
})
it('should remove "show" class if body if tabbing outside of menu, with multiple dropdowns', () => {
return new Promise(resolve => {
fixtureEl.innerHTML = [
'<div class="dropdown">',
' <a class="dropdown-toggle" data-bs-toggle="dropdown" href="#testmenu">Test menu</a>',
' <div class="dropdown-menu">',
' <a class="dropdown-item" href="#sub1">Submenu 1</a>',
' </div>',
'</div>',
'<div class="btn-group">',
' <button class="btn">Actions</button>',
' <button class="btn dropdown-toggle" data-bs-toggle="dropdown"></button>',
' <div class="dropdown-menu">',
' <a class="dropdown-item" href="#">Action 1</a>',
' </div>',
'</div>'
].join('')
const triggerDropdownList = fixtureEl.querySelectorAll('[data-bs-toggle="dropdown"]')
expect(triggerDropdownList).toHaveSize(2)
const [triggerDropdownFirst, triggerDropdownLast] = triggerDropdownList
triggerDropdownFirst.addEventListener('shown.bs.dropdown', () => {
expect(triggerDropdownFirst).toHaveClass('show')
expect(fixtureEl.querySelectorAll('.dropdown-menu.show')).toHaveSize(1)
const keyup = createEvent('keyup')
keyup.key = 'Tab'
document.dispatchEvent(keyup)
})
triggerDropdownFirst.addEventListener('hidden.bs.dropdown', () => {
expect(fixtureEl.querySelectorAll('.dropdown-menu.show')).toHaveSize(0)
triggerDropdownLast.click()
})
triggerDropdownLast.addEventListener('shown.bs.dropdown', () => {
expect(triggerDropdownLast).toHaveClass('show')
expect(fixtureEl.querySelectorAll('.dropdown-menu.show')).toHaveSize(1)
const keyup = createEvent('keyup')
keyup.key = 'Tab'
document.dispatchEvent(keyup)
})
triggerDropdownLast.addEventListener('hidden.bs.dropdown', () => {
expect(fixtureEl.querySelectorAll('.dropdown-menu.show')).toHaveSize(0)
resolve()
})
triggerDropdownFirst.click()
})
})
it('should fire hide and hidden event without a clickEvent if event type is not click', () => {
return new Promise(resolve => {
fixtureEl.innerHTML = [
'<div class="dropdown">',
' <button class="btn dropdown-toggle" data-bs-toggle="dropdown">Dropdown</button>',
' <div class="dropdown-menu">',
' <a class="dropdown-item" href="#sub1">Submenu 1</a>',
' </div>',
'</div>'
].join('')
const triggerDropdown = fixtureEl.querySelector('[data-bs-toggle="dropdown"]')
triggerDropdown.addEventListener('hide.bs.dropdown', event => {
expect(event.clickEvent).toBeUndefined()
})
triggerDropdown.addEventListener('hidden.bs.dropdown', event => {
expect(event.clickEvent).toBeUndefined()
resolve()
})
triggerDropdown.addEventListener('shown.bs.dropdown', () => {
const keydown = createEvent('keydown')
keydown.key = 'Escape'
triggerDropdown.dispatchEvent(keydown)
})
triggerDropdown.click()
})
})
it('should bubble up the events to the parent elements', () => {
return new Promise(resolve => {
fixtureEl.innerHTML = [
'<div class="dropdown">',
' <button class="btn dropdown-toggle" data-bs-toggle="dropdown">Dropdown</button>',
' <div class="dropdown-menu">',
' <a class="dropdown-item" href="#subMenu">Sub menu</a>',
' </div>',
'</div>'
].join('')
const triggerDropdown = fixtureEl.querySelector('[data-bs-toggle="dropdown"]')
const dropdownParent = fixtureEl.querySelector('.dropdown')
const dropdown = new Dropdown(triggerDropdown)
const showFunction = jasmine.createSpy('showFunction')
dropdownParent.addEventListener('show.bs.dropdown', showFunction)
const shownFunction = jasmine.createSpy('shownFunction')
dropdownParent.addEventListener('shown.bs.dropdown', () => {
shownFunction()
dropdown.hide()
})
const hideFunction = jasmine.createSpy('hideFunction')
dropdownParent.addEventListener('hide.bs.dropdown', hideFunction)
dropdownParent.addEventListener('hidden.bs.dropdown', () => {
expect(showFunction).toHaveBeenCalled()
expect(shownFunction).toHaveBeenCalled()
expect(hideFunction).toHaveBeenCalled()
resolve()
})
dropdown.show()
})
})
it('should ignore keyboard events within <input>s and <textarea>s', () => {
return new Promise(resolve => {
fixtureEl.innerHTML = [
'<div class="dropdown">',
' <button class="btn dropdown-toggle" data-bs-toggle="dropdown">Dropdown</button>',
' <div class="dropdown-menu">',
' <a class="dropdown-item" href="#sub1">Submenu 1</a>',
' <input type="text">',
' <textarea></textarea>',
' </div>',
'</div>'
].join('')
const triggerDropdown = fixtureEl.querySelector('[data-bs-toggle="dropdown"]')
const input = fixtureEl.querySelector('input')
const textarea = fixtureEl.querySelector('textarea')
triggerDropdown.addEventListener('shown.bs.dropdown', () => {
input.focus()
const keydown = createEvent('keydown')
keydown.key = 'ArrowUp'
input.dispatchEvent(keydown)
expect(document.activeElement).toEqual(input, 'input still focused')
textarea.focus()
textarea.dispatchEvent(keydown)
expect(document.activeElement).toEqual(textarea, 'textarea still focused')
resolve()
})
triggerDropdown.click()
})
})
it('should skip disabled element when using keyboard navigation', () => {
return new Promise(resolve => {
fixtureEl.innerHTML = [
'<div class="dropdown">',
' <button class="btn dropdown-toggle" data-bs-toggle="dropdown">Dropdown</button>',
' <div class="dropdown-menu">',
' <a class="dropdown-item disabled" href="#sub1">Submenu 1</a>',
' <button class="dropdown-item" type="button" disabled>Disabled button</button>',
' <a id="item1" class="dropdown-item" href="#">Another link</a>',
' </div>',
'</div>'
].join('')
const triggerDropdown = fixtureEl.querySelector('[data-bs-toggle="dropdown"]')
triggerDropdown.addEventListener('shown.bs.dropdown', () => {
const keydown = createEvent('keydown')
keydown.key = 'ArrowDown'
triggerDropdown.dispatchEvent(keydown)
triggerDropdown.dispatchEvent(keydown)
expect(document.activeElement).not.toHaveClass('disabled')
expect(document.activeElement.hasAttribute('disabled')).toBeFalse()
resolve()
})
triggerDropdown.click()
})
})
it('should skip hidden element when using keyboard navigation', () => {
return new Promise(resolve => {
fixtureEl.innerHTML = [
'<style>',
' .d-none {',
' display: none;',
' }',
'</style>',
'<div class="dropdown">',
' <button class="btn dropdown-toggle" data-bs-toggle="dropdown">Dropdown</button>',
' <div class="dropdown-menu">',
' <button class="dropdown-item d-none" type="button">Hidden button by class</button>',
' <a class="dropdown-item" href="#sub1" style="display: none">Hidden link</a>',
' <a class="dropdown-item" href="#sub1" style="visibility: hidden">Hidden link</a>',
' <a id="item1" class="dropdown-item" href="#">Another link</a>',
' </div>',
'</div>'
].join('')
const triggerDropdown = fixtureEl.querySelector('[data-bs-toggle="dropdown"]')
triggerDropdown.addEventListener('shown.bs.dropdown', () => {
const keydown = createEvent('keydown')
keydown.key = 'ArrowDown'
triggerDropdown.dispatchEvent(keydown)
expect(document.activeElement).not.toHaveClass('d-none')
expect(document.activeElement.style.display).not.toEqual('none')
expect(document.activeElement.style.visibility).not.toEqual('hidden')
resolve()
})
triggerDropdown.click()
})
})
it('should focus next/previous element when using keyboard navigation', () => {
return new Promise(resolve => {
fixtureEl.innerHTML = [
'<div class="dropdown">',
' <button class="btn dropdown-toggle" data-bs-toggle="dropdown">Dropdown</button>',
' <div class="dropdown-menu">',
' <a id="item1" class="dropdown-item" href="#">A link</a>',
' <a id="item2" class="dropdown-item" href="#">Another link</a>',
' </div>',
'</div>'
].join('')
const triggerDropdown = fixtureEl.querySelector('[data-bs-toggle="dropdown"]')
const item1 = fixtureEl.querySelector('#item1')
const item2 = fixtureEl.querySelector('#item2')
triggerDropdown.addEventListener('shown.bs.dropdown', () => {
const keydownArrowDown = createEvent('keydown')
keydownArrowDown.key = 'ArrowDown'
triggerDropdown.dispatchEvent(keydownArrowDown)
expect(document.activeElement).toEqual(item1, 'item1 is focused')
document.activeElement.dispatchEvent(keydownArrowDown)
expect(document.activeElement).toEqual(item2, 'item2 is focused')
const keydownArrowUp = createEvent('keydown')
keydownArrowUp.key = 'ArrowUp'
document.activeElement.dispatchEvent(keydownArrowUp)
expect(document.activeElement).toEqual(item1, 'item1 is focused')
resolve()
})
triggerDropdown.click()
})
})
it('should open the dropdown and focus on the last item when using ArrowUp for the first time', () => {
return new Promise(resolve => {
fixtureEl.innerHTML = [
'<div class="dropdown">',
' <button class="btn dropdown-toggle" data-bs-toggle="dropdown">Dropdown</button>',
' <div class="dropdown-menu">',
' <a id="item1" class="dropdown-item" href="#">A link</a>',
' <a id="item2" class="dropdown-item" href="#">Another link</a>',
' </div>',
'</div>'
].join('')
const triggerDropdown = fixtureEl.querySelector('[data-bs-toggle="dropdown"]')
const lastItem = fixtureEl.querySelector('#item2')
triggerDropdown.addEventListener('shown.bs.dropdown', () => {
setTimeout(() => {
expect(document.activeElement).toEqual(lastItem, 'item2 is focused')
resolve()
})
})
const keydown = createEvent('keydown')
keydown.key = 'ArrowUp'
triggerDropdown.dispatchEvent(keydown)
})
})
it('should open the dropdown and focus on the first item when using ArrowDown for the first time', () => {
return new Promise(resolve => {
fixtureEl.innerHTML = [
'<div class="dropdown">',
' <button class="btn dropdown-toggle" data-bs-toggle="dropdown">Dropdown</button>',
' <div class="dropdown-menu">',
' <a id="item1" class="dropdown-item" href="#">A link</a>',
' <a id="item2" class="dropdown-item" href="#">Another link</a>',
' </div>',
'</div>'
].join('')
const triggerDropdown = fixtureEl.querySelector('[data-bs-toggle="dropdown"]')
const firstItem = fixtureEl.querySelector('#item1')
triggerDropdown.addEventListener('shown.bs.dropdown', () => {
setTimeout(() => {
expect(document.activeElement).toEqual(firstItem, 'item1 is focused')
resolve()
})
})
const keydown = createEvent('keydown')
keydown.key = 'ArrowDown'
triggerDropdown.dispatchEvent(keydown)
})
})
it('should not close the dropdown if the user clicks on a text field within dropdown-menu', () => {
return new Promise(resolve => {
fixtureEl.innerHTML = [
'<div class="dropdown">',
' <button class="btn dropdown-toggle" data-bs-toggle="dropdown">Dropdown</button>',
' <div class="dropdown-menu">',
' <input type="text">',
' </div>',
'</div>'
].join('')
const triggerDropdown = fixtureEl.querySelector('[data-bs-toggle="dropdown"]')
const input = fixtureEl.querySelector('input')
input.addEventListener('click', () => {
expect(triggerDropdown).toHaveClass('show')
resolve()
})
triggerDropdown.addEventListener('shown.bs.dropdown', () => {
expect(triggerDropdown).toHaveClass('show')
input.dispatchEvent(createEvent('click'))
})
triggerDropdown.click()
})
})
it('should not close the dropdown if the user clicks on a textarea within dropdown-menu', () => {
return new Promise(resolve => {
fixtureEl.innerHTML = [
'<div class="dropdown">',
' <button class="btn dropdown-toggle" data-bs-toggle="dropdown">Dropdown</button>',
' <div class="dropdown-menu">',
' <textarea></textarea>',
' </div>',
'</div>'
].join('')
const triggerDropdown = fixtureEl.querySelector('[data-bs-toggle="dropdown"]')
const textarea = fixtureEl.querySelector('textarea')
textarea.addEventListener('click', () => {
expect(triggerDropdown).toHaveClass('show')
resolve()
})
triggerDropdown.addEventListener('shown.bs.dropdown', () => {
expect(triggerDropdown).toHaveClass('show')
textarea.dispatchEvent(createEvent('click'))
})
triggerDropdown.click()
})
})
it('should close the dropdown if the user clicks on a text field that is not contained within dropdown-menu', () => {
return new Promise(resolve => {
fixtureEl.innerHTML = [
'<div class="dropdown">',
' <button class="btn dropdown-toggle" data-bs-toggle="dropdown">Dropdown</button>',
' <div class="dropdown-menu">',
' </div>',
'</div>',
'<input type="text">'
].join('')
const triggerDropdown = fixtureEl.querySelector('[data-bs-toggle="dropdown"]')
const input = fixtureEl.querySelector('input')
triggerDropdown.addEventListener('hidden.bs.dropdown', () => {
expect().nothing()
resolve()
})
triggerDropdown.addEventListener('shown.bs.dropdown', () => {
input.dispatchEvent(createEvent('click', {
bubbles: true
}))
})
triggerDropdown.click()
})
})
it('should ignore keyboard events for <input>s and <textarea>s within dropdown-menu, except for escape key', () => {
return new Promise(resolve => {
fixtureEl.innerHTML = [
'<div class="dropdown">',
' <button class="btn dropdown-toggle" data-bs-toggle="dropdown">Dropdown</button>',
' <div class="dropdown-menu">',
' <a class="dropdown-item" href="#sub1">Submenu 1</a>',
' <input type="text">',
' <textarea></textarea>',
' </div>',
'</div>'
].join('')
const triggerDropdown = fixtureEl.querySelector('[data-bs-toggle="dropdown"]')
const input = fixtureEl.querySelector('input')
const textarea = fixtureEl.querySelector('textarea')
const test = (eventKey, elementToDispatch) => {
const event = createEvent('keydown')
event.key = eventKey
elementToDispatch.focus()
elementToDispatch.dispatchEvent(event)
expect(document.activeElement).toEqual(elementToDispatch, `${elementToDispatch.tagName} still focused`)
} }
expectDropdownToBeOpened(false) const keydownEscape = createEvent('keydown')
}, 150) keydownEscape.key = 'Escape'
dropdownToggle.addEventListener('shown.bs.dropdown', () => { triggerDropdown.addEventListener('shown.bs.dropdown', () => {
dropdownMenu.click() // Key Space
expectDropdownToBeOpened() test('Space', input)
test('Space', textarea)
// Key ArrowUp
test('ArrowUp', input)
test('ArrowUp', textarea)
// Key ArrowDown
test('ArrowDown', input)
test('ArrowDown', textarea)
// Key Escape
input.focus()
input.dispatchEvent(keydownEscape)
expect(triggerDropdown).not.toHaveClass('show')
resolve()
})
triggerDropdown.click()
}) })
})
dropdownToggle.click() it('should not open dropdown if escape key was pressed on the toggle', () => {
return new Promise(resolve => {
fixtureEl.innerHTML = [
'<div class="tabs">',
' <div class="dropdown">',
' <button disabled class="btn dropdown-toggle" data-bs-toggle="dropdown">Dropdown</button>',
' <div class="dropdown-menu">',
' <a class="dropdown-item" href="#">Secondary link</a>',
' <a class="dropdown-item" href="#">Something else here</a>',
' <div class="divider"></div>',
' <a class="dropdown-item" href="#">Another link</a>',
' </div>',
' </div>',
'</div>'
].join('')
const triggerDropdown = fixtureEl.querySelector('[data-bs-toggle="dropdown"]')
const dropdown = new Dropdown(triggerDropdown)
const button = fixtureEl.querySelector('button[data-bs-toggle="dropdown"]')
spyOn(dropdown, 'toggle')
// Key escape
button.focus()
// Key escape
const keydownEscape = createEvent('keydown')
keydownEscape.key = 'Escape'
button.dispatchEvent(keydownEscape)
setTimeout(() => {
expect(dropdown.toggle).not.toHaveBeenCalled()
expect(triggerDropdown).not.toHaveClass('show')
resolve()
}, 20)
})
})
it('should propagate escape key events if dropdown is closed', () => {
return new Promise(resolve => {
fixtureEl.innerHTML = [
'<div class="parent">',
' <div class="dropdown">',
' <button class="btn dropdown-toggle" data-bs-toggle="dropdown">Dropdown</button>',
' <div class="dropdown-menu">',
' <a class="dropdown-item" href="#">Some Item</a>',
' </div>',
' </div>',
'</div>'
].join('')
const parent = fixtureEl.querySelector('.parent')
const toggle = fixtureEl.querySelector('[data-bs-toggle="dropdown"]')
const parentKeyHandler = jasmine.createSpy('parentKeyHandler')
parent.addEventListener('keydown', parentKeyHandler)
parent.addEventListener('keyup', () => {
expect(parentKeyHandler).toHaveBeenCalled()
resolve()
})
const keydownEscape = createEvent('keydown', { bubbles: true })
keydownEscape.key = 'Escape'
const keyupEscape = createEvent('keyup', { bubbles: true })
keyupEscape.key = 'Escape'
toggle.focus()
toggle.dispatchEvent(keydownEscape)
toggle.dispatchEvent(keyupEscape)
})
})
it('should close dropdown using `escape` button, and return focus to its trigger', () => {
return new Promise(resolve => {
fixtureEl.innerHTML = [
'<div class="dropdown">',
' <button class="btn dropdown-toggle" data-bs-toggle="dropdown">Dropdown</button>',
' <div class="dropdown-menu">',
' <a class="dropdown-item" href="#">Some Item</a>',
' </div>',
'</div>'
].join('')
const toggle = fixtureEl.querySelector('[data-bs-toggle="dropdown"]')
toggle.addEventListener('shown.bs.dropdown', () => {
const keydownEvent = createEvent('keydown', { bubbles: true })
keydownEvent.key = 'ArrowDown'
toggle.dispatchEvent(keydownEvent)
keydownEvent.key = 'Escape'
toggle.dispatchEvent(keydownEvent)
})
toggle.addEventListener('hidden.bs.dropdown', () => setTimeout(() => {
expect(document.activeElement).toEqual(toggle)
resolve()
}))
toggle.click()
})
})
it('should close dropdown (only) by clicking inside the dropdown menu when it has data-attribute `data-bs-auto-close="inside"`', () => {
return new Promise(resolve => {
fixtureEl.innerHTML = [
'<div class="dropdown">',
' <button class="btn dropdown-toggle" data-bs-toggle="dropdown" data-bs-auto-close="inside">Dropdown toggle</button>',
' <div class="dropdown-menu">',
' <a class="dropdown-item" href="#">Dropdown item</a>',
' </div>',
'</div>'
].join('')
const dropdownToggle = fixtureEl.querySelector('[data-bs-toggle="dropdown"]')
const dropdownMenu = fixtureEl.querySelector('.dropdown-menu')
const expectDropdownToBeOpened = () => setTimeout(() => {
expect(dropdownToggle).toHaveClass('show')
dropdownMenu.click()
}, 150)
dropdownToggle.addEventListener('shown.bs.dropdown', () => {
document.documentElement.click()
expectDropdownToBeOpened()
})
dropdownToggle.addEventListener('hidden.bs.dropdown', () => setTimeout(() => {
expect(dropdownToggle).not.toHaveClass('show')
resolve()
}))
dropdownToggle.click()
})
})
it('should close dropdown (only) by clicking outside the dropdown menu when it has data-attribute `data-bs-auto-close="outside"`', () => {
return new Promise(resolve => {
fixtureEl.innerHTML = [
'<div class="dropdown">',
' <button class="btn dropdown-toggle" data-bs-toggle="dropdown" data-bs-auto-close="outside">Dropdown toggle</button>',
' <div class="dropdown-menu">',
' <a class="dropdown-item" href="#">Dropdown item</a>',
' </div>',
'</div>'
].join('')
const dropdownToggle = fixtureEl.querySelector('[data-bs-toggle="dropdown"]')
const dropdownMenu = fixtureEl.querySelector('.dropdown-menu')
const expectDropdownToBeOpened = () => setTimeout(() => {
expect(dropdownToggle).toHaveClass('show')
document.documentElement.click()
}, 150)
dropdownToggle.addEventListener('shown.bs.dropdown', () => {
dropdownMenu.click()
expectDropdownToBeOpened()
})
dropdownToggle.addEventListener('hidden.bs.dropdown', () => {
expect(dropdownToggle).not.toHaveClass('show')
resolve()
})
dropdownToggle.click()
})
})
it('should not close dropdown by clicking inside or outside the dropdown menu when it has data-attribute `data-bs-auto-close="false"`', () => {
return new Promise(resolve => {
fixtureEl.innerHTML = [
'<div class="dropdown">',
' <button class="btn dropdown-toggle" data-bs-toggle="dropdown" data-bs-auto-close="false">Dropdown toggle</button>',
' <div class="dropdown-menu">',
' <a class="dropdown-item" href="#">Dropdown item</a>',
' </div>',
'</div>'
].join('')
const dropdownToggle = fixtureEl.querySelector('[data-bs-toggle="dropdown"]')
const dropdownMenu = fixtureEl.querySelector('.dropdown-menu')
const expectDropdownToBeOpened = (shouldTriggerClick = true) => setTimeout(() => {
expect(dropdownToggle).toHaveClass('show')
if (shouldTriggerClick) {
document.documentElement.click()
} else {
resolve()
}
expectDropdownToBeOpened(false)
}, 150)
dropdownToggle.addEventListener('shown.bs.dropdown', () => {
dropdownMenu.click()
expectDropdownToBeOpened()
})
dropdownToggle.click()
})
}) })
}) })
@ -2030,52 +2142,54 @@ describe('Dropdown', () => {
}) })
}) })
it('should open dropdown when pressing keydown or keyup', done => { it('should open dropdown when pressing keydown or keyup', () => {
fixtureEl.innerHTML = [ return new Promise(resolve => {
'<div class="dropdown">', fixtureEl.innerHTML = [
' <button class="btn dropdown-toggle" data-bs-toggle="dropdown">Dropdown</button>', '<div class="dropdown">',
' <div class="dropdown-menu">', ' <button class="btn dropdown-toggle" data-bs-toggle="dropdown">Dropdown</button>',
' <a class="dropdown-item disabled" href="#sub1">Submenu 1</a>', ' <div class="dropdown-menu">',
' <button class="dropdown-item" type="button" disabled>Disabled button</button>', ' <a class="dropdown-item disabled" href="#sub1">Submenu 1</a>',
' <a id="item1" class="dropdown-item" href="#">Another link</a>', ' <button class="dropdown-item" type="button" disabled>Disabled button</button>',
' </div>', ' <a id="item1" class="dropdown-item" href="#">Another link</a>',
'</div>' ' </div>',
].join('') '</div>'
].join('')
const triggerDropdown = fixtureEl.querySelector('[data-bs-toggle="dropdown"]') const triggerDropdown = fixtureEl.querySelector('[data-bs-toggle="dropdown"]')
const dropdown = fixtureEl.querySelector('.dropdown') const dropdown = fixtureEl.querySelector('.dropdown')
const keydown = createEvent('keydown') const keydown = createEvent('keydown')
keydown.key = 'ArrowDown' keydown.key = 'ArrowDown'
const keyup = createEvent('keyup') const keyup = createEvent('keyup')
keyup.key = 'ArrowUp' keyup.key = 'ArrowUp'
const handleArrowDown = () => { const handleArrowDown = () => {
expect(triggerDropdown).toHaveClass('show') expect(triggerDropdown).toHaveClass('show')
expect(triggerDropdown.getAttribute('aria-expanded')).toEqual('true') expect(triggerDropdown.getAttribute('aria-expanded')).toEqual('true')
setTimeout(() => { setTimeout(() => {
dropdown.hide() dropdown.hide()
keydown.key = 'ArrowUp' keydown.key = 'ArrowUp'
triggerDropdown.dispatchEvent(keyup) triggerDropdown.dispatchEvent(keyup)
}, 20) }, 20)
}
const handleArrowUp = () => {
expect(triggerDropdown).toHaveClass('show')
expect(triggerDropdown.getAttribute('aria-expanded')).toEqual('true')
done()
}
dropdown.addEventListener('shown.bs.dropdown', event => {
if (event.target.key === 'ArrowDown') {
handleArrowDown()
} else {
handleArrowUp()
} }
})
triggerDropdown.dispatchEvent(keydown) const handleArrowUp = () => {
expect(triggerDropdown).toHaveClass('show')
expect(triggerDropdown.getAttribute('aria-expanded')).toEqual('true')
resolve()
}
dropdown.addEventListener('shown.bs.dropdown', event => {
if (event.target.key === 'ArrowDown') {
handleArrowDown()
} else {
handleArrowUp()
}
})
triggerDropdown.dispatchEvent(keydown)
})
}) })
it('should allow `data-bs-toggle="dropdown"` click events to bubble up', () => { it('should allow `data-bs-toggle="dropdown"` click events to bubble up', () => {
@ -2101,27 +2215,29 @@ describe('Dropdown', () => {
expect(delegatedClickListener).toHaveBeenCalled() expect(delegatedClickListener).toHaveBeenCalled()
}) })
it('should open the dropdown when clicking the child element inside `data-bs-toggle="dropdown"`', done => { it('should open the dropdown when clicking the child element inside `data-bs-toggle="dropdown"`', () => {
fixtureEl.innerHTML = [ return new Promise(resolve => {
'<div class="container">', fixtureEl.innerHTML = [
' <div class="dropdown">', '<div class="container">',
' <button class="btn dropdown-toggle" data-bs-toggle="dropdown"><span id="childElement">Dropdown</span></button>', ' <div class="dropdown">',
' <div class="dropdown-menu">', ' <button class="btn dropdown-toggle" data-bs-toggle="dropdown"><span id="childElement">Dropdown</span></button>',
' <a class="dropdown-item" href="#subMenu">Sub menu</a>', ' <div class="dropdown-menu">',
' </div>', ' <a class="dropdown-item" href="#subMenu">Sub menu</a>',
' </div>', ' </div>',
'</div>' ' </div>',
].join('') '</div>'
].join('')
const btnDropdown = fixtureEl.querySelector('[data-bs-toggle="dropdown"]') const btnDropdown = fixtureEl.querySelector('[data-bs-toggle="dropdown"]')
const childElement = fixtureEl.querySelector('#childElement') const childElement = fixtureEl.querySelector('#childElement')
btnDropdown.addEventListener('shown.bs.dropdown', () => setTimeout(() => { btnDropdown.addEventListener('shown.bs.dropdown', () => setTimeout(() => {
expect(btnDropdown).toHaveClass('show') expect(btnDropdown).toHaveClass('show')
expect(btnDropdown.getAttribute('aria-expanded')).toEqual('true') expect(btnDropdown.getAttribute('aria-expanded')).toEqual('true')
done() resolve()
})) }))
childElement.click() childElement.click()
})
}) })
}) })

View File

@ -12,7 +12,7 @@ import ScrollSpy from '../../src/scrollspy'
import Tab from '../../src/tab' import Tab from '../../src/tab'
import Toast from '../../src/toast' import Toast from '../../src/toast'
import Tooltip from '../../src/tooltip' import Tooltip from '../../src/tooltip'
import { getFixture, clearFixture } from '../helpers/fixture' import { clearFixture, getFixture } from '../helpers/fixture'
describe('jQuery', () => { describe('jQuery', () => {
let fixtureEl let fixtureEl
@ -40,19 +40,21 @@ describe('jQuery', () => {
expect(Tooltip.jQueryInterface).toEqual(jQuery.fn.tooltip) expect(Tooltip.jQueryInterface).toEqual(jQuery.fn.tooltip)
}) })
it('should use jQuery event system', done => { it('should use jQuery event system', () => {
fixtureEl.innerHTML = [ return new Promise(resolve => {
'<div class="alert">', fixtureEl.innerHTML = [
' <button type="button" data-bs-dismiss="alert">x</button>', '<div class="alert">',
'</div>' ' <button type="button" data-bs-dismiss="alert">x</button>',
].join('') '</div>'
].join('')
$(fixtureEl).find('.alert') $(fixtureEl).find('.alert')
.one('closed.bs.alert', () => { .one('closed.bs.alert', () => {
expect($(fixtureEl).find('.alert')).toHaveSize(0) expect($(fixtureEl).find('.alert')).toHaveSize(0)
done() resolve()
}) })
$(fixtureEl).find('button').trigger('click') $(fixtureEl).find('button').trigger('click')
})
}) })
}) })

View File

@ -56,93 +56,101 @@ describe('Modal', () => {
}) })
describe('toggle', () => { describe('toggle', () => {
it('should call ScrollBarHelper to handle scrollBar on body', done => { it('should call ScrollBarHelper to handle scrollBar on body', () => {
fixtureEl.innerHTML = '<div class="modal"><div class="modal-dialog"></div></div>' return new Promise(resolve => {
fixtureEl.innerHTML = '<div class="modal"><div class="modal-dialog"></div></div>'
spyOn(ScrollBarHelper.prototype, 'hide').and.callThrough() spyOn(ScrollBarHelper.prototype, 'hide').and.callThrough()
spyOn(ScrollBarHelper.prototype, 'reset').and.callThrough() spyOn(ScrollBarHelper.prototype, 'reset').and.callThrough()
const modalEl = fixtureEl.querySelector('.modal') const modalEl = fixtureEl.querySelector('.modal')
const modal = new Modal(modalEl) const modal = new Modal(modalEl)
modalEl.addEventListener('shown.bs.modal', () => {
expect(ScrollBarHelper.prototype.hide).toHaveBeenCalled()
modal.toggle()
})
modalEl.addEventListener('hidden.bs.modal', () => {
expect(ScrollBarHelper.prototype.reset).toHaveBeenCalled()
resolve()
})
modalEl.addEventListener('shown.bs.modal', () => {
expect(ScrollBarHelper.prototype.hide).toHaveBeenCalled()
modal.toggle() modal.toggle()
}) })
modalEl.addEventListener('hidden.bs.modal', () => {
expect(ScrollBarHelper.prototype.reset).toHaveBeenCalled()
done()
})
modal.toggle()
}) })
}) })
describe('show', () => { describe('show', () => {
it('should show a modal', done => { it('should show a modal', () => {
fixtureEl.innerHTML = '<div class="modal"><div class="modal-dialog"></div></div>' return new Promise(resolve => {
fixtureEl.innerHTML = '<div class="modal"><div class="modal-dialog"></div></div>'
const modalEl = fixtureEl.querySelector('.modal') const modalEl = fixtureEl.querySelector('.modal')
const modal = new Modal(modalEl) const modal = new Modal(modalEl)
modalEl.addEventListener('show.bs.modal', event => { modalEl.addEventListener('show.bs.modal', event => {
expect(event).toBeDefined() expect(event).toBeDefined()
})
modalEl.addEventListener('shown.bs.modal', () => {
expect(modalEl.getAttribute('aria-modal')).toEqual('true')
expect(modalEl.getAttribute('role')).toEqual('dialog')
expect(modalEl.getAttribute('aria-hidden')).toBeNull()
expect(modalEl.style.display).toEqual('block')
expect(document.querySelector('.modal-backdrop')).not.toBeNull()
resolve()
})
modal.show()
}) })
modalEl.addEventListener('shown.bs.modal', () => {
expect(modalEl.getAttribute('aria-modal')).toEqual('true')
expect(modalEl.getAttribute('role')).toEqual('dialog')
expect(modalEl.getAttribute('aria-hidden')).toBeNull()
expect(modalEl.style.display).toEqual('block')
expect(document.querySelector('.modal-backdrop')).not.toBeNull()
done()
})
modal.show()
}) })
it('should show a modal without backdrop', done => { it('should show a modal without backdrop', () => {
fixtureEl.innerHTML = '<div class="modal"><div class="modal-dialog"></div></div>' return new Promise(resolve => {
fixtureEl.innerHTML = '<div class="modal"><div class="modal-dialog"></div></div>'
const modalEl = fixtureEl.querySelector('.modal') const modalEl = fixtureEl.querySelector('.modal')
const modal = new Modal(modalEl, { const modal = new Modal(modalEl, {
backdrop: false backdrop: false
})
modalEl.addEventListener('show.bs.modal', event => {
expect(event).toBeDefined()
})
modalEl.addEventListener('shown.bs.modal', () => {
expect(modalEl.getAttribute('aria-modal')).toEqual('true')
expect(modalEl.getAttribute('role')).toEqual('dialog')
expect(modalEl.getAttribute('aria-hidden')).toBeNull()
expect(modalEl.style.display).toEqual('block')
expect(document.querySelector('.modal-backdrop')).toBeNull()
resolve()
})
modal.show()
}) })
modalEl.addEventListener('show.bs.modal', event => {
expect(event).toBeDefined()
})
modalEl.addEventListener('shown.bs.modal', () => {
expect(modalEl.getAttribute('aria-modal')).toEqual('true')
expect(modalEl.getAttribute('role')).toEqual('dialog')
expect(modalEl.getAttribute('aria-hidden')).toBeNull()
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 => { it('should show a modal and append the element', () => {
const modalEl = document.createElement('div') return new Promise(resolve => {
const id = 'dynamicModal' const modalEl = document.createElement('div')
const id = 'dynamicModal'
modalEl.setAttribute('id', id) modalEl.setAttribute('id', id)
modalEl.classList.add('modal') modalEl.classList.add('modal')
modalEl.innerHTML = '<div class="modal-dialog"></div>' modalEl.innerHTML = '<div class="modal-dialog"></div>'
const modal = new Modal(modalEl) const modal = new Modal(modalEl)
modalEl.addEventListener('shown.bs.modal', () => { modalEl.addEventListener('shown.bs.modal', () => {
const dynamicModal = document.getElementById(id) const dynamicModal = document.getElementById(id)
expect(dynamicModal).not.toBeNull() expect(dynamicModal).not.toBeNull()
dynamicModal.remove() dynamicModal.remove()
done() resolve()
})
modal.show()
}) })
modal.show()
}) })
it('should do nothing if a modal is shown', () => { it('should do nothing if a modal is shown', () => {
@ -173,511 +181,551 @@ describe('Modal', () => {
expect(EventHandler.trigger).not.toHaveBeenCalled() expect(EventHandler.trigger).not.toHaveBeenCalled()
}) })
it('should not fire shown event when show is prevented', done => { it('should not fire shown event when show is prevented', () => {
fixtureEl.innerHTML = '<div class="modal"><div class="modal-dialog"></div></div>' return new Promise(resolve => {
fixtureEl.innerHTML = '<div class="modal"><div class="modal-dialog"></div></div>'
const modalEl = fixtureEl.querySelector('.modal') const modalEl = fixtureEl.querySelector('.modal')
const modal = new Modal(modalEl) const modal = new Modal(modalEl)
modalEl.addEventListener('show.bs.modal', event => { modalEl.addEventListener('show.bs.modal', event => {
event.preventDefault() event.preventDefault()
const expectedDone = () => { const expectedDone = () => {
expect().nothing() expect().nothing()
done() resolve()
} }
setTimeout(expectedDone, 10) setTimeout(expectedDone, 10)
})
modalEl.addEventListener('shown.bs.modal', () => {
throw new Error('shown event triggered')
})
modal.show()
}) })
modalEl.addEventListener('shown.bs.modal', () => {
throw new Error('shown event triggered')
})
modal.show()
}) })
it('should be shown after the first call to show() has been prevented while fading is enabled ', done => { it('should be shown after the first call to show() has been prevented while fading is enabled ', () => {
fixtureEl.innerHTML = '<div class="modal fade"><div class="modal-dialog"></div></div>' return new Promise(resolve => {
fixtureEl.innerHTML = '<div class="modal fade"><div class="modal-dialog"></div></div>'
const modalEl = fixtureEl.querySelector('.modal') const modalEl = fixtureEl.querySelector('.modal')
const modal = new Modal(modalEl) const modal = new Modal(modalEl)
let prevented = false let prevented = false
modalEl.addEventListener('show.bs.modal', event => { modalEl.addEventListener('show.bs.modal', event => {
if (!prevented) { if (!prevented) {
event.preventDefault() event.preventDefault()
prevented = true prevented = true
setTimeout(() => {
modal.show()
})
}
})
modalEl.addEventListener('shown.bs.modal', () => {
expect(prevented).toBeTrue()
expect(modal._isAnimated()).toBeTrue()
resolve()
})
modal.show()
})
})
it('should set is transitioning if fade class is present', () => {
return new Promise(resolve => {
fixtureEl.innerHTML = '<div class="modal fade"><div class="modal-dialog"></div></div>'
const modalEl = fixtureEl.querySelector('.modal')
const modal = new Modal(modalEl)
modalEl.addEventListener('show.bs.modal', () => {
setTimeout(() => {
expect(modal._isTransitioning).toBeTrue()
})
})
modalEl.addEventListener('shown.bs.modal', () => {
expect(modal._isTransitioning).toBeFalse()
resolve()
})
modal.show()
})
})
it('should close modal when a click occurred on data-bs-dismiss="modal" inside modal', () => {
return new Promise(resolve => {
fixtureEl.innerHTML = [
'<div class="modal fade">',
' <div class="modal-dialog">',
' <div class="modal-header">',
' <button type="button" data-bs-dismiss="modal"></button>',
' </div>',
' </div>',
'</div>'
].join('')
const modalEl = fixtureEl.querySelector('.modal')
const btnClose = fixtureEl.querySelector('[data-bs-dismiss="modal"]')
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()
resolve()
})
modal.show()
})
})
it('should close modal when a click occurred on a data-bs-dismiss="modal" with "bs-target" outside of modal element', () => {
return new Promise(resolve => {
fixtureEl.innerHTML = [
'<button type="button" data-bs-dismiss="modal" data-bs-target="#modal1"></button>',
'<div id="modal1" class="modal fade">',
' <div class="modal-dialog"></div>',
'</div>'
].join('')
const modalEl = fixtureEl.querySelector('.modal')
const btnClose = fixtureEl.querySelector('[data-bs-dismiss="modal"]')
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()
resolve()
})
modal.show()
})
})
it('should set .modal\'s scroll top to 0', () => {
return new Promise(resolve => {
fixtureEl.innerHTML = [
'<div class="modal fade">',
' <div class="modal-dialog"></div>',
'</div>'
].join('')
const modalEl = fixtureEl.querySelector('.modal')
const modal = new Modal(modalEl)
modalEl.addEventListener('shown.bs.modal', () => {
expect(modalEl.scrollTop).toEqual(0)
resolve()
})
modal.show()
})
})
it('should set modal body scroll top to 0 if modal body do not exists', () => {
return new Promise(resolve => {
fixtureEl.innerHTML = [
'<div class="modal fade">',
' <div class="modal-dialog">',
' <div class="modal-body"></div>',
' </div>',
'</div>'
].join('')
const modalEl = fixtureEl.querySelector('.modal')
const modalBody = modalEl.querySelector('.modal-body')
const modal = new Modal(modalEl)
modalEl.addEventListener('shown.bs.modal', () => {
expect(modalBody.scrollTop).toEqual(0)
resolve()
})
modal.show()
})
})
it('should not trap focus if focus equal to false', () => {
return new Promise(resolve => {
fixtureEl.innerHTML = '<div class="modal fade"><div class="modal-dialog"></div></div>'
const modalEl = fixtureEl.querySelector('.modal')
const modal = new Modal(modalEl, {
focus: false
})
spyOn(modal._focustrap, 'activate').and.callThrough()
modalEl.addEventListener('shown.bs.modal', () => {
expect(modal._focustrap.activate).not.toHaveBeenCalled()
resolve()
})
modal.show()
})
})
it('should add listener when escape touch is pressed', () => {
return new Promise(resolve => {
fixtureEl.innerHTML = '<div class="modal"><div class="modal-dialog"></div></div>'
const modalEl = fixtureEl.querySelector('.modal')
const modal = new Modal(modalEl)
spyOn(modal, 'hide').and.callThrough()
modalEl.addEventListener('shown.bs.modal', () => {
const keydownEscape = createEvent('keydown')
keydownEscape.key = 'Escape'
modalEl.dispatchEvent(keydownEscape)
})
modalEl.addEventListener('hidden.bs.modal', () => {
expect(modal.hide).toHaveBeenCalled()
resolve()
})
modal.show()
})
})
it('should do nothing when the pressed key is not escape', () => {
return new Promise(resolve => {
fixtureEl.innerHTML = '<div class="modal"><div class="modal-dialog"></div></div>'
const modalEl = fixtureEl.querySelector('.modal')
const modal = new Modal(modalEl)
spyOn(modal, 'hide')
const expectDone = () => {
expect(modal.hide).not.toHaveBeenCalled()
resolve()
}
modalEl.addEventListener('shown.bs.modal', () => {
const keydownTab = createEvent('keydown')
keydownTab.key = 'Tab'
modalEl.dispatchEvent(keydownTab)
setTimeout(expectDone, 30)
})
modal.show()
})
})
it('should adjust dialog on resize', () => {
return new Promise(resolve => {
fixtureEl.innerHTML = '<div class="modal"><div class="modal-dialog"></div></div>'
const modalEl = fixtureEl.querySelector('.modal')
const modal = new Modal(modalEl)
spyOn(modal, '_adjustDialog').and.callThrough()
const expectDone = () => {
expect(modal._adjustDialog).toHaveBeenCalled()
resolve()
}
modalEl.addEventListener('shown.bs.modal', () => {
const resizeEvent = createEvent('resize')
window.dispatchEvent(resizeEvent)
setTimeout(expectDone, 10)
})
modal.show()
})
})
it('should not close modal when clicking on modal-content', () => {
return new Promise(resolve => {
fixtureEl.innerHTML = [
'<div class="modal">',
' <div class="modal-dialog">',
' <div class="modal-content"></div>',
' </div>',
'</div>'
].join('')
const modalEl = fixtureEl.querySelector('.modal')
const modal = new Modal(modalEl)
const shownCallback = () => {
setTimeout(() => {
expect(modal._isShown).toEqual(true)
resolve()
}, 10)
}
modalEl.addEventListener('shown.bs.modal', () => {
fixtureEl.querySelector('.modal-dialog').click()
fixtureEl.querySelector('.modal-content').click()
shownCallback()
})
modalEl.addEventListener('hidden.bs.modal', () => {
throw new Error('Should not hide a modal')
})
modal.show()
})
})
it('should not close modal when clicking outside of modal-content if backdrop = false', () => {
return new Promise(resolve => {
fixtureEl.innerHTML = '<div class="modal"><div class="modal-dialog"></div></div>'
const modalEl = fixtureEl.querySelector('.modal')
const modal = new Modal(modalEl, {
backdrop: false
})
const shownCallback = () => {
setTimeout(() => {
expect(modal._isShown).toBeTrue()
resolve()
}, 10)
}
modalEl.addEventListener('shown.bs.modal', () => {
modalEl.click()
shownCallback()
})
modalEl.addEventListener('hidden.bs.modal', () => {
throw new Error('Should not hide a modal')
})
modal.show()
})
})
it('should not close modal when clicking outside of modal-content if backdrop = static', () => {
return new Promise(resolve => {
fixtureEl.innerHTML = '<div class="modal"><div class="modal-dialog"></div></div>'
const modalEl = fixtureEl.querySelector('.modal')
const modal = new Modal(modalEl, {
backdrop: 'static'
})
const shownCallback = () => {
setTimeout(() => {
expect(modal._isShown).toBeTrue()
resolve()
}, 10)
}
modalEl.addEventListener('shown.bs.modal', () => {
modalEl.click()
shownCallback()
})
modalEl.addEventListener('hidden.bs.modal', () => {
throw new Error('Should not hide a modal')
})
modal.show()
})
})
it('should close modal when escape key is pressed with keyboard = true and backdrop is static', () => {
return new Promise(resolve => {
fixtureEl.innerHTML = '<div class="modal"><div class="modal-dialog"></div></div>'
const modalEl = fixtureEl.querySelector('.modal')
const modal = new Modal(modalEl, {
backdrop: 'static',
keyboard: true
})
const shownCallback = () => {
setTimeout(() => {
expect(modal._isShown).toBeFalse()
resolve()
}, 10)
}
modalEl.addEventListener('shown.bs.modal', () => {
const keydownEscape = createEvent('keydown')
keydownEscape.key = 'Escape'
modalEl.dispatchEvent(keydownEscape)
shownCallback()
})
modal.show()
})
})
it('should not close modal when escape key is pressed with keyboard = false', () => {
return new Promise(resolve => {
fixtureEl.innerHTML = '<div class="modal"><div class="modal-dialog"></div></div>'
const modalEl = fixtureEl.querySelector('.modal')
const modal = new Modal(modalEl, {
keyboard: false
})
const shownCallback = () => {
setTimeout(() => {
expect(modal._isShown).toBeTrue()
resolve()
}, 10)
}
modalEl.addEventListener('shown.bs.modal', () => {
const keydownEscape = createEvent('keydown')
keydownEscape.key = 'Escape'
modalEl.dispatchEvent(keydownEscape)
shownCallback()
})
modalEl.addEventListener('hidden.bs.modal', () => {
throw new Error('Should not hide a modal')
})
modal.show()
})
})
it('should not overflow when clicking outside of modal-content if backdrop = static', () => {
return new Promise(resolve => {
fixtureEl.innerHTML = '<div class="modal"><div class="modal-dialog" style="transition-duration: 20ms;"></div></div>'
const modalEl = fixtureEl.querySelector('.modal')
const modal = new Modal(modalEl, {
backdrop: 'static'
})
modalEl.addEventListener('shown.bs.modal', () => {
modalEl.click()
setTimeout(() => {
expect(modalEl.clientHeight).toEqual(modalEl.scrollHeight)
resolve()
}, 20)
})
modal.show()
})
})
it('should not queue multiple callbacks when clicking outside of modal-content and backdrop = static', () => {
return new Promise(resolve => {
fixtureEl.innerHTML = '<div class="modal"><div class="modal-dialog" style="transition-duration: 50ms;"></div></div>'
const modalEl = fixtureEl.querySelector('.modal')
const modal = new Modal(modalEl, {
backdrop: 'static'
})
modalEl.addEventListener('shown.bs.modal', () => {
const spy = spyOn(modal, '_queueCallback').and.callThrough()
modalEl.click()
modalEl.click()
setTimeout(() => { setTimeout(() => {
modal.show() expect(spy).toHaveBeenCalledTimes(1)
}) resolve()
} }, 20)
})
modalEl.addEventListener('shown.bs.modal', () => {
expect(prevented).toBeTrue()
expect(modal._isAnimated()).toBeTrue()
done()
})
modal.show()
})
it('should set is transitioning if fade class is present', done => {
fixtureEl.innerHTML = '<div class="modal fade"><div class="modal-dialog"></div></div>'
const modalEl = fixtureEl.querySelector('.modal')
const modal = new Modal(modalEl)
modalEl.addEventListener('show.bs.modal', () => {
setTimeout(() => {
expect(modal._isTransitioning).toBeTrue()
}) })
})
modalEl.addEventListener('shown.bs.modal', () => { modal.show()
expect(modal._isTransitioning).toBeFalse()
done()
}) })
modal.show()
}) })
it('should close modal when a click occurred on data-bs-dismiss="modal" inside modal', done => { it('should trap focus', () => {
fixtureEl.innerHTML = [ return new Promise(resolve => {
'<div class="modal fade">', fixtureEl.innerHTML = '<div class="modal"><div class="modal-dialog"></div></div>'
' <div class="modal-dialog">',
' <div class="modal-header">',
' <button type="button" data-bs-dismiss="modal"></button>',
' </div>',
' </div>',
'</div>'
].join('')
const modalEl = fixtureEl.querySelector('.modal') const modalEl = fixtureEl.querySelector('.modal')
const btnClose = fixtureEl.querySelector('[data-bs-dismiss="modal"]') const modal = new Modal(modalEl)
const modal = new Modal(modalEl)
spyOn(modal, 'hide').and.callThrough() spyOn(modal._focustrap, 'activate').and.callThrough()
modalEl.addEventListener('shown.bs.modal', () => { modalEl.addEventListener('shown.bs.modal', () => {
btnClose.click() expect(modal._focustrap.activate).toHaveBeenCalled()
resolve()
})
modal.show()
}) })
modalEl.addEventListener('hidden.bs.modal', () => {
expect(modal.hide).toHaveBeenCalled()
done()
})
modal.show()
})
it('should close modal when a click occurred on a data-bs-dismiss="modal" with "bs-target" outside of modal element', done => {
fixtureEl.innerHTML = [
'<button type="button" data-bs-dismiss="modal" data-bs-target="#modal1"></button>',
'<div id="modal1" class="modal fade">',
' <div class="modal-dialog"></div>',
'</div>'
].join('')
const modalEl = fixtureEl.querySelector('.modal')
const btnClose = fixtureEl.querySelector('[data-bs-dismiss="modal"]')
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()
})
it('should set .modal\'s scroll top to 0', done => {
fixtureEl.innerHTML = [
'<div class="modal fade">',
' <div class="modal-dialog"></div>',
'</div>'
].join('')
const modalEl = fixtureEl.querySelector('.modal')
const modal = new Modal(modalEl)
modalEl.addEventListener('shown.bs.modal', () => {
expect(modalEl.scrollTop).toEqual(0)
done()
})
modal.show()
})
it('should set modal body scroll top to 0 if modal body do not exists', done => {
fixtureEl.innerHTML = [
'<div class="modal fade">',
' <div class="modal-dialog">',
' <div class="modal-body"></div>',
' </div>',
'</div>'
].join('')
const modalEl = fixtureEl.querySelector('.modal')
const modalBody = modalEl.querySelector('.modal-body')
const modal = new Modal(modalEl)
modalEl.addEventListener('shown.bs.modal', () => {
expect(modalBody.scrollTop).toEqual(0)
done()
})
modal.show()
})
it('should not trap focus if focus equal to false', done => {
fixtureEl.innerHTML = '<div class="modal fade"><div class="modal-dialog"></div></div>'
const modalEl = fixtureEl.querySelector('.modal')
const modal = new Modal(modalEl, {
focus: false
})
spyOn(modal._focustrap, 'activate').and.callThrough()
modalEl.addEventListener('shown.bs.modal', () => {
expect(modal._focustrap.activate).not.toHaveBeenCalled()
done()
})
modal.show()
})
it('should add listener when escape touch is pressed', done => {
fixtureEl.innerHTML = '<div class="modal"><div class="modal-dialog"></div></div>'
const modalEl = fixtureEl.querySelector('.modal')
const modal = new Modal(modalEl)
spyOn(modal, 'hide').and.callThrough()
modalEl.addEventListener('shown.bs.modal', () => {
const keydownEscape = createEvent('keydown')
keydownEscape.key = 'Escape'
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 => {
fixtureEl.innerHTML = '<div class="modal"><div class="modal-dialog"></div></div>'
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')
keydownTab.key = 'Tab'
modalEl.dispatchEvent(keydownTab)
setTimeout(expectDone, 30)
})
modal.show()
})
it('should adjust dialog on resize', done => {
fixtureEl.innerHTML = '<div class="modal"><div class="modal-dialog"></div></div>'
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 on modal-content', done => {
fixtureEl.innerHTML = [
'<div class="modal">',
' <div class="modal-dialog">',
' <div class="modal-content"></div>',
' </div>',
'</div>'
].join('')
const modalEl = fixtureEl.querySelector('.modal')
const modal = new Modal(modalEl)
const shownCallback = () => {
setTimeout(() => {
expect(modal._isShown).toEqual(true)
done()
}, 10)
}
modalEl.addEventListener('shown.bs.modal', () => {
fixtureEl.querySelector('.modal-dialog').click()
fixtureEl.querySelector('.modal-content').click()
shownCallback()
})
modalEl.addEventListener('hidden.bs.modal', () => {
throw new Error('Should not hide a modal')
})
modal.show()
})
it('should not close modal when clicking outside of modal-content if backdrop = false', done => {
fixtureEl.innerHTML = '<div class="modal"><div class="modal-dialog"></div></div>'
const modalEl = fixtureEl.querySelector('.modal')
const modal = new Modal(modalEl, {
backdrop: false
})
const shownCallback = () => {
setTimeout(() => {
expect(modal._isShown).toBeTrue()
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()
})
it('should not close modal when clicking outside of modal-content if backdrop = static', done => {
fixtureEl.innerHTML = '<div class="modal"><div class="modal-dialog"></div></div>'
const modalEl = fixtureEl.querySelector('.modal')
const modal = new Modal(modalEl, {
backdrop: 'static'
})
const shownCallback = () => {
setTimeout(() => {
expect(modal._isShown).toBeTrue()
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()
})
it('should close modal when escape key is pressed with keyboard = true and backdrop is static', done => {
fixtureEl.innerHTML = '<div class="modal"><div class="modal-dialog"></div></div>'
const modalEl = fixtureEl.querySelector('.modal')
const modal = new Modal(modalEl, {
backdrop: 'static',
keyboard: true
})
const shownCallback = () => {
setTimeout(() => {
expect(modal._isShown).toBeFalse()
done()
}, 10)
}
modalEl.addEventListener('shown.bs.modal', () => {
const keydownEscape = createEvent('keydown')
keydownEscape.key = 'Escape'
modalEl.dispatchEvent(keydownEscape)
shownCallback()
})
modal.show()
})
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>'
const modalEl = fixtureEl.querySelector('.modal')
const modal = new Modal(modalEl, {
keyboard: false
})
const shownCallback = () => {
setTimeout(() => {
expect(modal._isShown).toBeTrue()
done()
}, 10)
}
modalEl.addEventListener('shown.bs.modal', () => {
const keydownEscape = createEvent('keydown')
keydownEscape.key = 'Escape'
modalEl.dispatchEvent(keydownEscape)
shownCallback()
})
modalEl.addEventListener('hidden.bs.modal', () => {
throw new Error('Should not hide a modal')
})
modal.show()
})
it('should not overflow when clicking outside of modal-content if backdrop = static', done => {
fixtureEl.innerHTML = '<div class="modal"><div class="modal-dialog" style="transition-duration: 20ms;"></div></div>'
const modalEl = fixtureEl.querySelector('.modal')
const modal = new Modal(modalEl, {
backdrop: 'static'
})
modalEl.addEventListener('shown.bs.modal', () => {
modalEl.click()
setTimeout(() => {
expect(modalEl.clientHeight).toEqual(modalEl.scrollHeight)
done()
}, 20)
})
modal.show()
})
it('should not queue multiple callbacks when clicking outside of modal-content and backdrop = static', done => {
fixtureEl.innerHTML = '<div class="modal"><div class="modal-dialog" style="transition-duration: 50ms;"></div></div>'
const modalEl = fixtureEl.querySelector('.modal')
const modal = new Modal(modalEl, {
backdrop: 'static'
})
modalEl.addEventListener('shown.bs.modal', () => {
const spy = spyOn(modal, '_queueCallback').and.callThrough()
modalEl.click()
modalEl.click()
setTimeout(() => {
expect(spy).toHaveBeenCalledTimes(1)
done()
}, 20)
})
modal.show()
})
it('should trap focus', done => {
fixtureEl.innerHTML = '<div class="modal"><div class="modal-dialog"></div></div>'
const modalEl = fixtureEl.querySelector('.modal')
const modal = new Modal(modalEl)
spyOn(modal._focustrap, 'activate').and.callThrough()
modalEl.addEventListener('shown.bs.modal', () => {
expect(modal._focustrap.activate).toHaveBeenCalled()
done()
})
modal.show()
}) })
}) })
describe('hide', () => { describe('hide', () => {
it('should hide a modal', done => { it('should hide a modal', () => {
fixtureEl.innerHTML = '<div class="modal"><div class="modal-dialog"></div></div>' return new Promise(resolve => {
fixtureEl.innerHTML = '<div class="modal"><div class="modal-dialog"></div></div>'
const modalEl = fixtureEl.querySelector('.modal') const modalEl = fixtureEl.querySelector('.modal')
const modal = new Modal(modalEl) const modal = new Modal(modalEl)
const backdropSpy = spyOn(modal._backdrop, 'hide').and.callThrough() const backdropSpy = spyOn(modal._backdrop, 'hide').and.callThrough()
modalEl.addEventListener('shown.bs.modal', () => { modalEl.addEventListener('shown.bs.modal', () => {
modal.hide() modal.hide()
})
modalEl.addEventListener('hide.bs.modal', event => {
expect(event).toBeDefined()
})
modalEl.addEventListener('hidden.bs.modal', () => {
expect(modalEl.getAttribute('aria-modal')).toBeNull()
expect(modalEl.getAttribute('role')).toBeNull()
expect(modalEl.getAttribute('aria-hidden')).toEqual('true')
expect(modalEl.style.display).toEqual('none')
expect(backdropSpy).toHaveBeenCalled()
resolve()
})
modal.show()
}) })
modalEl.addEventListener('hide.bs.modal', event => {
expect(event).toBeDefined()
})
modalEl.addEventListener('hidden.bs.modal', () => {
expect(modalEl.getAttribute('aria-modal')).toBeNull()
expect(modalEl.getAttribute('role')).toBeNull()
expect(modalEl.getAttribute('aria-hidden')).toEqual('true')
expect(modalEl.style.display).toEqual('none')
expect(backdropSpy).toHaveBeenCalled()
done()
})
modal.show()
}) })
it('should close modal when clicking outside of modal-content', done => { it('should close modal when clicking outside of modal-content', () => {
fixtureEl.innerHTML = '<div class="modal"><div class="modal-dialog"></div></div>' return new Promise(resolve => {
fixtureEl.innerHTML = '<div class="modal"><div class="modal-dialog"></div></div>'
const modalEl = fixtureEl.querySelector('.modal') const modalEl = fixtureEl.querySelector('.modal')
const modal = new Modal(modalEl) const modal = new Modal(modalEl)
modalEl.addEventListener('shown.bs.modal', () => { modalEl.addEventListener('shown.bs.modal', () => {
modalEl.click() modalEl.click()
})
modalEl.addEventListener('hidden.bs.modal', () => {
expect(modalEl.getAttribute('aria-modal')).toBeNull()
expect(modalEl.getAttribute('role')).toBeNull()
expect(modalEl.getAttribute('aria-hidden')).toEqual('true')
expect(modalEl.style.display).toEqual('none')
expect(document.querySelector('.modal-backdrop')).toBeNull()
resolve()
})
modal.show()
}) })
modalEl.addEventListener('hidden.bs.modal', () => {
expect(modalEl.getAttribute('aria-modal')).toBeNull()
expect(modalEl.getAttribute('role')).toBeNull()
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', () => { it('should do nothing is the modal is not shown', () => {
@ -703,52 +751,56 @@ describe('Modal', () => {
expect().nothing() expect().nothing()
}) })
it('should not hide a modal if hide is prevented', done => { it('should not hide a modal if hide is prevented', () => {
fixtureEl.innerHTML = '<div class="modal"><div class="modal-dialog"></div></div>' return new Promise(resolve => {
fixtureEl.innerHTML = '<div class="modal"><div class="modal-dialog"></div></div>'
const modalEl = fixtureEl.querySelector('.modal') const modalEl = fixtureEl.querySelector('.modal')
const modal = new Modal(modalEl) const modal = new Modal(modalEl)
modalEl.addEventListener('shown.bs.modal', () => { modalEl.addEventListener('shown.bs.modal', () => {
modal.hide() modal.hide()
})
const hideCallback = () => {
setTimeout(() => {
expect(modal._isShown).toBeTrue()
resolve()
}, 10)
}
modalEl.addEventListener('hide.bs.modal', event => {
event.preventDefault()
hideCallback()
})
modalEl.addEventListener('hidden.bs.modal', () => {
throw new Error('should not trigger hidden')
})
modal.show()
}) })
const hideCallback = () => {
setTimeout(() => {
expect(modal._isShown).toBeTrue()
done()
}, 10)
}
modalEl.addEventListener('hide.bs.modal', event => {
event.preventDefault()
hideCallback()
})
modalEl.addEventListener('hidden.bs.modal', () => {
throw new Error('should not trigger hidden')
})
modal.show()
}) })
it('should release focus trap', done => { it('should release focus trap', () => {
fixtureEl.innerHTML = '<div class="modal"><div class="modal-dialog"></div></div>' return new Promise(resolve => {
fixtureEl.innerHTML = '<div class="modal"><div class="modal-dialog"></div></div>'
const modalEl = fixtureEl.querySelector('.modal') const modalEl = fixtureEl.querySelector('.modal')
const modal = new Modal(modalEl) const modal = new Modal(modalEl)
spyOn(modal._focustrap, 'deactivate').and.callThrough() spyOn(modal._focustrap, 'deactivate').and.callThrough()
modalEl.addEventListener('shown.bs.modal', () => { modalEl.addEventListener('shown.bs.modal', () => {
modal.hide() modal.hide()
})
modalEl.addEventListener('hidden.bs.modal', () => {
expect(modal._focustrap.deactivate).toHaveBeenCalled()
resolve()
})
modal.show()
}) })
modalEl.addEventListener('hidden.bs.modal', () => {
expect(modal._focustrap.deactivate).toHaveBeenCalled()
done()
})
modal.show()
}) })
}) })
@ -789,246 +841,260 @@ describe('Modal', () => {
}) })
describe('data-api', () => { describe('data-api', () => {
it('should toggle modal', done => { it('should toggle modal', () => {
fixtureEl.innerHTML = [ return new Promise(resolve => {
'<button type="button" data-bs-toggle="modal" data-bs-target="#exampleModal"></button>', fixtureEl.innerHTML = [
'<div id="exampleModal" class="modal"><div class="modal-dialog"></div></div>' '<button type="button" data-bs-toggle="modal" data-bs-target="#exampleModal"></button>',
].join('') '<div id="exampleModal" class="modal"><div class="modal-dialog"></div></div>'
].join('')
const modalEl = fixtureEl.querySelector('.modal') const modalEl = fixtureEl.querySelector('.modal')
const trigger = fixtureEl.querySelector('[data-bs-toggle="modal"]') const trigger = fixtureEl.querySelector('[data-bs-toggle="modal"]')
modalEl.addEventListener('shown.bs.modal', () => { modalEl.addEventListener('shown.bs.modal', () => {
expect(modalEl.getAttribute('aria-modal')).toEqual('true') expect(modalEl.getAttribute('aria-modal')).toEqual('true')
expect(modalEl.getAttribute('role')).toEqual('dialog') expect(modalEl.getAttribute('role')).toEqual('dialog')
expect(modalEl.getAttribute('aria-hidden')).toBeNull() expect(modalEl.getAttribute('aria-hidden')).toBeNull()
expect(modalEl.style.display).toEqual('block') expect(modalEl.style.display).toEqual('block')
expect(document.querySelector('.modal-backdrop')).not.toBeNull() expect(document.querySelector('.modal-backdrop')).not.toBeNull()
setTimeout(() => trigger.click(), 10) setTimeout(() => trigger.click(), 10)
})
modalEl.addEventListener('hidden.bs.modal', () => {
expect(modalEl.getAttribute('aria-modal')).toBeNull()
expect(modalEl.getAttribute('role')).toBeNull()
expect(modalEl.getAttribute('aria-hidden')).toEqual('true')
expect(modalEl.style.display).toEqual('none')
expect(document.querySelector('.modal-backdrop')).toBeNull()
resolve()
})
trigger.click()
}) })
modalEl.addEventListener('hidden.bs.modal', () => {
expect(modalEl.getAttribute('aria-modal')).toBeNull()
expect(modalEl.getAttribute('role')).toBeNull()
expect(modalEl.getAttribute('aria-hidden')).toEqual('true')
expect(modalEl.style.display).toEqual('none')
expect(document.querySelector('.modal-backdrop')).toBeNull()
done()
})
trigger.click()
}) })
it('should not recreate a new modal', done => { it('should not recreate a new modal', () => {
fixtureEl.innerHTML = [ return new Promise(resolve => {
'<button type="button" data-bs-toggle="modal" data-bs-target="#exampleModal"></button>', fixtureEl.innerHTML = [
'<div id="exampleModal" class="modal"><div class="modal-dialog"></div></div>' '<button type="button" data-bs-toggle="modal" data-bs-target="#exampleModal"></button>',
].join('') '<div id="exampleModal" class="modal"><div class="modal-dialog"></div></div>'
].join('')
const modalEl = fixtureEl.querySelector('.modal') const modalEl = fixtureEl.querySelector('.modal')
const modal = new Modal(modalEl) const modal = new Modal(modalEl)
const trigger = fixtureEl.querySelector('[data-bs-toggle="modal"]') const trigger = fixtureEl.querySelector('[data-bs-toggle="modal"]')
spyOn(modal, 'show').and.callThrough() spyOn(modal, 'show').and.callThrough()
modalEl.addEventListener('shown.bs.modal', () => { modalEl.addEventListener('shown.bs.modal', () => {
expect(modal.show).toHaveBeenCalled() expect(modal.show).toHaveBeenCalled()
done() resolve()
})
trigger.click()
}) })
trigger.click()
}) })
it('should prevent default when the trigger is <a> or <area>', done => { it('should prevent default when the trigger is <a> or <area>', () => {
fixtureEl.innerHTML = [ return new Promise(resolve => {
'<a data-bs-toggle="modal" href="#" data-bs-target="#exampleModal"></a>', fixtureEl.innerHTML = [
'<div id="exampleModal" class="modal"><div class="modal-dialog"></div></div>' '<a data-bs-toggle="modal" href="#" data-bs-target="#exampleModal"></a>',
].join('') '<div id="exampleModal" class="modal"><div class="modal-dialog"></div></div>'
].join('')
const modalEl = fixtureEl.querySelector('.modal') const modalEl = fixtureEl.querySelector('.modal')
const trigger = fixtureEl.querySelector('[data-bs-toggle="modal"]') const trigger = fixtureEl.querySelector('[data-bs-toggle="modal"]')
spyOn(Event.prototype, 'preventDefault').and.callThrough() spyOn(Event.prototype, 'preventDefault').and.callThrough()
modalEl.addEventListener('shown.bs.modal', () => { modalEl.addEventListener('shown.bs.modal', () => {
expect(modalEl.getAttribute('aria-modal')).toEqual('true') expect(modalEl.getAttribute('aria-modal')).toEqual('true')
expect(modalEl.getAttribute('role')).toEqual('dialog') expect(modalEl.getAttribute('role')).toEqual('dialog')
expect(modalEl.getAttribute('aria-hidden')).toBeNull() expect(modalEl.getAttribute('aria-hidden')).toBeNull()
expect(modalEl.style.display).toEqual('block') expect(modalEl.style.display).toEqual('block')
expect(document.querySelector('.modal-backdrop')).not.toBeNull() expect(document.querySelector('.modal-backdrop')).not.toBeNull()
expect(Event.prototype.preventDefault).toHaveBeenCalled() expect(Event.prototype.preventDefault).toHaveBeenCalled()
done() resolve()
})
trigger.click()
}) })
trigger.click()
}) })
it('should focus the trigger on hide', done => { it('should focus the trigger on hide', () => {
fixtureEl.innerHTML = [ return new Promise(resolve => {
'<a data-bs-toggle="modal" href="#" data-bs-target="#exampleModal"></a>', fixtureEl.innerHTML = [
'<div id="exampleModal" class="modal"><div class="modal-dialog"></div></div>' '<a data-bs-toggle="modal" href="#" data-bs-target="#exampleModal"></a>',
].join('') '<div id="exampleModal" class="modal"><div class="modal-dialog"></div></div>'
].join('')
const modalEl = fixtureEl.querySelector('.modal') const modalEl = fixtureEl.querySelector('.modal')
const trigger = fixtureEl.querySelector('[data-bs-toggle="modal"]') const trigger = fixtureEl.querySelector('[data-bs-toggle="modal"]')
spyOn(trigger, 'focus') spyOn(trigger, 'focus')
modalEl.addEventListener('shown.bs.modal', () => { modalEl.addEventListener('shown.bs.modal', () => {
const modal = Modal.getInstance(modalEl) const modal = Modal.getInstance(modalEl)
modal.hide() modal.hide()
})
const hideListener = () => {
setTimeout(() => {
expect(trigger.focus).toHaveBeenCalled()
resolve()
}, 20)
}
modalEl.addEventListener('hidden.bs.modal', () => {
hideListener()
})
trigger.click()
}) })
})
it('should not prevent default when a click occurred on data-bs-dismiss="modal" where tagName is DIFFERENT than <a> or <area>', () => {
return new Promise(resolve => {
fixtureEl.innerHTML = [
'<div class="modal">',
' <div class="modal-dialog">',
' <button type="button" data-bs-dismiss="modal"></button>',
' </div>',
'</div>'
].join('')
const hideListener = () => { const modalEl = fixtureEl.querySelector('.modal')
setTimeout(() => { const btnClose = fixtureEl.querySelector('button[data-bs-dismiss="modal"]')
expect(trigger.focus).toHaveBeenCalled() const modal = new Modal(modalEl)
done()
}, 20)
}
modalEl.addEventListener('hidden.bs.modal', () => { spyOn(Event.prototype, 'preventDefault').and.callThrough()
hideListener()
modalEl.addEventListener('shown.bs.modal', () => {
btnClose.click()
})
modalEl.addEventListener('hidden.bs.modal', () => {
expect(Event.prototype.preventDefault).not.toHaveBeenCalled()
resolve()
})
modal.show()
}) })
trigger.click()
}) })
it('should not prevent default when a click occurred on data-bs-dismiss="modal" where tagName is DIFFERENT than <a> or <area>', done => { it('should prevent default when a click occurred on data-bs-dismiss="modal" where tagName is <a> or <area>', () => {
fixtureEl.innerHTML = [ return new Promise(resolve => {
'<div class="modal">', fixtureEl.innerHTML = [
' <div class="modal-dialog">', '<div class="modal">',
' <button type="button" data-bs-dismiss="modal"></button>', ' <div class="modal-dialog">',
' </div>', ' <a type="button" data-bs-dismiss="modal"></a>',
'</div>' ' </div>',
].join('') '</div>'
].join('')
const modalEl = fixtureEl.querySelector('.modal') const modalEl = fixtureEl.querySelector('.modal')
const btnClose = fixtureEl.querySelector('button[data-bs-dismiss="modal"]') const btnClose = fixtureEl.querySelector('a[data-bs-dismiss="modal"]')
const modal = new Modal(modalEl) const modal = new Modal(modalEl)
spyOn(Event.prototype, 'preventDefault').and.callThrough() spyOn(Event.prototype, 'preventDefault').and.callThrough()
modalEl.addEventListener('shown.bs.modal', () => { modalEl.addEventListener('shown.bs.modal', () => {
btnClose.click() btnClose.click()
})
modalEl.addEventListener('hidden.bs.modal', () => {
expect(Event.prototype.preventDefault).toHaveBeenCalled()
resolve()
})
modal.show()
}) })
})
it('should not focus the trigger if the modal is not visible', () => {
return new Promise(resolve => {
fixtureEl.innerHTML = [
'<a data-bs-toggle="modal" href="#" data-bs-target="#exampleModal" style="display: none;"></a>',
'<div id="exampleModal" class="modal" style="display: none;"><div class="modal-dialog"></div></div>'
].join('')
modalEl.addEventListener('hidden.bs.modal', () => { const modalEl = fixtureEl.querySelector('.modal')
expect(Event.prototype.preventDefault).not.toHaveBeenCalled() const trigger = fixtureEl.querySelector('[data-bs-toggle="modal"]')
done()
spyOn(trigger, 'focus')
modalEl.addEventListener('shown.bs.modal', () => {
const modal = Modal.getInstance(modalEl)
modal.hide()
})
const hideListener = () => {
setTimeout(() => {
expect(trigger.focus).not.toHaveBeenCalled()
resolve()
}, 20)
}
modalEl.addEventListener('hidden.bs.modal', () => {
hideListener()
})
trigger.click()
}) })
})
it('should not focus the trigger if the modal is not shown', () => {
return new Promise(resolve => {
fixtureEl.innerHTML = [
'<a data-bs-toggle="modal" href="#" data-bs-target="#exampleModal"></a>',
'<div id="exampleModal" class="modal"><div class="modal-dialog"></div></div>'
].join('')
modal.show() const modalEl = fixtureEl.querySelector('.modal')
const trigger = fixtureEl.querySelector('[data-bs-toggle="modal"]')
spyOn(trigger, 'focus')
const showListener = () => {
setTimeout(() => {
expect(trigger.focus).not.toHaveBeenCalled()
resolve()
}, 10)
}
modalEl.addEventListener('show.bs.modal', event => {
event.preventDefault()
showListener()
})
trigger.click()
})
}) })
it('should prevent default when a click occurred on data-bs-dismiss="modal" where tagName is <a> or <area>', done => { it('should call hide first, if another modal is open', () => {
fixtureEl.innerHTML = [ return new Promise(resolve => {
'<div class="modal">', fixtureEl.innerHTML = [
' <div class="modal-dialog">', '<button data-bs-toggle="modal" data-bs-target="#modal2"></button>',
' <a type="button" data-bs-dismiss="modal"></a>', '<div id="modal1" class="modal fade"><div class="modal-dialog"></div></div>',
' </div>', '<div id="modal2" class="modal"><div class="modal-dialog"></div></div>'
'</div>' ].join('')
].join('')
const modalEl = fixtureEl.querySelector('.modal') const trigger2 = fixtureEl.querySelector('button')
const btnClose = fixtureEl.querySelector('a[data-bs-dismiss="modal"]') const modalEl1 = document.querySelector('#modal1')
const modal = new Modal(modalEl) const modalEl2 = document.querySelector('#modal2')
const modal1 = new Modal(modalEl1)
spyOn(Event.prototype, 'preventDefault').and.callThrough() modalEl1.addEventListener('shown.bs.modal', () => {
trigger2.click()
modalEl.addEventListener('shown.bs.modal', () => { })
btnClose.click() modalEl1.addEventListener('hidden.bs.modal', () => {
expect(Modal.getInstance(modalEl2)).not.toBeNull()
expect(modalEl2).toHaveClass('show')
resolve()
})
modal1.show()
}) })
modalEl.addEventListener('hidden.bs.modal', () => {
expect(Event.prototype.preventDefault).toHaveBeenCalled()
done()
})
modal.show()
})
it('should not focus the trigger if the modal is not visible', done => {
fixtureEl.innerHTML = [
'<a data-bs-toggle="modal" href="#" data-bs-target="#exampleModal" style="display: none;"></a>',
'<div id="exampleModal" class="modal" style="display: none;"><div class="modal-dialog"></div></div>'
].join('')
const modalEl = fixtureEl.querySelector('.modal')
const trigger = fixtureEl.querySelector('[data-bs-toggle="modal"]')
spyOn(trigger, 'focus')
modalEl.addEventListener('shown.bs.modal', () => {
const modal = Modal.getInstance(modalEl)
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 = [
'<a data-bs-toggle="modal" href="#" data-bs-target="#exampleModal"></a>',
'<div id="exampleModal" class="modal"><div class="modal-dialog"></div></div>'
].join('')
const modalEl = fixtureEl.querySelector('.modal')
const trigger = fixtureEl.querySelector('[data-bs-toggle="modal"]')
spyOn(trigger, 'focus')
const showListener = () => {
setTimeout(() => {
expect(trigger.focus).not.toHaveBeenCalled()
done()
}, 10)
}
modalEl.addEventListener('show.bs.modal', event => {
event.preventDefault()
showListener()
})
trigger.click()
})
it('should call hide first, if another modal is open', done => {
fixtureEl.innerHTML = [
'<button data-bs-toggle="modal" data-bs-target="#modal2"></button>',
'<div id="modal1" class="modal fade"><div class="modal-dialog"></div></div>',
'<div id="modal2" class="modal"><div class="modal-dialog"></div></div>'
].join('')
const trigger2 = fixtureEl.querySelector('button')
const modalEl1 = document.querySelector('#modal1')
const modalEl2 = document.querySelector('#modal2')
const modal1 = new Modal(modalEl1)
modalEl1.addEventListener('shown.bs.modal', () => {
trigger2.click()
})
modalEl1.addEventListener('hidden.bs.modal', () => {
expect(Modal.getInstance(modalEl2)).not.toBeNull()
expect(modalEl2).toHaveClass('show')
done()
})
modal1.show()
}) })
}) })
describe('jQueryInterface', () => { describe('jQueryInterface', () => {
it('should create a modal', () => { it('should create a modal', () => {
fixtureEl.innerHTML = '<div class="modal"><div class="modal-dialog"></div></div>' fixtureEl.innerHTML = '<div class="modal"><div class="modal-dialog"></div></div>'

View File

@ -147,83 +147,91 @@ describe('Offcanvas', () => {
}) })
describe('options', () => { describe('options', () => {
it('if scroll is enabled, should allow body to scroll while offcanvas is open', done => { it('if scroll is enabled, should allow body to scroll while offcanvas is open', () => {
fixtureEl.innerHTML = '<div class="offcanvas"></div>' return new Promise(resolve => {
fixtureEl.innerHTML = '<div class="offcanvas"></div>'
spyOn(ScrollBarHelper.prototype, 'hide').and.callThrough() spyOn(ScrollBarHelper.prototype, 'hide').and.callThrough()
spyOn(ScrollBarHelper.prototype, 'reset').and.callThrough() spyOn(ScrollBarHelper.prototype, 'reset').and.callThrough()
const offCanvasEl = fixtureEl.querySelector('.offcanvas') const offCanvasEl = fixtureEl.querySelector('.offcanvas')
const offCanvas = new Offcanvas(offCanvasEl, { scroll: true }) const offCanvas = new Offcanvas(offCanvasEl, { scroll: true })
offCanvasEl.addEventListener('shown.bs.offcanvas', () => { offCanvasEl.addEventListener('shown.bs.offcanvas', () => {
expect(ScrollBarHelper.prototype.hide).not.toHaveBeenCalled() expect(ScrollBarHelper.prototype.hide).not.toHaveBeenCalled()
offCanvas.hide() offCanvas.hide()
})
offCanvasEl.addEventListener('hidden.bs.offcanvas', () => {
expect(ScrollBarHelper.prototype.reset).not.toHaveBeenCalled()
resolve()
})
offCanvas.show()
}) })
offCanvasEl.addEventListener('hidden.bs.offcanvas', () => {
expect(ScrollBarHelper.prototype.reset).not.toHaveBeenCalled()
done()
})
offCanvas.show()
}) })
it('if scroll is disabled, should call ScrollBarHelper to handle scrollBar on body', done => { it('if scroll is disabled, should call ScrollBarHelper to handle scrollBar on body', () => {
fixtureEl.innerHTML = '<div class="offcanvas"></div>' return new Promise(resolve => {
fixtureEl.innerHTML = '<div class="offcanvas"></div>'
spyOn(ScrollBarHelper.prototype, 'hide').and.callThrough() spyOn(ScrollBarHelper.prototype, 'hide').and.callThrough()
spyOn(ScrollBarHelper.prototype, 'reset').and.callThrough() spyOn(ScrollBarHelper.prototype, 'reset').and.callThrough()
const offCanvasEl = fixtureEl.querySelector('.offcanvas') const offCanvasEl = fixtureEl.querySelector('.offcanvas')
const offCanvas = new Offcanvas(offCanvasEl, { scroll: false }) const offCanvas = new Offcanvas(offCanvasEl, { scroll: false })
offCanvasEl.addEventListener('shown.bs.offcanvas', () => { offCanvasEl.addEventListener('shown.bs.offcanvas', () => {
expect(ScrollBarHelper.prototype.hide).toHaveBeenCalled() expect(ScrollBarHelper.prototype.hide).toHaveBeenCalled()
offCanvas.hide() offCanvas.hide()
})
offCanvasEl.addEventListener('hidden.bs.offcanvas', () => {
expect(ScrollBarHelper.prototype.reset).toHaveBeenCalled()
resolve()
})
offCanvas.show()
}) })
offCanvasEl.addEventListener('hidden.bs.offcanvas', () => {
expect(ScrollBarHelper.prototype.reset).toHaveBeenCalled()
done()
})
offCanvas.show()
}) })
it('should hide a shown element if user click on backdrop', done => { it('should hide a shown element if user click on backdrop', () => {
fixtureEl.innerHTML = '<div class="offcanvas"></div>' return new Promise(resolve => {
fixtureEl.innerHTML = '<div class="offcanvas"></div>'
const offCanvasEl = fixtureEl.querySelector('div') const offCanvasEl = fixtureEl.querySelector('div')
const offCanvas = new Offcanvas(offCanvasEl, { backdrop: true }) const offCanvas = new Offcanvas(offCanvasEl, { backdrop: true })
const clickEvent = new Event('mousedown', { bubbles: true, cancelable: true }) const clickEvent = new Event('mousedown', { bubbles: true, cancelable: true })
spyOn(offCanvas._backdrop._config, 'clickCallback').and.callThrough() spyOn(offCanvas._backdrop._config, 'clickCallback').and.callThrough()
offCanvasEl.addEventListener('shown.bs.offcanvas', () => { offCanvasEl.addEventListener('shown.bs.offcanvas', () => {
expect(offCanvas._backdrop._config.clickCallback).toEqual(jasmine.any(Function)) expect(offCanvas._backdrop._config.clickCallback).toEqual(jasmine.any(Function))
offCanvas._backdrop._getElement().dispatchEvent(clickEvent) offCanvas._backdrop._getElement().dispatchEvent(clickEvent)
})
offCanvasEl.addEventListener('hidden.bs.offcanvas', () => {
expect(offCanvas._backdrop._config.clickCallback).toHaveBeenCalled()
resolve()
})
offCanvas.show()
}) })
offCanvasEl.addEventListener('hidden.bs.offcanvas', () => {
expect(offCanvas._backdrop._config.clickCallback).toHaveBeenCalled()
done()
})
offCanvas.show()
}) })
it('should not trap focus if scroll is allowed', done => { it('should not trap focus if scroll is allowed', () => {
fixtureEl.innerHTML = '<div class="offcanvas"></div>' return new Promise(resolve => {
fixtureEl.innerHTML = '<div class="offcanvas"></div>'
const offCanvasEl = fixtureEl.querySelector('.offcanvas') const offCanvasEl = fixtureEl.querySelector('.offcanvas')
const offCanvas = new Offcanvas(offCanvasEl, { const offCanvas = new Offcanvas(offCanvasEl, {
scroll: true scroll: true
})
spyOn(offCanvas._focustrap, 'activate').and.callThrough()
offCanvasEl.addEventListener('shown.bs.offcanvas', () => {
expect(offCanvas._focustrap.activate).not.toHaveBeenCalled()
resolve()
})
offCanvas.show()
}) })
spyOn(offCanvas._focustrap, 'activate').and.callThrough()
offCanvasEl.addEventListener('shown.bs.offcanvas', () => {
expect(offCanvas._focustrap.activate).not.toHaveBeenCalled()
done()
})
offCanvas.show()
}) })
}) })
@ -241,44 +249,48 @@ describe('Offcanvas', () => {
expect(offCanvas.show).toHaveBeenCalled() expect(offCanvas.show).toHaveBeenCalled()
}) })
it('should call hide method if show class is present', done => { it('should call hide method if show class is present', () => {
fixtureEl.innerHTML = '<div class="offcanvas"></div>' return new Promise(resolve => {
fixtureEl.innerHTML = '<div class="offcanvas"></div>'
const offCanvasEl = fixtureEl.querySelector('.offcanvas') const offCanvasEl = fixtureEl.querySelector('.offcanvas')
const offCanvas = new Offcanvas(offCanvasEl) const offCanvas = new Offcanvas(offCanvasEl)
offCanvasEl.addEventListener('shown.bs.offcanvas', () => { offCanvasEl.addEventListener('shown.bs.offcanvas', () => {
expect(offCanvasEl).toHaveClass('show') expect(offCanvasEl).toHaveClass('show')
spyOn(offCanvas, 'hide') spyOn(offCanvas, 'hide')
offCanvas.toggle() offCanvas.toggle()
expect(offCanvas.hide).toHaveBeenCalled() expect(offCanvas.hide).toHaveBeenCalled()
done() resolve()
})
offCanvas.show()
}) })
offCanvas.show()
}) })
}) })
describe('show', () => { describe('show', () => {
it('should add `showing` class during opening and `show` class on end', done => { it('should add `showing` class during opening and `show` class on end', () => {
fixtureEl.innerHTML = '<div class="offcanvas"></div>' return new Promise(resolve => {
const offCanvasEl = fixtureEl.querySelector('.offcanvas') fixtureEl.innerHTML = '<div class="offcanvas"></div>'
const offCanvas = new Offcanvas(offCanvasEl) const offCanvasEl = fixtureEl.querySelector('.offcanvas')
const offCanvas = new Offcanvas(offCanvasEl)
offCanvasEl.addEventListener('show.bs.offcanvas', () => { offCanvasEl.addEventListener('show.bs.offcanvas', () => {
expect(offCanvasEl).not.toHaveClass('show') expect(offCanvasEl).not.toHaveClass('show')
})
offCanvasEl.addEventListener('shown.bs.offcanvas', () => {
expect(offCanvasEl).not.toHaveClass('showing')
expect(offCanvasEl).toHaveClass('show')
resolve()
})
offCanvas.show()
expect(offCanvasEl).toHaveClass('showing')
}) })
offCanvasEl.addEventListener('shown.bs.offcanvas', () => {
expect(offCanvasEl).not.toHaveClass('showing')
expect(offCanvasEl).toHaveClass('show')
done()
})
offCanvas.show()
expect(offCanvasEl).toHaveClass('showing')
}) })
it('should do nothing if already shown', () => { it('should do nothing if already shown', () => {
@ -298,104 +310,114 @@ describe('Offcanvas', () => {
expect(offCanvas._backdrop.show).not.toHaveBeenCalled() expect(offCanvas._backdrop.show).not.toHaveBeenCalled()
}) })
it('should show a hidden element', done => { it('should show a hidden element', () => {
fixtureEl.innerHTML = '<div class="offcanvas"></div>' return new Promise(resolve => {
fixtureEl.innerHTML = '<div class="offcanvas"></div>'
const offCanvasEl = fixtureEl.querySelector('div') const offCanvasEl = fixtureEl.querySelector('div')
const offCanvas = new Offcanvas(offCanvasEl) const offCanvas = new Offcanvas(offCanvasEl)
spyOn(offCanvas._backdrop, 'show').and.callThrough() spyOn(offCanvas._backdrop, 'show').and.callThrough()
offCanvasEl.addEventListener('shown.bs.offcanvas', () => { offCanvasEl.addEventListener('shown.bs.offcanvas', () => {
expect(offCanvasEl).toHaveClass('show') expect(offCanvasEl).toHaveClass('show')
expect(offCanvas._backdrop.show).toHaveBeenCalled() expect(offCanvas._backdrop.show).toHaveBeenCalled()
done() resolve()
})
offCanvas.show()
}) })
offCanvas.show()
}) })
it('should not fire shown when show is prevented', done => { it('should not fire shown when show is prevented', () => {
fixtureEl.innerHTML = '<div class="offcanvas"></div>' return new Promise(resolve => {
fixtureEl.innerHTML = '<div class="offcanvas"></div>'
const offCanvasEl = fixtureEl.querySelector('div') const offCanvasEl = fixtureEl.querySelector('div')
const offCanvas = new Offcanvas(offCanvasEl) const offCanvas = new Offcanvas(offCanvasEl)
spyOn(offCanvas._backdrop, 'show').and.callThrough() spyOn(offCanvas._backdrop, 'show').and.callThrough()
const expectEnd = () => { const expectEnd = () => {
setTimeout(() => { setTimeout(() => {
expect(offCanvas._backdrop.show).not.toHaveBeenCalled() expect(offCanvas._backdrop.show).not.toHaveBeenCalled()
done() resolve()
}, 10) }, 10)
} }
offCanvasEl.addEventListener('show.bs.offcanvas', event => { offCanvasEl.addEventListener('show.bs.offcanvas', event => {
event.preventDefault() event.preventDefault()
expectEnd() expectEnd()
})
offCanvasEl.addEventListener('shown.bs.offcanvas', () => {
throw new Error('should not fire shown event')
})
offCanvas.show()
}) })
offCanvasEl.addEventListener('shown.bs.offcanvas', () => {
throw new Error('should not fire shown event')
})
offCanvas.show()
}) })
it('on window load, should make visible an offcanvas element, if its markup contains class "show"', done => { it('on window load, should make visible an offcanvas element, if its markup contains class "show"', () => {
fixtureEl.innerHTML = '<div class="offcanvas show"></div>' return new Promise(resolve => {
fixtureEl.innerHTML = '<div class="offcanvas show"></div>'
const offCanvasEl = fixtureEl.querySelector('div') const offCanvasEl = fixtureEl.querySelector('div')
spyOn(Offcanvas.prototype, 'show').and.callThrough() spyOn(Offcanvas.prototype, 'show').and.callThrough()
offCanvasEl.addEventListener('shown.bs.offcanvas', () => { offCanvasEl.addEventListener('shown.bs.offcanvas', () => {
done() resolve()
})
window.dispatchEvent(createEvent('load'))
const instance = Offcanvas.getInstance(offCanvasEl)
expect(instance).not.toBeNull()
expect(Offcanvas.prototype.show).toHaveBeenCalled()
}) })
window.dispatchEvent(createEvent('load'))
const instance = Offcanvas.getInstance(offCanvasEl)
expect(instance).not.toBeNull()
expect(Offcanvas.prototype.show).toHaveBeenCalled()
}) })
it('should trap focus', done => { it('should trap focus', () => {
fixtureEl.innerHTML = '<div class="offcanvas"></div>' return new Promise(resolve => {
fixtureEl.innerHTML = '<div class="offcanvas"></div>'
const offCanvasEl = fixtureEl.querySelector('.offcanvas') const offCanvasEl = fixtureEl.querySelector('.offcanvas')
const offCanvas = new Offcanvas(offCanvasEl) const offCanvas = new Offcanvas(offCanvasEl)
spyOn(offCanvas._focustrap, 'activate').and.callThrough() spyOn(offCanvas._focustrap, 'activate').and.callThrough()
offCanvasEl.addEventListener('shown.bs.offcanvas', () => { offCanvasEl.addEventListener('shown.bs.offcanvas', () => {
expect(offCanvas._focustrap.activate).toHaveBeenCalled() expect(offCanvas._focustrap.activate).toHaveBeenCalled()
done() resolve()
})
offCanvas.show()
}) })
offCanvas.show()
}) })
}) })
describe('hide', () => { describe('hide', () => {
it('should add `hiding` class during closing and remover `show` & `hiding` classes on end', done => { it('should add `hiding` class during closing and remover `show` & `hiding` classes on end', () => {
fixtureEl.innerHTML = '<div class="offcanvas"></div>' return new Promise(resolve => {
const offCanvasEl = fixtureEl.querySelector('.offcanvas') fixtureEl.innerHTML = '<div class="offcanvas"></div>'
const offCanvas = new Offcanvas(offCanvasEl) const offCanvasEl = fixtureEl.querySelector('.offcanvas')
const offCanvas = new Offcanvas(offCanvasEl)
offCanvasEl.addEventListener('hide.bs.offcanvas', () => { offCanvasEl.addEventListener('hide.bs.offcanvas', () => {
expect(offCanvasEl).not.toHaveClass('showing') expect(offCanvasEl).not.toHaveClass('showing')
expect(offCanvasEl).toHaveClass('show') expect(offCanvasEl).toHaveClass('show')
}) })
offCanvasEl.addEventListener('hidden.bs.offcanvas', () => { offCanvasEl.addEventListener('hidden.bs.offcanvas', () => {
expect(offCanvasEl).not.toHaveClass('hiding') expect(offCanvasEl).not.toHaveClass('hiding')
expect(offCanvasEl).not.toHaveClass('show') expect(offCanvasEl).not.toHaveClass('show')
done() resolve()
}) })
offCanvas.show() offCanvas.show()
offCanvasEl.addEventListener('shown.bs.offcanvas', () => { offCanvasEl.addEventListener('shown.bs.offcanvas', () => {
offCanvas.hide() offCanvas.hide()
expect(offCanvasEl).not.toHaveClass('showing') expect(offCanvasEl).not.toHaveClass('showing')
expect(offCanvasEl).toHaveClass('hiding') expect(offCanvasEl).toHaveClass('hiding')
})
}) })
}) })
@ -413,65 +435,71 @@ describe('Offcanvas', () => {
expect(EventHandler.trigger).not.toHaveBeenCalled() expect(EventHandler.trigger).not.toHaveBeenCalled()
}) })
it('should hide a shown element', done => { it('should hide a shown element', () => {
fixtureEl.innerHTML = '<div class="offcanvas"></div>' return new Promise(resolve => {
fixtureEl.innerHTML = '<div class="offcanvas"></div>'
const offCanvasEl = fixtureEl.querySelector('div') const offCanvasEl = fixtureEl.querySelector('div')
const offCanvas = new Offcanvas(offCanvasEl) const offCanvas = new Offcanvas(offCanvasEl)
spyOn(offCanvas._backdrop, 'hide').and.callThrough() spyOn(offCanvas._backdrop, 'hide').and.callThrough()
offCanvas.show() offCanvas.show()
offCanvasEl.addEventListener('hidden.bs.offcanvas', () => { offCanvasEl.addEventListener('hidden.bs.offcanvas', () => {
expect(offCanvasEl).not.toHaveClass('show') expect(offCanvasEl).not.toHaveClass('show')
expect(offCanvas._backdrop.hide).toHaveBeenCalled() expect(offCanvas._backdrop.hide).toHaveBeenCalled()
done() resolve()
})
offCanvas.hide()
}) })
offCanvas.hide()
}) })
it('should not fire hidden when hide is prevented', done => { it('should not fire hidden when hide is prevented', () => {
fixtureEl.innerHTML = '<div class="offcanvas"></div>' return new Promise(resolve => {
fixtureEl.innerHTML = '<div class="offcanvas"></div>'
const offCanvasEl = fixtureEl.querySelector('div') const offCanvasEl = fixtureEl.querySelector('div')
const offCanvas = new Offcanvas(offCanvasEl) const offCanvas = new Offcanvas(offCanvasEl)
spyOn(offCanvas._backdrop, 'hide').and.callThrough() spyOn(offCanvas._backdrop, 'hide').and.callThrough()
offCanvas.show() offCanvas.show()
const expectEnd = () => { const expectEnd = () => {
setTimeout(() => { setTimeout(() => {
expect(offCanvas._backdrop.hide).not.toHaveBeenCalled() expect(offCanvas._backdrop.hide).not.toHaveBeenCalled()
done() resolve()
}, 10) }, 10)
} }
offCanvasEl.addEventListener('hide.bs.offcanvas', event => { offCanvasEl.addEventListener('hide.bs.offcanvas', event => {
event.preventDefault() event.preventDefault()
expectEnd() expectEnd()
})
offCanvasEl.addEventListener('hidden.bs.offcanvas', () => {
throw new Error('should not fire hidden event')
})
offCanvas.hide()
}) })
offCanvasEl.addEventListener('hidden.bs.offcanvas', () => {
throw new Error('should not fire hidden event')
})
offCanvas.hide()
}) })
it('should release focus trap', done => { it('should release focus trap', () => {
fixtureEl.innerHTML = '<div class="offcanvas"></div>' return new Promise(resolve => {
fixtureEl.innerHTML = '<div class="offcanvas"></div>'
const offCanvasEl = fixtureEl.querySelector('div') const offCanvasEl = fixtureEl.querySelector('div')
const offCanvas = new Offcanvas(offCanvasEl) const offCanvas = new Offcanvas(offCanvasEl)
spyOn(offCanvas._focustrap, 'deactivate').and.callThrough() spyOn(offCanvas._focustrap, 'deactivate').and.callThrough()
offCanvas.show() offCanvas.show()
offCanvasEl.addEventListener('hidden.bs.offcanvas', () => { offCanvasEl.addEventListener('hidden.bs.offcanvas', () => {
expect(offCanvas._focustrap.deactivate).toHaveBeenCalled() expect(offCanvas._focustrap.deactivate).toHaveBeenCalled()
done() resolve()
})
offCanvas.hide()
}) })
offCanvas.hide()
}) })
}) })
@ -501,22 +529,24 @@ describe('Offcanvas', () => {
}) })
describe('data-api', () => { describe('data-api', () => {
it('should not prevent event for input', done => { it('should not prevent event for input', () => {
fixtureEl.innerHTML = [ return new Promise(resolve => {
'<input type="checkbox" data-bs-toggle="offcanvas" data-bs-target="#offcanvasdiv1" />', fixtureEl.innerHTML = [
'<div id="offcanvasdiv1" class="offcanvas"></div>' '<input type="checkbox" data-bs-toggle="offcanvas" data-bs-target="#offcanvasdiv1" />',
].join('') '<div id="offcanvasdiv1" class="offcanvas"></div>'
].join('')
const target = fixtureEl.querySelector('input') const target = fixtureEl.querySelector('input')
const offCanvasEl = fixtureEl.querySelector('#offcanvasdiv1') const offCanvasEl = fixtureEl.querySelector('#offcanvasdiv1')
offCanvasEl.addEventListener('shown.bs.offcanvas', () => { offCanvasEl.addEventListener('shown.bs.offcanvas', () => {
expect(offCanvasEl).toHaveClass('show') expect(offCanvasEl).toHaveClass('show')
expect(target.checked).toBeTrue() expect(target.checked).toBeTrue()
done() resolve()
})
target.click()
}) })
target.click()
}) })
it('should not call toggle on disabled elements', () => { it('should not call toggle on disabled elements', () => {
@ -534,76 +564,82 @@ describe('Offcanvas', () => {
expect(Offcanvas.prototype.toggle).not.toHaveBeenCalled() expect(Offcanvas.prototype.toggle).not.toHaveBeenCalled()
}) })
it('should call hide first, if another offcanvas is open', done => { it('should call hide first, if another offcanvas is open', () => {
fixtureEl.innerHTML = [ return new Promise(resolve => {
'<button id="btn2" data-bs-toggle="offcanvas" data-bs-target="#offcanvas2"></button>', fixtureEl.innerHTML = [
'<div id="offcanvas1" class="offcanvas"></div>', '<button id="btn2" data-bs-toggle="offcanvas" data-bs-target="#offcanvas2"></button>',
'<div id="offcanvas2" class="offcanvas"></div>' '<div id="offcanvas1" class="offcanvas"></div>',
].join('') '<div id="offcanvas2" class="offcanvas"></div>'
].join('')
const trigger2 = fixtureEl.querySelector('#btn2') const trigger2 = fixtureEl.querySelector('#btn2')
const offcanvasEl1 = document.querySelector('#offcanvas1') const offcanvasEl1 = document.querySelector('#offcanvas1')
const offcanvasEl2 = document.querySelector('#offcanvas2') const offcanvasEl2 = document.querySelector('#offcanvas2')
const offcanvas1 = new Offcanvas(offcanvasEl1) const offcanvas1 = new Offcanvas(offcanvasEl1)
offcanvasEl1.addEventListener('shown.bs.offcanvas', () => { offcanvasEl1.addEventListener('shown.bs.offcanvas', () => {
trigger2.click() trigger2.click()
})
offcanvasEl1.addEventListener('hidden.bs.offcanvas', () => {
expect(Offcanvas.getInstance(offcanvasEl2)).not.toBeNull()
resolve()
})
offcanvas1.show()
}) })
offcanvasEl1.addEventListener('hidden.bs.offcanvas', () => {
expect(Offcanvas.getInstance(offcanvasEl2)).not.toBeNull()
done()
})
offcanvas1.show()
}) })
it('should focus on trigger element after closing offcanvas', done => { it('should focus on trigger element after closing offcanvas', () => {
fixtureEl.innerHTML = [ return new Promise(resolve => {
'<button id="btn" data-bs-toggle="offcanvas" data-bs-target="#offcanvas"></button>', fixtureEl.innerHTML = [
'<div id="offcanvas" class="offcanvas"></div>' '<button id="btn" data-bs-toggle="offcanvas" data-bs-target="#offcanvas"></button>',
].join('') '<div id="offcanvas" class="offcanvas"></div>'
].join('')
const trigger = fixtureEl.querySelector('#btn') const trigger = fixtureEl.querySelector('#btn')
const offcanvasEl = fixtureEl.querySelector('#offcanvas') const offcanvasEl = fixtureEl.querySelector('#offcanvas')
const offcanvas = new Offcanvas(offcanvasEl) const offcanvas = new Offcanvas(offcanvasEl)
spyOn(trigger, 'focus') spyOn(trigger, 'focus')
offcanvasEl.addEventListener('shown.bs.offcanvas', () => { offcanvasEl.addEventListener('shown.bs.offcanvas', () => {
offcanvas.hide() offcanvas.hide()
})
offcanvasEl.addEventListener('hidden.bs.offcanvas', () => {
setTimeout(() => {
expect(trigger.focus).toHaveBeenCalled()
resolve()
}, 5)
})
trigger.click()
}) })
offcanvasEl.addEventListener('hidden.bs.offcanvas', () => {
setTimeout(() => {
expect(trigger.focus).toHaveBeenCalled()
done()
}, 5)
})
trigger.click()
}) })
it('should not focus on trigger element after closing offcanvas, if it is not visible', done => { it('should not focus on trigger element after closing offcanvas, if it is not visible', () => {
fixtureEl.innerHTML = [ return new Promise(resolve => {
'<button id="btn" data-bs-toggle="offcanvas" data-bs-target="#offcanvas"></button>', fixtureEl.innerHTML = [
'<div id="offcanvas" class="offcanvas"></div>' '<button id="btn" data-bs-toggle="offcanvas" data-bs-target="#offcanvas"></button>',
].join('') '<div id="offcanvas" class="offcanvas"></div>'
].join('')
const trigger = fixtureEl.querySelector('#btn') const trigger = fixtureEl.querySelector('#btn')
const offcanvasEl = fixtureEl.querySelector('#offcanvas') const offcanvasEl = fixtureEl.querySelector('#offcanvas')
const offcanvas = new Offcanvas(offcanvasEl) const offcanvas = new Offcanvas(offcanvasEl)
spyOn(trigger, 'focus') spyOn(trigger, 'focus')
offcanvasEl.addEventListener('shown.bs.offcanvas', () => { offcanvasEl.addEventListener('shown.bs.offcanvas', () => {
trigger.style.display = 'none' trigger.style.display = 'none'
offcanvas.hide() offcanvas.hide()
})
offcanvasEl.addEventListener('hidden.bs.offcanvas', () => {
setTimeout(() => {
expect(isVisible(trigger)).toBeFalse()
expect(trigger.focus).not.toHaveBeenCalled()
resolve()
}, 5)
})
trigger.click()
}) })
offcanvasEl.addEventListener('hidden.bs.offcanvas', () => {
setTimeout(() => {
expect(isVisible(trigger)).toBeFalse()
expect(trigger.focus).not.toHaveBeenCalled()
done()
}, 5)
})
trigger.click()
}) })
}) })

View File

@ -62,113 +62,125 @@ describe('Popover', () => {
}) })
describe('show', () => { describe('show', () => {
it('should show a popover', done => { it('should show a popover', () => {
fixtureEl.innerHTML = '<a href="#" title="Popover" data-bs-content="https://twitter.com/getbootstrap">BS twitter</a>' return new Promise(resolve => {
fixtureEl.innerHTML = '<a href="#" title="Popover" data-bs-content="https://twitter.com/getbootstrap">BS twitter</a>'
const popoverEl = fixtureEl.querySelector('a') const popoverEl = fixtureEl.querySelector('a')
const popover = new Popover(popoverEl) const popover = new Popover(popoverEl)
popoverEl.addEventListener('shown.bs.popover', () => { popoverEl.addEventListener('shown.bs.popover', () => {
expect(document.querySelector('.popover')).not.toBeNull() expect(document.querySelector('.popover')).not.toBeNull()
done() resolve()
})
popover.show()
}) })
popover.show()
}) })
it('should set title and content from functions', done => { it('should set title and content from functions', () => {
fixtureEl.innerHTML = '<a href="#">BS twitter</a>' return new Promise(resolve => {
fixtureEl.innerHTML = '<a href="#">BS twitter</a>'
const popoverEl = fixtureEl.querySelector('a') const popoverEl = fixtureEl.querySelector('a')
const popover = new Popover(popoverEl, { const popover = new Popover(popoverEl, {
title: () => 'Bootstrap', title: () => 'Bootstrap',
content: () => 'loves writing tests (╯°□°)╯︵ ┻━┻' content: () => 'loves writing tests (╯°□°)╯︵ ┻━┻'
})
popoverEl.addEventListener('shown.bs.popover', () => {
const popoverDisplayed = document.querySelector('.popover')
expect(popoverDisplayed).not.toBeNull()
expect(popoverDisplayed.querySelector('.popover-header').textContent).toEqual('Bootstrap')
expect(popoverDisplayed.querySelector('.popover-body').textContent).toEqual('loves writing tests (╯°□°)╯︵ ┻━┻')
resolve()
})
popover.show()
}) })
popoverEl.addEventListener('shown.bs.popover', () => {
const popoverDisplayed = document.querySelector('.popover')
expect(popoverDisplayed).not.toBeNull()
expect(popoverDisplayed.querySelector('.popover-header').textContent).toEqual('Bootstrap')
expect(popoverDisplayed.querySelector('.popover-body').textContent).toEqual('loves writing tests (╯°□°)╯︵ ┻━┻')
done()
})
popover.show()
}) })
it('should show a popover with just content without having header', done => { it('should show a popover with just content without having header', () => {
fixtureEl.innerHTML = '<a href="#">Nice link</a>' return new Promise(resolve => {
fixtureEl.innerHTML = '<a href="#">Nice link</a>'
const popoverEl = fixtureEl.querySelector('a') const popoverEl = fixtureEl.querySelector('a')
const popover = new Popover(popoverEl, { const popover = new Popover(popoverEl, {
content: 'Some beautiful content :)' content: 'Some beautiful content :)'
})
popoverEl.addEventListener('shown.bs.popover', () => {
const popoverDisplayed = document.querySelector('.popover')
expect(popoverDisplayed).not.toBeNull()
expect(popoverDisplayed.querySelector('.popover-header')).toBeNull()
expect(popoverDisplayed.querySelector('.popover-body').textContent).toEqual('Some beautiful content :)')
resolve()
})
popover.show()
}) })
popoverEl.addEventListener('shown.bs.popover', () => {
const popoverDisplayed = document.querySelector('.popover')
expect(popoverDisplayed).not.toBeNull()
expect(popoverDisplayed.querySelector('.popover-header')).toBeNull()
expect(popoverDisplayed.querySelector('.popover-body').textContent).toEqual('Some beautiful content :)')
done()
})
popover.show()
}) })
it('should show a popover with just title without having body', done => { it('should show a popover with just title without having body', () => {
fixtureEl.innerHTML = '<a href="#">Nice link</a>' return new Promise(resolve => {
fixtureEl.innerHTML = '<a href="#">Nice link</a>'
const popoverEl = fixtureEl.querySelector('a') const popoverEl = fixtureEl.querySelector('a')
const popover = new Popover(popoverEl, { const popover = new Popover(popoverEl, {
title: 'Title which does not require content' title: 'Title which does not require content'
})
popoverEl.addEventListener('shown.bs.popover', () => {
const popoverDisplayed = document.querySelector('.popover')
expect(popoverDisplayed).not.toBeNull()
expect(popoverDisplayed.querySelector('.popover-body')).toBeNull()
expect(popoverDisplayed.querySelector('.popover-header').textContent).toEqual('Title which does not require content')
resolve()
})
popover.show()
}) })
popoverEl.addEventListener('shown.bs.popover', () => {
const popoverDisplayed = document.querySelector('.popover')
expect(popoverDisplayed).not.toBeNull()
expect(popoverDisplayed.querySelector('.popover-body')).toBeNull()
expect(popoverDisplayed.querySelector('.popover-header').textContent).toEqual('Title which does not require content')
done()
})
popover.show()
}) })
it('should show a popover with just title without having body using data-attribute to get config', done => { it('should show a popover with just title without having body using data-attribute to get config', () => {
fixtureEl.innerHTML = '<a href="#" data-bs-content="" title="Title which does not require content">Nice link</a>' return new Promise(resolve => {
fixtureEl.innerHTML = '<a href="#" data-bs-content="" title="Title which does not require content">Nice link</a>'
const popoverEl = fixtureEl.querySelector('a') const popoverEl = fixtureEl.querySelector('a')
const popover = new Popover(popoverEl) const popover = new Popover(popoverEl)
popoverEl.addEventListener('shown.bs.popover', () => { popoverEl.addEventListener('shown.bs.popover', () => {
const popoverDisplayed = document.querySelector('.popover') const popoverDisplayed = document.querySelector('.popover')
expect(popoverDisplayed).not.toBeNull() expect(popoverDisplayed).not.toBeNull()
expect(popoverDisplayed.querySelector('.popover-body')).toBeNull() expect(popoverDisplayed.querySelector('.popover-body')).toBeNull()
expect(popoverDisplayed.querySelector('.popover-header').textContent).toEqual('Title which does not require content') expect(popoverDisplayed.querySelector('.popover-header').textContent).toEqual('Title which does not require content')
done() resolve()
})
popover.show()
}) })
popover.show()
}) })
it('should NOT show a popover without `title` and `content`', done => { it('should NOT show a popover without `title` and `content`', () => {
fixtureEl.innerHTML = '<a href="#" data-bs-content="" title="">Nice link</a>' return new Promise(resolve => {
fixtureEl.innerHTML = '<a href="#" data-bs-content="" title="">Nice link</a>'
const popoverEl = fixtureEl.querySelector('a') const popoverEl = fixtureEl.querySelector('a')
const popover = new Popover(popoverEl, { animation: false }) const popover = new Popover(popoverEl, { animation: false })
spyOn(EventHandler, 'trigger').and.callThrough() spyOn(EventHandler, 'trigger').and.callThrough()
setTimeout(() => { setTimeout(() => {
expect(EventHandler.trigger).not.toHaveBeenCalled() expect(EventHandler.trigger).not.toHaveBeenCalled()
expect(document.querySelector('.popover')).toBeNull() expect(document.querySelector('.popover')).toBeNull()
done() resolve()
})
popover.show()
}) })
popover.show()
}) })
it('"setContent" should keep the initial template', () => { it('"setContent" should keep the initial template', () => {
@ -187,72 +199,78 @@ describe('Popover', () => {
expect(tip.querySelector('.popover-body')).not.toBeNull() expect(tip.querySelector('.popover-body')).not.toBeNull()
}) })
it('should call setContent once', done => { it('should call setContent once', () => {
fixtureEl.innerHTML = '<a href="#">BS twitter</a>' return new Promise(resolve => {
fixtureEl.innerHTML = '<a href="#">BS twitter</a>'
const popoverEl = fixtureEl.querySelector('a') const popoverEl = fixtureEl.querySelector('a')
const popover = new Popover(popoverEl, { const popover = new Popover(popoverEl, {
content: 'Popover content' content: 'Popover content'
}) })
expect(popover._templateFactory).toBeNull() expect(popover._templateFactory).toBeNull()
let spy = null let spy = null
let times = 1 let times = 1
popoverEl.addEventListener('hidden.bs.popover', () => { popoverEl.addEventListener('hidden.bs.popover', () => {
popover.show()
})
popoverEl.addEventListener('shown.bs.popover', () => {
spy = spy || spyOn(popover._templateFactory, 'constructor').and.callThrough()
const popoverDisplayed = document.querySelector('.popover')
expect(popoverDisplayed).not.toBeNull()
expect(popoverDisplayed.querySelector('.popover-body').textContent).toEqual('Popover content')
expect(spy).toHaveBeenCalledTimes(0)
if (times > 1) {
resolve()
}
times++
popover.hide()
})
popover.show() popover.show()
}) })
popoverEl.addEventListener('shown.bs.popover', () => {
spy = spy || spyOn(popover._templateFactory, 'constructor').and.callThrough()
const popoverDisplayed = document.querySelector('.popover')
expect(popoverDisplayed).not.toBeNull()
expect(popoverDisplayed.querySelector('.popover-body').textContent).toEqual('Popover content')
expect(spy).toHaveBeenCalledTimes(0)
if (times > 1) {
done()
}
times++
popover.hide()
})
popover.show()
}) })
it('should show a popover with provided custom class', done => { it('should show a popover with provided custom class', () => {
fixtureEl.innerHTML = '<a href="#" title="Popover" data-bs-content="https://twitter.com/getbootstrap" data-bs-custom-class="custom-class">BS twitter</a>' return new Promise(resolve => {
fixtureEl.innerHTML = '<a href="#" title="Popover" data-bs-content="https://twitter.com/getbootstrap" data-bs-custom-class="custom-class">BS twitter</a>'
const popoverEl = fixtureEl.querySelector('a') const popoverEl = fixtureEl.querySelector('a')
const popover = new Popover(popoverEl) const popover = new Popover(popoverEl)
popoverEl.addEventListener('shown.bs.popover', () => { popoverEl.addEventListener('shown.bs.popover', () => {
const tip = document.querySelector('.popover') const tip = document.querySelector('.popover')
expect(tip).not.toBeNull() expect(tip).not.toBeNull()
expect(tip).toHaveClass('custom-class') expect(tip).toHaveClass('custom-class')
done() resolve()
})
popover.show()
}) })
popover.show()
}) })
}) })
describe('hide', () => { describe('hide', () => {
it('should hide a popover', done => { it('should hide a popover', () => {
fixtureEl.innerHTML = '<a href="#" title="Popover" data-bs-content="https://twitter.com/getbootstrap">BS twitter</a>' return new Promise(resolve => {
fixtureEl.innerHTML = '<a href="#" title="Popover" data-bs-content="https://twitter.com/getbootstrap">BS twitter</a>'
const popoverEl = fixtureEl.querySelector('a') const popoverEl = fixtureEl.querySelector('a')
const popover = new Popover(popoverEl) const popover = new Popover(popoverEl)
popoverEl.addEventListener('shown.bs.popover', () => { popoverEl.addEventListener('shown.bs.popover', () => {
popover.hide() popover.hide()
})
popoverEl.addEventListener('hidden.bs.popover', () => {
expect(document.querySelector('.popover')).toBeNull()
resolve()
})
popover.show()
}) })
popoverEl.addEventListener('hidden.bs.popover', () => {
expect(document.querySelector('.popover')).toBeNull()
done()
})
popover.show()
}) })
}) })

View File

@ -1,6 +1,6 @@
import ScrollSpy from '../../src/scrollspy' import ScrollSpy from '../../src/scrollspy'
import Manipulator from '../../src/dom/manipulator' import Manipulator from '../../src/dom/manipulator'
import { getFixture, clearFixture, createEvent, jQueryMock } from '../helpers/fixture' import { clearFixture, createEvent, getFixture, jQueryMock } from '../helpers/fixture'
describe('ScrollSpy', () => { describe('ScrollSpy', () => {
let fixtureEl let fixtureEl
@ -85,418 +85,436 @@ describe('ScrollSpy', () => {
expect(scrollSpy._targets).toHaveSize(2) expect(scrollSpy._targets).toHaveSize(2)
}) })
it('should only switch "active" class on current target', done => { it('should only switch "active" class on current target', () => {
fixtureEl.innerHTML = [ return new Promise(resolve => {
'<div id="root" class="active" style="display: block">', fixtureEl.innerHTML = [
' <div class="topbar">', '<div id="root" class="active" style="display: block">',
' <div class="topbar-inner">', ' <div class="topbar">',
' <div class="container" id="ss-target">', ' <div class="topbar-inner">',
' <ul class="nav">', ' <div class="container" id="ss-target">',
' <li class="nav-item"><a href="#masthead">Overview</a></li>', ' <ul class="nav">',
' <li class="nav-item"><a href="#detail">Detail</a></li>', ' <li class="nav-item"><a href="#masthead">Overview</a></li>',
' </ul>', ' <li class="nav-item"><a href="#detail">Detail</a></li>',
' </div>', ' </ul>',
' </div>', ' </div>',
' </div>', ' </div>',
' <div id="scrollspy-example" style="height: 100px; overflow: auto;">', ' </div>',
' <div style="height: 200px;">', ' <div id="scrollspy-example" style="height: 100px; overflow: auto;">',
' <h4 id="masthead">Overview</h4>', ' <div style="height: 200px;">',
' <p style="height: 200px;"></p>', ' <h4 id="masthead">Overview</h4>',
' </div>', ' <p style="height: 200px;"></p>',
' <div style="height: 200px;">', ' </div>',
' <h4 id="detail">Detail</h4>', ' <div style="height: 200px;">',
' <p style="height: 200px;"></p>', ' <h4 id="detail">Detail</h4>',
' </div>', ' <p style="height: 200px;"></p>',
' </div>', ' </div>',
'</div>' ' </div>',
].join('') '</div>'
].join('')
const scrollSpyEl = fixtureEl.querySelector('#scrollspy-example') const scrollSpyEl = fixtureEl.querySelector('#scrollspy-example')
const rootEl = fixtureEl.querySelector('#root') const rootEl = fixtureEl.querySelector('#root')
const scrollSpy = new ScrollSpy(scrollSpyEl, { const scrollSpy = new ScrollSpy(scrollSpyEl, {
target: 'ss-target' target: 'ss-target'
}) })
spyOn(scrollSpy, '_process').and.callThrough() spyOn(scrollSpy, '_process').and.callThrough()
scrollSpyEl.addEventListener('scroll', () => { scrollSpyEl.addEventListener('scroll', () => {
expect(rootEl).toHaveClass('active') expect(rootEl).toHaveClass('active')
expect(scrollSpy._process).toHaveBeenCalled() expect(scrollSpy._process).toHaveBeenCalled()
done() resolve()
}) })
scrollSpyEl.scrollTop = 350 scrollSpyEl.scrollTop = 350
})
it('should only switch "active" class on current target specified w element', done => {
fixtureEl.innerHTML = [
'<div id="root" class="active" style="display: block">',
' <div class="topbar">',
' <div class="topbar-inner">',
' <div class="container" id="ss-target">',
' <ul class="nav">',
' <li class="nav-item"><a href="#masthead">Overview</a></li>',
' <li class="nav-item"><a href="#detail">Detail</a></li>',
' </ul>',
' </div>',
' </div>',
' </div>',
' <div id="scrollspy-example" style="height: 100px; overflow: auto;">',
' <div style="height: 200px;">',
' <h4 id="masthead">Overview</h4>',
' <p style="height: 200px;"></p>',
' </div>',
' <div style="height: 200px;">',
' <h4 id="detail">Detail</h4>',
' <p style="height: 200px;"></p>',
' </div>',
' </div>',
'</div>'
].join('')
const scrollSpyEl = fixtureEl.querySelector('#scrollspy-example')
const rootEl = fixtureEl.querySelector('#root')
const scrollSpy = new ScrollSpy(scrollSpyEl, {
target: fixtureEl.querySelector('#ss-target')
})
spyOn(scrollSpy, '_process').and.callThrough()
scrollSpyEl.addEventListener('scroll', () => {
expect(rootEl).toHaveClass('active')
expect(scrollSpy._process).toHaveBeenCalled()
done()
})
scrollSpyEl.scrollTop = 350
})
it('should correctly select middle navigation option when large offset is used', done => {
fixtureEl.innerHTML = [
'<div id="header" style="height: 500px;"></div>',
'<nav id="navigation" class="navbar">',
' <ul class="navbar-nav">',
' <li class="nav-item"><a class="nav-link active" id="one-link" href="#one">One</a></li>',
' <li class="nav-item"><a class="nav-link" id="two-link" href="#two">Two</a></li>',
' <li class="nav-item"><a class="nav-link" id="three-link" href="#three">Three</a></li>',
' </ul>',
'</nav>',
'<div id="content" style="height: 200px; overflow-y: auto;">',
' <div id="one" style="height: 500px;"></div>',
' <div id="two" style="height: 300px;"></div>',
' <div id="three" style="height: 10px;"></div>',
'</div>'
].join('')
const contentEl = fixtureEl.querySelector('#content')
const scrollSpy = new ScrollSpy(contentEl, {
target: '#navigation',
offset: Manipulator.position(contentEl).top
})
spyOn(scrollSpy, '_process').and.callThrough()
contentEl.addEventListener('scroll', () => {
expect(fixtureEl.querySelector('#one-link')).not.toHaveClass('active')
expect(fixtureEl.querySelector('#two-link')).toHaveClass('active')
expect(fixtureEl.querySelector('#three-link')).not.toHaveClass('active')
expect(scrollSpy._process).toHaveBeenCalled()
done()
})
contentEl.scrollTop = 550
})
it('should add the active class to the correct element', done => {
fixtureEl.innerHTML = [
'<nav class="navbar">',
' <ul class="nav">',
' <li class="nav-item"><a class="nav-link" id="a-1" href="#div-1">div 1</a></li>',
' <li class="nav-item"><a class="nav-link" id="a-2" href="#div-2">div 2</a></li>',
' </ul>',
'</nav>',
'<div class="content" style="overflow: auto; height: 50px">',
' <div id="div-1" style="height: 100px; padding: 0; margin: 0">div 1</div>',
' <div id="div-2" style="height: 200px; padding: 0; margin: 0">div 2</div>',
'</div>'
].join('')
const contentEl = fixtureEl.querySelector('.content')
const scrollSpy = new ScrollSpy(contentEl, {
offset: 0,
target: '.navbar'
})
const spy = spyOn(scrollSpy, '_process').and.callThrough()
testElementIsActiveAfterScroll({
elementSelector: '#a-1',
targetSelector: '#div-1',
contentEl,
scrollSpy,
spy,
cb: () => {
testElementIsActiveAfterScroll({
elementSelector: '#a-2',
targetSelector: '#div-2',
contentEl,
scrollSpy,
spy,
cb: () => done()
})
}
}) })
}) })
it('should add the active class to the correct element (nav markup)', done => { it('should only switch "active" class on current target specified w element', () => {
fixtureEl.innerHTML = [ return new Promise(resolve => {
'<nav class="navbar">', fixtureEl.innerHTML = [
' <nav class="nav">', '<div id="root" class="active" style="display: block">',
' <a class="nav-link" id="a-1" href="#div-1">div 1</a>', ' <div class="topbar">',
' <a class="nav-link" id="a-2" href="#div-2">div 2</a>', ' <div class="topbar-inner">',
' </nav>', ' <div class="container" id="ss-target">',
'</nav>', ' <ul class="nav">',
'<div class="content" style="overflow: auto; height: 50px">', ' <li class="nav-item"><a href="#masthead">Overview</a></li>',
' <div id="div-1" style="height: 100px; padding: 0; margin: 0">div 1</div>', ' <li class="nav-item"><a href="#detail">Detail</a></li>',
' <div id="div-2" style="height: 200px; padding: 0; margin: 0">div 2</div>', ' </ul>',
'</div>' ' </div>',
].join('') ' </div>',
' </div>',
' <div id="scrollspy-example" style="height: 100px; overflow: auto;">',
' <div style="height: 200px;">',
' <h4 id="masthead">Overview</h4>',
' <p style="height: 200px;"></p>',
' </div>',
' <div style="height: 200px;">',
' <h4 id="detail">Detail</h4>',
' <p style="height: 200px;"></p>',
' </div>',
' </div>',
'</div>'
].join('')
const contentEl = fixtureEl.querySelector('.content') const scrollSpyEl = fixtureEl.querySelector('#scrollspy-example')
const scrollSpy = new ScrollSpy(contentEl, { const rootEl = fixtureEl.querySelector('#root')
offset: 0, const scrollSpy = new ScrollSpy(scrollSpyEl, {
target: '.navbar' target: fixtureEl.querySelector('#ss-target')
}) })
const spy = spyOn(scrollSpy, '_process').and.callThrough()
testElementIsActiveAfterScroll({ spyOn(scrollSpy, '_process').and.callThrough()
elementSelector: '#a-1',
targetSelector: '#div-1', scrollSpyEl.addEventListener('scroll', () => {
contentEl, expect(rootEl).toHaveClass('active')
scrollSpy, expect(scrollSpy._process).toHaveBeenCalled()
spy, resolve()
cb: () => { })
testElementIsActiveAfterScroll({
elementSelector: '#a-2', scrollSpyEl.scrollTop = 350
targetSelector: '#div-2',
contentEl,
scrollSpy,
spy,
cb: () => done()
})
}
}) })
}) })
it('should add the active class to the correct element (list-group markup)', done => { it('should correctly select middle navigation option when large offset is used', () => {
fixtureEl.innerHTML = [ return new Promise(resolve => {
'<nav class="navbar">', fixtureEl.innerHTML = [
' <div class="list-group">', '<div id="header" style="height: 500px;"></div>',
' <a class="list-group-item" id="a-1" href="#div-1">div 1</a>', '<nav id="navigation" class="navbar">',
' <a class="list-group-item" id="a-2" href="#div-2">div 2</a>', ' <ul class="navbar-nav">',
' </div>', ' <li class="nav-item"><a class="nav-link active" id="one-link" href="#one">One</a></li>',
'</nav>', ' <li class="nav-item"><a class="nav-link" id="two-link" href="#two">Two</a></li>',
'<div class="content" style="overflow: auto; height: 50px">', ' <li class="nav-item"><a class="nav-link" id="three-link" href="#three">Three</a></li>',
' <div id="div-1" style="height: 100px; padding: 0; margin: 0">div 1</div>', ' </ul>',
' <div id="div-2" style="height: 200px; padding: 0; margin: 0">div 2</div>', '</nav>',
'</div>' '<div id="content" style="height: 200px; overflow-y: auto;">',
].join('') ' <div id="one" style="height: 500px;"></div>',
' <div id="two" style="height: 300px;"></div>',
' <div id="three" style="height: 10px;"></div>',
'</div>'
].join('')
const contentEl = fixtureEl.querySelector('.content') const contentEl = fixtureEl.querySelector('#content')
const scrollSpy = new ScrollSpy(contentEl, { const scrollSpy = new ScrollSpy(contentEl, {
offset: 0, target: '#navigation',
target: '.navbar' offset: Manipulator.position(contentEl).top
}) })
const spy = spyOn(scrollSpy, '_process').and.callThrough()
testElementIsActiveAfterScroll({ spyOn(scrollSpy, '_process').and.callThrough()
elementSelector: '#a-1',
targetSelector: '#div-1', contentEl.addEventListener('scroll', () => {
contentEl, expect(fixtureEl.querySelector('#one-link')).not.toHaveClass('active')
scrollSpy, expect(fixtureEl.querySelector('#two-link')).toHaveClass('active')
spy, expect(fixtureEl.querySelector('#three-link')).not.toHaveClass('active')
cb: () => { expect(scrollSpy._process).toHaveBeenCalled()
testElementIsActiveAfterScroll({ resolve()
elementSelector: '#a-2', })
targetSelector: '#div-2',
contentEl, contentEl.scrollTop = 550
scrollSpy,
spy,
cb: () => done()
})
}
}) })
}) })
it('should clear selection if above the first section', done => { it('should add the active class to the correct element', () => {
fixtureEl.innerHTML = [ return new Promise(resolve => {
'<div id="header" style="height: 500px;"></div>', fixtureEl.innerHTML = [
'<nav id="navigation" class="navbar">', '<nav class="navbar">',
' <ul class="navbar-nav">', ' <ul class="nav">',
' <li class="nav-item"><a id="one-link" class="nav-link active" href="#one">One</a></li>', ' <li class="nav-item"><a class="nav-link" id="a-1" href="#div-1">div 1</a></li>',
' <li class="nav-item"><a id="two-link" class="nav-link" href="#two">Two</a></li>', ' <li class="nav-item"><a class="nav-link" id="a-2" href="#div-2">div 2</a></li>',
' <li class="nav-item"><a id="three-link" class="nav-link" href="#three">Three</a></li>', ' </ul>',
' </ul>', '</nav>',
'</nav>', '<div class="content" style="overflow: auto; height: 50px">',
'<div id="content" style="height: 200px; overflow-y: auto;">', ' <div id="div-1" style="height: 100px; padding: 0; margin: 0">div 1</div>',
' <div id="spacer" style="height: 100px;"></div>', ' <div id="div-2" style="height: 200px; padding: 0; margin: 0">div 2</div>',
' <div id="one" style="height: 100px;"></div>', '</div>'
' <div id="two" style="height: 100px;"></div>', ].join('')
' <div id="three" style="height: 100px;"></div>',
' <div id="spacer" style="height: 100px;"></div>',
'</div>'
].join('')
const contentEl = fixtureEl.querySelector('#content') const contentEl = fixtureEl.querySelector('.content')
const scrollSpy = new ScrollSpy(contentEl, { const scrollSpy = new ScrollSpy(contentEl, {
target: '#navigation', offset: 0,
offset: Manipulator.position(contentEl).top target: '.navbar'
})
const spy = spyOn(scrollSpy, '_process').and.callThrough()
testElementIsActiveAfterScroll({
elementSelector: '#a-1',
targetSelector: '#div-1',
contentEl,
scrollSpy,
spy,
cb: () => {
testElementIsActiveAfterScroll({
elementSelector: '#a-2',
targetSelector: '#div-2',
contentEl,
scrollSpy,
spy,
cb: resolve
})
}
})
}) })
const spy = spyOn(scrollSpy, '_process').and.callThrough()
let firstTime = true
contentEl.addEventListener('scroll', () => {
const active = fixtureEl.querySelector('.active')
expect(spy).toHaveBeenCalled()
spy.calls.reset()
if (firstTime) {
expect(fixtureEl.querySelectorAll('.active')).toHaveSize(1)
expect(active.getAttribute('id')).toEqual('two-link')
firstTime = false
contentEl.scrollTop = 0
} else {
expect(active).toBeNull()
done()
}
})
contentEl.scrollTop = 201
}) })
it('should not clear selection if above the first section and first section is at the top', done => { it('should add the active class to the correct element (nav markup)', () => {
fixtureEl.innerHTML = [ return new Promise(resolve => {
'<div id="header" style="height: 500px;"></div>', fixtureEl.innerHTML = [
'<nav id="navigation" class="navbar">', '<nav class="navbar">',
' <ul class="navbar-nav">', ' <nav class="nav">',
' <li class="nav-item"><a id="one-link" class="nav-link active" href="#one">One</a></li>', ' <a class="nav-link" id="a-1" href="#div-1">div 1</a>',
' <li class="nav-item"><a id="two-link" class="nav-link" href="#two">Two</a></li>', ' <a class="nav-link" id="a-2" href="#div-2">div 2</a>',
' <li class="nav-item"><a id="three-link" class="nav-link" href="#three">Three</a></li>', ' </nav>',
' </ul>', '</nav>',
'</nav>', '<div class="content" style="overflow: auto; height: 50px">',
'<div id="content" style="height: 200px; overflow-y: auto;">', ' <div id="div-1" style="height: 100px; padding: 0; margin: 0">div 1</div>',
' <div id="one" style="height: 100px;"></div>', ' <div id="div-2" style="height: 200px; padding: 0; margin: 0">div 2</div>',
' <div id="two" style="height: 100px;"></div>', '</div>'
' <div id="three" style="height: 100px;"></div>', ].join('')
' <div id="spacer" style="height: 100px;"></div>',
'</div>'
].join('')
const negativeHeight = -10 const contentEl = fixtureEl.querySelector('.content')
const startOfSectionTwo = 101 const scrollSpy = new ScrollSpy(contentEl, {
const contentEl = fixtureEl.querySelector('#content') offset: 0,
const scrollSpy = new ScrollSpy(contentEl, { target: '.navbar'
target: '#navigation', })
offset: contentEl.offsetTop const spy = spyOn(scrollSpy, '_process').and.callThrough()
testElementIsActiveAfterScroll({
elementSelector: '#a-1',
targetSelector: '#div-1',
contentEl,
scrollSpy,
spy,
cb: () => {
testElementIsActiveAfterScroll({
elementSelector: '#a-2',
targetSelector: '#div-2',
contentEl,
scrollSpy,
spy,
cb: resolve
})
}
})
}) })
const spy = spyOn(scrollSpy, '_process').and.callThrough()
let firstTime = true
contentEl.addEventListener('scroll', () => {
const active = fixtureEl.querySelector('.active')
expect(spy).toHaveBeenCalled()
spy.calls.reset()
if (firstTime) {
expect(fixtureEl.querySelectorAll('.active')).toHaveSize(1)
expect(active.getAttribute('id')).toEqual('two-link')
firstTime = false
contentEl.scrollTop = negativeHeight
} else {
expect(fixtureEl.querySelectorAll('.active')).toHaveSize(1)
expect(active.getAttribute('id')).toEqual('one-link')
done()
}
})
contentEl.scrollTop = startOfSectionTwo
}) })
it('should correctly select navigation element on backward scrolling when each target section height is 100%', done => { it('should add the active class to the correct element (list-group markup)', () => {
fixtureEl.innerHTML = [ return new Promise(resolve => {
'<nav class="navbar">', fixtureEl.innerHTML = [
' <ul class="nav">', '<nav class="navbar">',
' <li class="nav-item"><a id="li-100-1" class="nav-link" href="#div-100-1">div 1</a></li>', ' <div class="list-group">',
' <li class="nav-item"><a id="li-100-2" class="nav-link" href="#div-100-2">div 2</a></li>', ' <a class="list-group-item" id="a-1" href="#div-1">div 1</a>',
' <li class="nav-item"><a id="li-100-3" class="nav-link" href="#div-100-3">div 3</a></li>', ' <a class="list-group-item" id="a-2" href="#div-2">div 2</a>',
' <li class="nav-item"><a id="li-100-4" class="nav-link" href="#div-100-4">div 4</a></li>', ' </div>',
' <li class="nav-item"><a id="li-100-5" class="nav-link" href="#div-100-5">div 5</a></li>', '</nav>',
' </ul>', '<div class="content" style="overflow: auto; height: 50px">',
'</nav>', ' <div id="div-1" style="height: 100px; padding: 0; margin: 0">div 1</div>',
'<div class="content" style="position: relative; overflow: auto; height: 100px">', ' <div id="div-2" style="height: 200px; padding: 0; margin: 0">div 2</div>',
' <div id="div-100-1" style="position: relative; height: 100%; padding: 0; margin: 0">div 1</div>', '</div>'
' <div id="div-100-2" style="position: relative; height: 100%; padding: 0; margin: 0">div 2</div>', ].join('')
' <div id="div-100-3" style="position: relative; height: 100%; padding: 0; margin: 0">div 3</div>',
' <div id="div-100-4" style="position: relative; height: 100%; padding: 0; margin: 0">div 4</div>',
' <div id="div-100-5" style="position: relative; height: 100%; padding: 0; margin: 0">div 5</div>',
'</div>'
].join('')
const contentEl = fixtureEl.querySelector('.content') const contentEl = fixtureEl.querySelector('.content')
const scrollSpy = new ScrollSpy(contentEl, { const scrollSpy = new ScrollSpy(contentEl, {
offset: 0, offset: 0,
target: '.navbar' target: '.navbar'
})
const spy = spyOn(scrollSpy, '_process').and.callThrough()
testElementIsActiveAfterScroll({
elementSelector: '#a-1',
targetSelector: '#div-1',
contentEl,
scrollSpy,
spy,
cb: () => {
testElementIsActiveAfterScroll({
elementSelector: '#a-2',
targetSelector: '#div-2',
contentEl,
scrollSpy,
spy,
cb: resolve
})
}
})
}) })
const spy = spyOn(scrollSpy, '_process').and.callThrough() })
testElementIsActiveAfterScroll({ it('should clear selection if above the first section', () => {
elementSelector: '#li-100-5', return new Promise(resolve => {
targetSelector: '#div-100-5', fixtureEl.innerHTML = [
scrollSpy, '<div id="header" style="height: 500px;"></div>',
spy, '<nav id="navigation" class="navbar">',
contentEl, ' <ul class="navbar-nav">',
cb() { ' <li class="nav-item"><a id="one-link" class="nav-link active" href="#one">One</a></li>',
contentEl.scrollTop = 0 ' <li class="nav-item"><a id="two-link" class="nav-link" href="#two">Two</a></li>',
testElementIsActiveAfterScroll({ ' <li class="nav-item"><a id="three-link" class="nav-link" href="#three">Three</a></li>',
elementSelector: '#li-100-4', ' </ul>',
targetSelector: '#div-100-4', '</nav>',
scrollSpy, '<div id="content" style="height: 200px; overflow-y: auto;">',
spy, ' <div id="spacer" style="height: 100px;"></div>',
contentEl, ' <div id="one" style="height: 100px;"></div>',
cb() { ' <div id="two" style="height: 100px;"></div>',
contentEl.scrollTop = 0 ' <div id="three" style="height: 100px;"></div>',
testElementIsActiveAfterScroll({ ' <div id="spacer" style="height: 100px;"></div>',
elementSelector: '#li-100-3', '</div>'
targetSelector: '#div-100-3', ].join('')
scrollSpy,
spy, const contentEl = fixtureEl.querySelector('#content')
contentEl, const scrollSpy = new ScrollSpy(contentEl, {
cb() { target: '#navigation',
contentEl.scrollTop = 0 offset: Manipulator.position(contentEl).top
testElementIsActiveAfterScroll({ })
elementSelector: '#li-100-2', const spy = spyOn(scrollSpy, '_process').and.callThrough()
targetSelector: '#div-100-2',
scrollSpy, let firstTime = true
spy,
contentEl, contentEl.addEventListener('scroll', () => {
cb() { const active = fixtureEl.querySelector('.active')
contentEl.scrollTop = 0
testElementIsActiveAfterScroll({ expect(spy).toHaveBeenCalled()
elementSelector: '#li-100-1', spy.calls.reset()
targetSelector: '#div-100-1', if (firstTime) {
scrollSpy, expect(fixtureEl.querySelectorAll('.active')).toHaveSize(1)
spy, expect(active.getAttribute('id')).toEqual('two-link')
contentEl, firstTime = false
cb: done contentEl.scrollTop = 0
}) } else {
} expect(active).toBeNull()
}) resolve()
} }
}) })
}
}) contentEl.scrollTop = 201
} })
})
it('should not clear selection if above the first section and first section is at the top', () => {
return new Promise(resolve => {
fixtureEl.innerHTML = [
'<div id="header" style="height: 500px;"></div>',
'<nav id="navigation" class="navbar">',
' <ul class="navbar-nav">',
' <li class="nav-item"><a id="one-link" class="nav-link active" href="#one">One</a></li>',
' <li class="nav-item"><a id="two-link" class="nav-link" href="#two">Two</a></li>',
' <li class="nav-item"><a id="three-link" class="nav-link" href="#three">Three</a></li>',
' </ul>',
'</nav>',
'<div id="content" style="height: 200px; overflow-y: auto;">',
' <div id="one" style="height: 100px;"></div>',
' <div id="two" style="height: 100px;"></div>',
' <div id="three" style="height: 100px;"></div>',
' <div id="spacer" style="height: 100px;"></div>',
'</div>'
].join('')
const negativeHeight = -10
const startOfSectionTwo = 101
const contentEl = fixtureEl.querySelector('#content')
const scrollSpy = new ScrollSpy(contentEl, {
target: '#navigation',
offset: contentEl.offsetTop
})
const spy = spyOn(scrollSpy, '_process').and.callThrough()
let firstTime = true
contentEl.addEventListener('scroll', () => {
const active = fixtureEl.querySelector('.active')
expect(spy).toHaveBeenCalled()
spy.calls.reset()
if (firstTime) {
expect(fixtureEl.querySelectorAll('.active')).toHaveSize(1)
expect(active.getAttribute('id')).toEqual('two-link')
firstTime = false
contentEl.scrollTop = negativeHeight
} else {
expect(fixtureEl.querySelectorAll('.active')).toHaveSize(1)
expect(active.getAttribute('id')).toEqual('one-link')
resolve()
}
})
contentEl.scrollTop = startOfSectionTwo
})
})
it('should correctly select navigation element on backward scrolling when each target section height is 100%', () => {
return new Promise(resolve => {
fixtureEl.innerHTML = [
'<nav class="navbar">',
' <ul class="nav">',
' <li class="nav-item"><a id="li-100-1" class="nav-link" href="#div-100-1">div 1</a></li>',
' <li class="nav-item"><a id="li-100-2" class="nav-link" href="#div-100-2">div 2</a></li>',
' <li class="nav-item"><a id="li-100-3" class="nav-link" href="#div-100-3">div 3</a></li>',
' <li class="nav-item"><a id="li-100-4" class="nav-link" href="#div-100-4">div 4</a></li>',
' <li class="nav-item"><a id="li-100-5" class="nav-link" href="#div-100-5">div 5</a></li>',
' </ul>',
'</nav>',
'<div class="content" style="position: relative; overflow: auto; height: 100px">',
' <div id="div-100-1" style="position: relative; height: 100%; padding: 0; margin: 0">div 1</div>',
' <div id="div-100-2" style="position: relative; height: 100%; padding: 0; margin: 0">div 2</div>',
' <div id="div-100-3" style="position: relative; height: 100%; padding: 0; margin: 0">div 3</div>',
' <div id="div-100-4" style="position: relative; height: 100%; padding: 0; margin: 0">div 4</div>',
' <div id="div-100-5" style="position: relative; height: 100%; padding: 0; margin: 0">div 5</div>',
'</div>'
].join('')
const contentEl = fixtureEl.querySelector('.content')
const scrollSpy = new ScrollSpy(contentEl, {
offset: 0,
target: '.navbar'
})
const spy = spyOn(scrollSpy, '_process').and.callThrough()
testElementIsActiveAfterScroll({
elementSelector: '#li-100-5',
targetSelector: '#div-100-5',
scrollSpy,
spy,
contentEl,
cb() {
contentEl.scrollTop = 0
testElementIsActiveAfterScroll({
elementSelector: '#li-100-4',
targetSelector: '#div-100-4',
scrollSpy,
spy,
contentEl,
cb() {
contentEl.scrollTop = 0
testElementIsActiveAfterScroll({
elementSelector: '#li-100-3',
targetSelector: '#div-100-3',
scrollSpy,
spy,
contentEl,
cb() {
contentEl.scrollTop = 0
testElementIsActiveAfterScroll({
elementSelector: '#li-100-2',
targetSelector: '#div-100-2',
scrollSpy,
spy,
contentEl,
cb() {
contentEl.scrollTop = 0
testElementIsActiveAfterScroll({
elementSelector: '#li-100-1',
targetSelector: '#div-100-1',
scrollSpy,
spy,
contentEl,
cb: resolve
})
}
})
}
})
}
})
}
})
}) })
}) })

View File

@ -1,5 +1,5 @@
import Tab from '../../src/tab' import Tab from '../../src/tab'
import { getFixture, clearFixture, jQueryMock } from '../helpers/fixture' import { clearFixture, getFixture, jQueryMock } from '../helpers/fixture'
describe('Tab', () => { describe('Tab', () => {
let fixtureEl let fixtureEl
@ -39,314 +39,336 @@ describe('Tab', () => {
}) })
describe('show', () => { describe('show', () => {
it('should activate element by tab id (using buttons, the preferred semantic way)', done => { it('should activate element by tab id (using buttons, the preferred semantic way)', () => {
fixtureEl.innerHTML = [ return new Promise(resolve => {
'<ul class="nav" role="tablist">', fixtureEl.innerHTML = [
' <li><button type="button" data-bs-target="#home" role="tab">Home</button></li>', '<ul class="nav" role="tablist">',
' <li><button type="button" id="triggerProfile" data-bs-target="#profile" role="tab">Profile</button></li>', ' <li><button type="button" data-bs-target="#home" role="tab">Home</button></li>',
'</ul>', ' <li><button type="button" id="triggerProfile" data-bs-target="#profile" role="tab">Profile</button></li>',
'<ul>', '</ul>',
' <li id="home" role="tabpanel"></li>', '<ul>',
' <li id="profile" role="tabpanel"></li>', ' <li id="home" role="tabpanel"></li>',
'</ul>' ' <li id="profile" role="tabpanel"></li>',
].join('') '</ul>'
].join('')
const profileTriggerEl = fixtureEl.querySelector('#triggerProfile') const profileTriggerEl = fixtureEl.querySelector('#triggerProfile')
const tab = new Tab(profileTriggerEl) const tab = new Tab(profileTriggerEl)
profileTriggerEl.addEventListener('shown.bs.tab', () => { profileTriggerEl.addEventListener('shown.bs.tab', () => {
expect(fixtureEl.querySelector('#profile')).toHaveClass('active') expect(fixtureEl.querySelector('#profile')).toHaveClass('active')
expect(profileTriggerEl.getAttribute('aria-selected')).toEqual('true') expect(profileTriggerEl.getAttribute('aria-selected')).toEqual('true')
done() resolve()
})
tab.show()
}) })
tab.show()
}) })
it('should activate element by tab id (using links for tabs - not ideal, but still supported)', done => { it('should activate element by tab id (using links for tabs - not ideal, but still supported)', () => {
fixtureEl.innerHTML = [ return new Promise(resolve => {
'<ul class="nav" role="tablist">', fixtureEl.innerHTML = [
' <li><a href="#home" role="tab">Home</a></li>', '<ul class="nav" role="tablist">',
' <li><a id="triggerProfile" href="#profile" role="tab">Profile</a></li>', ' <li><a href="#home" role="tab">Home</a></li>',
'</ul>', ' <li><a id="triggerProfile" href="#profile" role="tab">Profile</a></li>',
'<ul>', '</ul>',
' <li id="home" role="tabpanel"></li>', '<ul>',
' <li id="profile" role="tabpanel"></li>', ' <li id="home" role="tabpanel"></li>',
'</ul>' ' <li id="profile" role="tabpanel"></li>',
].join('') '</ul>'
].join('')
const profileTriggerEl = fixtureEl.querySelector('#triggerProfile') const profileTriggerEl = fixtureEl.querySelector('#triggerProfile')
const tab = new Tab(profileTriggerEl) const tab = new Tab(profileTriggerEl)
profileTriggerEl.addEventListener('shown.bs.tab', () => { profileTriggerEl.addEventListener('shown.bs.tab', () => {
expect(fixtureEl.querySelector('#profile')).toHaveClass('active') expect(fixtureEl.querySelector('#profile')).toHaveClass('active')
expect(profileTriggerEl.getAttribute('aria-selected')).toEqual('true') expect(profileTriggerEl.getAttribute('aria-selected')).toEqual('true')
done() resolve()
})
tab.show()
}) })
tab.show()
}) })
it('should activate element by tab id in ordered list', done => { it('should activate element by tab id in ordered list', () => {
fixtureEl.innerHTML = [ return new Promise(resolve => {
'<ol class="nav nav-pills">', fixtureEl.innerHTML = [
' <li><button type="button" data-bs-target="#home" role="tab">Home</button></li>', '<ol class="nav nav-pills">',
' <li><button type="button" id="triggerProfile" href="#profile" role="tab">Profile</button></li>', ' <li><button type="button" data-bs-target="#home" role="tab">Home</button></li>',
'</ol>', ' <li><button type="button" id="triggerProfile" href="#profile" role="tab">Profile</button></li>',
'<ol>', '</ol>',
' <li id="home" role="tabpanel"></li>', '<ol>',
' <li id="profile" role="tabpanel"></li>', ' <li id="home" role="tabpanel"></li>',
'</ol>' ' <li id="profile" role="tabpanel"></li>',
].join('') '</ol>'
].join('')
const profileTriggerEl = fixtureEl.querySelector('#triggerProfile') const profileTriggerEl = fixtureEl.querySelector('#triggerProfile')
const tab = new Tab(profileTriggerEl) const tab = new Tab(profileTriggerEl)
profileTriggerEl.addEventListener('shown.bs.tab', () => { profileTriggerEl.addEventListener('shown.bs.tab', () => {
expect(fixtureEl.querySelector('#profile')).toHaveClass('active') expect(fixtureEl.querySelector('#profile')).toHaveClass('active')
done() resolve()
})
tab.show()
}) })
tab.show()
}) })
it('should activate element by tab id in nav list', done => { it('should activate element by tab id in nav list', () => {
fixtureEl.innerHTML = [ return new Promise(resolve => {
'<nav class="nav">', fixtureEl.innerHTML = [
' <button type="button" data-bs-target="#home" role="tab">Home</button>', '<nav class="nav">',
' <button type="button" id="triggerProfile" data-bs-target="#profile" role="tab">Profile</button>', ' <button type="button" data-bs-target="#home" role="tab">Home</button>',
'</nav>', ' <button type="button" id="triggerProfile" data-bs-target="#profile" role="tab">Profile</button>',
'<div>', '</nav>',
' <div id="home" role="tabpanel"></div>', '<div>',
' <div id="profile" role="tabpanel"></div>', ' <div id="home" role="tabpanel"></div>',
'</div>' ' <div id="profile" role="tabpanel"></div>',
].join('') '</div>'
].join('')
const profileTriggerEl = fixtureEl.querySelector('#triggerProfile') const profileTriggerEl = fixtureEl.querySelector('#triggerProfile')
const tab = new Tab(profileTriggerEl) const tab = new Tab(profileTriggerEl)
profileTriggerEl.addEventListener('shown.bs.tab', () => { profileTriggerEl.addEventListener('shown.bs.tab', () => {
expect(fixtureEl.querySelector('#profile')).toHaveClass('active') expect(fixtureEl.querySelector('#profile')).toHaveClass('active')
done() resolve()
})
tab.show()
}) })
tab.show()
}) })
it('should activate element by tab id in list group', done => { it('should activate element by tab id in list group', () => {
fixtureEl.innerHTML = [ return new Promise(resolve => {
'<div class="list-group" role="tablist">', fixtureEl.innerHTML = [
' <button type="button" data-bs-target="#home" role="tab">Home</button>', '<div class="list-group" role="tablist">',
' <button type="button" id="triggerProfile" data-bs-target="#profile" role="tab">Profile</button>', ' <button type="button" data-bs-target="#home" role="tab">Home</button>',
'</div>', ' <button type="button" id="triggerProfile" data-bs-target="#profile" role="tab">Profile</button>',
'<div>', '</div>',
' <div id="home" role="tabpanel"></div>', '<div>',
' <div id="profile" role="tabpanel"></div>', ' <div id="home" role="tabpanel"></div>',
'</div>' ' <div id="profile" role="tabpanel"></div>',
].join('') '</div>'
].join('')
const profileTriggerEl = fixtureEl.querySelector('#triggerProfile') const profileTriggerEl = fixtureEl.querySelector('#triggerProfile')
const tab = new Tab(profileTriggerEl) const tab = new Tab(profileTriggerEl)
profileTriggerEl.addEventListener('shown.bs.tab', () => { profileTriggerEl.addEventListener('shown.bs.tab', () => {
expect(fixtureEl.querySelector('#profile')).toHaveClass('active') expect(fixtureEl.querySelector('#profile')).toHaveClass('active')
done() resolve()
})
tab.show()
}) })
tab.show()
}) })
it('should not fire shown when show is prevented', done => { it('should not fire shown when show is prevented', () => {
fixtureEl.innerHTML = '<div class="nav"></div>' return new Promise(resolve => {
fixtureEl.innerHTML = '<div class="nav"></div>'
const navEl = fixtureEl.querySelector('div') const navEl = fixtureEl.querySelector('div')
const tab = new Tab(navEl) const tab = new Tab(navEl)
const expectDone = () => { const expectDone = () => {
setTimeout(() => {
expect().nothing()
resolve()
}, 30)
}
navEl.addEventListener('show.bs.tab', ev => {
ev.preventDefault()
expectDone()
})
navEl.addEventListener('shown.bs.tab', () => {
throw new Error('should not trigger shown event')
})
tab.show()
})
})
it('should not fire shown when tab is already active', () => {
return new Promise(resolve => {
fixtureEl.innerHTML = [
'<ul class="nav nav-tabs" role="tablist">',
' <li class="nav-item" role="presentation"><button type="button" data-bs-target="#home" class="nav-link active" role="tab" aria-selected="true">Home</button></li>',
' <li class="nav-item" role="presentation"><button type="button" data-bs-target="#profile" class="nav-link" role="tab">Profile</button></li>',
'</ul>',
'<div class="tab-content">',
' <div class="tab-pane active" id="home" role="tabpanel"></div>',
' <div class="tab-pane" id="profile" role="tabpanel"></div>',
'</div>'
].join('')
const triggerActive = fixtureEl.querySelector('button.active')
const tab = new Tab(triggerActive)
triggerActive.addEventListener('shown.bs.tab', () => {
throw new Error('should not trigger shown event')
})
tab.show()
setTimeout(() => { setTimeout(() => {
expect().nothing() expect().nothing()
done() resolve()
}, 30) }, 30)
}
navEl.addEventListener('show.bs.tab', ev => {
ev.preventDefault()
expectDone()
}) })
navEl.addEventListener('shown.bs.tab', () => {
throw new Error('should not trigger shown event')
})
tab.show()
}) })
it('should not fire shown when tab is already active', done => { it('show and shown events should reference correct relatedTarget', () => {
fixtureEl.innerHTML = [ return new Promise(resolve => {
'<ul class="nav nav-tabs" role="tablist">', fixtureEl.innerHTML = [
' <li class="nav-item" role="presentation"><button type="button" data-bs-target="#home" class="nav-link active" role="tab" aria-selected="true">Home</button></li>', '<ul class="nav nav-tabs" role="tablist">',
' <li class="nav-item" role="presentation"><button type="button" data-bs-target="#profile" class="nav-link" role="tab">Profile</button></li>', ' <li class="nav-item" role="presentation"><button type="button" data-bs-target="#home" class="nav-link active" role="tab" aria-selected="true">Home</button></li>',
'</ul>', ' <li class="nav-item" role="presentation"><button type="button" id="triggerProfile" data-bs-target="#profile" class="nav-link" role="tab">Profile</button></li>',
'<div class="tab-content">', '</ul>',
' <div class="tab-pane active" id="home" role="tabpanel"></div>', '<div class="tab-content">',
' <div class="tab-pane" id="profile" role="tabpanel"></div>', ' <div class="tab-pane active" id="home" role="tabpanel"></div>',
'</div>' ' <div class="tab-pane" id="profile" role="tabpanel"></div>',
].join('') '</div>'
].join('')
const triggerActive = fixtureEl.querySelector('button.active') const secondTabTrigger = fixtureEl.querySelector('#triggerProfile')
const tab = new Tab(triggerActive) const secondTab = new Tab(secondTabTrigger)
triggerActive.addEventListener('shown.bs.tab', () => { secondTabTrigger.addEventListener('show.bs.tab', ev => {
throw new Error('should not trigger shown event') expect(ev.relatedTarget.getAttribute('data-bs-target')).toEqual('#home')
}) })
tab.show() secondTabTrigger.addEventListener('shown.bs.tab', ev => {
setTimeout(() => { expect(ev.relatedTarget.getAttribute('data-bs-target')).toEqual('#home')
expect().nothing() expect(secondTabTrigger.getAttribute('aria-selected')).toEqual('true')
done() expect(fixtureEl.querySelector('button:not(.active)').getAttribute('aria-selected')).toEqual('false')
}, 30) resolve()
}) })
it('show and shown events should reference correct relatedTarget', done => {
fixtureEl.innerHTML = [
'<ul class="nav nav-tabs" role="tablist">',
' <li class="nav-item" role="presentation"><button type="button" data-bs-target="#home" class="nav-link active" role="tab" aria-selected="true">Home</button></li>',
' <li class="nav-item" role="presentation"><button type="button" id="triggerProfile" data-bs-target="#profile" class="nav-link" role="tab">Profile</button></li>',
'</ul>',
'<div class="tab-content">',
' <div class="tab-pane active" id="home" role="tabpanel"></div>',
' <div class="tab-pane" id="profile" role="tabpanel"></div>',
'</div>'
].join('')
const secondTabTrigger = fixtureEl.querySelector('#triggerProfile')
const secondTab = new Tab(secondTabTrigger)
secondTabTrigger.addEventListener('show.bs.tab', ev => {
expect(ev.relatedTarget.getAttribute('data-bs-target')).toEqual('#home')
})
secondTabTrigger.addEventListener('shown.bs.tab', ev => {
expect(ev.relatedTarget.getAttribute('data-bs-target')).toEqual('#home')
expect(secondTabTrigger.getAttribute('aria-selected')).toEqual('true')
expect(fixtureEl.querySelector('button:not(.active)').getAttribute('aria-selected')).toEqual('false')
done()
})
secondTab.show()
})
it('should fire hide and hidden events', done => {
fixtureEl.innerHTML = [
'<ul class="nav" role="tablist">',
' <li><button type="button" data-bs-target="#home" role="tab">Home</button></li>',
' <li><button type="button" data-bs-target="#profile">Profile</button></li>',
'</ul>'
].join('')
const triggerList = fixtureEl.querySelectorAll('button')
const firstTab = new Tab(triggerList[0])
const secondTab = new Tab(triggerList[1])
let hideCalled = false
triggerList[0].addEventListener('shown.bs.tab', () => {
secondTab.show() secondTab.show()
}) })
triggerList[0].addEventListener('hide.bs.tab', ev => {
hideCalled = true
expect(ev.relatedTarget.getAttribute('data-bs-target')).toEqual('#profile')
})
triggerList[0].addEventListener('hidden.bs.tab', ev => {
expect(hideCalled).toBeTrue()
expect(ev.relatedTarget.getAttribute('data-bs-target')).toEqual('#profile')
done()
})
firstTab.show()
}) })
it('should not fire hidden when hide is prevented', done => { it('should fire hide and hidden events', () => {
fixtureEl.innerHTML = [ return new Promise(resolve => {
'<ul class="nav" role="tablist">', fixtureEl.innerHTML = [
' <li><button type="button" data-bs-target="#home" role="tab">Home</button></li>', '<ul class="nav" role="tablist">',
' <li><button type="button" data-bs-target="#profile" role="tab">Profile</button></li>', ' <li><button type="button" data-bs-target="#home" role="tab">Home</button></li>',
'</ul>' ' <li><button type="button" data-bs-target="#profile">Profile</button></li>',
].join('') '</ul>'
].join('')
const triggerList = fixtureEl.querySelectorAll('button') const triggerList = fixtureEl.querySelectorAll('button')
const firstTab = new Tab(triggerList[0]) const firstTab = new Tab(triggerList[0])
const secondTab = new Tab(triggerList[1]) const secondTab = new Tab(triggerList[1])
const expectDone = () => {
setTimeout(() => {
expect().nothing()
done()
}, 30)
}
triggerList[0].addEventListener('shown.bs.tab', () => { let hideCalled = false
secondTab.show() triggerList[0].addEventListener('shown.bs.tab', () => {
secondTab.show()
})
triggerList[0].addEventListener('hide.bs.tab', ev => {
hideCalled = true
expect(ev.relatedTarget.getAttribute('data-bs-target')).toEqual('#profile')
})
triggerList[0].addEventListener('hidden.bs.tab', ev => {
expect(hideCalled).toBeTrue()
expect(ev.relatedTarget.getAttribute('data-bs-target')).toEqual('#profile')
resolve()
})
firstTab.show()
}) })
triggerList[0].addEventListener('hide.bs.tab', ev => {
ev.preventDefault()
expectDone()
})
triggerList[0].addEventListener('hidden.bs.tab', () => {
throw new Error('should not trigger hidden')
})
firstTab.show()
}) })
it('should handle removed tabs', done => { it('should not fire hidden when hide is prevented', () => {
fixtureEl.innerHTML = [ return new Promise(resolve => {
'<ul class="nav nav-tabs" role="tablist">', fixtureEl.innerHTML = [
' <li class="nav-item" role="presentation">', '<ul class="nav" role="tablist">',
' <a class="nav-link nav-tab" href="#profile" role="tab" data-bs-toggle="tab">', ' <li><button type="button" data-bs-target="#home" role="tab">Home</button></li>',
' <button class="btn-close" aria-label="Close"></button>', ' <li><button type="button" data-bs-target="#profile" role="tab">Profile</button></li>',
' </a>', '</ul>'
' </li>', ].join('')
' <li class="nav-item" role="presentation">',
' <a id="secondNav" class="nav-link nav-tab" href="#buzz" role="tab" data-bs-toggle="tab">',
' <button class="btn-close" aria-label="Close"></button>',
' </a>',
' </li>',
' <li class="nav-item" role="presentation">',
' <a class="nav-link nav-tab" href="#references" role="tab" data-bs-toggle="tab">',
' <button id="btnClose" class="btn-close" aria-label="Close"></button>',
' </a>',
' </li>',
'</ul>',
'<div class="tab-content">',
' <div role="tabpanel" class="tab-pane fade show active" id="profile">test 1</div>',
' <div role="tabpanel" class="tab-pane fade" id="buzz">test 2</div>',
' <div role="tabpanel" class="tab-pane fade" id="references">test 3</div>',
'</div>'
].join('')
const secondNavEl = fixtureEl.querySelector('#secondNav') const triggerList = fixtureEl.querySelectorAll('button')
const btnCloseEl = fixtureEl.querySelector('#btnClose') const firstTab = new Tab(triggerList[0])
const secondNavTab = new Tab(secondNavEl) const secondTab = new Tab(triggerList[1])
const expectDone = () => {
setTimeout(() => {
expect().nothing()
resolve()
}, 30)
}
secondNavEl.addEventListener('shown.bs.tab', () => { triggerList[0].addEventListener('shown.bs.tab', () => {
expect(fixtureEl.querySelectorAll('.nav-tab')).toHaveSize(2) secondTab.show()
done() })
triggerList[0].addEventListener('hide.bs.tab', ev => {
ev.preventDefault()
expectDone()
})
triggerList[0].addEventListener('hidden.bs.tab', () => {
throw new Error('should not trigger hidden')
})
firstTab.show()
}) })
})
btnCloseEl.addEventListener('click', () => { it('should handle removed tabs', () => {
const linkEl = btnCloseEl.parentNode return new Promise(resolve => {
const liEl = linkEl.parentNode fixtureEl.innerHTML = [
const tabId = linkEl.getAttribute('href') '<ul class="nav nav-tabs" role="tablist">',
const tabIdEl = fixtureEl.querySelector(tabId) ' <li class="nav-item" role="presentation">',
' <a class="nav-link nav-tab" href="#profile" role="tab" data-bs-toggle="tab">',
' <button class="btn-close" aria-label="Close"></button>',
' </a>',
' </li>',
' <li class="nav-item" role="presentation">',
' <a id="secondNav" class="nav-link nav-tab" href="#buzz" role="tab" data-bs-toggle="tab">',
' <button class="btn-close" aria-label="Close"></button>',
' </a>',
' </li>',
' <li class="nav-item" role="presentation">',
' <a class="nav-link nav-tab" href="#references" role="tab" data-bs-toggle="tab">',
' <button id="btnClose" class="btn-close" aria-label="Close"></button>',
' </a>',
' </li>',
'</ul>',
'<div class="tab-content">',
' <div role="tabpanel" class="tab-pane fade show active" id="profile">test 1</div>',
' <div role="tabpanel" class="tab-pane fade" id="buzz">test 2</div>',
' <div role="tabpanel" class="tab-pane fade" id="references">test 3</div>',
'</div>'
].join('')
liEl.remove() const secondNavEl = fixtureEl.querySelector('#secondNav')
tabIdEl.remove() const btnCloseEl = fixtureEl.querySelector('#btnClose')
secondNavTab.show() const secondNavTab = new Tab(secondNavEl)
secondNavEl.addEventListener('shown.bs.tab', () => {
expect(fixtureEl.querySelectorAll('.nav-tab')).toHaveSize(2)
resolve()
})
btnCloseEl.addEventListener('click', () => {
const linkEl = btnCloseEl.parentNode
const liEl = linkEl.parentNode
const tabId = linkEl.getAttribute('href')
const tabIdEl = fixtureEl.querySelector(tabId)
liEl.remove()
tabIdEl.remove()
secondNavTab.show()
})
btnCloseEl.click()
}) })
btnCloseEl.click()
}) })
}) })
@ -464,27 +486,29 @@ describe('Tab', () => {
}) })
describe('data-api', () => { describe('data-api', () => {
it('should create dynamically a tab', done => { it('should create dynamically a tab', () => {
fixtureEl.innerHTML = [ return new Promise(resolve => {
'<ul class="nav nav-tabs" role="tablist">', fixtureEl.innerHTML = [
' <li class="nav-item" role="presentation"><button type="button" data-bs-target="#home" class="nav-link active" role="tab" aria-selected="true">Home</button></li>', '<ul class="nav nav-tabs" role="tablist">',
' <li class="nav-item" role="presentation"><button type="button" id="triggerProfile" data-bs-toggle="tab" data-bs-target="#profile" class="nav-link" role="tab">Profile</button></li>', ' <li class="nav-item" role="presentation"><button type="button" data-bs-target="#home" class="nav-link active" role="tab" aria-selected="true">Home</button></li>',
'</ul>', ' <li class="nav-item" role="presentation"><button type="button" id="triggerProfile" data-bs-toggle="tab" data-bs-target="#profile" class="nav-link" role="tab">Profile</button></li>',
'<div class="tab-content">', '</ul>',
' <div class="tab-pane active" id="home" role="tabpanel"></div>', '<div class="tab-content">',
' <div class="tab-pane" id="profile" role="tabpanel"></div>', ' <div class="tab-pane active" id="home" role="tabpanel"></div>',
'</div>' ' <div class="tab-pane" id="profile" role="tabpanel"></div>',
].join('') '</div>'
].join('')
const secondTabTrigger = fixtureEl.querySelector('#triggerProfile') const secondTabTrigger = fixtureEl.querySelector('#triggerProfile')
secondTabTrigger.addEventListener('shown.bs.tab', () => { secondTabTrigger.addEventListener('shown.bs.tab', () => {
expect(secondTabTrigger).toHaveClass('active') expect(secondTabTrigger).toHaveClass('active')
expect(fixtureEl.querySelector('#profile')).toHaveClass('active') expect(fixtureEl.querySelector('#profile')).toHaveClass('active')
done() resolve()
})
secondTabTrigger.click()
}) })
secondTabTrigger.click()
}) })
it('selected tab should deactivate previous selected link in dropdown', () => { it('selected tab should deactivate previous selected link in dropdown', () => {
@ -567,202 +591,216 @@ describe('Tab', () => {
expect(fixtureEl.querySelector('.nav-link')).not.toHaveClass('active') expect(fixtureEl.querySelector('.nav-link')).not.toHaveClass('active')
}) })
it('should handle nested tabs', done => { it('should handle nested tabs', () => {
fixtureEl.innerHTML = [ return new Promise(resolve => {
'<nav class="nav nav-tabs" role="tablist">', fixtureEl.innerHTML = [
' <button type="button" id="tab1" data-bs-target="#x-tab1" class="nav-link" data-bs-toggle="tab" role="tab" aria-controls="x-tab1">Tab 1</button>', '<nav class="nav nav-tabs" role="tablist">',
' <button type="button" data-bs-target="#x-tab2" class="nav-link active" data-bs-toggle="tab" role="tab" aria-controls="x-tab2" aria-selected="true">Tab 2</button>', ' <button type="button" id="tab1" data-bs-target="#x-tab1" class="nav-link" data-bs-toggle="tab" role="tab" aria-controls="x-tab1">Tab 1</button>',
' <button type="button" data-bs-target="#x-tab3" class="nav-link" data-bs-toggle="tab" role="tab" aria-controls="x-tab3">Tab 3</button>', ' <button type="button" data-bs-target="#x-tab2" class="nav-link active" data-bs-toggle="tab" role="tab" aria-controls="x-tab2" aria-selected="true">Tab 2</button>',
'</nav>', ' <button type="button" data-bs-target="#x-tab3" class="nav-link" data-bs-toggle="tab" role="tab" aria-controls="x-tab3">Tab 3</button>',
'<div class="tab-content">', '</nav>',
' <div class="tab-pane" id="x-tab1" role="tabpanel">', '<div class="tab-content">',
' <nav class="nav nav-tabs" role="tablist">', ' <div class="tab-pane" id="x-tab1" role="tabpanel">',
' <button type="button" data-bs-target="#nested-tab1" class="nav-link active" data-bs-toggle="tab" role="tab" aria-controls="x-tab1" aria-selected="true">Nested Tab 1</button>', ' <nav class="nav nav-tabs" role="tablist">',
' <button type="button" id="tabNested2" data-bs-target="#nested-tab2" class="nav-link" data-bs-toggle="tab" role="tab" aria-controls="x-profile">Nested Tab2</button>', ' <button type="button" data-bs-target="#nested-tab1" class="nav-link active" data-bs-toggle="tab" role="tab" aria-controls="x-tab1" aria-selected="true">Nested Tab 1</button>',
' </nav>', ' <button type="button" id="tabNested2" data-bs-target="#nested-tab2" class="nav-link" data-bs-toggle="tab" role="tab" aria-controls="x-profile">Nested Tab2</button>',
' <div class="tab-content">', ' </nav>',
' <div class="tab-pane active" id="nested-tab1" role="tabpanel">Nested Tab1 Content</div>', ' <div class="tab-content">',
' <div class="tab-pane" id="nested-tab2" role="tabpanel">Nested Tab2 Content</div>', ' <div class="tab-pane active" id="nested-tab1" role="tabpanel">Nested Tab1 Content</div>',
' </div>', ' <div class="tab-pane" id="nested-tab2" role="tabpanel">Nested Tab2 Content</div>',
' </div>', ' </div>',
' <div class="tab-pane active" id="x-tab2" role="tabpanel">Tab2 Content</div>', ' </div>',
' <div class="tab-pane" id="x-tab3" role="tabpanel">Tab3 Content</div>', ' <div class="tab-pane active" id="x-tab2" role="tabpanel">Tab2 Content</div>',
'</div>' ' <div class="tab-pane" id="x-tab3" role="tabpanel">Tab3 Content</div>',
].join('') '</div>'
].join('')
const tab1El = fixtureEl.querySelector('#tab1') const tab1El = fixtureEl.querySelector('#tab1')
const tabNested2El = fixtureEl.querySelector('#tabNested2') const tabNested2El = fixtureEl.querySelector('#tabNested2')
const xTab1El = fixtureEl.querySelector('#x-tab1') const xTab1El = fixtureEl.querySelector('#x-tab1')
tabNested2El.addEventListener('shown.bs.tab', () => { tabNested2El.addEventListener('shown.bs.tab', () => {
expect(xTab1El).toHaveClass('active') expect(xTab1El).toHaveClass('active')
done() resolve()
})
tab1El.addEventListener('shown.bs.tab', () => {
expect(xTab1El).toHaveClass('active')
tabNested2El.click()
})
tab1El.click()
})
it('should not remove fade class if no active pane is present', done => {
fixtureEl.innerHTML = [
'<ul class="nav nav-tabs" role="tablist">',
' <li class="nav-item" role="presentation"><button type="button" id="tab-home" data-bs-target="#home" class="nav-link" data-bs-toggle="tab" role="tab">Home</button></li>',
' <li class="nav-item" role="presentation"><button type="button" id="tab-profile" data-bs-target="#profile" class="nav-link" data-bs-toggle="tab" role="tab">Profile</button></li>',
'</ul>',
'<div class="tab-content">',
' <div class="tab-pane fade" id="home" role="tabpanel"></div>',
' <div class="tab-pane fade" id="profile" role="tabpanel"></div>',
'</div>'
].join('')
const triggerTabProfileEl = fixtureEl.querySelector('#tab-profile')
const triggerTabHomeEl = fixtureEl.querySelector('#tab-home')
const tabProfileEl = fixtureEl.querySelector('#profile')
const tabHomeEl = fixtureEl.querySelector('#home')
triggerTabProfileEl.addEventListener('shown.bs.tab', () => {
expect(tabProfileEl).toHaveClass('fade')
expect(tabProfileEl).toHaveClass('show')
triggerTabHomeEl.addEventListener('shown.bs.tab', () => {
expect(tabProfileEl).toHaveClass('fade')
expect(tabProfileEl).not.toHaveClass('show')
expect(tabHomeEl).toHaveClass('fade')
expect(tabHomeEl).toHaveClass('show')
done()
}) })
triggerTabHomeEl.click() tab1El.addEventListener('shown.bs.tab', () => {
}) expect(xTab1El).toHaveClass('active')
tabNested2El.click()
})
triggerTabProfileEl.click() tab1El.click()
})
}) })
it('should not add show class to tab panes if there is no `.fade` class', done => { it('should not remove fade class if no active pane is present', () => {
fixtureEl.innerHTML = [ return new Promise(resolve => {
'<ul class="nav nav-tabs" role="tablist">', fixtureEl.innerHTML = [
' <li class="nav-item" role="presentation">', '<ul class="nav nav-tabs" role="tablist">',
' <button type="button" class="nav-link nav-tab" data-bs-target="#home" role="tab" data-bs-toggle="tab">Home</button>', ' <li class="nav-item" role="presentation"><button type="button" id="tab-home" data-bs-target="#home" class="nav-link" data-bs-toggle="tab" role="tab">Home</button></li>',
' </li>', ' <li class="nav-item" role="presentation"><button type="button" id="tab-profile" data-bs-target="#profile" class="nav-link" data-bs-toggle="tab" role="tab">Profile</button></li>',
' <li class="nav-item" role="presentation">', '</ul>',
' <button type="button" id="secondNav" class="nav-link nav-tab" data-bs-target="#profile" role="tab" data-bs-toggle="tab">Profile</button>', '<div class="tab-content">',
' </li>', ' <div class="tab-pane fade" id="home" role="tabpanel"></div>',
'</ul>', ' <div class="tab-pane fade" id="profile" role="tabpanel"></div>',
'<div class="tab-content">', '</div>'
' <div role="tabpanel" class="tab-pane" id="home">test 1</div>', ].join('')
' <div role="tabpanel" class="tab-pane" id="profile">test 2</div>',
'</div>'
].join('')
const secondNavEl = fixtureEl.querySelector('#secondNav') const triggerTabProfileEl = fixtureEl.querySelector('#tab-profile')
const triggerTabHomeEl = fixtureEl.querySelector('#tab-home')
const tabProfileEl = fixtureEl.querySelector('#profile')
const tabHomeEl = fixtureEl.querySelector('#home')
secondNavEl.addEventListener('shown.bs.tab', () => { triggerTabProfileEl.addEventListener('shown.bs.tab', () => {
expect(fixtureEl.querySelectorAll('.show')).toHaveSize(0) expect(tabProfileEl).toHaveClass('fade')
done() expect(tabProfileEl).toHaveClass('show')
triggerTabHomeEl.addEventListener('shown.bs.tab', () => {
expect(tabProfileEl).toHaveClass('fade')
expect(tabProfileEl).not.toHaveClass('show')
expect(tabHomeEl).toHaveClass('fade')
expect(tabHomeEl).toHaveClass('show')
resolve()
})
triggerTabHomeEl.click()
})
triggerTabProfileEl.click()
}) })
secondNavEl.click()
}) })
it('should add show class to tab panes if there is a `.fade` class', done => { it('should not add show class to tab panes if there is no `.fade` class', () => {
fixtureEl.innerHTML = [ return new Promise(resolve => {
'<ul class="nav nav-tabs" role="tablist">', fixtureEl.innerHTML = [
' <li class="nav-item" role="presentation">', '<ul class="nav nav-tabs" role="tablist">',
' <button type="button" class="nav-link nav-tab" data-bs-target="#home" role="tab" data-bs-toggle="tab">Home</button>', ' <li class="nav-item" role="presentation">',
' </li>', ' <button type="button" class="nav-link nav-tab" data-bs-target="#home" role="tab" data-bs-toggle="tab">Home</button>',
' <li class="nav-item" role="presentation">', ' </li>',
' <button type="button" id="secondNav" class="nav-link nav-tab" data-bs-target="#profile" role="tab" data-bs-toggle="tab">Profile</button>', ' <li class="nav-item" role="presentation">',
' </li>', ' <button type="button" id="secondNav" class="nav-link nav-tab" data-bs-target="#profile" role="tab" data-bs-toggle="tab">Profile</button>',
'</ul>', ' </li>',
'<div class="tab-content">', '</ul>',
' <div role="tabpanel" class="tab-pane fade" id="home">test 1</div>', '<div class="tab-content">',
' <div role="tabpanel" class="tab-pane fade" id="profile">test 2</div>', ' <div role="tabpanel" class="tab-pane" id="home">test 1</div>',
'</div>' ' <div role="tabpanel" class="tab-pane" id="profile">test 2</div>',
].join('') '</div>'
].join('')
const secondNavEl = fixtureEl.querySelector('#secondNav') const secondNavEl = fixtureEl.querySelector('#secondNav')
secondNavEl.addEventListener('shown.bs.tab', () => { secondNavEl.addEventListener('shown.bs.tab', () => {
expect(fixtureEl.querySelectorAll('.show')).toHaveSize(1) expect(fixtureEl.querySelectorAll('.show')).toHaveSize(0)
done() resolve()
})
secondNavEl.click()
}) })
secondNavEl.click()
}) })
it('should prevent default when the trigger is <a> or <area>', done => { it('should add show class to tab panes if there is a `.fade` class', () => {
fixtureEl.innerHTML = [ return new Promise(resolve => {
'<ul class="nav" role="tablist">', fixtureEl.innerHTML = [
' <li><a type="button" href="#test" class="active" role="tab" data-bs-toggle="tab">Home</a></li>', '<ul class="nav nav-tabs" role="tablist">',
' <li><a type="button" href="#test2" role="tab" data-bs-toggle="tab">Home</a></li>', ' <li class="nav-item" role="presentation">',
'</ul>' ' <button type="button" class="nav-link nav-tab" data-bs-target="#home" role="tab" data-bs-toggle="tab">Home</button>',
].join('') ' </li>',
' <li class="nav-item" role="presentation">',
' <button type="button" id="secondNav" class="nav-link nav-tab" data-bs-target="#profile" role="tab" data-bs-toggle="tab">Profile</button>',
' </li>',
'</ul>',
'<div class="tab-content">',
' <div role="tabpanel" class="tab-pane fade" id="home">test 1</div>',
' <div role="tabpanel" class="tab-pane fade" id="profile">test 2</div>',
'</div>'
].join('')
const tabEl = fixtureEl.querySelector('[href="#test2"]') const secondNavEl = fixtureEl.querySelector('#secondNav')
spyOn(Event.prototype, 'preventDefault').and.callThrough()
tabEl.addEventListener('shown.bs.tab', () => { secondNavEl.addEventListener('shown.bs.tab', () => {
expect(tabEl).toHaveClass('active') expect(fixtureEl.querySelectorAll('.show')).toHaveSize(1)
expect(Event.prototype.preventDefault).toHaveBeenCalled() resolve()
done() })
secondNavEl.click()
}) })
tabEl.click()
}) })
it('should not fire shown when tab has disabled attribute', done => { it('should prevent default when the trigger is <a> or <area>', () => {
fixtureEl.innerHTML = [ return new Promise(resolve => {
'<ul class="nav nav-tabs" role="tablist">', fixtureEl.innerHTML = [
' <li class="nav-item" role="presentation"><button type="button" data-bs-target="#home" class="nav-link active" role="tab" aria-selected="true">Home</button></li>', '<ul class="nav" role="tablist">',
' <li class="nav-item" role="presentation"><button type="button" data-bs-target="#profile" class="nav-link" disabled role="tab">Profile</button></li>', ' <li><a type="button" href="#test" class="active" role="tab" data-bs-toggle="tab">Home</a></li>',
'</ul>', ' <li><a type="button" href="#test2" role="tab" data-bs-toggle="tab">Home</a></li>',
'<div class="tab-content">', '</ul>'
' <div class="tab-pane active" id="home" role="tabpanel"></div>', ].join('')
' <div class="tab-pane" id="profile" role="tabpanel"></div>',
'</div>'
].join('')
const triggerDisabled = fixtureEl.querySelector('button[disabled]') const tabEl = fixtureEl.querySelector('[href="#test2"]')
triggerDisabled.addEventListener('shown.bs.tab', () => { spyOn(Event.prototype, 'preventDefault').and.callThrough()
throw new Error('should not trigger shown event')
tabEl.addEventListener('shown.bs.tab', () => {
expect(tabEl).toHaveClass('active')
expect(Event.prototype.preventDefault).toHaveBeenCalled()
resolve()
})
tabEl.click()
}) })
triggerDisabled.click()
setTimeout(() => {
expect().nothing()
done()
}, 30)
}) })
it('should not fire shown when tab has disabled class', done => { it('should not fire shown when tab has disabled attribute', () => {
fixtureEl.innerHTML = [ return new Promise(resolve => {
'<ul class="nav nav-tabs" role="tablist">', fixtureEl.innerHTML = [
' <li class="nav-item" role="presentation"><a href="#home" class="nav-link active" role="tab" aria-selected="true">Home</a></li>', '<ul class="nav nav-tabs" role="tablist">',
' <li class="nav-item" role="presentation"><a href="#profile" class="nav-link disabled" role="tab">Profile</a></li>', ' <li class="nav-item" role="presentation"><button type="button" data-bs-target="#home" class="nav-link active" role="tab" aria-selected="true">Home</button></li>',
'</ul>', ' <li class="nav-item" role="presentation"><button type="button" data-bs-target="#profile" class="nav-link" disabled role="tab">Profile</button></li>',
'<div class="tab-content">', '</ul>',
' <div class="tab-pane active" id="home" role="tabpanel"></div>', '<div class="tab-content">',
' <div class="tab-pane" id="profile" role="tabpanel"></div>', ' <div class="tab-pane active" id="home" role="tabpanel"></div>',
'</div>' ' <div class="tab-pane" id="profile" role="tabpanel"></div>',
].join('') '</div>'
].join('')
const triggerDisabled = fixtureEl.querySelector('a.disabled') const triggerDisabled = fixtureEl.querySelector('button[disabled]')
triggerDisabled.addEventListener('shown.bs.tab', () => {
throw new Error('should not trigger shown event')
})
triggerDisabled.addEventListener('shown.bs.tab', () => { triggerDisabled.click()
throw new Error('should not trigger shown event') setTimeout(() => {
expect().nothing()
resolve()
}, 30)
}) })
})
triggerDisabled.click() it('should not fire shown when tab has disabled class', () => {
setTimeout(() => { return new Promise(resolve => {
expect().nothing() fixtureEl.innerHTML = [
done() '<ul class="nav nav-tabs" role="tablist">',
}, 30) ' <li class="nav-item" role="presentation"><a href="#home" class="nav-link active" role="tab" aria-selected="true">Home</a></li>',
' <li class="nav-item" role="presentation"><a href="#profile" class="nav-link disabled" role="tab">Profile</a></li>',
'</ul>',
'<div class="tab-content">',
' <div class="tab-pane active" id="home" role="tabpanel"></div>',
' <div class="tab-pane" id="profile" role="tabpanel"></div>',
'</div>'
].join('')
const triggerDisabled = fixtureEl.querySelector('a.disabled')
triggerDisabled.addEventListener('shown.bs.tab', () => {
throw new Error('should not trigger shown event')
})
triggerDisabled.click()
setTimeout(() => {
expect().nothing()
resolve()
}, 30)
})
}) })
}) })
}) })

View File

@ -1,5 +1,5 @@
import Toast from '../../src/toast' import Toast from '../../src/toast'
import { getFixture, clearFixture, createEvent, jQueryMock } from '../helpers/fixture' import { clearFixture, createEvent, getFixture, jQueryMock } from '../helpers/fixture'
describe('Toast', () => { describe('Toast', () => {
let fixtureEl let fixtureEl
@ -36,52 +36,56 @@ describe('Toast', () => {
expect(toastByElement._element).toEqual(toastEl) expect(toastByElement._element).toEqual(toastEl)
}) })
it('should allow to config in js', done => { it('should allow to config in js', () => {
fixtureEl.innerHTML = [ return new Promise(resolve => {
'<div class="toast">', fixtureEl.innerHTML = [
' <div class="toast-body">', '<div class="toast">',
' a simple toast', ' <div class="toast-body">',
' </div>', ' a simple toast',
'</div>' ' </div>',
].join('') '</div>'
].join('')
const toastEl = fixtureEl.querySelector('div') const toastEl = fixtureEl.querySelector('div')
const toast = new Toast(toastEl, { const toast = new Toast(toastEl, {
delay: 1 delay: 1
})
toastEl.addEventListener('shown.bs.toast', () => {
expect(toastEl).toHaveClass('show')
resolve()
})
toast.show()
}) })
toastEl.addEventListener('shown.bs.toast', () => {
expect(toastEl).toHaveClass('show')
done()
})
toast.show()
}) })
it('should close toast when close element with data-bs-dismiss attribute is set', done => { it('should close toast when close element with data-bs-dismiss attribute is set', () => {
fixtureEl.innerHTML = [ return new Promise(resolve => {
'<div class="toast" data-bs-delay="1" data-bs-autohide="false" data-bs-animation="false">', fixtureEl.innerHTML = [
' <button type="button" class="ms-2 mb-1 btn-close" data-bs-dismiss="toast" aria-label="Close"></button>', '<div class="toast" data-bs-delay="1" data-bs-autohide="false" data-bs-animation="false">',
'</div>' ' <button type="button" class="ms-2 mb-1 btn-close" data-bs-dismiss="toast" aria-label="Close"></button>',
].join('') '</div>'
].join('')
const toastEl = fixtureEl.querySelector('div') const toastEl = fixtureEl.querySelector('div')
const toast = new Toast(toastEl) const toast = new Toast(toastEl)
toastEl.addEventListener('shown.bs.toast', () => { toastEl.addEventListener('shown.bs.toast', () => {
expect(toastEl).toHaveClass('show') expect(toastEl).toHaveClass('show')
const button = toastEl.querySelector('.btn-close') const button = toastEl.querySelector('.btn-close')
button.click() button.click()
})
toastEl.addEventListener('hidden.bs.toast', () => {
expect(toastEl).not.toHaveClass('show')
resolve()
})
toast.show()
}) })
toastEl.addEventListener('hidden.bs.toast', () => {
expect(toastEl).not.toHaveClass('show')
done()
})
toast.show()
}) })
}) })
@ -111,304 +115,324 @@ describe('Toast', () => {
}) })
describe('show', () => { describe('show', () => {
it('should auto hide', done => { it('should auto hide', () => {
fixtureEl.innerHTML = [ return new Promise(resolve => {
'<div class="toast" data-bs-delay="1">', fixtureEl.innerHTML = [
' <div class="toast-body">', '<div class="toast" data-bs-delay="1">',
' a simple toast', ' <div class="toast-body">',
' </div>', ' a simple toast',
'</div>' ' </div>',
].join('') '</div>'
].join('')
const toastEl = fixtureEl.querySelector('.toast') const toastEl = fixtureEl.querySelector('.toast')
const toast = new Toast(toastEl) const toast = new Toast(toastEl)
toastEl.addEventListener('hidden.bs.toast', () => { toastEl.addEventListener('hidden.bs.toast', () => {
expect(toastEl).not.toHaveClass('show')
done()
})
toast.show()
})
it('should not add fade class', done => {
fixtureEl.innerHTML = [
'<div class="toast" data-bs-delay="1" data-bs-animation="false">',
' <div class="toast-body">',
' a simple toast',
' </div>',
'</div>'
].join('')
const toastEl = fixtureEl.querySelector('.toast')
const toast = new Toast(toastEl)
toastEl.addEventListener('shown.bs.toast', () => {
expect(toastEl).not.toHaveClass('fade')
done()
})
toast.show()
})
it('should not trigger shown if show is prevented', done => {
fixtureEl.innerHTML = [
'<div class="toast" data-bs-delay="1" data-bs-animation="false">',
' <div class="toast-body">',
' a simple toast',
' </div>',
'</div>'
].join('')
const toastEl = fixtureEl.querySelector('.toast')
const toast = new Toast(toastEl)
const assertDone = () => {
setTimeout(() => {
expect(toastEl).not.toHaveClass('show') expect(toastEl).not.toHaveClass('show')
done() resolve()
}, 20)
}
toastEl.addEventListener('show.bs.toast', event => {
event.preventDefault()
assertDone()
})
toastEl.addEventListener('shown.bs.toast', () => {
throw new Error('shown event should not be triggered if show is prevented')
})
toast.show()
})
it('should clear timeout if toast is shown again before it is hidden', done => {
fixtureEl.innerHTML = [
'<div class="toast">',
' <div class="toast-body">',
' a simple toast',
' </div>',
'</div>'
].join('')
const toastEl = fixtureEl.querySelector('.toast')
const toast = new Toast(toastEl)
setTimeout(() => {
toast._config.autohide = false
toastEl.addEventListener('shown.bs.toast', () => {
expect(toast._clearTimeout).toHaveBeenCalled()
expect(toast._timeout).toBeNull()
done()
}) })
toast.show() toast.show()
}, toast._config.delay / 2) })
spyOn(toast, '_clearTimeout').and.callThrough()
toast.show()
}) })
it('should clear timeout if toast is interacted with mouse', done => { it('should not add fade class', () => {
fixtureEl.innerHTML = [ return new Promise(resolve => {
'<div class="toast">', fixtureEl.innerHTML = [
' <div class="toast-body">', '<div class="toast" data-bs-delay="1" data-bs-animation="false">',
' a simple toast', ' <div class="toast-body">',
' </div>', ' a simple toast',
'</div>' ' </div>',
].join('') '</div>'
].join('')
const toastEl = fixtureEl.querySelector('.toast') const toastEl = fixtureEl.querySelector('.toast')
const toast = new Toast(toastEl) const toast = new Toast(toastEl)
const spy = spyOn(toast, '_clearTimeout').and.callThrough()
setTimeout(() => { toastEl.addEventListener('shown.bs.toast', () => {
spy.calls.reset() expect(toastEl).not.toHaveClass('fade')
resolve()
toastEl.addEventListener('mouseover', () => {
expect(toast._clearTimeout).toHaveBeenCalledTimes(1)
expect(toast._timeout).toBeNull()
done()
}) })
const mouseOverEvent = createEvent('mouseover') toast.show()
toastEl.dispatchEvent(mouseOverEvent) })
}, toast._config.delay / 2)
toast.show()
}) })
it('should clear timeout if toast is interacted with keyboard', done => { it('should not trigger shown if show is prevented', () => {
fixtureEl.innerHTML = [ return new Promise(resolve => {
'<button id="outside-focusable">outside focusable</button>', fixtureEl.innerHTML = [
'<div class="toast">', '<div class="toast" data-bs-delay="1" data-bs-animation="false">',
' <div class="toast-body">', ' <div class="toast-body">',
' a simple toast', ' a simple toast',
' <button>with a button</button>', ' </div>',
' </div>', '</div>'
'</div>' ].join('')
].join('')
const toastEl = fixtureEl.querySelector('.toast') const toastEl = fixtureEl.querySelector('.toast')
const toast = new Toast(toastEl) const toast = new Toast(toastEl)
const spy = spyOn(toast, '_clearTimeout').and.callThrough()
setTimeout(() => { const assertDone = () => {
spy.calls.reset() setTimeout(() => {
expect(toastEl).not.toHaveClass('show')
resolve()
}, 20)
}
toastEl.addEventListener('focusin', () => { toastEl.addEventListener('show.bs.toast', event => {
expect(toast._clearTimeout).toHaveBeenCalledTimes(1) event.preventDefault()
expect(toast._timeout).toBeNull() assertDone()
done()
}) })
const insideFocusable = toastEl.querySelector('button') toastEl.addEventListener('shown.bs.toast', () => {
insideFocusable.focus() throw new Error('shown event should not be triggered if show is prevented')
}, toast._config.delay / 2) })
toast.show() toast.show()
})
}) })
it('should still auto hide after being interacted with mouse and keyboard', done => { it('should clear timeout if toast is shown again before it is hidden', () => {
fixtureEl.innerHTML = [ return new Promise(resolve => {
'<button id="outside-focusable">outside focusable</button>', fixtureEl.innerHTML = [
'<div class="toast">', '<div class="toast">',
' <div class="toast-body">', ' <div class="toast-body">',
' a simple toast', ' a simple toast',
' <button>with a button</button>', ' </div>',
' </div>', '</div>'
'</div>' ].join('')
].join('')
const toastEl = fixtureEl.querySelector('.toast') const toastEl = fixtureEl.querySelector('.toast')
const toast = new Toast(toastEl) const toast = new Toast(toastEl)
setTimeout(() => {
toast._config.autohide = false
toastEl.addEventListener('shown.bs.toast', () => {
expect(toast._clearTimeout).toHaveBeenCalled()
expect(toast._timeout).toBeNull()
resolve()
})
toast.show()
}, toast._config.delay / 2)
spyOn(toast, '_clearTimeout').and.callThrough()
toast.show()
})
})
it('should clear timeout if toast is interacted with mouse', () => {
return new Promise(resolve => {
fixtureEl.innerHTML = [
'<div class="toast">',
' <div class="toast-body">',
' a simple toast',
' </div>',
'</div>'
].join('')
const toastEl = fixtureEl.querySelector('.toast')
const toast = new Toast(toastEl)
const spy = spyOn(toast, '_clearTimeout').and.callThrough()
setTimeout(() => {
spy.calls.reset()
toastEl.addEventListener('mouseover', () => {
expect(toast._clearTimeout).toHaveBeenCalledTimes(1)
expect(toast._timeout).toBeNull()
resolve()
})
const mouseOverEvent = createEvent('mouseover')
toastEl.dispatchEvent(mouseOverEvent)
}, toast._config.delay / 2)
toast.show()
})
})
it('should clear timeout if toast is interacted with keyboard', () => {
return new Promise(resolve => {
fixtureEl.innerHTML = [
'<button id="outside-focusable">outside focusable</button>',
'<div class="toast">',
' <div class="toast-body">',
' a simple toast',
' <button>with a button</button>',
' </div>',
'</div>'
].join('')
const toastEl = fixtureEl.querySelector('.toast')
const toast = new Toast(toastEl)
const spy = spyOn(toast, '_clearTimeout').and.callThrough()
setTimeout(() => {
spy.calls.reset()
toastEl.addEventListener('focusin', () => {
expect(toast._clearTimeout).toHaveBeenCalledTimes(1)
expect(toast._timeout).toBeNull()
resolve()
})
setTimeout(() => {
toastEl.addEventListener('mouseover', () => {
const insideFocusable = toastEl.querySelector('button') const insideFocusable = toastEl.querySelector('button')
insideFocusable.focus() insideFocusable.focus()
}) }, toast._config.delay / 2)
toastEl.addEventListener('focusin', () => { toast.show()
const mouseOutEvent = createEvent('mouseout') })
toastEl.dispatchEvent(mouseOutEvent)
})
toastEl.addEventListener('mouseout', () => {
const outsideFocusable = document.getElementById('outside-focusable')
outsideFocusable.focus()
})
toastEl.addEventListener('focusout', () => {
expect(toast._timeout).not.toBeNull()
done()
})
const mouseOverEvent = createEvent('mouseover')
toastEl.dispatchEvent(mouseOverEvent)
}, toast._config.delay / 2)
toast.show()
}) })
it('should not auto hide if focus leaves but mouse pointer remains inside', done => { it('should still auto hide after being interacted with mouse and keyboard', () => {
fixtureEl.innerHTML = [ return new Promise(resolve => {
'<button id="outside-focusable">outside focusable</button>', fixtureEl.innerHTML = [
'<div class="toast">', '<button id="outside-focusable">outside focusable</button>',
' <div class="toast-body">', '<div class="toast">',
' a simple toast', ' <div class="toast-body">',
' <button>with a button</button>', ' a simple toast',
' </div>', ' <button>with a button</button>',
'</div>' ' </div>',
].join('') '</div>'
].join('')
const toastEl = fixtureEl.querySelector('.toast') const toastEl = fixtureEl.querySelector('.toast')
const toast = new Toast(toastEl) const toast = new Toast(toastEl)
setTimeout(() => { setTimeout(() => {
toastEl.addEventListener('mouseover', () => { toastEl.addEventListener('mouseover', () => {
const insideFocusable = toastEl.querySelector('button') const insideFocusable = toastEl.querySelector('button')
insideFocusable.focus() insideFocusable.focus()
}) })
toastEl.addEventListener('focusin', () => { toastEl.addEventListener('focusin', () => {
const outsideFocusable = document.getElementById('outside-focusable') const mouseOutEvent = createEvent('mouseout')
outsideFocusable.focus() toastEl.dispatchEvent(mouseOutEvent)
}) })
toastEl.addEventListener('focusout', () => { toastEl.addEventListener('mouseout', () => {
expect(toast._timeout).toBeNull() const outsideFocusable = document.getElementById('outside-focusable')
done() outsideFocusable.focus()
}) })
const mouseOverEvent = createEvent('mouseover') toastEl.addEventListener('focusout', () => {
toastEl.dispatchEvent(mouseOverEvent) expect(toast._timeout).not.toBeNull()
}, toast._config.delay / 2) resolve()
})
toast.show() const mouseOverEvent = createEvent('mouseover')
toastEl.dispatchEvent(mouseOverEvent)
}, toast._config.delay / 2)
toast.show()
})
}) })
it('should not auto hide if mouse pointer leaves but focus remains inside', done => { it('should not auto hide if focus leaves but mouse pointer remains inside', () => {
fixtureEl.innerHTML = [ return new Promise(resolve => {
'<button id="outside-focusable">outside focusable</button>', fixtureEl.innerHTML = [
'<div class="toast">', '<button id="outside-focusable">outside focusable</button>',
' <div class="toast-body">', '<div class="toast">',
' a simple toast', ' <div class="toast-body">',
' <button>with a button</button>', ' a simple toast',
' </div>', ' <button>with a button</button>',
'</div>' ' </div>',
].join('') '</div>'
].join('')
const toastEl = fixtureEl.querySelector('.toast') const toastEl = fixtureEl.querySelector('.toast')
const toast = new Toast(toastEl) const toast = new Toast(toastEl)
setTimeout(() => { setTimeout(() => {
toastEl.addEventListener('mouseover', () => { toastEl.addEventListener('mouseover', () => {
const insideFocusable = toastEl.querySelector('button') const insideFocusable = toastEl.querySelector('button')
insideFocusable.focus() insideFocusable.focus()
}) })
toastEl.addEventListener('focusin', () => { toastEl.addEventListener('focusin', () => {
const mouseOutEvent = createEvent('mouseout') const outsideFocusable = document.getElementById('outside-focusable')
toastEl.dispatchEvent(mouseOutEvent) outsideFocusable.focus()
}) })
toastEl.addEventListener('mouseout', () => { toastEl.addEventListener('focusout', () => {
expect(toast._timeout).toBeNull() expect(toast._timeout).toBeNull()
done() resolve()
}) })
const mouseOverEvent = createEvent('mouseover') const mouseOverEvent = createEvent('mouseover')
toastEl.dispatchEvent(mouseOverEvent) toastEl.dispatchEvent(mouseOverEvent)
}, toast._config.delay / 2) }, toast._config.delay / 2)
toast.show() toast.show()
})
})
it('should not auto hide if mouse pointer leaves but focus remains inside', () => {
return new Promise(resolve => {
fixtureEl.innerHTML = [
'<button id="outside-focusable">outside focusable</button>',
'<div class="toast">',
' <div class="toast-body">',
' a simple toast',
' <button>with a button</button>',
' </div>',
'</div>'
].join('')
const toastEl = fixtureEl.querySelector('.toast')
const toast = new Toast(toastEl)
setTimeout(() => {
toastEl.addEventListener('mouseover', () => {
const insideFocusable = toastEl.querySelector('button')
insideFocusable.focus()
})
toastEl.addEventListener('focusin', () => {
const mouseOutEvent = createEvent('mouseout')
toastEl.dispatchEvent(mouseOutEvent)
})
toastEl.addEventListener('mouseout', () => {
expect(toast._timeout).toBeNull()
resolve()
})
const mouseOverEvent = createEvent('mouseover')
toastEl.dispatchEvent(mouseOverEvent)
}, toast._config.delay / 2)
toast.show()
})
}) })
}) })
describe('hide', () => { describe('hide', () => {
it('should allow to hide toast manually', done => { it('should allow to hide toast manually', () => {
fixtureEl.innerHTML = [ return new Promise(resolve => {
'<div class="toast" data-bs-delay="1" data-bs-autohide="false">', fixtureEl.innerHTML = [
' <div class="toast-body">', '<div class="toast" data-bs-delay="1" data-bs-autohide="false">',
' a simple toast', ' <div class="toast-body">',
' </div>', ' a simple toast',
'</div>' ' </div>',
].join('') '</div>'
].join('')
const toastEl = fixtureEl.querySelector('.toast') const toastEl = fixtureEl.querySelector('.toast')
const toast = new Toast(toastEl) const toast = new Toast(toastEl)
toastEl.addEventListener('shown.bs.toast', () => { toastEl.addEventListener('shown.bs.toast', () => {
toast.hide() toast.hide()
})
toastEl.addEventListener('hidden.bs.toast', () => {
expect(toastEl).not.toHaveClass('show')
resolve()
})
toast.show()
}) })
toastEl.addEventListener('hidden.bs.toast', () => {
expect(toastEl).not.toHaveClass('show')
done()
})
toast.show()
}) })
it('should do nothing when we call hide on a non shown toast', () => { it('should do nothing when we call hide on a non shown toast', () => {
@ -424,39 +448,41 @@ describe('Toast', () => {
expect(toastEl.classList.contains).toHaveBeenCalled() expect(toastEl.classList.contains).toHaveBeenCalled()
}) })
it('should not trigger hidden if hide is prevented', done => { it('should not trigger hidden if hide is prevented', () => {
fixtureEl.innerHTML = [ return new Promise(resolve => {
'<div class="toast" data-bs-delay="1" data-bs-animation="false">', fixtureEl.innerHTML = [
' <div class="toast-body">', '<div class="toast" data-bs-delay="1" data-bs-animation="false">',
' a simple toast', ' <div class="toast-body">',
' </div>', ' a simple toast',
'</div>' ' </div>',
].join('') '</div>'
].join('')
const toastEl = fixtureEl.querySelector('.toast') const toastEl = fixtureEl.querySelector('.toast')
const toast = new Toast(toastEl) const toast = new Toast(toastEl)
const assertDone = () => { const assertDone = () => {
setTimeout(() => { setTimeout(() => {
expect(toastEl).toHaveClass('show') expect(toastEl).toHaveClass('show')
done() resolve()
}, 20) }, 20)
} }
toastEl.addEventListener('shown.bs.toast', () => { toastEl.addEventListener('shown.bs.toast', () => {
toast.hide() toast.hide()
})
toastEl.addEventListener('hide.bs.toast', event => {
event.preventDefault()
assertDone()
})
toastEl.addEventListener('hidden.bs.toast', () => {
throw new Error('hidden event should not be triggered if hide is prevented')
})
toast.show()
}) })
toastEl.addEventListener('hide.bs.toast', event => {
event.preventDefault()
assertDone()
})
toastEl.addEventListener('hidden.bs.toast', () => {
throw new Error('hidden event should not be triggered if hide is prevented')
})
toast.show()
}) })
}) })
@ -475,34 +501,36 @@ describe('Toast', () => {
expect(Toast.getInstance(toastEl)).toBeNull() expect(Toast.getInstance(toastEl)).toBeNull()
}) })
it('should allow to destroy toast and hide it before that', done => { it('should allow to destroy toast and hide it before that', () => {
fixtureEl.innerHTML = [ return new Promise(resolve => {
'<div class="toast" data-bs-delay="0" data-bs-autohide="false">', fixtureEl.innerHTML = [
' <div class="toast-body">', '<div class="toast" data-bs-delay="0" data-bs-autohide="false">',
' a simple toast', ' <div class="toast-body">',
' </div>', ' a simple toast',
'</div>' ' </div>',
].join('') '</div>'
].join('')
const toastEl = fixtureEl.querySelector('div') const toastEl = fixtureEl.querySelector('div')
const toast = new Toast(toastEl) const toast = new Toast(toastEl)
const expected = () => { const expected = () => {
expect(toastEl).toHaveClass('show') expect(toastEl).toHaveClass('show')
expect(Toast.getInstance(toastEl)).not.toBeNull() expect(Toast.getInstance(toastEl)).not.toBeNull()
toast.dispose() toast.dispose()
expect(Toast.getInstance(toastEl)).toBeNull() expect(Toast.getInstance(toastEl)).toBeNull()
expect(toastEl).not.toHaveClass('show') expect(toastEl).not.toHaveClass('show')
done() resolve()
} }
toastEl.addEventListener('shown.bs.toast', () => { toastEl.addEventListener('shown.bs.toast', () => {
setTimeout(expected, 1) setTimeout(expected, 1)
})
toast.show()
}) })
toast.show()
}) })
}) })

View File

@ -94,52 +94,56 @@ describe('Tooltip', () => {
expect(tooltip._config.content).toEqual('7') expect(tooltip._config.content).toEqual('7')
}) })
it('should enable selector delegation', done => { it('should enable selector delegation', () => {
fixtureEl.innerHTML = '<div></div>' return new Promise(resolve => {
fixtureEl.innerHTML = '<div></div>'
const containerEl = fixtureEl.querySelector('div') const containerEl = fixtureEl.querySelector('div')
const tooltipContainer = new Tooltip(containerEl, { const tooltipContainer = new Tooltip(containerEl, {
selector: 'a[rel="tooltip"]', selector: 'a[rel="tooltip"]',
trigger: 'click' trigger: 'click'
})
containerEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
const tooltipInContainerEl = containerEl.querySelector('a')
tooltipInContainerEl.addEventListener('shown.bs.tooltip', () => {
expect(document.querySelector('.tooltip')).not.toBeNull()
tooltipContainer.dispose()
resolve()
})
tooltipInContainerEl.click()
}) })
containerEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
const tooltipInContainerEl = containerEl.querySelector('a')
tooltipInContainerEl.addEventListener('shown.bs.tooltip', () => {
expect(document.querySelector('.tooltip')).not.toBeNull()
tooltipContainer.dispose()
done()
})
tooltipInContainerEl.click()
}) })
it('should create offset modifier when offset is passed as a function', done => { it('should create offset modifier when offset is passed as a function', () => {
fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Offset from function">' return new Promise(resolve => {
fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Offset from function">'
const getOffset = jasmine.createSpy('getOffset').and.returnValue([10, 20]) const getOffset = jasmine.createSpy('getOffset').and.returnValue([10, 20])
const tooltipEl = fixtureEl.querySelector('a') const tooltipEl = fixtureEl.querySelector('a')
const tooltip = new Tooltip(tooltipEl, { const tooltip = new Tooltip(tooltipEl, {
offset: getOffset, offset: getOffset,
popperConfig: { popperConfig: {
onFirstUpdate: state => { onFirstUpdate: state => {
expect(getOffset).toHaveBeenCalledWith({ expect(getOffset).toHaveBeenCalledWith({
popper: state.rects.popper, popper: state.rects.popper,
reference: state.rects.reference, reference: state.rects.reference,
placement: state.placement placement: state.placement
}, tooltipEl) }, tooltipEl)
done() resolve()
}
} }
} })
const offset = tooltip._getOffset()
expect(offset).toEqual(jasmine.any(Function))
tooltip.show()
}) })
const offset = tooltip._getOffset()
expect(offset).toEqual(jasmine.any(Function))
tooltip.show()
}) })
it('should create offset modifier when offset option is passed in data attribute', () => { it('should create offset modifier when offset option is passed in data attribute', () => {
@ -192,42 +196,46 @@ describe('Tooltip', () => {
}) })
describe('enable', () => { describe('enable', () => {
it('should enable a tooltip', done => { it('should enable a tooltip', () => {
fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">' return new Promise(resolve => {
fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
const tooltipEl = fixtureEl.querySelector('a') const tooltipEl = fixtureEl.querySelector('a')
const tooltip = new Tooltip(tooltipEl) const tooltip = new Tooltip(tooltipEl)
tooltip.enable() tooltip.enable()
tooltipEl.addEventListener('shown.bs.tooltip', () => { tooltipEl.addEventListener('shown.bs.tooltip', () => {
expect(document.querySelector('.tooltip')).not.toBeNull() expect(document.querySelector('.tooltip')).not.toBeNull()
done() resolve()
})
tooltip.show()
}) })
tooltip.show()
}) })
}) })
describe('disable', () => { describe('disable', () => {
it('should disable tooltip', done => { it('should disable tooltip', () => {
fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">' return new Promise(resolve => {
fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
const tooltipEl = fixtureEl.querySelector('a') const tooltipEl = fixtureEl.querySelector('a')
const tooltip = new Tooltip(tooltipEl) const tooltip = new Tooltip(tooltipEl)
tooltip.disable() tooltip.disable()
tooltipEl.addEventListener('show.bs.tooltip', () => { tooltipEl.addEventListener('show.bs.tooltip', () => {
throw new Error('should not show a disabled tooltip') throw new Error('should not show a disabled tooltip')
})
tooltip.show()
setTimeout(() => {
expect().nothing()
resolve()
}, 10)
}) })
tooltip.show()
setTimeout(() => {
expect().nothing()
done()
}, 10)
}) })
}) })
@ -247,96 +255,106 @@ describe('Tooltip', () => {
}) })
describe('toggle', () => { describe('toggle', () => {
it('should do nothing if disabled', done => { it('should do nothing if disabled', () => {
fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">' return new Promise(resolve => {
fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
const tooltipEl = fixtureEl.querySelector('a') const tooltipEl = fixtureEl.querySelector('a')
const tooltip = new Tooltip(tooltipEl) const tooltip = new Tooltip(tooltipEl)
tooltip.disable() tooltip.disable()
tooltipEl.addEventListener('show.bs.tooltip', () => { tooltipEl.addEventListener('show.bs.tooltip', () => {
throw new Error('should not show a disabled tooltip') throw new Error('should not show a disabled tooltip')
})
tooltip.toggle()
setTimeout(() => {
expect().nothing()
resolve()
}, 10)
}) })
tooltip.toggle()
setTimeout(() => {
expect().nothing()
done()
}, 10)
}) })
it('should show a tooltip', done => { it('should show a tooltip', () => {
fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">' return new Promise(resolve => {
fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
const tooltipEl = fixtureEl.querySelector('a') const tooltipEl = fixtureEl.querySelector('a')
const tooltip = new Tooltip(tooltipEl) const tooltip = new Tooltip(tooltipEl)
tooltipEl.addEventListener('shown.bs.tooltip', () => { tooltipEl.addEventListener('shown.bs.tooltip', () => {
expect(document.querySelector('.tooltip')).not.toBeNull() expect(document.querySelector('.tooltip')).not.toBeNull()
done() resolve()
}) })
tooltip.toggle()
})
it('should call toggle and show the tooltip when trigger is "click"', done => {
fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
const tooltipEl = fixtureEl.querySelector('a')
const tooltip = new Tooltip(tooltipEl, {
trigger: 'click'
})
spyOn(tooltip, 'toggle').and.callThrough()
tooltipEl.addEventListener('shown.bs.tooltip', () => {
expect(tooltip.toggle).toHaveBeenCalled()
done()
})
tooltipEl.click()
})
it('should hide a tooltip', done => {
fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
const tooltipEl = fixtureEl.querySelector('a')
const tooltip = new Tooltip(tooltipEl)
tooltipEl.addEventListener('shown.bs.tooltip', () => {
tooltip.toggle() tooltip.toggle()
}) })
tooltipEl.addEventListener('hidden.bs.tooltip', () => {
expect(document.querySelector('.tooltip')).toBeNull()
done()
})
tooltip.toggle()
}) })
it('should call toggle and hide the tooltip when trigger is "click"', done => { it('should call toggle and show the tooltip when trigger is "click"', () => {
fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">' return new Promise(resolve => {
fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
const tooltipEl = fixtureEl.querySelector('a') const tooltipEl = fixtureEl.querySelector('a')
const tooltip = new Tooltip(tooltipEl, { const tooltip = new Tooltip(tooltipEl, {
trigger: 'click' trigger: 'click'
}) })
spyOn(tooltip, 'toggle').and.callThrough() spyOn(tooltip, 'toggle').and.callThrough()
tooltipEl.addEventListener('shown.bs.tooltip', () => {
expect(tooltip.toggle).toHaveBeenCalled()
resolve()
})
tooltipEl.addEventListener('shown.bs.tooltip', () => {
tooltipEl.click() tooltipEl.click()
}) })
})
tooltipEl.addEventListener('hidden.bs.tooltip', () => { it('should hide a tooltip', () => {
expect(tooltip.toggle).toHaveBeenCalled() return new Promise(resolve => {
done() fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
const tooltipEl = fixtureEl.querySelector('a')
const tooltip = new Tooltip(tooltipEl)
tooltipEl.addEventListener('shown.bs.tooltip', () => {
tooltip.toggle()
})
tooltipEl.addEventListener('hidden.bs.tooltip', () => {
expect(document.querySelector('.tooltip')).toBeNull()
resolve()
})
tooltip.toggle()
}) })
})
tooltipEl.click() it('should call toggle and hide the tooltip when trigger is "click"', () => {
return new Promise(resolve => {
fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
const tooltipEl = fixtureEl.querySelector('a')
const tooltip = new Tooltip(tooltipEl, {
trigger: 'click'
})
spyOn(tooltip, 'toggle').and.callThrough()
tooltipEl.addEventListener('shown.bs.tooltip', () => {
tooltipEl.click()
})
tooltipEl.addEventListener('hidden.bs.tooltip', () => {
expect(tooltip.toggle).toHaveBeenCalled()
resolve()
})
tooltipEl.click()
})
}) })
}) })
@ -367,239 +385,263 @@ describe('Tooltip', () => {
expect(removeEventSpy.calls.allArgs()).toEqual(expectedArgs) expect(removeEventSpy.calls.allArgs()).toEqual(expectedArgs)
}) })
it('should destroy a tooltip after it is shown and hidden', done => { it('should destroy a tooltip after it is shown and hidden', () => {
fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">' return new Promise(resolve => {
fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
const tooltipEl = fixtureEl.querySelector('a') const tooltipEl = fixtureEl.querySelector('a')
const tooltip = new Tooltip(tooltipEl) const tooltip = new Tooltip(tooltipEl)
tooltipEl.addEventListener('shown.bs.tooltip', () => { tooltipEl.addEventListener('shown.bs.tooltip', () => {
tooltip.hide() tooltip.hide()
})
tooltipEl.addEventListener('hidden.bs.tooltip', () => {
tooltip.dispose()
expect(tooltip.tip).toBeNull()
expect(Tooltip.getInstance(tooltipEl)).toBeNull()
resolve()
})
tooltip.show()
}) })
tooltipEl.addEventListener('hidden.bs.tooltip', () => {
tooltip.dispose()
expect(tooltip.tip).toBeNull()
expect(Tooltip.getInstance(tooltipEl)).toBeNull()
done()
})
tooltip.show()
}) })
it('should destroy a tooltip and remove it from the dom', done => { it('should destroy a tooltip and remove it from the dom', () => {
fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">' return new Promise(resolve => {
fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
const tooltipEl = fixtureEl.querySelector('a') const tooltipEl = fixtureEl.querySelector('a')
const tooltip = new Tooltip(tooltipEl) const tooltip = new Tooltip(tooltipEl)
tooltipEl.addEventListener('shown.bs.tooltip', () => { tooltipEl.addEventListener('shown.bs.tooltip', () => {
expect(document.querySelector('.tooltip')).not.toBeNull() expect(document.querySelector('.tooltip')).not.toBeNull()
tooltip.dispose() tooltip.dispose()
expect(document.querySelector('.tooltip')).toBeNull() expect(document.querySelector('.tooltip')).toBeNull()
done() resolve()
})
tooltip.show()
}) })
tooltip.show()
}) })
}) })
describe('show', () => { describe('show', () => {
it('should show a tooltip', done => { it('should show a tooltip', () => {
fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">' return new Promise(resolve => {
fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
const tooltipEl = fixtureEl.querySelector('a') const tooltipEl = fixtureEl.querySelector('a')
const tooltip = new Tooltip(tooltipEl) const tooltip = new Tooltip(tooltipEl)
tooltipEl.addEventListener('shown.bs.tooltip', () => {
const tooltipShown = document.querySelector('.tooltip')
expect(tooltipShown).not.toBeNull()
expect(tooltipEl.getAttribute('aria-describedby')).toEqual(tooltipShown.getAttribute('id'))
expect(tooltipShown.getAttribute('id')).toContain('tooltip')
done()
})
tooltip.show()
})
it('should show a tooltip when hovering a child element', done => {
fixtureEl.innerHTML = [
'<a href="#" rel="tooltip" title="Another tooltip">',
' <svg xmlns="http://www.w3.org/2000/svg" width="50" height="50" viewBox="0 0 100 100">',
' <rect width="100%" fill="#563d7c"/>',
' <circle cx="50" cy="50" r="30" fill="#fff"/>',
' </svg>',
'</a>'
].join('')
const tooltipEl = fixtureEl.querySelector('a')
const tooltip = new Tooltip(tooltipEl)
spyOn(tooltip, 'show')
tooltipEl.querySelector('rect').dispatchEvent(createEvent('mouseover', { bubbles: true }))
setTimeout(() => {
expect(tooltip.show).toHaveBeenCalled()
done()
}, 0)
})
it('should show a tooltip on mobile', done => {
fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
const tooltipEl = fixtureEl.querySelector('a')
const tooltip = new Tooltip(tooltipEl)
document.documentElement.ontouchstart = noop
spyOn(EventHandler, 'on').and.callThrough()
tooltipEl.addEventListener('shown.bs.tooltip', () => {
expect(document.querySelector('.tooltip')).not.toBeNull()
expect(EventHandler.on).toHaveBeenCalledWith(jasmine.any(Object), 'mouseover', noop)
document.documentElement.ontouchstart = undefined
done()
})
tooltip.show()
})
it('should show a tooltip relative to placement option', done => {
fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
const tooltipEl = fixtureEl.querySelector('a')
const tooltip = new Tooltip(tooltipEl, {
placement: 'bottom'
})
tooltipEl.addEventListener('inserted.bs.tooltip', () => {
expect(tooltip._getTipElement()).toHaveClass('bs-tooltip-auto')
})
tooltipEl.addEventListener('shown.bs.tooltip', () => {
expect(tooltip._getTipElement()).toHaveClass('bs-tooltip-auto')
expect(tooltip._getTipElement().getAttribute('data-popper-placement')).toEqual('bottom')
done()
})
tooltip.show()
})
it('should not error when trying to show a tooltip that has been removed from the dom', done => {
fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
const tooltipEl = fixtureEl.querySelector('a')
const tooltip = new Tooltip(tooltipEl)
const firstCallback = () => {
tooltipEl.removeEventListener('shown.bs.tooltip', firstCallback)
let tooltipShown = document.querySelector('.tooltip')
tooltipShown.remove()
tooltipEl.addEventListener('shown.bs.tooltip', () => { tooltipEl.addEventListener('shown.bs.tooltip', () => {
tooltipShown = document.querySelector('.tooltip') const tooltipShown = document.querySelector('.tooltip')
expect(tooltipShown).not.toBeNull() expect(tooltipShown).not.toBeNull()
done() expect(tooltipEl.getAttribute('aria-describedby')).toEqual(tooltipShown.getAttribute('id'))
expect(tooltipShown.getAttribute('id')).toContain('tooltip')
resolve()
}) })
tooltip.show() tooltip.show()
} })
tooltipEl.addEventListener('shown.bs.tooltip', firstCallback)
tooltip.show()
}) })
it('should show a tooltip with a dom element container', done => { it('should show a tooltip when hovering a child element', () => {
fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">' return new Promise(resolve => {
fixtureEl.innerHTML = [
'<a href="#" rel="tooltip" title="Another tooltip">',
' <svg xmlns="http://www.w3.org/2000/svg" width="50" height="50" viewBox="0 0 100 100">',
' <rect width="100%" fill="#563d7c"/>',
' <circle cx="50" cy="50" r="30" fill="#fff"/>',
' </svg>',
'</a>'
].join('')
const tooltipEl = fixtureEl.querySelector('a') const tooltipEl = fixtureEl.querySelector('a')
const tooltip = new Tooltip(tooltipEl, { const tooltip = new Tooltip(tooltipEl)
container: fixtureEl
spyOn(tooltip, 'show')
tooltipEl.querySelector('rect').dispatchEvent(createEvent('mouseover', { bubbles: true }))
setTimeout(() => {
expect(tooltip.show).toHaveBeenCalled()
resolve()
}, 0)
}) })
tooltipEl.addEventListener('shown.bs.tooltip', () => {
expect(fixtureEl.querySelector('.tooltip')).not.toBeNull()
done()
})
tooltip.show()
}) })
it('should show a tooltip with a jquery element container', done => { it('should show a tooltip on mobile', () => {
fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">' return new Promise(resolve => {
fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
const tooltipEl = fixtureEl.querySelector('a') const tooltipEl = fixtureEl.querySelector('a')
const tooltip = new Tooltip(tooltipEl, { const tooltip = new Tooltip(tooltipEl)
container: { document.documentElement.ontouchstart = noop
0: fixtureEl,
jquery: 'jQuery' spyOn(EventHandler, 'on').and.callThrough()
tooltipEl.addEventListener('shown.bs.tooltip', () => {
expect(document.querySelector('.tooltip')).not.toBeNull()
expect(EventHandler.on).toHaveBeenCalledWith(jasmine.any(Object), 'mouseover', noop)
document.documentElement.ontouchstart = undefined
resolve()
})
tooltip.show()
})
})
it('should show a tooltip relative to placement option', () => {
return new Promise(resolve => {
fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
const tooltipEl = fixtureEl.querySelector('a')
const tooltip = new Tooltip(tooltipEl, {
placement: 'bottom'
})
tooltipEl.addEventListener('inserted.bs.tooltip', () => {
expect(tooltip._getTipElement()).toHaveClass('bs-tooltip-auto')
})
tooltipEl.addEventListener('shown.bs.tooltip', () => {
expect(tooltip._getTipElement()).toHaveClass('bs-tooltip-auto')
expect(tooltip._getTipElement().getAttribute('data-popper-placement')).toEqual('bottom')
resolve()
})
tooltip.show()
})
})
it('should not error when trying to show a tooltip that has been removed from the dom', () => {
return new Promise(resolve => {
fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
const tooltipEl = fixtureEl.querySelector('a')
const tooltip = new Tooltip(tooltipEl)
const firstCallback = () => {
tooltipEl.removeEventListener('shown.bs.tooltip', firstCallback)
let tooltipShown = document.querySelector('.tooltip')
tooltipShown.remove()
tooltipEl.addEventListener('shown.bs.tooltip', () => {
tooltipShown = document.querySelector('.tooltip')
expect(tooltipShown).not.toBeNull()
resolve()
})
tooltip.show()
} }
})
tooltipEl.addEventListener('shown.bs.tooltip', () => { tooltipEl.addEventListener('shown.bs.tooltip', firstCallback)
expect(fixtureEl.querySelector('.tooltip')).not.toBeNull()
done()
})
tooltip.show() tooltip.show()
})
}) })
it('should show a tooltip with a selector in container', done => { it('should show a tooltip with a dom element container', () => {
fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">' return new Promise(resolve => {
fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
const tooltipEl = fixtureEl.querySelector('a') const tooltipEl = fixtureEl.querySelector('a')
const tooltip = new Tooltip(tooltipEl, { const tooltip = new Tooltip(tooltipEl, {
container: '#fixture' container: fixtureEl
})
tooltipEl.addEventListener('shown.bs.tooltip', () => {
expect(fixtureEl.querySelector('.tooltip')).not.toBeNull()
resolve()
})
tooltip.show()
}) })
tooltipEl.addEventListener('shown.bs.tooltip', () => {
expect(fixtureEl.querySelector('.tooltip')).not.toBeNull()
done()
})
tooltip.show()
}) })
it('should show a tooltip with placement as a function', done => { it('should show a tooltip with a jquery element container', () => {
fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">' return new Promise(resolve => {
fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
const spy = jasmine.createSpy('placement').and.returnValue('top') const tooltipEl = fixtureEl.querySelector('a')
const tooltipEl = fixtureEl.querySelector('a') const tooltip = new Tooltip(tooltipEl, {
const tooltip = new Tooltip(tooltipEl, { container: {
placement: spy 0: fixtureEl,
jquery: 'jQuery'
}
})
tooltipEl.addEventListener('shown.bs.tooltip', () => {
expect(fixtureEl.querySelector('.tooltip')).not.toBeNull()
resolve()
})
tooltip.show()
}) })
tooltipEl.addEventListener('shown.bs.tooltip', () => {
expect(document.querySelector('.tooltip')).not.toBeNull()
expect(spy).toHaveBeenCalled()
done()
})
tooltip.show()
}) })
it('should show a tooltip without the animation', done => { it('should show a tooltip with a selector in container', () => {
fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">' return new Promise(resolve => {
fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
const tooltipEl = fixtureEl.querySelector('a') const tooltipEl = fixtureEl.querySelector('a')
const tooltip = new Tooltip(tooltipEl, { const tooltip = new Tooltip(tooltipEl, {
animation: false container: '#fixture'
})
tooltipEl.addEventListener('shown.bs.tooltip', () => {
expect(fixtureEl.querySelector('.tooltip')).not.toBeNull()
resolve()
})
tooltip.show()
}) })
})
tooltipEl.addEventListener('shown.bs.tooltip', () => { it('should show a tooltip with placement as a function', () => {
const tip = document.querySelector('.tooltip') return new Promise(resolve => {
fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
expect(tip).not.toBeNull() const spy = jasmine.createSpy('placement').and.returnValue('top')
expect(tip).not.toHaveClass('fade') const tooltipEl = fixtureEl.querySelector('a')
done() const tooltip = new Tooltip(tooltipEl, {
placement: spy
})
tooltipEl.addEventListener('shown.bs.tooltip', () => {
expect(document.querySelector('.tooltip')).not.toBeNull()
expect(spy).toHaveBeenCalled()
resolve()
})
tooltip.show()
}) })
})
tooltip.show() it('should show a tooltip without the animation', () => {
return new Promise(resolve => {
fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
const tooltipEl = fixtureEl.querySelector('a')
const tooltip = new Tooltip(tooltipEl, {
animation: false
})
tooltipEl.addEventListener('shown.bs.tooltip', () => {
const tip = document.querySelector('.tooltip')
expect(tip).not.toBeNull()
expect(tip).not.toHaveClass('fade')
resolve()
})
tooltip.show()
})
}) })
it('should throw an error the element is not visible', () => { it('should throw an error the element is not visible', () => {
@ -615,352 +657,382 @@ describe('Tooltip', () => {
} }
}) })
it('should not show a tooltip if show.bs.tooltip is prevented', done => { it('should not show a tooltip if show.bs.tooltip is prevented', () => {
fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">' return new Promise(resolve => {
fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
const tooltipEl = fixtureEl.querySelector('a') const tooltipEl = fixtureEl.querySelector('a')
const tooltip = new Tooltip(tooltipEl) const tooltip = new Tooltip(tooltipEl)
const expectedDone = () => { const expectedDone = () => {
setTimeout(() => { setTimeout(() => {
expect(document.querySelector('.tooltip')).toBeNull() expect(document.querySelector('.tooltip')).toBeNull()
done() resolve()
}, 10) }, 10)
}
tooltipEl.addEventListener('show.bs.tooltip', ev => {
ev.preventDefault()
expectedDone()
})
tooltipEl.addEventListener('shown.bs.tooltip', () => {
throw new Error('Tooltip should not be shown')
})
tooltip.show()
})
it('should show tooltip if leave event hasn\'t occurred before delay expires', done => {
fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
const tooltipEl = fixtureEl.querySelector('a')
const tooltip = new Tooltip(tooltipEl, {
delay: 150
})
spyOn(tooltip, 'show')
setTimeout(() => {
expect(tooltip.show).not.toHaveBeenCalled()
}, 100)
setTimeout(() => {
expect(tooltip.show).toHaveBeenCalled()
done()
}, 200)
tooltipEl.dispatchEvent(createEvent('mouseover'))
})
it('should not show tooltip if leave event occurs before delay expires', done => {
fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
const tooltipEl = fixtureEl.querySelector('a')
const tooltip = new Tooltip(tooltipEl, {
delay: 150
})
spyOn(tooltip, 'show')
setTimeout(() => {
expect(tooltip.show).not.toHaveBeenCalled()
tooltipEl.dispatchEvent(createEvent('mouseover'))
}, 100)
setTimeout(() => {
expect(tooltip.show).toHaveBeenCalled()
expect(document.querySelectorAll('.tooltip')).toHaveSize(0)
done()
}, 200)
tooltipEl.dispatchEvent(createEvent('mouseover'))
})
it('should not hide tooltip if leave event occurs and enter event occurs within the hide delay', done => {
fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
const tooltipEl = fixtureEl.querySelector('a')
const tooltip = new Tooltip(tooltipEl, {
delay: {
show: 0,
hide: 150
} }
})
setTimeout(() => { tooltipEl.addEventListener('show.bs.tooltip', ev => {
expect(tooltip._getTipElement()).toHaveClass('show') ev.preventDefault()
tooltipEl.dispatchEvent(createEvent('mouseout')) expectedDone()
setTimeout(() => {
expect(tooltip._getTipElement()).toHaveClass('show')
tooltipEl.dispatchEvent(createEvent('mouseover'))
}, 100)
setTimeout(() => {
expect(tooltip._getTipElement()).toHaveClass('show')
expect(document.querySelectorAll('.tooltip')).toHaveSize(1)
done()
}, 200)
}, 10)
tooltipEl.dispatchEvent(createEvent('mouseover'))
})
it('should not hide tooltip if leave event occurs and interaction remains inside trigger', done => {
fixtureEl.innerHTML = [
'<a href="#" rel="tooltip" title="Another tooltip">',
'<b>Trigger</b>',
'the tooltip',
'</a>'
].join('')
const tooltipEl = fixtureEl.querySelector('a')
const tooltip = new Tooltip(tooltipEl)
const triggerChild = tooltipEl.querySelector('b')
spyOn(tooltip, 'hide').and.callThrough()
tooltipEl.addEventListener('mouseover', () => {
const moveMouseToChildEvent = createEvent('mouseout')
Object.defineProperty(moveMouseToChildEvent, 'relatedTarget', {
value: triggerChild
}) })
tooltipEl.dispatchEvent(moveMouseToChildEvent) tooltipEl.addEventListener('shown.bs.tooltip', () => {
}) throw new Error('Tooltip should not be shown')
})
tooltipEl.addEventListener('mouseout', () => { tooltip.show()
expect(tooltip.hide).not.toHaveBeenCalled()
done()
}) })
tooltipEl.dispatchEvent(createEvent('mouseover'))
}) })
it('should properly maintain tooltip state if leave event occurs and enter event occurs during hide transition', done => { it('should show tooltip if leave event hasn\'t occurred before delay expires', () => {
// Style this tooltip to give it plenty of room for popper to do what it wants return new Promise(resolve => {
fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip" data-bs-placement="top" style="position:fixed;left:50%;top:50%;">Trigger</a>' fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
const tooltipEl = fixtureEl.querySelector('a') const tooltipEl = fixtureEl.querySelector('a')
const tooltip = new Tooltip(tooltipEl) const tooltip = new Tooltip(tooltipEl, {
delay: 150
})
spyOn(window, 'getComputedStyle').and.returnValue({ spyOn(tooltip, 'show')
transitionDuration: '0.15s',
transitionDelay: '0s'
})
setTimeout(() => {
expect(tooltip._popper).not.toBeNull()
expect(tooltip._getTipElement().getAttribute('data-popper-placement')).toEqual('top')
tooltipEl.dispatchEvent(createEvent('mouseout'))
setTimeout(() => { setTimeout(() => {
expect(tooltip._getTipElement()).not.toHaveClass('show') expect(tooltip.show).not.toHaveBeenCalled()
}, 100)
setTimeout(() => {
expect(tooltip.show).toHaveBeenCalled()
resolve()
}, 200)
tooltipEl.dispatchEvent(createEvent('mouseover'))
})
})
it('should not show tooltip if leave event occurs before delay expires', () => {
return new Promise(resolve => {
fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
const tooltipEl = fixtureEl.querySelector('a')
const tooltip = new Tooltip(tooltipEl, {
delay: 150
})
spyOn(tooltip, 'show')
setTimeout(() => {
expect(tooltip.show).not.toHaveBeenCalled()
tooltipEl.dispatchEvent(createEvent('mouseover')) tooltipEl.dispatchEvent(createEvent('mouseover'))
}, 100) }, 100)
setTimeout(() => {
expect(tooltip.show).toHaveBeenCalled()
expect(document.querySelectorAll('.tooltip')).toHaveSize(0)
resolve()
}, 200)
tooltipEl.dispatchEvent(createEvent('mouseover'))
})
})
it('should not hide tooltip if leave event occurs and enter event occurs within the hide delay', () => {
return new Promise(resolve => {
fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
const tooltipEl = fixtureEl.querySelector('a')
const tooltip = new Tooltip(tooltipEl, {
delay: {
show: 0,
hide: 150
}
})
setTimeout(() => {
expect(tooltip._getTipElement()).toHaveClass('show')
tooltipEl.dispatchEvent(createEvent('mouseout'))
setTimeout(() => {
expect(tooltip._getTipElement()).toHaveClass('show')
tooltipEl.dispatchEvent(createEvent('mouseover'))
}, 100)
setTimeout(() => {
expect(tooltip._getTipElement()).toHaveClass('show')
expect(document.querySelectorAll('.tooltip')).toHaveSize(1)
resolve()
}, 200)
}, 10)
tooltipEl.dispatchEvent(createEvent('mouseover'))
})
})
it('should not hide tooltip if leave event occurs and interaction remains inside trigger', () => {
return new Promise(resolve => {
fixtureEl.innerHTML = [
'<a href="#" rel="tooltip" title="Another tooltip">',
'<b>Trigger</b>',
'the tooltip',
'</a>'
].join('')
const tooltipEl = fixtureEl.querySelector('a')
const tooltip = new Tooltip(tooltipEl)
const triggerChild = tooltipEl.querySelector('b')
spyOn(tooltip, 'hide').and.callThrough()
tooltipEl.addEventListener('mouseover', () => {
const moveMouseToChildEvent = createEvent('mouseout')
Object.defineProperty(moveMouseToChildEvent, 'relatedTarget', {
value: triggerChild
})
tooltipEl.dispatchEvent(moveMouseToChildEvent)
})
tooltipEl.addEventListener('mouseout', () => {
expect(tooltip.hide).not.toHaveBeenCalled()
resolve()
})
tooltipEl.dispatchEvent(createEvent('mouseover'))
})
})
it('should properly maintain tooltip state if leave event occurs and enter event occurs during hide transition', () => {
return new Promise(resolve => {
// Style this tooltip to give it plenty of room for popper to do what it wants
fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip" data-bs-placement="top" style="position:fixed;left:50%;top:50%;">Trigger</a>'
const tooltipEl = fixtureEl.querySelector('a')
const tooltip = new Tooltip(tooltipEl)
spyOn(window, 'getComputedStyle').and.returnValue({
transitionDuration: '0.15s',
transitionDelay: '0s'
})
setTimeout(() => { setTimeout(() => {
expect(tooltip._popper).not.toBeNull() expect(tooltip._popper).not.toBeNull()
expect(tooltip._getTipElement().getAttribute('data-popper-placement')).toEqual('top') expect(tooltip._getTipElement().getAttribute('data-popper-placement')).toEqual('top')
done() tooltipEl.dispatchEvent(createEvent('mouseout'))
}, 200)
}, 10)
tooltipEl.dispatchEvent(createEvent('mouseover')) setTimeout(() => {
expect(tooltip._getTipElement()).not.toHaveClass('show')
tooltipEl.dispatchEvent(createEvent('mouseover'))
}, 100)
setTimeout(() => {
expect(tooltip._popper).not.toBeNull()
expect(tooltip._getTipElement().getAttribute('data-popper-placement')).toEqual('top')
resolve()
}, 200)
}, 10)
tooltipEl.dispatchEvent(createEvent('mouseover'))
})
}) })
it('should only trigger inserted event if a new tooltip element was created', done => { it('should only trigger inserted event if a new tooltip element was created', () => {
fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">' return new Promise(resolve => {
fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
const tooltipEl = fixtureEl.querySelector('a') const tooltipEl = fixtureEl.querySelector('a')
const tooltip = new Tooltip(tooltipEl) const tooltip = new Tooltip(tooltipEl)
spyOn(window, 'getComputedStyle').and.returnValue({ spyOn(window, 'getComputedStyle').and.returnValue({
transitionDuration: '0.15s', transitionDuration: '0.15s',
transitionDelay: '0s' transitionDelay: '0s'
}) })
const insertedFunc = jasmine.createSpy() const insertedFunc = jasmine.createSpy()
tooltipEl.addEventListener('inserted.bs.tooltip', insertedFunc) tooltipEl.addEventListener('inserted.bs.tooltip', insertedFunc)
setTimeout(() => {
expect(insertedFunc).toHaveBeenCalledTimes(1)
tooltip.hide()
setTimeout(() => {
tooltip.show()
}, 100)
setTimeout(() => { setTimeout(() => {
expect(insertedFunc).toHaveBeenCalledTimes(1) expect(insertedFunc).toHaveBeenCalledTimes(1)
done() tooltip.hide()
}, 200)
}, 0)
tooltip.show() setTimeout(() => {
tooltip.show()
}, 100)
setTimeout(() => {
expect(insertedFunc).toHaveBeenCalledTimes(1)
resolve()
}, 200)
}, 0)
tooltip.show()
})
}) })
it('should show a tooltip with custom class provided in data attributes', done => { it('should show a tooltip with custom class provided in data attributes', () => {
fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip" data-bs-custom-class="custom-class">' return new Promise(resolve => {
fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip" data-bs-custom-class="custom-class">'
const tooltipEl = fixtureEl.querySelector('a') const tooltipEl = fixtureEl.querySelector('a')
const tooltip = new Tooltip(tooltipEl) const tooltip = new Tooltip(tooltipEl)
tooltipEl.addEventListener('shown.bs.tooltip', () => { tooltipEl.addEventListener('shown.bs.tooltip', () => {
const tip = document.querySelector('.tooltip') const tip = document.querySelector('.tooltip')
expect(tip).not.toBeNull() expect(tip).not.toBeNull()
expect(tip).toHaveClass('custom-class') expect(tip).toHaveClass('custom-class')
done() resolve()
})
tooltip.show()
}) })
tooltip.show()
}) })
it('should show a tooltip with custom class provided as a string in config', done => { it('should show a tooltip with custom class provided as a string in config', () => {
fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">' return new Promise(resolve => {
fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
const tooltipEl = fixtureEl.querySelector('a') const tooltipEl = fixtureEl.querySelector('a')
const tooltip = new Tooltip(tooltipEl, { const tooltip = new Tooltip(tooltipEl, {
customClass: 'custom-class custom-class-2' customClass: 'custom-class custom-class-2'
})
tooltipEl.addEventListener('shown.bs.tooltip', () => {
const tip = document.querySelector('.tooltip')
expect(tip).not.toBeNull()
expect(tip).toHaveClass('custom-class')
expect(tip).toHaveClass('custom-class-2')
resolve()
})
tooltip.show()
}) })
tooltipEl.addEventListener('shown.bs.tooltip', () => {
const tip = document.querySelector('.tooltip')
expect(tip).not.toBeNull()
expect(tip).toHaveClass('custom-class')
expect(tip).toHaveClass('custom-class-2')
done()
})
tooltip.show()
}) })
it('should show a tooltip with custom class provided as a function in config', done => { it('should show a tooltip with custom class provided as a function in config', () => {
fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">' return new Promise(resolve => {
fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
const spy = jasmine.createSpy('customClass').and.returnValue('custom-class') const spy = jasmine.createSpy('customClass').and.returnValue('custom-class')
const tooltipEl = fixtureEl.querySelector('a') const tooltipEl = fixtureEl.querySelector('a')
const tooltip = new Tooltip(tooltipEl, { const tooltip = new Tooltip(tooltipEl, {
customClass: spy customClass: spy
})
tooltipEl.addEventListener('shown.bs.tooltip', () => {
const tip = document.querySelector('.tooltip')
expect(tip).not.toBeNull()
expect(spy).toHaveBeenCalled()
expect(tip).toHaveClass('custom-class')
resolve()
})
tooltip.show()
}) })
tooltipEl.addEventListener('shown.bs.tooltip', () => {
const tip = document.querySelector('.tooltip')
expect(tip).not.toBeNull()
expect(spy).toHaveBeenCalled()
expect(tip).toHaveClass('custom-class')
done()
})
tooltip.show()
}) })
it('should remove `title` attribute if exists', done => { it('should remove `title` attribute if exists', () => {
fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip"></a>' return new Promise(resolve => {
fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip"></a>'
const tooltipEl = fixtureEl.querySelector('a') const tooltipEl = fixtureEl.querySelector('a')
const tooltip = new Tooltip(tooltipEl) const tooltip = new Tooltip(tooltipEl)
tooltipEl.addEventListener('shown.bs.tooltip', () => { tooltipEl.addEventListener('shown.bs.tooltip', () => {
expect(tooltipEl.getAttribute('title')).toBeNull() expect(tooltipEl.getAttribute('title')).toBeNull()
done() resolve()
})
tooltip.show()
}) })
tooltip.show()
}) })
}) })
describe('hide', () => { describe('hide', () => {
it('should hide a tooltip', done => { it('should hide a tooltip', () => {
fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">' return new Promise(resolve => {
fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
const tooltipEl = fixtureEl.querySelector('a') const tooltipEl = fixtureEl.querySelector('a')
const tooltip = new Tooltip(tooltipEl) const tooltip = new Tooltip(tooltipEl)
tooltipEl.addEventListener('shown.bs.tooltip', () => tooltip.hide()) tooltipEl.addEventListener('shown.bs.tooltip', () => tooltip.hide())
tooltipEl.addEventListener('hidden.bs.tooltip', () => { tooltipEl.addEventListener('hidden.bs.tooltip', () => {
expect(document.querySelector('.tooltip')).toBeNull() expect(document.querySelector('.tooltip')).toBeNull()
expect(tooltipEl.getAttribute('aria-describedby')).toBeNull() expect(tooltipEl.getAttribute('aria-describedby')).toBeNull()
done() resolve()
})
tooltip.show()
}) })
tooltip.show()
}) })
it('should hide a tooltip on mobile', done => { it('should hide a tooltip on mobile', () => {
fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">' return new Promise(resolve => {
fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
const tooltipEl = fixtureEl.querySelector('a') const tooltipEl = fixtureEl.querySelector('a')
const tooltip = new Tooltip(tooltipEl) const tooltip = new Tooltip(tooltipEl)
tooltipEl.addEventListener('shown.bs.tooltip', () => { tooltipEl.addEventListener('shown.bs.tooltip', () => {
document.documentElement.ontouchstart = noop document.documentElement.ontouchstart = noop
spyOn(EventHandler, 'off') spyOn(EventHandler, 'off')
tooltip.hide() tooltip.hide()
})
tooltipEl.addEventListener('hidden.bs.tooltip', () => {
expect(document.querySelector('.tooltip')).toBeNull()
expect(EventHandler.off).toHaveBeenCalledWith(jasmine.any(Object), 'mouseover', noop)
document.documentElement.ontouchstart = undefined
resolve()
})
tooltip.show()
}) })
tooltipEl.addEventListener('hidden.bs.tooltip', () => {
expect(document.querySelector('.tooltip')).toBeNull()
expect(EventHandler.off).toHaveBeenCalledWith(jasmine.any(Object), 'mouseover', noop)
document.documentElement.ontouchstart = undefined
done()
})
tooltip.show()
}) })
it('should hide a tooltip without animation', done => { it('should hide a tooltip without animation', () => {
fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">' return new Promise(resolve => {
fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
const tooltipEl = fixtureEl.querySelector('a') const tooltipEl = fixtureEl.querySelector('a')
const tooltip = new Tooltip(tooltipEl, { const tooltip = new Tooltip(tooltipEl, {
animation: false animation: false
})
tooltipEl.addEventListener('shown.bs.tooltip', () => tooltip.hide())
tooltipEl.addEventListener('hidden.bs.tooltip', () => {
expect(document.querySelector('.tooltip')).toBeNull()
expect(tooltipEl.getAttribute('aria-describedby')).toBeNull()
resolve()
})
tooltip.show()
}) })
tooltipEl.addEventListener('shown.bs.tooltip', () => tooltip.hide())
tooltipEl.addEventListener('hidden.bs.tooltip', () => {
expect(document.querySelector('.tooltip')).toBeNull()
expect(tooltipEl.getAttribute('aria-describedby')).toBeNull()
done()
})
tooltip.show()
}) })
it('should not hide a tooltip if hide event is prevented', done => { it('should not hide a tooltip if hide event is prevented', () => {
fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">' return new Promise(resolve => {
fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
const assertDone = () => { const assertDone = () => {
setTimeout(() => { setTimeout(() => {
expect(document.querySelector('.tooltip')).not.toBeNull() expect(document.querySelector('.tooltip')).not.toBeNull()
done() resolve()
}, 20) }, 20)
} }
const tooltipEl = fixtureEl.querySelector('a') const tooltipEl = fixtureEl.querySelector('a')
const tooltip = new Tooltip(tooltipEl, { const tooltip = new Tooltip(tooltipEl, {
animation: false animation: false
})
tooltipEl.addEventListener('shown.bs.tooltip', () => tooltip.hide())
tooltipEl.addEventListener('hide.bs.tooltip', event => {
event.preventDefault()
assertDone()
})
tooltipEl.addEventListener('hidden.bs.tooltip', () => {
throw new Error('should not trigger hidden event')
})
tooltip.show()
}) })
tooltipEl.addEventListener('shown.bs.tooltip', () => tooltip.hide())
tooltipEl.addEventListener('hide.bs.tooltip', event => {
event.preventDefault()
assertDone()
})
tooltipEl.addEventListener('hidden.bs.tooltip', () => {
throw new Error('should not trigger hidden event')
})
tooltip.show()
}) })
it('should not throw error running hide if popper hasn\'t been shown', () => { it('should not throw error running hide if popper hasn\'t been shown', () => {
@ -979,22 +1051,24 @@ describe('Tooltip', () => {
}) })
describe('update', () => { describe('update', () => {
it('should call popper update', done => { it('should call popper update', () => {
fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">' return new Promise(resolve => {
fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">'
const tooltipEl = fixtureEl.querySelector('a') const tooltipEl = fixtureEl.querySelector('a')
const tooltip = new Tooltip(tooltipEl) const tooltip = new Tooltip(tooltipEl)
tooltipEl.addEventListener('shown.bs.tooltip', () => { tooltipEl.addEventListener('shown.bs.tooltip', () => {
spyOn(tooltip._popper, 'update') spyOn(tooltip._popper, 'update')
tooltip.update() tooltip.update()
expect(tooltip._popper.update).toHaveBeenCalled() expect(tooltip._popper.update).toHaveBeenCalled()
done() resolve()
})
tooltip.show()
}) })
tooltip.show()
}) })
it('should do nothing if the tooltip is not shown', () => { it('should do nothing if the tooltip is not shown', () => {
@ -1272,55 +1346,61 @@ describe('Tooltip', () => {
}) })
describe('aria-label', () => { describe('aria-label', () => {
it('should add the aria-label attribute for referencing original title', done => { it('should add the aria-label attribute for referencing original title', () => {
fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip"></a>' return new Promise(resolve => {
fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip"></a>'
const tooltipEl = fixtureEl.querySelector('a') const tooltipEl = fixtureEl.querySelector('a')
const tooltip = new Tooltip(tooltipEl) const tooltip = new Tooltip(tooltipEl)
tooltipEl.addEventListener('shown.bs.tooltip', () => { tooltipEl.addEventListener('shown.bs.tooltip', () => {
const tooltipShown = document.querySelector('.tooltip') const tooltipShown = document.querySelector('.tooltip')
expect(tooltipShown).not.toBeNull() expect(tooltipShown).not.toBeNull()
expect(tooltipEl.getAttribute('aria-label')).toEqual('Another tooltip') expect(tooltipEl.getAttribute('aria-label')).toEqual('Another tooltip')
done() resolve()
})
tooltip.show()
}) })
tooltip.show()
}) })
it('should not add the aria-label attribute if the attribute already exists', done => { it('should not add the aria-label attribute if the attribute already exists', () => {
fixtureEl.innerHTML = '<a href="#" rel="tooltip" aria-label="Different label" title="Another tooltip"></a>' return new Promise(resolve => {
fixtureEl.innerHTML = '<a href="#" rel="tooltip" aria-label="Different label" title="Another tooltip"></a>'
const tooltipEl = fixtureEl.querySelector('a') const tooltipEl = fixtureEl.querySelector('a')
const tooltip = new Tooltip(tooltipEl) const tooltip = new Tooltip(tooltipEl)
tooltipEl.addEventListener('shown.bs.tooltip', () => { tooltipEl.addEventListener('shown.bs.tooltip', () => {
const tooltipShown = document.querySelector('.tooltip') const tooltipShown = document.querySelector('.tooltip')
expect(tooltipShown).not.toBeNull() expect(tooltipShown).not.toBeNull()
expect(tooltipEl.getAttribute('aria-label')).toEqual('Different label') expect(tooltipEl.getAttribute('aria-label')).toEqual('Different label')
done() resolve()
})
tooltip.show()
}) })
tooltip.show()
}) })
it('should not add the aria-label attribute if the element has text content', done => { it('should not add the aria-label attribute if the element has text content', () => {
fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">text content</a>' return new Promise(resolve => {
fixtureEl.innerHTML = '<a href="#" rel="tooltip" title="Another tooltip">text content</a>'
const tooltipEl = fixtureEl.querySelector('a') const tooltipEl = fixtureEl.querySelector('a')
const tooltip = new Tooltip(tooltipEl) const tooltip = new Tooltip(tooltipEl)
tooltipEl.addEventListener('shown.bs.tooltip', () => { tooltipEl.addEventListener('shown.bs.tooltip', () => {
const tooltipShown = document.querySelector('.tooltip') const tooltipShown = document.querySelector('.tooltip')
expect(tooltipShown).not.toBeNull() expect(tooltipShown).not.toBeNull()
expect(tooltipEl.getAttribute('aria-label')).toBeNull() expect(tooltipEl.getAttribute('aria-label')).toBeNull()
done() resolve()
})
tooltip.show()
}) })
tooltip.show()
}) })
}) })

View File

@ -23,267 +23,297 @@ describe('Backdrop', () => {
}) })
describe('show', () => { describe('show', () => {
it('should append the backdrop html once on show and include the "show" class if it is "shown"', done => { it('should append the backdrop html once on show and include the "show" class if it is "shown"', () => {
const instance = new Backdrop({ return new Promise(resolve => {
isVisible: true, const instance = new Backdrop({
isAnimated: false isVisible: true,
}) isAnimated: false
const getElements = () => document.querySelectorAll(CLASS_BACKDROP) })
const getElements = () => document.querySelectorAll(CLASS_BACKDROP)
expect(getElements()).toHaveSize(0)
instance.show()
instance.show(() => {
expect(getElements()).toHaveSize(1)
for (const el of getElements()) {
expect(el).toHaveClass(CLASS_NAME_SHOW)
}
done()
})
})
it('should not append the backdrop html if it is not "shown"', done => {
const instance = new Backdrop({
isVisible: false,
isAnimated: true
})
const getElements = () => document.querySelectorAll(CLASS_BACKDROP)
expect(getElements()).toHaveSize(0)
instance.show(() => {
expect(getElements()).toHaveSize(0) expect(getElements()).toHaveSize(0)
done()
instance.show()
instance.show(() => {
expect(getElements()).toHaveSize(1)
for (const el of getElements()) {
expect(el).toHaveClass(CLASS_NAME_SHOW)
}
resolve()
})
}) })
}) })
it('should append the backdrop html once and include the "fade" class if it is "shown" and "animated"', done => { it('should not append the backdrop html if it is not "shown"', () => {
const instance = new Backdrop({ return new Promise(resolve => {
isVisible: true, const instance = new Backdrop({
isAnimated: true isVisible: false,
isAnimated: true
})
const getElements = () => document.querySelectorAll(CLASS_BACKDROP)
expect(getElements()).toHaveSize(0)
instance.show(() => {
expect(getElements()).toHaveSize(0)
resolve()
})
}) })
const getElements = () => document.querySelectorAll(CLASS_BACKDROP) })
expect(getElements()).toHaveSize(0) it('should append the backdrop html once and include the "fade" class if it is "shown" and "animated"', () => {
return new Promise(resolve => {
const instance = new Backdrop({
isVisible: true,
isAnimated: true
})
const getElements = () => document.querySelectorAll(CLASS_BACKDROP)
instance.show(() => { expect(getElements()).toHaveSize(0)
expect(getElements()).toHaveSize(1)
for (const el of getElements()) {
expect(el).toHaveClass(CLASS_NAME_FADE)
}
done() instance.show(() => {
expect(getElements()).toHaveSize(1)
for (const el of getElements()) {
expect(el).toHaveClass(CLASS_NAME_FADE)
}
resolve()
})
}) })
}) })
}) })
describe('hide', () => { describe('hide', () => {
it('should remove the backdrop html', done => { it('should remove the backdrop html', () => {
const instance = new Backdrop({ return new Promise(resolve => {
isVisible: true, const instance = new Backdrop({
isAnimated: true isVisible: true,
}) isAnimated: true
})
const getElements = () => document.body.querySelectorAll(CLASS_BACKDROP) const getElements = () => document.body.querySelectorAll(CLASS_BACKDROP)
expect(getElements()).toHaveSize(0) expect(getElements()).toHaveSize(0)
instance.show(() => { instance.show(() => {
expect(getElements()).toHaveSize(1) expect(getElements()).toHaveSize(1)
instance.hide(() => { instance.hide(() => {
expect(getElements()).toHaveSize(0) expect(getElements()).toHaveSize(0)
done() resolve()
})
}) })
}) })
}) })
it('should remove the "show" class', done => { it('should remove the "show" class', () => {
const instance = new Backdrop({ return new Promise(resolve => {
isVisible: true, const instance = new Backdrop({
isAnimated: true isVisible: true,
}) isAnimated: true
const elem = instance._getElement() })
const elem = instance._getElement()
instance.show() instance.show()
instance.hide(() => {
expect(elem).not.toHaveClass(CLASS_NAME_SHOW)
done()
})
})
it('should not try to remove Node on remove method if it is not "shown"', done => {
const instance = new Backdrop({
isVisible: false,
isAnimated: true
})
const getElements = () => document.querySelectorAll(CLASS_BACKDROP)
const spy = spyOn(instance, 'dispose').and.callThrough()
expect(getElements()).toHaveSize(0)
expect(instance._isAppended).toBeFalse()
instance.show(() => {
instance.hide(() => { instance.hide(() => {
expect(getElements()).toHaveSize(0) expect(elem).not.toHaveClass(CLASS_NAME_SHOW)
expect(spy).not.toHaveBeenCalled() resolve()
expect(instance._isAppended).toBeFalse()
done()
}) })
}) })
}) })
it('should not error if the backdrop no longer has a parent', done => { it('should not try to remove Node on remove method if it is not "shown"', () => {
fixtureEl.innerHTML = '<div id="wrapper"></div>' return new Promise(resolve => {
const instance = new Backdrop({
isVisible: false,
isAnimated: true
})
const getElements = () => document.querySelectorAll(CLASS_BACKDROP)
const spy = spyOn(instance, 'dispose').and.callThrough()
const wrapper = fixtureEl.querySelector('#wrapper') expect(getElements()).toHaveSize(0)
const instance = new Backdrop({ expect(instance._isAppended).toBeFalse()
isVisible: true, instance.show(() => {
isAnimated: true, instance.hide(() => {
rootElement: wrapper expect(getElements()).toHaveSize(0)
expect(spy).not.toHaveBeenCalled()
expect(instance._isAppended).toBeFalse()
resolve()
})
})
}) })
})
const getElements = () => document.querySelectorAll(CLASS_BACKDROP) it('should not error if the backdrop no longer has a parent', () => {
return new Promise(resolve => {
fixtureEl.innerHTML = '<div id="wrapper"></div>'
instance.show(() => { const wrapper = fixtureEl.querySelector('#wrapper')
wrapper.remove() const instance = new Backdrop({
instance.hide(() => { isVisible: true,
expect(getElements()).toHaveSize(0) isAnimated: true,
done() rootElement: wrapper
})
const getElements = () => document.querySelectorAll(CLASS_BACKDROP)
instance.show(() => {
wrapper.remove()
instance.hide(() => {
expect(getElements()).toHaveSize(0)
resolve()
})
}) })
}) })
}) })
}) })
describe('click callback', () => { describe('click callback', () => {
it('should execute callback on click', done => { it('should execute callback on click', () => {
const spy = jasmine.createSpy('spy') return new Promise(resolve => {
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 = new Event('mousedown', { bubbles: true, cancelable: true })
document.querySelector(CLASS_BACKDROP).dispatchEvent(clickEvent)
endTest()
})
})
})
describe('animation callbacks', () => {
it('should show and hide backdrop after counting transition duration if it is animated', done => {
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()
})
it('should show and hide backdrop without a delay if it is not animated', done => {
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)
})
it('should not call delay callbacks if it is not "shown"', done => {
const instance = new Backdrop({
isVisible: false,
isAnimated: true
})
const spy = jasmine.createSpy('spy', getTransitionDurationFromElement)
instance.show()
instance.hide(() => {
expect(spy).not.toHaveBeenCalled()
done()
})
})
})
describe('Config', () => {
describe('rootElement initialization', () => {
it('should be appended on "document.body" by default', done => {
const instance = new Backdrop({
isVisible: true
})
const getElement = () => document.querySelector(CLASS_BACKDROP)
instance.show(() => {
expect(getElement().parentElement).toEqual(document.body)
done()
})
})
it('should find the rootElement if passed as a string', done => {
const instance = new Backdrop({ const instance = new Backdrop({
isVisible: true, isVisible: true,
rootElement: 'body' isAnimated: false,
clickCallback: () => spy()
}) })
const getElement = () => document.querySelector(CLASS_BACKDROP) const endTest = () => {
instance.show(() => { setTimeout(() => {
expect(getElement().parentElement).toEqual(document.body) expect(spy).toHaveBeenCalled()
done() resolve()
}) }, 10)
}) }
it('should be appended on any element given by the proper config', done => {
fixtureEl.innerHTML = '<div id="wrapper"></div>'
const wrapper = fixtureEl.querySelector('#wrapper')
const instance = new Backdrop({
isVisible: true,
rootElement: wrapper
})
const getElement = () => document.querySelector(CLASS_BACKDROP)
instance.show(() => { instance.show(() => {
expect(getElement().parentElement).toEqual(wrapper) const clickEvent = new Event('mousedown', { bubbles: true, cancelable: true })
done() document.querySelector(CLASS_BACKDROP).dispatchEvent(clickEvent)
endTest()
}) })
}) })
}) })
describe('ClassName', () => { describe('animation callbacks', () => {
it('should allow configuring className', done => { it('should show and hide backdrop after counting transition duration if it is animated', () => {
const instance = new Backdrop({ return new Promise(resolve => {
isVisible: true, const instance = new Backdrop({
className: 'foo' isVisible: true,
isAnimated: true
})
const spy2 = jasmine.createSpy('spy2')
const execDone = () => {
setTimeout(() => {
expect(spy2).toHaveBeenCalledTimes(2)
resolve()
}, 10)
}
instance.show(spy2)
instance.hide(() => {
spy2()
execDone()
})
expect(spy2).not.toHaveBeenCalled()
}) })
const getElement = () => document.querySelector('.foo') })
instance.show(() => {
expect(getElement()).toEqual(instance._getElement()) it('should show and hide backdrop without a delay if it is not animated', () => {
instance.dispose() return new Promise(resolve => {
done() 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()
resolve()
}, 10)
})
})
it('should not call delay callbacks if it is not "shown"', () => {
return new Promise(resolve => {
const instance = new Backdrop({
isVisible: false,
isAnimated: true
})
const spy = jasmine.createSpy('spy', getTransitionDurationFromElement)
instance.show()
instance.hide(() => {
expect(spy).not.toHaveBeenCalled()
resolve()
})
})
})
})
describe('Config', () => {
describe('rootElement initialization', () => {
it('should be appended on "document.body" by default', () => {
return new Promise(resolve => {
const instance = new Backdrop({
isVisible: true
})
const getElement = () => document.querySelector(CLASS_BACKDROP)
instance.show(() => {
expect(getElement().parentElement).toEqual(document.body)
resolve()
})
})
})
it('should find the rootElement if passed as a string', () => {
return new Promise(resolve => {
const instance = new Backdrop({
isVisible: true,
rootElement: 'body'
})
const getElement = () => document.querySelector(CLASS_BACKDROP)
instance.show(() => {
expect(getElement().parentElement).toEqual(document.body)
resolve()
})
})
})
it('should be appended on any element given by the proper config', () => {
return new Promise(resolve => {
fixtureEl.innerHTML = '<div id="wrapper"></div>'
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)
resolve()
})
})
})
})
describe('ClassName', () => {
it('should allow configuring className', () => {
return new Promise(resolve => {
const instance = new Backdrop({
isVisible: true,
className: 'foo'
})
const getElement = () => document.querySelector('.foo')
instance.show(() => {
expect(getElement()).toEqual(instance._getElement())
instance.dispose()
resolve()
})
})
}) })
}) })
}) })

View File

@ -1,7 +1,7 @@
import FocusTrap from '../../../src/util/focustrap' import FocusTrap from '../../../src/util/focustrap'
import EventHandler from '../../../src/dom/event-handler' import EventHandler from '../../../src/dom/event-handler'
import SelectorEngine from '../../../src/dom/selector-engine' import SelectorEngine from '../../../src/dom/selector-engine'
import { clearFixture, getFixture, createEvent } from '../../helpers/fixture' import { clearFixture, createEvent, getFixture } from '../../helpers/fixture'
describe('FocusTrap', () => { describe('FocusTrap', () => {
let fixtureEl let fixtureEl
@ -41,140 +41,148 @@ describe('FocusTrap', () => {
expect(trapElement.focus).not.toHaveBeenCalled() expect(trapElement.focus).not.toHaveBeenCalled()
}) })
it('should force focus inside focus trap if it can', done => { it('should force focus inside focus trap if it can', () => {
fixtureEl.innerHTML = [ return new Promise(resolve => {
'<a href="#" id="outside">outside</a>', fixtureEl.innerHTML = [
'<div id="focustrap" tabindex="-1">', '<a href="#" id="outside">outside</a>',
' <a href="#" id="inside">inside</a>', '<div id="focustrap" tabindex="-1">',
'</div>' ' <a href="#" id="inside">inside</a>',
].join('') '</div>'
].join('')
const trapElement = fixtureEl.querySelector('div') const trapElement = fixtureEl.querySelector('div')
const focustrap = new FocusTrap({ trapElement }) const focustrap = new FocusTrap({ trapElement })
focustrap.activate() focustrap.activate()
const inside = document.getElementById('inside') const inside = document.getElementById('inside')
const focusInListener = () => { const focusInListener = () => {
expect(inside.focus).toHaveBeenCalled() expect(inside.focus).toHaveBeenCalled()
document.removeEventListener('focusin', focusInListener) document.removeEventListener('focusin', focusInListener)
done() resolve()
} }
spyOn(inside, 'focus') spyOn(inside, 'focus')
spyOn(SelectorEngine, 'focusableChildren').and.callFake(() => [inside]) spyOn(SelectorEngine, 'focusableChildren').and.callFake(() => [inside])
document.addEventListener('focusin', focusInListener) document.addEventListener('focusin', focusInListener)
const focusInEvent = createEvent('focusin', { bubbles: true }) const focusInEvent = createEvent('focusin', { bubbles: true })
Object.defineProperty(focusInEvent, 'target', { Object.defineProperty(focusInEvent, 'target', {
value: document.getElementById('outside') value: document.getElementById('outside')
})
document.dispatchEvent(focusInEvent)
}) })
document.dispatchEvent(focusInEvent)
}) })
it('should wrap focus around forward on tab', done => { it('should wrap focus around forward on tab', () => {
fixtureEl.innerHTML = [ return new Promise(resolve => {
'<a href="#" id="outside">outside</a>', fixtureEl.innerHTML = [
'<div id="focustrap" tabindex="-1">', '<a href="#" id="outside">outside</a>',
' <a href="#" id="first">first</a>', '<div id="focustrap" tabindex="-1">',
' <a href="#" id="inside">inside</a>', ' <a href="#" id="first">first</a>',
' <a href="#" id="last">last</a>', ' <a href="#" id="inside">inside</a>',
'</div>' ' <a href="#" id="last">last</a>',
].join('') '</div>'
].join('')
const trapElement = fixtureEl.querySelector('div') const trapElement = fixtureEl.querySelector('div')
const focustrap = new FocusTrap({ trapElement }) const focustrap = new FocusTrap({ trapElement })
focustrap.activate() focustrap.activate()
const first = document.getElementById('first') const first = document.getElementById('first')
const inside = document.getElementById('inside') const inside = document.getElementById('inside')
const last = document.getElementById('last') const last = document.getElementById('last')
const outside = document.getElementById('outside') const outside = document.getElementById('outside')
spyOn(SelectorEngine, 'focusableChildren').and.callFake(() => [first, inside, last]) spyOn(SelectorEngine, 'focusableChildren').and.callFake(() => [first, inside, last])
spyOn(first, 'focus').and.callThrough() spyOn(first, 'focus').and.callThrough()
const focusInListener = () => { const focusInListener = () => {
expect(first.focus).toHaveBeenCalled() expect(first.focus).toHaveBeenCalled()
first.removeEventListener('focusin', focusInListener) first.removeEventListener('focusin', focusInListener)
done() resolve()
} }
first.addEventListener('focusin', focusInListener) first.addEventListener('focusin', focusInListener)
const keydown = createEvent('keydown') const keydown = createEvent('keydown')
keydown.key = 'Tab' keydown.key = 'Tab'
document.dispatchEvent(keydown) document.dispatchEvent(keydown)
outside.focus() outside.focus()
})
it('should wrap focus around backwards on shift-tab', done => {
fixtureEl.innerHTML = [
'<a href="#" id="outside">outside</a>',
'<div id="focustrap" tabindex="-1">',
' <a href="#" id="first">first</a>',
' <a href="#" id="inside">inside</a>',
' <a href="#" id="last">last</a>',
'</div>'
].join('')
const trapElement = fixtureEl.querySelector('div')
const focustrap = new FocusTrap({ trapElement })
focustrap.activate()
const first = document.getElementById('first')
const inside = document.getElementById('inside')
const last = document.getElementById('last')
const outside = document.getElementById('outside')
spyOn(SelectorEngine, 'focusableChildren').and.callFake(() => [first, inside, last])
spyOn(last, 'focus').and.callThrough()
const focusInListener = () => {
expect(last.focus).toHaveBeenCalled()
last.removeEventListener('focusin', focusInListener)
done()
}
last.addEventListener('focusin', focusInListener)
const keydown = createEvent('keydown')
keydown.key = 'Tab'
keydown.shiftKey = true
document.dispatchEvent(keydown)
outside.focus()
})
it('should force focus on itself if there is no focusable content', done => {
fixtureEl.innerHTML = [
'<a href="#" id="outside">outside</a>',
'<div id="focustrap" tabindex="-1"></div>'
].join('')
const trapElement = fixtureEl.querySelector('div')
const focustrap = new FocusTrap({ trapElement })
focustrap.activate()
const focusInListener = () => {
expect(focustrap._config.trapElement.focus).toHaveBeenCalled()
document.removeEventListener('focusin', focusInListener)
done()
}
spyOn(focustrap._config.trapElement, 'focus')
document.addEventListener('focusin', focusInListener)
const focusInEvent = createEvent('focusin', { bubbles: true })
Object.defineProperty(focusInEvent, 'target', {
value: document.getElementById('outside')
}) })
})
document.dispatchEvent(focusInEvent) it('should wrap focus around backwards on shift-tab', () => {
return new Promise(resolve => {
fixtureEl.innerHTML = [
'<a href="#" id="outside">outside</a>',
'<div id="focustrap" tabindex="-1">',
' <a href="#" id="first">first</a>',
' <a href="#" id="inside">inside</a>',
' <a href="#" id="last">last</a>',
'</div>'
].join('')
const trapElement = fixtureEl.querySelector('div')
const focustrap = new FocusTrap({ trapElement })
focustrap.activate()
const first = document.getElementById('first')
const inside = document.getElementById('inside')
const last = document.getElementById('last')
const outside = document.getElementById('outside')
spyOn(SelectorEngine, 'focusableChildren').and.callFake(() => [first, inside, last])
spyOn(last, 'focus').and.callThrough()
const focusInListener = () => {
expect(last.focus).toHaveBeenCalled()
last.removeEventListener('focusin', focusInListener)
resolve()
}
last.addEventListener('focusin', focusInListener)
const keydown = createEvent('keydown')
keydown.key = 'Tab'
keydown.shiftKey = true
document.dispatchEvent(keydown)
outside.focus()
})
})
it('should force focus on itself if there is no focusable content', () => {
return new Promise(resolve => {
fixtureEl.innerHTML = [
'<a href="#" id="outside">outside</a>',
'<div id="focustrap" tabindex="-1"></div>'
].join('')
const trapElement = fixtureEl.querySelector('div')
const focustrap = new FocusTrap({ trapElement })
focustrap.activate()
const focusInListener = () => {
expect(focustrap._config.trapElement.focus).toHaveBeenCalled()
document.removeEventListener('focusin', focusInListener)
resolve()
}
spyOn(focustrap._config.trapElement, 'focus')
document.addEventListener('focusin', focusInListener)
const focusInEvent = createEvent('focusin', { bubbles: true })
Object.defineProperty(focusInEvent, 'target', {
value: document.getElementById('outside')
})
document.dispatchEvent(focusInEvent)
})
}) })
}) })

View File

@ -1,5 +1,6 @@
import * as Util from '../../../src/util/index' import * as Util from '../../../src/util/index'
import { clearFixture, getFixture } from '../../helpers/fixture' import { clearFixture, getFixture } from '../../helpers/fixture'
import { noop } from '../../../src/util/index'
describe('Util', () => { describe('Util', () => {
let fixtureEl let fixtureEl
@ -154,18 +155,20 @@ describe('Util', () => {
}) })
describe('triggerTransitionEnd', () => { describe('triggerTransitionEnd', () => {
it('should trigger transitionend event', done => { it('should trigger transitionend event', () => {
fixtureEl.innerHTML = '<div></div>' return new Promise(resolve => {
fixtureEl.innerHTML = '<div></div>'
const el = fixtureEl.querySelector('div') const el = fixtureEl.querySelector('div')
const spy = spyOn(el, 'dispatchEvent').and.callThrough() const spy = spyOn(el, 'dispatchEvent').and.callThrough()
el.addEventListener('transitionend', () => { el.addEventListener('transitionend', () => {
expect(spy).toHaveBeenCalled() expect(spy).toHaveBeenCalled()
done() resolve()
})
Util.triggerTransitionEnd(el)
}) })
Util.triggerTransitionEnd(el)
}) })
}) })
@ -611,9 +614,9 @@ describe('Util', () => {
}) })
it('should define a plugin on the jQuery instance', () => { it('should define a plugin on the jQuery instance', () => {
const pluginMock = function () {} const pluginMock = Util.noop
pluginMock.NAME = 'test' pluginMock.NAME = 'test'
pluginMock.jQueryInterface = function () {} pluginMock.jQueryInterface = Util.noop
Util.defineJQueryPlugin(pluginMock) Util.defineJQueryPlugin(pluginMock)
expect(fakejQuery.fn.test).toEqual(pluginMock.jQueryInterface) expect(fakejQuery.fn.test).toEqual(pluginMock.jQueryInterface)
@ -658,96 +661,104 @@ describe('Util', () => {
expect(callbackSpy).toHaveBeenCalled() expect(callbackSpy).toHaveBeenCalled()
}) })
it('should execute a function after a computed CSS transition duration and there was no transitionend event dispatched', done => { it('should execute a function after a computed CSS transition duration and there was no transitionend event dispatched', () => {
const el = document.createElement('div') return new Promise(resolve => {
const callbackSpy = jasmine.createSpy('callback spy') const el = document.createElement('div')
const callbackSpy = jasmine.createSpy('callback spy')
spyOn(window, 'getComputedStyle').and.returnValue({ spyOn(window, 'getComputedStyle').and.returnValue({
transitionDuration: '0.05s', transitionDuration: '0.05s',
transitionDelay: '0s' transitionDelay: '0s'
})
Util.executeAfterTransition(callbackSpy, el)
setTimeout(() => {
expect(callbackSpy).toHaveBeenCalled()
resolve()
}, 70)
}) })
Util.executeAfterTransition(callbackSpy, el)
setTimeout(() => {
expect(callbackSpy).toHaveBeenCalled()
done()
}, 70)
}) })
it('should not execute a function a second time after a computed CSS transition duration and if a transitionend event has already been dispatched', done => { it('should not execute a function a second time after a computed CSS transition duration and if a transitionend event has already been dispatched', () => {
const el = document.createElement('div') return new Promise(resolve => {
const callbackSpy = jasmine.createSpy('callback spy') const el = document.createElement('div')
const callbackSpy = jasmine.createSpy('callback spy')
spyOn(window, 'getComputedStyle').and.returnValue({ spyOn(window, 'getComputedStyle').and.returnValue({
transitionDuration: '0.05s', transitionDuration: '0.05s',
transitionDelay: '0s' transitionDelay: '0s'
})
Util.executeAfterTransition(callbackSpy, el)
setTimeout(() => {
el.dispatchEvent(new TransitionEvent('transitionend'))
}, 50)
setTimeout(() => {
expect(callbackSpy).toHaveBeenCalledTimes(1)
resolve()
}, 70)
}) })
})
Util.executeAfterTransition(callbackSpy, el) it('should not trigger a transitionend event if another transitionend event had already happened', () => {
return new Promise(resolve => {
const el = document.createElement('div')
setTimeout(() => { spyOn(window, 'getComputedStyle').and.returnValue({
transitionDuration: '0.05s',
transitionDelay: '0s'
})
Util.executeAfterTransition(noop, el)
// simulate a event dispatched by the browser
el.dispatchEvent(new TransitionEvent('transitionend')) el.dispatchEvent(new TransitionEvent('transitionend'))
}, 50)
setTimeout(() => { const dispatchSpy = spyOn(el, 'dispatchEvent').and.callThrough()
expect(callbackSpy).toHaveBeenCalledTimes(1)
done() setTimeout(() => {
}, 70) // setTimeout should not have triggered another transitionend event.
expect(dispatchSpy).not.toHaveBeenCalled()
resolve()
}, 70)
})
}) })
it('should not trigger a transitionend event if another transitionend event had already happened', done => { it('should ignore transitionend events from nested elements', () => {
const el = document.createElement('div') return new Promise(resolve => {
fixtureEl.innerHTML = [
'<div class="outer">',
' <div class="nested"></div>',
'</div>'
].join('')
spyOn(window, 'getComputedStyle').and.returnValue({ const outer = fixtureEl.querySelector('.outer')
transitionDuration: '0.05s', const nested = fixtureEl.querySelector('.nested')
transitionDelay: '0s' const callbackSpy = jasmine.createSpy('callback spy')
spyOn(window, 'getComputedStyle').and.returnValue({
transitionDuration: '0.05s',
transitionDelay: '0s'
})
Util.executeAfterTransition(callbackSpy, outer)
nested.dispatchEvent(new TransitionEvent('transitionend', {
bubbles: true
}))
setTimeout(() => {
expect(callbackSpy).not.toHaveBeenCalled()
}, 20)
setTimeout(() => {
expect(callbackSpy).toHaveBeenCalled()
resolve()
}, 70)
}) })
Util.executeAfterTransition(() => {}, el)
// simulate a event dispatched by the browser
el.dispatchEvent(new TransitionEvent('transitionend'))
const dispatchSpy = spyOn(el, 'dispatchEvent').and.callThrough()
setTimeout(() => {
// setTimeout should not have triggered another transitionend event.
expect(dispatchSpy).not.toHaveBeenCalled()
done()
}, 70)
})
it('should ignore transitionend events from nested elements', done => {
fixtureEl.innerHTML = [
'<div class="outer">',
' <div class="nested"></div>',
'</div>'
].join('')
const outer = fixtureEl.querySelector('.outer')
const nested = fixtureEl.querySelector('.nested')
const callbackSpy = jasmine.createSpy('callback spy')
spyOn(window, 'getComputedStyle').and.returnValue({
transitionDuration: '0.05s',
transitionDelay: '0s'
})
Util.executeAfterTransition(callbackSpy, outer)
nested.dispatchEvent(new TransitionEvent('transitionend', {
bubbles: true
}))
setTimeout(() => {
expect(callbackSpy).not.toHaveBeenCalled()
}, 20)
setTimeout(() => {
expect(callbackSpy).toHaveBeenCalled()
done()
}, 70)
}) })
}) })

View File

@ -101,7 +101,7 @@ describe('ScrollBar', () => {
}) })
describe('hide - reset', () => { describe('hide - reset', () => {
it('should adjust the inline padding of fixed elements which are full-width', done => { it('should adjust the inline padding of fixed elements which are full-width', () => {
fixtureEl.innerHTML = [ fixtureEl.innerHTML = [
'<div style="height: 110vh; width: 100%">', '<div style="height: 110vh; width: 100%">',
' <div class="fixed-top" id="fixed1" style="padding-right: 0px; width: 100vw"></div>', ' <div class="fixed-top" id="fixed1" style="padding-right: 0px; width: 100vw"></div>',
@ -134,10 +134,9 @@ describe('ScrollBar', () => {
expect(getPaddingAttr(fixedEl2)).toBeNull() expect(getPaddingAttr(fixedEl2)).toBeNull()
expect(currentPadding).toEqual(originalPadding) expect(currentPadding).toEqual(originalPadding)
expect(currentPadding2).toEqual(originalPadding2) expect(currentPadding2).toEqual(originalPadding2)
done()
}) })
it('should remove padding & margin if not existed before adjustment', done => { it('should remove padding & margin if not existed before adjustment', () => {
fixtureEl.innerHTML = [ fixtureEl.innerHTML = [
'<div style="height: 110vh; width: 100%">', '<div style="height: 110vh; width: 100%">',
' <div class="fixed" id="fixed" style="width: 100vw;"></div>', ' <div class="fixed" id="fixed" style="width: 100vw;"></div>',
@ -155,10 +154,9 @@ describe('ScrollBar', () => {
expect(fixedEl.getAttribute('style').includes('padding-right')).toBeFalse() expect(fixedEl.getAttribute('style').includes('padding-right')).toBeFalse()
expect(stickyEl.getAttribute('style').includes('margin-right')).toBeFalse() expect(stickyEl.getAttribute('style').includes('margin-right')).toBeFalse()
done()
}) })
it('should adjust the inline margin and padding of sticky elements', done => { it('should adjust the inline margin and padding of sticky elements', () => {
fixtureEl.innerHTML = [ fixtureEl.innerHTML = [
'<div style="height: 110vh">', '<div style="height: 110vh">',
' <div class="sticky-top" style="margin-right: 10px; padding-right: 20px; width: 100vw; height: 10px"></div>', ' <div class="sticky-top" style="margin-right: 10px; padding-right: 20px; width: 100vw; height: 10px"></div>',
@ -184,7 +182,6 @@ describe('ScrollBar', () => {
expect(getMarginX(stickyTopEl)).toEqual(originalMargin) expect(getMarginX(stickyTopEl)).toEqual(originalMargin)
expect(getPaddingAttr(stickyTopEl)).toBeNull() expect(getPaddingAttr(stickyTopEl)).toBeNull()
expect(getPaddingX(stickyTopEl)).toEqual(originalPadding) expect(getPaddingX(stickyTopEl)).toEqual(originalPadding)
done()
}) })
it('should not adjust the inline margin and padding of sticky and fixed elements when element do not have full width', () => { it('should not adjust the inline margin and padding of sticky and fixed elements when element do not have full width', () => {

View File

@ -78,74 +78,80 @@ describe('Swipe', () => {
}) })
describe('Config', () => { describe('Config', () => {
it('Test leftCallback', done => { it('Test leftCallback', () => {
const spyRight = jasmine.createSpy('spy') return new Promise(resolve => {
clearPointerEvents() const spyRight = jasmine.createSpy('spy')
defineDocumentElementOntouchstart() clearPointerEvents()
// eslint-disable-next-line no-new defineDocumentElementOntouchstart()
new Swipe(swipeEl, { // eslint-disable-next-line no-new
leftCallback: () => { new Swipe(swipeEl, {
expect(spyRight).not.toHaveBeenCalled() leftCallback: () => {
restorePointerEvents() expect(spyRight).not.toHaveBeenCalled()
done() restorePointerEvents()
}, resolve()
rightCallback: spyRight },
}) rightCallback: spyRight
})
mockSwipeGesture(swipeEl, { mockSwipeGesture(swipeEl, {
pos: [300, 10], pos: [300, 10],
deltaX: -300 deltaX: -300
})
}) })
}) })
it('Test rightCallback', done => { it('Test rightCallback', () => {
const spyLeft = jasmine.createSpy('spy') return new Promise(resolve => {
clearPointerEvents() const spyLeft = jasmine.createSpy('spy')
defineDocumentElementOntouchstart() clearPointerEvents()
// eslint-disable-next-line no-new defineDocumentElementOntouchstart()
new Swipe(swipeEl, { // eslint-disable-next-line no-new
rightCallback: () => { new Swipe(swipeEl, {
expect(spyLeft).not.toHaveBeenCalled() rightCallback: () => {
restorePointerEvents() expect(spyLeft).not.toHaveBeenCalled()
done() restorePointerEvents()
}, resolve()
leftCallback: spyLeft },
}) leftCallback: spyLeft
})
mockSwipeGesture(swipeEl, { mockSwipeGesture(swipeEl, {
pos: [10, 10], pos: [10, 10],
deltaX: 300 deltaX: 300
})
}) })
}) })
it('Test endCallback', done => { it('Test endCallback', () => {
clearPointerEvents() return new Promise(resolve => {
defineDocumentElementOntouchstart() clearPointerEvents()
let isFirstTime = true defineDocumentElementOntouchstart()
let isFirstTime = true
const callback = () => { const callback = () => {
if (isFirstTime) { if (isFirstTime) {
isFirstTime = false isFirstTime = false
return return
}
expect().nothing()
restorePointerEvents()
resolve()
} }
expect().nothing() // eslint-disable-next-line no-new
restorePointerEvents() new Swipe(swipeEl, {
done() endCallback: callback
} })
mockSwipeGesture(swipeEl, {
pos: [10, 10],
deltaX: 300
})
// eslint-disable-next-line no-new mockSwipeGesture(swipeEl, {
new Swipe(swipeEl, { pos: [300, 10],
endCallback: callback deltaX: -300
}) })
mockSwipeGesture(swipeEl, {
pos: [10, 10],
deltaX: 300
})
mockSwipeGesture(swipeEl, {
pos: [300, 10],
deltaX: -300
}) })
}) })
}) })
@ -170,53 +176,57 @@ describe('Swipe', () => {
expect(swipe._handleSwipe).not.toHaveBeenCalled() expect(swipe._handleSwipe).not.toHaveBeenCalled()
}) })
it('should allow swipeRight and call "rightCallback" with pointer events', done => { it('should allow swipeRight and call "rightCallback" with pointer events', () => {
if (!supportPointerEvent) { return new Promise(resolve => {
expect().nothing() if (!supportPointerEvent) {
done()
return
}
const style = '#fixture .pointer-event { touch-action: none !important; }'
fixtureEl.innerHTML += style
defineDocumentElementOntouchstart()
// eslint-disable-next-line no-new
new Swipe(swipeEl, {
rightCallback: () => {
deleteDocumentElementOntouchstart()
expect().nothing() expect().nothing()
done() resolve()
return
} }
})
mockSwipeGesture(swipeEl, { deltaX: 300 }, 'pointer') const style = '#fixture .pointer-event { touch-action: none !important; }'
fixtureEl.innerHTML += style
defineDocumentElementOntouchstart()
// eslint-disable-next-line no-new
new Swipe(swipeEl, {
rightCallback: () => {
deleteDocumentElementOntouchstart()
expect().nothing()
resolve()
}
})
mockSwipeGesture(swipeEl, { deltaX: 300 }, 'pointer')
})
}) })
it('should allow swipeLeft and call "leftCallback" with pointer events', done => { it('should allow swipeLeft and call "leftCallback" with pointer events', () => {
if (!supportPointerEvent) { return new Promise(resolve => {
expect().nothing() if (!supportPointerEvent) {
done()
return
}
const style = '#fixture .pointer-event { touch-action: none !important; }'
fixtureEl.innerHTML += style
defineDocumentElementOntouchstart()
// eslint-disable-next-line no-new
new Swipe(swipeEl, {
leftCallback: () => {
expect().nothing() expect().nothing()
deleteDocumentElementOntouchstart() resolve()
done() return
} }
})
mockSwipeGesture(swipeEl, { const style = '#fixture .pointer-event { touch-action: none !important; }'
pos: [300, 10], fixtureEl.innerHTML += style
deltaX: -300
}, 'pointer') defineDocumentElementOntouchstart()
// eslint-disable-next-line no-new
new Swipe(swipeEl, {
leftCallback: () => {
expect().nothing()
deleteDocumentElementOntouchstart()
resolve()
}
})
mockSwipeGesture(swipeEl, {
pos: [300, 10],
deltaX: -300
}, 'pointer')
})
}) })
}) })