1
0
mirror of https://github.com/twbs/bootstrap.git synced 2025-08-30 00:29:52 +02:00

tests: tweak Jasmine's matchers usage

Use:

* toBeNull
* toEqual
* toBeTrue
* toBeFalse
* toHaveSize
* toHaveClass
This commit is contained in:
XhmikosR
2021-10-14 18:16:54 +03:00
parent 5739bf7637
commit eb54e1a1ce
24 changed files with 438 additions and 437 deletions

View File

@@ -30,13 +30,13 @@ describe('Backdrop', () => {
})
const getElements = () => document.querySelectorAll(CLASS_BACKDROP)
expect(getElements().length).toEqual(0)
expect(getElements()).toHaveSize(0)
instance.show()
instance.show(() => {
expect(getElements().length).toEqual(1)
expect(getElements()).toHaveSize(1)
for (const el of getElements()) {
expect(el.classList.contains(CLASS_NAME_SHOW)).toEqual(true)
expect(el).toHaveClass(CLASS_NAME_SHOW)
}
done()
@@ -50,9 +50,9 @@ describe('Backdrop', () => {
})
const getElements = () => document.querySelectorAll(CLASS_BACKDROP)
expect(getElements().length).toEqual(0)
expect(getElements()).toHaveSize(0)
instance.show(() => {
expect(getElements().length).toEqual(0)
expect(getElements()).toHaveSize(0)
done()
})
})
@@ -64,12 +64,12 @@ describe('Backdrop', () => {
})
const getElements = () => document.querySelectorAll(CLASS_BACKDROP)
expect(getElements().length).toEqual(0)
expect(getElements()).toHaveSize(0)
instance.show(() => {
expect(getElements().length).toEqual(1)
expect(getElements()).toHaveSize(1)
for (const el of getElements()) {
expect(el.classList.contains(CLASS_NAME_FADE)).toEqual(true)
expect(el).toHaveClass(CLASS_NAME_FADE)
}
done()
@@ -86,11 +86,11 @@ describe('Backdrop', () => {
const getElements = () => document.body.querySelectorAll(CLASS_BACKDROP)
expect(getElements().length).toEqual(0)
expect(getElements()).toHaveSize(0)
instance.show(() => {
expect(getElements().length).toEqual(1)
expect(getElements()).toHaveSize(1)
instance.hide(() => {
expect(getElements().length).toEqual(0)
expect(getElements()).toHaveSize(0)
done()
})
})
@@ -105,7 +105,7 @@ describe('Backdrop', () => {
instance.show()
instance.hide(() => {
expect(elem.classList.contains(CLASS_NAME_SHOW)).toEqual(false)
expect(elem).not.toHaveClass(CLASS_NAME_SHOW)
done()
})
})
@@ -118,13 +118,13 @@ describe('Backdrop', () => {
const getElements = () => document.querySelectorAll(CLASS_BACKDROP)
const spy = spyOn(instance, 'dispose').and.callThrough()
expect(getElements().length).toEqual(0)
expect(instance._isAppended).toEqual(false)
expect(getElements()).toHaveSize(0)
expect(instance._isAppended).toBeFalse()
instance.show(() => {
instance.hide(() => {
expect(getElements().length).toEqual(0)
expect(getElements()).toHaveSize(0)
expect(spy).not.toHaveBeenCalled()
expect(instance._isAppended).toEqual(false)
expect(instance._isAppended).toBeFalse()
done()
})
})
@@ -145,7 +145,7 @@ describe('Backdrop', () => {
instance.show(() => {
wrapper.remove()
instance.hide(() => {
expect(getElements().length).toEqual(0)
expect(getElements()).toHaveSize(0)
done()
})
})

View File

@@ -182,10 +182,10 @@ describe('FocusTrap', () => {
it('should flag itself as no longer active', () => {
const focustrap = new FocusTrap({ trapElement: fixtureEl })
focustrap.activate()
expect(focustrap._isActive).toBe(true)
expect(focustrap._isActive).toBeTrue()
focustrap.deactivate()
expect(focustrap._isActive).toBe(false)
expect(focustrap._isActive).toBeFalse()
})
it('should remove all event listeners', () => {

View File

@@ -179,9 +179,9 @@ describe('Util', () => {
const el = fixtureEl.querySelector('#foo')
expect(Util.isElement(el)).toEqual(true)
expect(Util.isElement({})).toEqual(false)
expect(Util.isElement(fixtureEl.querySelectorAll('.test'))).toEqual(false)
expect(Util.isElement(el)).toBeTrue()
expect(Util.isElement({})).toBeFalse()
expect(Util.isElement(fixtureEl.querySelectorAll('.test'))).toBeFalse()
})
it('should detect jQuery element', () => {
@@ -193,7 +193,7 @@ describe('Util', () => {
jquery: 'foo'
}
expect(Util.isElement(fakejQuery)).toEqual(true)
expect(Util.isElement(fakejQuery)).toBeTrue()
})
})
@@ -274,12 +274,12 @@ describe('Util', () => {
describe('isVisible', () => {
it('should return false if the element is not defined', () => {
expect(Util.isVisible(null)).toEqual(false)
expect(Util.isVisible(undefined)).toEqual(false)
expect(Util.isVisible(null)).toBeFalse()
expect(Util.isVisible(undefined)).toBeFalse()
})
it('should return false if the element provided is not a dom element', () => {
expect(Util.isVisible({})).toEqual(false)
expect(Util.isVisible({})).toBeFalse()
})
it('should return false if the element is not visible with display none', () => {
@@ -287,7 +287,7 @@ describe('Util', () => {
const div = fixtureEl.querySelector('div')
expect(Util.isVisible(div)).toEqual(false)
expect(Util.isVisible(div)).toBeFalse()
})
it('should return false if the element is not visible with visibility hidden', () => {
@@ -295,7 +295,7 @@ describe('Util', () => {
const div = fixtureEl.querySelector('div')
expect(Util.isVisible(div)).toEqual(false)
expect(Util.isVisible(div)).toBeFalse()
})
it('should return false if an ancestor element is display none', () => {
@@ -311,7 +311,7 @@ describe('Util', () => {
const div = fixtureEl.querySelector('.content')
expect(Util.isVisible(div)).toEqual(false)
expect(Util.isVisible(div)).toBeFalse()
})
it('should return false if an ancestor element is visibility hidden', () => {
@@ -327,7 +327,7 @@ describe('Util', () => {
const div = fixtureEl.querySelector('.content')
expect(Util.isVisible(div)).toEqual(false)
expect(Util.isVisible(div)).toBeFalse()
})
it('should return true if an ancestor element is visibility hidden, but reverted', () => {
@@ -343,7 +343,7 @@ describe('Util', () => {
const div = fixtureEl.querySelector('.content')
expect(Util.isVisible(div)).toEqual(true)
expect(Util.isVisible(div)).toBeTrue()
})
it('should return true if the element is visible', () => {
@@ -355,7 +355,7 @@ describe('Util', () => {
const div = fixtureEl.querySelector('#element')
expect(Util.isVisible(div)).toEqual(true)
expect(Util.isVisible(div)).toBeTrue()
})
it('should return false if the element is hidden, but not via display or visibility', () => {
@@ -367,20 +367,20 @@ describe('Util', () => {
const div = fixtureEl.querySelector('#element')
expect(Util.isVisible(div)).toEqual(false)
expect(Util.isVisible(div)).toBeFalse()
})
})
describe('isDisabled', () => {
it('should return true if the element is not defined', () => {
expect(Util.isDisabled(null)).toEqual(true)
expect(Util.isDisabled(undefined)).toEqual(true)
expect(Util.isDisabled()).toEqual(true)
expect(Util.isDisabled(null)).toBeTrue()
expect(Util.isDisabled(undefined)).toBeTrue()
expect(Util.isDisabled()).toBeTrue()
})
it('should return true if the element provided is not a dom element', () => {
expect(Util.isDisabled({})).toEqual(true)
expect(Util.isDisabled('test')).toEqual(true)
expect(Util.isDisabled({})).toBeTrue()
expect(Util.isDisabled('test')).toBeTrue()
})
it('should return true if the element has disabled attribute', () => {
@@ -396,9 +396,9 @@ describe('Util', () => {
const div1 = fixtureEl.querySelector('#element1')
const div2 = fixtureEl.querySelector('#element2')
expect(Util.isDisabled(div)).toEqual(true)
expect(Util.isDisabled(div1)).toEqual(true)
expect(Util.isDisabled(div2)).toEqual(true)
expect(Util.isDisabled(div)).toBeTrue()
expect(Util.isDisabled(div1)).toBeTrue()
expect(Util.isDisabled(div2)).toBeTrue()
})
it('should return false if the element has disabled attribute with "false" value, or doesn\'t have attribute', () => {
@@ -412,8 +412,8 @@ describe('Util', () => {
const div = fixtureEl.querySelector('#element')
const div1 = fixtureEl.querySelector('#element1')
expect(Util.isDisabled(div)).toEqual(false)
expect(Util.isDisabled(div1)).toEqual(false)
expect(Util.isDisabled(div)).toBeFalse()
expect(Util.isDisabled(div1)).toBeFalse()
})
it('should return false if the element is not disabled ', () => {
@@ -427,9 +427,9 @@ describe('Util', () => {
const el = selector => fixtureEl.querySelector(selector)
expect(Util.isDisabled(el('#button'))).toEqual(false)
expect(Util.isDisabled(el('#select'))).toEqual(false)
expect(Util.isDisabled(el('#input'))).toEqual(false)
expect(Util.isDisabled(el('#button'))).toBeFalse()
expect(Util.isDisabled(el('#select'))).toBeFalse()
expect(Util.isDisabled(el('#input'))).toBeFalse()
})
it('should return true if the element has disabled attribute', () => {
fixtureEl.innerHTML = [
@@ -446,12 +446,12 @@ describe('Util', () => {
const el = selector => fixtureEl.querySelector(selector)
expect(Util.isDisabled(el('#input'))).toEqual(true)
expect(Util.isDisabled(el('#input1'))).toEqual(true)
expect(Util.isDisabled(el('#button'))).toEqual(true)
expect(Util.isDisabled(el('#button1'))).toEqual(true)
expect(Util.isDisabled(el('#button2'))).toEqual(true)
expect(Util.isDisabled(el('#input'))).toEqual(true)
expect(Util.isDisabled(el('#input'))).toBeTrue()
expect(Util.isDisabled(el('#input1'))).toBeTrue()
expect(Util.isDisabled(el('#button'))).toBeTrue()
expect(Util.isDisabled(el('#button1'))).toBeTrue()
expect(Util.isDisabled(el('#button2'))).toBeTrue()
expect(Util.isDisabled(el('#input'))).toBeTrue()
})
it('should return true if the element has class "disabled"', () => {
@@ -463,7 +463,7 @@ describe('Util', () => {
const div = fixtureEl.querySelector('#element')
expect(Util.isDisabled(div)).toEqual(true)
expect(Util.isDisabled(div)).toBeTrue()
})
it('should return true if the element has class "disabled" but disabled attribute is false', () => {
@@ -475,7 +475,7 @@ describe('Util', () => {
const div = fixtureEl.querySelector('#input')
expect(Util.isDisabled(div)).toEqual(true)
expect(Util.isDisabled(div)).toBeTrue()
})
})
@@ -493,7 +493,7 @@ describe('Util', () => {
spyOn(document.documentElement, 'attachShadow').and.returnValue(null)
expect(Util.findShadowRoot(div)).toEqual(null)
expect(Util.findShadowRoot(div)).toBeNull()
})
it('should return null when we do not find a shadow root', () => {
@@ -505,7 +505,7 @@ describe('Util', () => {
spyOn(document, 'getRootNode').and.returnValue(undefined)
expect(Util.findShadowRoot(document)).toEqual(null)
expect(Util.findShadowRoot(document)).toBeNull()
})
it('should return the shadow root when found', () => {
@@ -532,7 +532,7 @@ describe('Util', () => {
describe('noop', () => {
it('should be a function', () => {
expect(typeof Util.noop).toEqual('function')
expect(Util.noop).toEqual(jasmine.any(Function))
})
})
@@ -569,14 +569,14 @@ describe('Util', () => {
document.body.setAttribute('data-bs-no-jquery', '')
expect(window.jQuery).toEqual(fakejQuery)
expect(Util.getjQuery()).toEqual(null)
expect(Util.getjQuery()).toBeNull()
document.body.removeAttribute('data-bs-no-jquery')
})
it('should not return jQuery if not present', () => {
window.jQuery = undefined
expect(Util.getjQuery()).toEqual(null)
expect(Util.getjQuery()).toBeNull()
})
})
@@ -628,9 +628,9 @@ describe('Util', () => {
pluginMock.jQueryInterface = function () {}
Util.defineJQueryPlugin(pluginMock)
expect(fakejQuery.fn.test).toBe(pluginMock.jQueryInterface)
expect(fakejQuery.fn.test.Constructor).toBe(pluginMock)
expect(typeof fakejQuery.fn.test.noConflict).toEqual('function')
expect(fakejQuery.fn.test).toEqual(pluginMock.jQueryInterface)
expect(fakejQuery.fn.test.Constructor).toEqual(pluginMock)
expect(fakejQuery.fn.test.noConflict).toEqual(jasmine.any(Function))
})
})

View File

@@ -58,9 +58,9 @@ describe('ScrollBar', () => {
const result = new ScrollBarHelper().isOverflowing()
if (isScrollBarHidden()) {
expect(result).toEqual(false)
expect(result).toBeFalse()
} else {
expect(result).toEqual(true)
expect(result).toBeTrue()
}
})
@@ -73,7 +73,7 @@ describe('ScrollBar', () => {
const scrollBar = new ScrollBarHelper()
const result = scrollBar.isOverflowing()
expect(result).toEqual(false)
expect(result).toBeFalse()
})
})
@@ -87,7 +87,7 @@ describe('ScrollBar', () => {
const result = new ScrollBarHelper().getWidth()
if (isScrollBarHidden()) {
expect(result).toBe(0)
expect(result).toEqual(0)
} else {
expect(result).toBeGreaterThan(1)
}
@@ -128,18 +128,18 @@ describe('ScrollBar', () => {
let currentPadding = getPaddingX(fixedEl)
let currentPadding2 = getPaddingX(fixedEl2)
expect(getPaddingAttr(fixedEl)).toEqual(`${originalPadding}px`, 'original fixed element padding should be stored in data-bs-padding-right')
expect(getPaddingAttr(fixedEl2)).toEqual(`${originalPadding2}px`, 'original fixed element padding should be stored in data-bs-padding-right')
expect(currentPadding).toEqual(expectedPadding, 'fixed element padding should be adjusted while opening')
expect(currentPadding2).toEqual(expectedPadding2, 'fixed element padding should be adjusted while opening')
expect(getPaddingAttr(fixedEl)).toEqual(`${originalPadding}px`)
expect(getPaddingAttr(fixedEl2)).toEqual(`${originalPadding2}px`)
expect(currentPadding).toEqual(expectedPadding)
expect(currentPadding2).toEqual(expectedPadding2)
scrollBar.reset()
currentPadding = getPaddingX(fixedEl)
currentPadding2 = getPaddingX(fixedEl2)
expect(getPaddingAttr(fixedEl)).toEqual(null, 'data-bs-padding-right should be cleared after closing')
expect(getPaddingAttr(fixedEl2)).toEqual(null, 'data-bs-padding-right should be cleared after closing')
expect(currentPadding).toEqual(originalPadding, 'fixed element padding should be reset after closing')
expect(currentPadding2).toEqual(originalPadding2, 'fixed element padding should be reset after closing')
expect(getPaddingAttr(fixedEl)).toBeNull()
expect(getPaddingAttr(fixedEl2)).toBeNull()
expect(currentPadding).toEqual(originalPadding)
expect(currentPadding2).toEqual(originalPadding2)
done()
})
@@ -159,16 +159,16 @@ describe('ScrollBar', () => {
const expectedPadding = originalPadding + scrollBar.getWidth()
scrollBar.hide()
expect(getMarginAttr(stickyTopEl)).toEqual(`${originalMargin}px`, 'original sticky element margin should be stored in data-bs-margin-right')
expect(getMarginX(stickyTopEl)).toEqual(expectedMargin, 'sticky element margin should be adjusted while opening')
expect(getPaddingAttr(stickyTopEl)).toEqual(`${originalPadding}px`, 'original sticky element margin should be stored in data-bs-margin-right')
expect(getPaddingX(stickyTopEl)).toEqual(expectedPadding, 'sticky element margin should be adjusted while opening')
expect(getMarginAttr(stickyTopEl)).toEqual(`${originalMargin}px`)
expect(getMarginX(stickyTopEl)).toEqual(expectedMargin)
expect(getPaddingAttr(stickyTopEl)).toEqual(`${originalPadding}px`)
expect(getPaddingX(stickyTopEl)).toEqual(expectedPadding)
scrollBar.reset()
expect(getMarginAttr(stickyTopEl)).toEqual(null, 'data-bs-margin-right should be cleared after closing')
expect(getMarginX(stickyTopEl)).toEqual(originalMargin, 'sticky element margin should be reset after closing')
expect(getPaddingAttr(stickyTopEl)).toEqual(null, 'data-bs-margin-right should be cleared after closing')
expect(getPaddingX(stickyTopEl)).toEqual(originalPadding, 'sticky element margin should be reset after closing')
expect(getMarginAttr(stickyTopEl)).toBeNull()
expect(getMarginX(stickyTopEl)).toEqual(originalMargin)
expect(getPaddingAttr(stickyTopEl)).toBeNull()
expect(getPaddingX(stickyTopEl)).toEqual(originalPadding)
done()
})
@@ -187,8 +187,8 @@ describe('ScrollBar', () => {
const currentMargin = getMarginX(stickyTopEl)
const currentPadding = getPaddingX(stickyTopEl)
expect(currentMargin).toEqual(originalMargin, 'sticky element\'s margin should not be adjusted while opening')
expect(currentPadding).toEqual(originalPadding, 'sticky element\'s padding should not be adjusted while opening')
expect(currentMargin).toEqual(originalMargin)
expect(currentPadding).toEqual(originalPadding)
scrollBar.reset()
})
@@ -232,8 +232,8 @@ describe('ScrollBar', () => {
const scrollBarWidth = scrollBar.getWidth()
scrollBar.hide()
expect(getPaddingX(document.body)).toEqual(scrollBarWidth, 'body does not have inline padding set')
expect(document.body.style.color).toEqual('red', 'body still has other inline styles set')
expect(getPaddingX(document.body)).toEqual(scrollBarWidth)
expect(document.body.style.color).toEqual('red')
scrollBar.reset()
})
@@ -273,9 +273,9 @@ describe('ScrollBar', () => {
const currentPadding1 = getPaddingX(el)
expect(currentPadding1).toEqual(originalPadding)
expect(getPaddingAttr(el)).toEqual(null)
expect(getPaddingAttr(el)).toBeNull()
expect(getOverFlow(el)).toEqual(originalOverFlow)
expect(getOverFlowAttr(el)).toEqual(null)
expect(getOverFlowAttr(el)).toBeNull()
})
it('should hide scrollbar and reset it to its initial value - respecting css rules', () => {
@@ -308,9 +308,9 @@ describe('ScrollBar', () => {
const currentPadding1 = getPaddingX(el)
expect(currentPadding1).toEqual(originalPadding)
expect(getPaddingAttr(el)).toEqual(null)
expect(getPaddingAttr(el)).toBeNull()
expect(getOverFlow(el)).toEqual(originalOverFlow)
expect(getOverFlowAttr(el)).toEqual(null)
expect(getOverFlowAttr(el)).toBeNull()
})
it('should not adjust the inline body padding when it does not overflow', () => {
@@ -324,7 +324,7 @@ describe('ScrollBar', () => {
scrollBar.hide()
const currentPadding = getPaddingX(document.body)
expect(currentPadding).toEqual(originalPadding, 'body padding should not be adjusted')
expect(currentPadding).toEqual(originalPadding)
scrollBar.reset()
})
@@ -344,7 +344,7 @@ describe('ScrollBar', () => {
const currentPadding = getPaddingX(document.body)
expect(currentPadding).toEqual(originalPadding, 'body padding should not be adjusted')
expect(currentPadding).toEqual(originalPadding)
scrollBar.reset()
})

View File

@@ -86,15 +86,15 @@ describe('TemplateFactory', () => {
const factory = new TemplateFactory({
extraClass: 'testClass'
})
expect(factory.toHtml().classList.contains('testClass')).toBeTrue()
expect(factory.toHtml()).toHaveClass('testClass')
})
it('should add extra classes', () => {
const factory = new TemplateFactory({
extraClass: 'testClass testClass2'
})
expect(factory.toHtml().classList.contains('testClass')).toBeTrue()
expect(factory.toHtml().classList.contains('testClass2')).toBeTrue()
expect(factory.toHtml()).toHaveClass('testClass')
expect(factory.toHtml()).toHaveClass('testClass2')
})
it('should resolve class if function is given', () => {
@@ -105,7 +105,7 @@ describe('TemplateFactory', () => {
}
})
expect(factory.toHtml().classList.contains('testClass')).toBeTrue()
expect(factory.toHtml()).toHaveClass('testClass')
})
})
})
@@ -128,8 +128,8 @@ describe('TemplateFactory', () => {
})
const html = factory.toHtml()
expect(html.querySelector('.foo').textContent).toBe('bar')
expect(html.querySelector('.foo2').textContent).toBe('bar2')
expect(html.querySelector('.foo').textContent).toEqual('bar')
expect(html.querySelector('.foo2').textContent).toEqual('bar2')
})
it('should not fill template if selector not exists', () => {
@@ -140,7 +140,7 @@ describe('TemplateFactory', () => {
content: { '#bar': 'test' }
})
expect(factory.toHtml().outerHTML).toBe('<div id="foo"></div>')
expect(factory.toHtml().outerHTML).toEqual('<div id="foo"></div>')
})
it('should remove template selector, if content is null', () => {
@@ -151,7 +151,7 @@ describe('TemplateFactory', () => {
content: { '#foo': null }
})
expect(factory.toHtml().outerHTML).toBe('<div></div>')
expect(factory.toHtml().outerHTML).toEqual('<div></div>')
})
it('should resolve content if is function', () => {
@@ -162,7 +162,7 @@ describe('TemplateFactory', () => {
content: { '#foo': () => null }
})
expect(factory.toHtml().outerHTML).toBe('<div></div>')
expect(factory.toHtml().outerHTML).toEqual('<div></div>')
})
it('if content is element and "config.html=false", should put content\'s textContent', () => {
@@ -176,9 +176,9 @@ describe('TemplateFactory', () => {
})
const fooEl = factory.toHtml().querySelector('#foo')
expect(fooEl.innerHTML).not.toBe(contentElement.innerHTML)
expect(fooEl.textContent).toBe(contentElement.textContent)
expect(fooEl.textContent).toBe('foobar')
expect(fooEl.innerHTML).not.toEqual(contentElement.innerHTML)
expect(fooEl.textContent).toEqual(contentElement.textContent)
expect(fooEl.textContent).toEqual('foobar')
})
it('if content is element and "config.html=true", should put content\'s outerHtml as child', () => {
@@ -192,8 +192,8 @@ describe('TemplateFactory', () => {
})
const fooEl = factory.toHtml().querySelector('#foo')
expect(fooEl.innerHTML).toBe(contentElement.outerHTML)
expect(fooEl.textContent).toBe(contentElement.textContent)
expect(fooEl.innerHTML).toEqual(contentElement.outerHTML)
expect(fooEl.textContent).toEqual(contentElement.textContent)
})
})