import * as Util from '../../../src/util/index' /** Test helpers */ import { getFixture, clearFixture } from '../../helpers/fixture' describe('Util', () => { let fixtureEl beforeAll(() => { fixtureEl = getFixture() }) afterEach(() => { clearFixture() }) describe('getUID', () => { it('should generate uid', () => { const uid = Util.getUID('bs') const uid2 = Util.getUID('bs') expect(uid).not.toEqual(uid2) }) }) describe('getSelectorFromElement', () => { it('should get selector from data-bs-target', () => { fixtureEl.innerHTML = [ '
', '' ].join('') const testEl = fixtureEl.querySelector('#test') expect(Util.getSelectorFromElement(testEl)).toEqual('.target') }) it('should get selector from href if no data-bs-target set', () => { fixtureEl.innerHTML = [ '', '' ].join('') const testEl = fixtureEl.querySelector('#test') expect(Util.getSelectorFromElement(testEl)).toEqual('.target') }) it('should get selector from href if data-bs-target equal to #', () => { fixtureEl.innerHTML = [ '', '' ].join('') const testEl = fixtureEl.querySelector('#test') expect(Util.getSelectorFromElement(testEl)).toEqual('.target') }) it('should return null if selector not found', () => { fixtureEl.innerHTML = '' const testEl = fixtureEl.querySelector('#test') expect(Util.getSelectorFromElement(testEl)).toBeNull() }) it('should return null if no selector', () => { fixtureEl.innerHTML = '' const testEl = fixtureEl.querySelector('div') expect(Util.getSelectorFromElement(testEl)).toBeNull() }) }) describe('getElementFromSelector', () => { it('should get element from data-bs-target', () => { fixtureEl.innerHTML = [ '', '' ].join('') const testEl = fixtureEl.querySelector('#test') expect(Util.getElementFromSelector(testEl)).toEqual(fixtureEl.querySelector('.target')) }) it('should get element from href if no data-bs-target set', () => { fixtureEl.innerHTML = [ '', '' ].join('') const testEl = fixtureEl.querySelector('#test') expect(Util.getElementFromSelector(testEl)).toEqual(fixtureEl.querySelector('.target')) }) it('should return null if element not found', () => { fixtureEl.innerHTML = '' const testEl = fixtureEl.querySelector('#test') expect(Util.getElementFromSelector(testEl)).toBeNull() }) it('should return null if no selector', () => { fixtureEl.innerHTML = '' const testEl = fixtureEl.querySelector('div') expect(Util.getElementFromSelector(testEl)).toBeNull() }) }) describe('getTransitionDurationFromElement', () => { it('should get transition from element', () => { fixtureEl.innerHTML = '' expect(Util.getTransitionDurationFromElement(fixtureEl.querySelector('div'))).toEqual(300) }) it('should return 0 if the element is undefined or null', () => { expect(Util.getTransitionDurationFromElement(null)).toEqual(0) expect(Util.getTransitionDurationFromElement(undefined)).toEqual(0) }) it('should return 0 if the element do not possess transition', () => { fixtureEl.innerHTML = '' expect(Util.getTransitionDurationFromElement(fixtureEl.querySelector('div'))).toEqual(0) }) }) describe('triggerTransitionEnd', () => { it('should trigger transitionend event', done => { fixtureEl.innerHTML = '' const el = fixtureEl.querySelector('div') el.addEventListener('transitionend', () => { expect().nothing() done() }) Util.triggerTransitionEnd(el) }) }) describe('isElement', () => { it('should detect if the parameter is an element or not', () => { fixtureEl.innerHTML = '' const el = document.querySelector('div') expect(Util.isElement(el)).toEqual(el.nodeType) expect(Util.isElement({})).toEqual(undefined) }) it('should detect jQuery element', () => { fixtureEl.innerHTML = '' const el = document.querySelector('div') const fakejQuery = { 0: el } expect(Util.isElement(fakejQuery)).toEqual(el.nodeType) }) }) describe('emulateTransitionEnd', () => { it('should emulate transition end', () => { fixtureEl.innerHTML = '' const el = document.querySelector('div') const spy = spyOn(window, 'setTimeout') Util.emulateTransitionEnd(el, 10) expect(spy).toHaveBeenCalled() }) it('should not emulate transition end if already triggered', done => { fixtureEl.innerHTML = '' const el = fixtureEl.querySelector('div') const spy = spyOn(el, 'removeEventListener') Util.emulateTransitionEnd(el, 10) Util.triggerTransitionEnd(el) setTimeout(() => { expect(spy).toHaveBeenCalled() done() }, 20) }) }) describe('typeCheckConfig', () => { const namePlugin = 'collapse' it('should check type of the config object', () => { const defaultType = { toggle: 'boolean', parent: '(string|element)' } const config = { toggle: true, parent: 777 } expect(() => { Util.typeCheckConfig(namePlugin, config, defaultType) }).toThrow(new Error('COLLAPSE: Option "parent" provided type "number" but expected type "(string|element)".')) }) it('should return null stringified when null is passed', () => { const defaultType = { toggle: 'boolean', parent: '(null|element)' } const config = { toggle: true, parent: null } Util.typeCheckConfig(namePlugin, config, defaultType) expect().nothing() }) it('should return undefined stringified when undefined is passed', () => { const defaultType = { toggle: 'boolean', parent: '(undefined|element)' } const config = { toggle: true, parent: undefined } Util.typeCheckConfig(namePlugin, config, defaultType) expect().nothing() }) }) describe('isVisible', () => { it('should return false if the element is not defined', () => { expect(Util.isVisible(null)).toEqual(false) expect(Util.isVisible(undefined)).toEqual(false) }) it('should return false if the element provided is not a dom element', () => { expect(Util.isVisible({})).toEqual(false) }) it('should return false if the element is not visible with display none', () => { fixtureEl.innerHTML = '' const div = fixtureEl.querySelector('div') expect(Util.isVisible(div)).toEqual(false) }) it('should return false if the element is not visible with visibility hidden', () => { fixtureEl.innerHTML = '' const div = fixtureEl.querySelector('div') expect(Util.isVisible(div)).toEqual(false) }) it('should return false if the parent element is not visible', () => { fixtureEl.innerHTML = [ ' ' ].join('') const div = fixtureEl.querySelector('.content') expect(Util.isVisible(div)).toEqual(false) }) it('should return true if the element is visible', () => { fixtureEl.innerHTML = [ '