1
0
mirror of https://github.com/twbs/bootstrap.git synced 2025-08-28 23:59:53 +02:00

JS: tests fixes & standardization of spies usage (#36398)

* Fix carousel spec typo
* Change carousel test name in align with testing method
* Make the spies declarations the same everywhere
This commit is contained in:
Louis-Maxime Piton
2022-05-31 10:18:32 +02:00
committed by GitHub
parent 78c0ad8044
commit d388bd6e1b
18 changed files with 338 additions and 342 deletions

View File

@@ -37,8 +37,8 @@ describe('Plugin functions', () => {
'</div>'
].join('')
spyOn(DummyClass2, 'getOrCreateInstance').and.callThrough()
spyOn(DummyClass2.prototype, 'testMethod')
const spyGet = spyOn(DummyClass2, 'getOrCreateInstance').and.callThrough()
const spyTest = spyOn(DummyClass2.prototype, 'testMethod')
const componentWrapper = fixtureEl.querySelector('#foo')
const btnClose = fixtureEl.querySelector('[data-bs-dismiss="test"]')
const event = createEvent('click')
@@ -46,8 +46,8 @@ describe('Plugin functions', () => {
enableDismissTrigger(DummyClass2, 'testMethod')
btnClose.dispatchEvent(event)
expect(DummyClass2.getOrCreateInstance).toHaveBeenCalledWith(componentWrapper)
expect(DummyClass2.prototype.testMethod).toHaveBeenCalled()
expect(spyGet).toHaveBeenCalledWith(componentWrapper)
expect(spyTest).toHaveBeenCalled()
})
it('if data-bs-dismiss="PluginName" hasn\'t got "data-bs-target", "getOrCreateInstance" has to be initialized by closest "plugin.Name" class', () => {
@@ -57,8 +57,8 @@ describe('Plugin functions', () => {
'</div>'
].join('')
spyOn(DummyClass2, 'getOrCreateInstance').and.callThrough()
spyOn(DummyClass2.prototype, 'hide')
const spyGet = spyOn(DummyClass2, 'getOrCreateInstance').and.callThrough()
const spyHide = spyOn(DummyClass2.prototype, 'hide')
const componentWrapper = fixtureEl.querySelector('#foo')
const btnClose = fixtureEl.querySelector('[data-bs-dismiss="test"]')
const event = createEvent('click')
@@ -66,8 +66,8 @@ describe('Plugin functions', () => {
enableDismissTrigger(DummyClass2)
btnClose.dispatchEvent(event)
expect(DummyClass2.getOrCreateInstance).toHaveBeenCalledWith(componentWrapper)
expect(DummyClass2.prototype.hide).toHaveBeenCalled()
expect(spyGet).toHaveBeenCalledWith(componentWrapper)
expect(spyHide).toHaveBeenCalled()
})
it('if data-bs-dismiss="PluginName" is disabled, must not trigger function', () => {
@@ -77,14 +77,14 @@ describe('Plugin functions', () => {
'</div>'
].join('')
spyOn(DummyClass2, 'getOrCreateInstance').and.callThrough()
const spy = spyOn(DummyClass2, 'getOrCreateInstance').and.callThrough()
const btnClose = fixtureEl.querySelector('[data-bs-dismiss="test"]')
const event = createEvent('click')
enableDismissTrigger(DummyClass2)
btnClose.dispatchEvent(event)
expect(DummyClass2.getOrCreateInstance).not.toHaveBeenCalled()
expect(spy).not.toHaveBeenCalled()
})
it('should prevent default when the trigger is <a> or <area>', () => {
@@ -98,11 +98,11 @@ describe('Plugin functions', () => {
const event = createEvent('click')
enableDismissTrigger(DummyClass2)
spyOn(Event.prototype, 'preventDefault').and.callThrough()
const spy = spyOn(Event.prototype, 'preventDefault').and.callThrough()
btnClose.dispatchEvent(event)
expect(Event.prototype.preventDefault).toHaveBeenCalled()
expect(spy).toHaveBeenCalled()
})
})
})

View File

@@ -20,12 +20,12 @@ describe('FocusTrap', () => {
const trapElement = fixtureEl.querySelector('div')
spyOn(trapElement, 'focus')
const spy = spyOn(trapElement, 'focus')
const focustrap = new FocusTrap({ trapElement })
focustrap.activate()
expect(trapElement.focus).toHaveBeenCalled()
expect(spy).toHaveBeenCalled()
})
it('if configured not to autofocus, should not autofocus itself', () => {
@@ -33,12 +33,12 @@ describe('FocusTrap', () => {
const trapElement = fixtureEl.querySelector('div')
spyOn(trapElement, 'focus')
const spy = spyOn(trapElement, 'focus')
const focustrap = new FocusTrap({ trapElement, autofocus: false })
focustrap.activate()
expect(trapElement.focus).not.toHaveBeenCalled()
expect(spy).not.toHaveBeenCalled()
})
it('should force focus inside focus trap if it can', () => {
@@ -57,12 +57,12 @@ describe('FocusTrap', () => {
const inside = document.getElementById('inside')
const focusInListener = () => {
expect(inside.focus).toHaveBeenCalled()
expect(spy).toHaveBeenCalled()
document.removeEventListener('focusin', focusInListener)
resolve()
}
spyOn(inside, 'focus')
const spy = spyOn(inside, 'focus')
spyOn(SelectorEngine, 'focusableChildren').and.callFake(() => [inside])
document.addEventListener('focusin', focusInListener)
@@ -97,10 +97,10 @@ describe('FocusTrap', () => {
const outside = document.getElementById('outside')
spyOn(SelectorEngine, 'focusableChildren').and.callFake(() => [first, inside, last])
spyOn(first, 'focus').and.callThrough()
const spy = spyOn(first, 'focus').and.callThrough()
const focusInListener = () => {
expect(first.focus).toHaveBeenCalled()
expect(spy).toHaveBeenCalled()
first.removeEventListener('focusin', focusInListener)
resolve()
}
@@ -136,10 +136,10 @@ describe('FocusTrap', () => {
const outside = document.getElementById('outside')
spyOn(SelectorEngine, 'focusableChildren').and.callFake(() => [first, inside, last])
spyOn(last, 'focus').and.callThrough()
const spy = spyOn(last, 'focus').and.callThrough()
const focusInListener = () => {
expect(last.focus).toHaveBeenCalled()
expect(spy).toHaveBeenCalled()
last.removeEventListener('focusin', focusInListener)
resolve()
}
@@ -167,12 +167,12 @@ describe('FocusTrap', () => {
focustrap.activate()
const focusInListener = () => {
expect(focustrap._config.trapElement.focus).toHaveBeenCalled()
expect(spy).toHaveBeenCalled()
document.removeEventListener('focusin', focusInListener)
resolve()
}
spyOn(focustrap._config.trapElement, 'focus')
const spy = spyOn(focustrap._config.trapElement, 'focus')
document.addEventListener('focusin', focusInListener)
@@ -200,19 +200,19 @@ describe('FocusTrap', () => {
const focustrap = new FocusTrap({ trapElement: fixtureEl })
focustrap.activate()
spyOn(EventHandler, 'off')
const spy = spyOn(EventHandler, 'off')
focustrap.deactivate()
expect(EventHandler.off).toHaveBeenCalled()
expect(spy).toHaveBeenCalled()
})
it('doesn\'t try removing event listeners unless it needs to (in case it hasn\'t been activated)', () => {
const focustrap = new FocusTrap({ trapElement: fixtureEl })
spyOn(EventHandler, 'off')
const spy = spyOn(EventHandler, 'off')
focustrap.deactivate()
expect(EventHandler.off).not.toHaveBeenCalled()
expect(spy).not.toHaveBeenCalled()
})
})
})

View File

@@ -576,7 +576,7 @@ describe('Util', () => {
const spy = jasmine.createSpy()
const spy2 = jasmine.createSpy()
spyOn(document, 'addEventListener').and.callThrough()
const spyAdd = spyOn(document, 'addEventListener').and.callThrough()
spyOnProperty(document, 'readyState').and.returnValue('loading')
Util.onDOMContentLoaded(spy)
@@ -589,7 +589,7 @@ describe('Util', () => {
expect(spy).toHaveBeenCalled()
expect(spy2).toHaveBeenCalled()
expect(document.addEventListener).toHaveBeenCalledTimes(1)
expect(spyAdd).toHaveBeenCalledTimes(1)
})
it('should execute callback if readyState is not "loading"', () => {

View File

@@ -84,12 +84,12 @@ describe('Sanitizer', () => {
return htmlUnsafe
}
spyOn(DOMParser.prototype, 'parseFromString')
const spy = spyOn(DOMParser.prototype, 'parseFromString')
const result = sanitizeHtml(template, DefaultAllowlist, mySanitize)
expect(result).toEqual(template)
expect(DOMParser.prototype.parseFromString).not.toHaveBeenCalled()
expect(spy).not.toHaveBeenCalled()
})
it('should allow multiple sanitation passes of the same template', () => {

View File

@@ -163,7 +163,7 @@ describe('Swipe', () => {
deleteDocumentElementOntouchstart()
const swipe = new Swipe(swipeEl)
spyOn(swipe, '_handleSwipe')
const spy = spyOn(swipe, '_handleSwipe')
mockSwipeGesture(swipeEl, {
pos: [300, 10],
@@ -173,7 +173,7 @@ describe('Swipe', () => {
})
restorePointerEvents()
expect(swipe._handleSwipe).not.toHaveBeenCalled()
expect(spy).not.toHaveBeenCalled()
})
it('should allow swipeRight and call "rightCallback" with pointer events', () => {