mirror of
https://github.com/twbs/bootstrap.git
synced 2025-08-20 04:11:39 +02:00
Register only one DOMContentLoaded
event listener in onDOMContentLoaded
(#34158)
* refactor: reuse one DOMContentLoaded event listener in onDOMContentLoaded function Instead of adding an event listener everytime the utility function is called, cache the callbacks and execute them all at once. * refactor: drop iife for onDOMContentLoaded Co-authored-by: XhmikosR <xhmikosr@gmail.com>
This commit is contained in:
@@ -582,15 +582,24 @@ describe('Util', () => {
|
||||
})
|
||||
|
||||
describe('onDOMContentLoaded', () => {
|
||||
it('should execute callback when DOMContentLoaded is fired', () => {
|
||||
it('should execute callbacks when DOMContentLoaded is fired and should not add more than one listener', () => {
|
||||
const spy = jasmine.createSpy()
|
||||
const spy2 = jasmine.createSpy()
|
||||
|
||||
spyOn(document, 'addEventListener').and.callThrough()
|
||||
spyOnProperty(document, 'readyState').and.returnValue('loading')
|
||||
|
||||
Util.onDOMContentLoaded(spy)
|
||||
window.document.dispatchEvent(new Event('DOMContentLoaded', {
|
||||
Util.onDOMContentLoaded(spy2)
|
||||
|
||||
document.dispatchEvent(new Event('DOMContentLoaded', {
|
||||
bubbles: true,
|
||||
cancelable: true
|
||||
}))
|
||||
|
||||
expect(spy).toHaveBeenCalled()
|
||||
expect(spy2).toHaveBeenCalled()
|
||||
expect(document.addEventListener).toHaveBeenCalledTimes(1)
|
||||
})
|
||||
|
||||
it('should execute callback if readyState is not "loading"', () => {
|
||||
|
Reference in New Issue
Block a user