0
0
mirror of https://github.com/twbs/bootstrap.git synced 2024-12-01 13:24:25 +01:00

Remove potential false positive assertions (#33288)

* Remove potential false positive assertions

querySelector() returns null but

expect(document.querySelector('...')).toBeDefined()

tests that the value is not undefined

* Migrated assertions from .toBeDefined() to .not.toBeNull() for .getInstance() calls in tests

* Migrate offcanvas assertions from .toBeDefined() to .not.toBeNull() for .getInstance() call

* convert more cases to not.toBeNull assertions

Co-authored-by: XhmikosR <xhmikosr@gmail.com>
This commit is contained in:
Ben Lertlumprasertkul 2021-05-11 06:45:57 +01:00 committed by GitHub
parent 9c3ab6557e
commit 052def4568
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 67 additions and 67 deletions

View File

@ -61,7 +61,7 @@ it('should show a tooltip without the animation', done => {
tooltipEl.addEventListener('shown.bs.tooltip', () => {
const tip = document.querySelector('.tooltip')
expect(tip).toBeDefined()
expect(tip).not.toBeNull()
expect(tip.classList.contains('fade')).toEqual(false)
done()
})

View File

@ -134,7 +134,7 @@ describe('Alert', () => {
const alertEl = document.querySelector('.alert')
const alert = new Alert(alertEl)
expect(Alert.getInstance(alertEl)).toBeDefined()
expect(Alert.getInstance(alertEl)).not.toBeNull()
alert.dispose()
@ -169,7 +169,7 @@ describe('Alert', () => {
jQueryMock.fn.alert.call(jQueryMock, 'close')
expect(Alert.getInstance(alertEl)).toBeDefined()
expect(Alert.getInstance(alertEl)).not.toBeNull()
expect(fixtureEl.querySelector('.alert')).toBeNull()
})
@ -183,7 +183,7 @@ describe('Alert', () => {
jQueryMock.fn.alert.call(jQueryMock)
expect(Alert.getInstance(alertEl)).toBeDefined()
expect(Alert.getInstance(alertEl)).not.toBeNull()
expect(fixtureEl.querySelector('.alert')).not.toBeNull()
})
})

View File

@ -91,7 +91,7 @@ describe('Button', () => {
const btnEl = fixtureEl.querySelector('.btn')
const button = new Button(btnEl)
expect(Button.getInstance(btnEl)).toBeDefined()
expect(Button.getInstance(btnEl)).not.toBeNull()
button.dispose()
@ -126,7 +126,7 @@ describe('Button', () => {
jQueryMock.fn.button.call(jQueryMock, 'toggle')
expect(Button.getInstance(btnEl)).toBeDefined()
expect(Button.getInstance(btnEl)).not.toBeNull()
expect(btnEl.classList.contains('active')).toEqual(true)
})
@ -140,7 +140,7 @@ describe('Button', () => {
jQueryMock.fn.button.call(jQueryMock)
expect(Button.getInstance(btnEl)).toBeDefined()
expect(Button.getInstance(btnEl)).not.toBeNull()
expect(btnEl.classList.contains('active')).toEqual(false)
})
})

View File

@ -1213,7 +1213,7 @@ describe('Carousel', () => {
jQueryMock.fn.carousel.call(jQueryMock)
expect(Carousel.getInstance(div)).toBeDefined()
expect(Carousel.getInstance(div)).not.toBeNull()
})
it('should not re create a carousel', () => {
@ -1271,7 +1271,7 @@ describe('Carousel', () => {
window.dispatchEvent(loadEvent)
expect(Carousel.getInstance(carouselEl)).toBeDefined()
expect(Carousel.getInstance(carouselEl)).not.toBeNull()
})
it('should create carousel and go to the next slide on click (with real button controls)', done => {

View File

@ -810,7 +810,7 @@ describe('Collapse', () => {
jQueryMock.fn.collapse.call(jQueryMock)
expect(Collapse.getInstance(div)).toBeDefined()
expect(Collapse.getInstance(div)).not.toBeNull()
})
it('should not re create a collapse', () => {

View File

@ -948,8 +948,8 @@ describe('Dropdown', () => {
const dropdown = new Dropdown(btnDropdown)
expect(dropdown._popper).toBeNull()
expect(dropdown._menu).toBeDefined()
expect(dropdown._element).toBeDefined()
expect(dropdown._menu).not.toBeNull()
expect(dropdown._element).not.toBeNull()
expect(btnDropdown.addEventListener).toHaveBeenCalledWith('click', jasmine.any(Function), jasmine.any(Boolean))
dropdown.dispose()
@ -974,9 +974,9 @@ describe('Dropdown', () => {
dropdown.toggle()
expect(dropdown._popper).toBeDefined()
expect(dropdown._menu).toBeDefined()
expect(dropdown._element).toBeDefined()
expect(dropdown._popper).not.toBeNull()
expect(dropdown._menu).not.toBeNull()
expect(dropdown._element).not.toBeNull()
dropdown.dispose()
@ -1002,7 +1002,7 @@ describe('Dropdown', () => {
dropdown.toggle()
expect(dropdown._popper).toBeDefined()
expect(dropdown._popper).not.toBeNull()
spyOn(dropdown._popper, 'update')
spyOn(dropdown, '_detectNavbar')
@ -1886,7 +1886,7 @@ describe('Dropdown', () => {
jQueryMock.fn.dropdown.call(jQueryMock)
expect(Dropdown.getInstance(div)).toBeDefined()
expect(Dropdown.getInstance(div)).not.toBeNull()
})
it('should not re create a dropdown', () => {

View File

@ -247,9 +247,9 @@ describe('Modal', () => {
modalEl.addEventListener('shown.bs.modal', () => {
expect(modalEl.getAttribute('aria-modal')).toEqual('true')
expect(modalEl.getAttribute('role')).toEqual('dialog')
expect(modalEl.getAttribute('aria-hidden')).toEqual(null)
expect(modalEl.getAttribute('aria-hidden')).toBeNull()
expect(modalEl.style.display).toEqual('block')
expect(document.querySelector('.modal-backdrop')).toBeDefined()
expect(document.querySelector('.modal-backdrop')).not.toBeNull()
done()
})
@ -271,7 +271,7 @@ describe('Modal', () => {
modalEl.addEventListener('shown.bs.modal', () => {
expect(modalEl.getAttribute('aria-modal')).toEqual('true')
expect(modalEl.getAttribute('role')).toEqual('dialog')
expect(modalEl.getAttribute('aria-hidden')).toEqual(null)
expect(modalEl.getAttribute('aria-hidden')).toBeNull()
expect(modalEl.style.display).toEqual('block')
expect(document.querySelector('.modal-backdrop')).toBeNull()
done()
@ -292,7 +292,7 @@ describe('Modal', () => {
modalEl.addEventListener('shown.bs.modal', () => {
const dynamicModal = document.getElementById(id)
expect(dynamicModal).toBeDefined()
expect(dynamicModal).not.toBeNull()
dynamicModal.parentNode.removeChild(dynamicModal)
done()
})
@ -763,8 +763,8 @@ describe('Modal', () => {
})
modalEl.addEventListener('hidden.bs.modal', () => {
expect(modalEl.getAttribute('aria-modal')).toEqual(null)
expect(modalEl.getAttribute('role')).toEqual(null)
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()
@ -785,8 +785,8 @@ describe('Modal', () => {
})
modalEl.addEventListener('hidden.bs.modal', () => {
expect(modalEl.getAttribute('aria-modal')).toEqual(null)
expect(modalEl.getAttribute('role')).toEqual(null)
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()
@ -862,7 +862,7 @@ describe('Modal', () => {
modal.dispose()
expect(Modal.getInstance(modalEl)).toEqual(null)
expect(Modal.getInstance(modalEl)).toBeNull()
expect(EventHandler.off).toHaveBeenCalledTimes(4)
})
})
@ -895,18 +895,18 @@ describe('Modal', () => {
modalEl.addEventListener('shown.bs.modal', () => {
expect(modalEl.getAttribute('aria-modal')).toEqual('true')
expect(modalEl.getAttribute('role')).toEqual('dialog')
expect(modalEl.getAttribute('aria-hidden')).toEqual(null)
expect(modalEl.getAttribute('aria-hidden')).toBeNull()
expect(modalEl.style.display).toEqual('block')
expect(document.querySelector('.modal-backdrop')).toBeDefined()
expect(document.querySelector('.modal-backdrop')).not.toBeNull()
setTimeout(() => trigger.click(), 10)
})
modalEl.addEventListener('hidden.bs.modal', () => {
expect(modalEl.getAttribute('aria-modal')).toEqual(null)
expect(modalEl.getAttribute('role')).toEqual(null)
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')).toEqual(null)
expect(document.querySelector('.modal-backdrop')).toBeNull()
done()
})
@ -947,9 +947,9 @@ describe('Modal', () => {
modalEl.addEventListener('shown.bs.modal', () => {
expect(modalEl.getAttribute('aria-modal')).toEqual('true')
expect(modalEl.getAttribute('role')).toEqual('dialog')
expect(modalEl.getAttribute('aria-hidden')).toEqual(null)
expect(modalEl.getAttribute('aria-hidden')).toBeNull()
expect(modalEl.style.display).toEqual('block')
expect(document.querySelector('.modal-backdrop')).toBeDefined()
expect(document.querySelector('.modal-backdrop')).not.toBeNull()
expect(Event.prototype.preventDefault).toHaveBeenCalled()
done()
})
@ -1057,7 +1057,7 @@ describe('Modal', () => {
jQueryMock.fn.modal.call(jQueryMock)
expect(Modal.getInstance(div)).toBeDefined()
expect(Modal.getInstance(div)).not.toBeNull()
})
it('should create a modal with given config', () => {
@ -1073,7 +1073,7 @@ describe('Modal', () => {
expect(Modal.prototype.constructor).not.toHaveBeenCalledWith(div, { keyboard: false })
const modal = Modal.getInstance(div)
expect(modal).toBeDefined()
expect(modal).not.toBeNull()
expect(modal._config.keyboard).toBe(false)
})
@ -1153,7 +1153,7 @@ describe('Modal', () => {
const div = fixtureEl.querySelector('div')
expect(Modal.getInstance(div)).toEqual(null)
expect(Modal.getInstance(div)).toBeNull()
})
})
})

View File

@ -560,7 +560,7 @@ describe('Offcanvas', () => {
jQueryMock.fn.offcanvas.call(jQueryMock)
expect(Offcanvas.getInstance(div)).toBeDefined()
expect(Offcanvas.getInstance(div)).not.toBeNull()
})
it('should not re create an offcanvas', () => {
@ -678,7 +678,7 @@ describe('Offcanvas', () => {
expect(Offcanvas.prototype.constructor).not.toHaveBeenCalledWith(div, { scroll: true })
const offcanvas = Offcanvas.getInstance(div)
expect(offcanvas).toBeDefined()
expect(offcanvas).not.toBeNull()
expect(offcanvas._config.scroll).toBe(true)
})
})
@ -699,7 +699,7 @@ describe('Offcanvas', () => {
const div = fixtureEl.querySelector('div')
expect(Offcanvas.getInstance(div)).toEqual(null)
expect(Offcanvas.getInstance(div)).toBeNull()
})
})
})

View File

@ -70,7 +70,7 @@ describe('Popover', () => {
const popover = new Popover(popoverEl)
popoverEl.addEventListener('shown.bs.popover', () => {
expect(document.querySelector('.popover')).toBeDefined()
expect(document.querySelector('.popover')).not.toBeNull()
done()
})
@ -89,7 +89,7 @@ describe('Popover', () => {
popoverEl.addEventListener('shown.bs.popover', () => {
const popoverDisplayed = document.querySelector('.popover')
expect(popoverDisplayed).toBeDefined()
expect(popoverDisplayed).not.toBeNull()
expect(popoverDisplayed.querySelector('.popover-header').textContent).toEqual('Bootstrap')
expect(popoverDisplayed.querySelector('.popover-body').textContent).toEqual('loves writing tests (╯°□°)╯︵ ┻━┻')
done()
@ -109,7 +109,7 @@ describe('Popover', () => {
popoverEl.addEventListener('shown.bs.popover', () => {
const popoverDisplayed = document.querySelector('.popover')
expect(popoverDisplayed).toBeDefined()
expect(popoverDisplayed).not.toBeNull()
expect(popoverDisplayed.querySelector('.popover-body').textContent).toEqual('Popover content')
done()
})
@ -125,7 +125,7 @@ describe('Popover', () => {
popoverEl.addEventListener('shown.bs.popover', () => {
const tip = document.querySelector('.popover')
expect(tip).toBeDefined()
expect(tip).not.toBeNull()
expect(tip.classList.contains('custom-class')).toBeTrue()
done()
})
@ -165,7 +165,7 @@ describe('Popover', () => {
jQueryMock.fn.popover.call(jQueryMock)
expect(Popover.getInstance(popoverEl)).toBeDefined()
expect(Popover.getInstance(popoverEl)).not.toBeNull()
})
it('should create a popover with a config object', () => {
@ -180,7 +180,7 @@ describe('Popover', () => {
content: 'Popover content'
})
expect(Popover.getInstance(popoverEl)).toBeDefined()
expect(Popover.getInstance(popoverEl)).not.toBeNull()
})
it('should not re create a popover', () => {

View File

@ -602,7 +602,7 @@ describe('ScrollSpy', () => {
jQueryMock.fn.scrollspy.call(jQueryMock)
expect(ScrollSpy.getInstance(div)).toBeDefined()
expect(ScrollSpy.getInstance(div)).not.toBeNull()
})
it('should create a scrollspy with given config', () => {
@ -618,7 +618,7 @@ describe('ScrollSpy', () => {
expect(ScrollSpy.prototype.constructor).not.toHaveBeenCalledWith(div, { offset: 15 })
const scrollspy = ScrollSpy.getInstance(div)
expect(scrollspy).toBeDefined()
expect(scrollspy).not.toBeNull()
expect(scrollspy._config.offset).toBe(15)
})

View File

@ -368,7 +368,7 @@ describe('Tab', () => {
jQueryMock.fn.tab.call(jQueryMock)
expect(Tab.getInstance(div)).toBeDefined()
expect(Tab.getInstance(div)).not.toBeNull()
})
it('should not re create a tab', () => {

View File

@ -472,7 +472,7 @@ describe('Toast', () => {
const toast = new Toast(toastEl)
expect(Toast.getInstance(toastEl)).toBeDefined()
expect(Toast.getInstance(toastEl)).not.toBeNull()
expect(toastEl.addEventListener).toHaveBeenCalledWith('click', jasmine.any(Function), jasmine.any(Boolean))
toast.dispose()
@ -494,7 +494,7 @@ describe('Toast', () => {
const toast = new Toast(toastEl)
const expected = () => {
expect(toastEl.classList.contains('show')).toEqual(true)
expect(Toast.getInstance(toastEl)).toBeDefined()
expect(Toast.getInstance(toastEl)).not.toBeNull()
toast.dispose()
@ -523,7 +523,7 @@ describe('Toast', () => {
jQueryMock.fn.toast.call(jQueryMock)
expect(Toast.getInstance(div)).toBeDefined()
expect(Toast.getInstance(div)).not.toBeNull()
})
it('should not re create a toast', () => {

View File

@ -194,7 +194,7 @@ describe('Tooltip', () => {
tooltip.enable()
tooltipEl.addEventListener('shown.bs.tooltip', () => {
expect(document.querySelector('.tooltip')).toBeDefined()
expect(document.querySelector('.tooltip')).not.toBeNull()
done()
})
@ -267,7 +267,7 @@ describe('Tooltip', () => {
const tooltip = new Tooltip(tooltipEl)
tooltipEl.addEventListener('shown.bs.tooltip', () => {
expect(document.querySelector('.tooltip')).toBeDefined()
expect(document.querySelector('.tooltip')).not.toBeNull()
done()
})
@ -386,7 +386,7 @@ describe('Tooltip', () => {
const tooltip = new Tooltip(tooltipEl)
tooltipEl.addEventListener('shown.bs.tooltip', () => {
expect(document.querySelector('.tooltip')).toBeDefined()
expect(document.querySelector('.tooltip')).not.toBeNull()
tooltip.dispose()
@ -408,7 +408,7 @@ describe('Tooltip', () => {
tooltipEl.addEventListener('shown.bs.tooltip', () => {
const tooltipShown = document.querySelector('.tooltip')
expect(tooltipShown).toBeDefined()
expect(tooltipShown).not.toBeNull()
expect(tooltipEl.getAttribute('aria-describedby')).toEqual(tooltipShown.getAttribute('id'))
expect(tooltipShown.getAttribute('id')).toContain('tooltip')
done()
@ -516,7 +516,7 @@ describe('Tooltip', () => {
})
tooltipEl.addEventListener('shown.bs.tooltip', () => {
expect(fixtureEl.querySelector('.tooltip')).toBeDefined()
expect(fixtureEl.querySelector('.tooltip')).not.toBeNull()
done()
})
@ -535,7 +535,7 @@ describe('Tooltip', () => {
})
tooltipEl.addEventListener('shown.bs.tooltip', () => {
expect(fixtureEl.querySelector('.tooltip')).toBeDefined()
expect(fixtureEl.querySelector('.tooltip')).not.toBeNull()
done()
})
@ -551,7 +551,7 @@ describe('Tooltip', () => {
})
tooltipEl.addEventListener('shown.bs.tooltip', () => {
expect(fixtureEl.querySelector('.tooltip')).toBeDefined()
expect(fixtureEl.querySelector('.tooltip')).not.toBeNull()
done()
})
@ -568,7 +568,7 @@ describe('Tooltip', () => {
})
tooltipEl.addEventListener('shown.bs.tooltip', () => {
expect(document.querySelector('.tooltip')).toBeDefined()
expect(document.querySelector('.tooltip')).not.toBeNull()
expect(spy).toHaveBeenCalled()
done()
})
@ -587,7 +587,7 @@ describe('Tooltip', () => {
tooltipEl.addEventListener('shown.bs.tooltip', () => {
const tip = document.querySelector('.tooltip')
expect(tip).toBeDefined()
expect(tip).not.toBeNull()
expect(tip.classList.contains('fade')).toEqual(false)
done()
})
@ -810,7 +810,7 @@ describe('Tooltip', () => {
tooltipEl.addEventListener('shown.bs.tooltip', () => {
const tip = document.querySelector('.tooltip')
expect(tip).toBeDefined()
expect(tip).not.toBeNull()
expect(tip.classList.contains('custom-class')).toBeTrue()
done()
})
@ -828,7 +828,7 @@ describe('Tooltip', () => {
tooltipEl.addEventListener('shown.bs.tooltip', () => {
const tip = document.querySelector('.tooltip')
expect(tip).toBeDefined()
expect(tip).not.toBeNull()
expect(tip.classList.contains('custom-class')).toBeTrue()
expect(tip.classList.contains('custom-class-2')).toBeTrue()
done()
@ -848,7 +848,7 @@ describe('Tooltip', () => {
tooltipEl.addEventListener('shown.bs.tooltip', () => {
const tip = document.querySelector('.tooltip')
expect(tip).toBeDefined()
expect(tip).not.toBeNull()
expect(spy).toHaveBeenCalled()
expect(tip.classList.contains('custom-class')).toBeTrue()
done()
@ -1263,7 +1263,7 @@ describe('Tooltip', () => {
tooltipEl.addEventListener('shown.bs.tooltip', () => {
const tooltipShown = document.querySelector('.tooltip')
expect(tooltipShown).toBeDefined()
expect(tooltipShown).not.toBeNull()
expect(tooltipEl.getAttribute('aria-label')).toEqual('Another tooltip')
done()
})
@ -1280,7 +1280,7 @@ describe('Tooltip', () => {
tooltipEl.addEventListener('shown.bs.tooltip', () => {
const tooltipShown = document.querySelector('.tooltip')
expect(tooltipShown).toBeDefined()
expect(tooltipShown).not.toBeNull()
expect(tooltipEl.getAttribute('aria-label')).toEqual('Different label')
done()
})
@ -1297,7 +1297,7 @@ describe('Tooltip', () => {
tooltipEl.addEventListener('shown.bs.tooltip', () => {
const tooltipShown = document.querySelector('.tooltip')
expect(tooltipShown).toBeDefined()
expect(tooltipShown).not.toBeNull()
expect(tooltipEl.getAttribute('aria-label')).toBeNull()
done()
})
@ -1317,7 +1317,7 @@ describe('Tooltip', () => {
jQueryMock.fn.tooltip.call(jQueryMock)
expect(Tooltip.getInstance(div)).toBeDefined()
expect(Tooltip.getInstance(div)).not.toBeNull()
})
it('should not re create a tooltip', () => {