0
0
mirror of https://github.com/twbs/bootstrap.git synced 2024-12-02 14:24:19 +01:00
Bootstrap/js/tests/unit/dom/manipulator.spec.js

133 lines
3.8 KiB
JavaScript
Raw Normal View History

import Manipulator from '../../../src/dom/manipulator'
2019-03-22 08:10:03 +01:00
/** Test helpers */
import { getFixture, clearFixture } from '../../helpers/fixture'
2019-03-22 08:10:03 +01:00
describe('Manipulator', () => {
let fixtureEl
beforeAll(() => {
fixtureEl = getFixture()
})
afterEach(() => {
clearFixture()
})
describe('setDataAttribute', () => {
it('should set data attribute', () => {
fixtureEl.innerHTML = '<div></div>'
const div = fixtureEl.querySelector('div')
Manipulator.setDataAttribute(div, 'key', 'value')
expect(div.getAttribute('data-key')).toEqual('value')
})
it('should set data attribute in kebab case', () => {
2019-03-22 08:10:03 +01:00
fixtureEl.innerHTML = '<div></div>'
const div = fixtureEl.querySelector('div')
Manipulator.setDataAttribute(div, 'testKey', 'value')
expect(div.getAttribute('data-test-key')).toEqual('value')
2019-03-22 08:10:03 +01:00
})
})
describe('removeDataAttribute', () => {
it('should remove data attribute', () => {
fixtureEl.innerHTML = '<div data-key="value"></div>'
const div = fixtureEl.querySelector('div')
Manipulator.removeDataAttribute(div, 'key')
expect(div.getAttribute('data-key')).toBeNull()
})
it('should remove data attribute in kebab case', () => {
fixtureEl.innerHTML = '<div data-test-key="value"></div>'
2019-03-22 08:10:03 +01:00
const div = fixtureEl.querySelector('div')
Manipulator.removeDataAttribute(div, 'testKey')
expect(div.getAttribute('data-test-key')).toBeNull()
2019-03-22 08:10:03 +01:00
})
})
describe('getDataAttributes', () => {
it('should return empty object for null', () => {
expect(Manipulator.getDataAttributes(null)).toEqual({})
2019-03-22 08:10:03 +01:00
expect().nothing()
})
2020-11-11 07:37:04 +01:00
it('should get only bs prefixed data attributes without bs namespace', () => {
fixtureEl.innerHTML = '<div data-bs-toggle="tabs" data-bs-target="#element" data-another="value" data-target-bs="#element" data-in-bs-out="in-between"></div>'
2019-03-22 08:10:03 +01:00
const div = fixtureEl.querySelector('div')
expect(Manipulator.getDataAttributes(div)).toEqual({
toggle: 'tabs',
target: '#element'
})
})
2019-03-22 08:10:03 +01:00
})
describe('getDataAttribute', () => {
it('should get data attribute', () => {
fixtureEl.innerHTML = '<div data-test="null" ></div>'
const div = fixtureEl.querySelector('div')
expect(Manipulator.getDataAttribute(div, 'test')).toBeNull()
})
it('should get data attribute in kebab case', () => {
fixtureEl.innerHTML = '<div data-test-key="value" ></div>'
2019-03-22 08:10:03 +01:00
const div = fixtureEl.querySelector('div')
expect(Manipulator.getDataAttribute(div, 'testKey')).toEqual('value')
2019-03-22 08:10:03 +01:00
})
it('should normalize data', () => {
fixtureEl.innerHTML = '<div data-test="false" ></div>'
const div = fixtureEl.querySelector('div')
expect(Manipulator.getDataAttribute(div, 'test')).toEqual(false)
div.setAttribute('data-test', 'true')
expect(Manipulator.getDataAttribute(div, 'test')).toEqual(true)
div.setAttribute('data-test', '1')
expect(Manipulator.getDataAttribute(div, 'test')).toEqual(1)
})
})
describe('offset', () => {
it('should return object with two properties top and left, both numbers', () => {
fixtureEl.innerHTML = '<div></div>'
const div = fixtureEl.querySelector('div')
const offset = Manipulator.offset(div)
expect(offset).toBeDefined()
expect(offset.top).toEqual(jasmine.any(Number))
expect(offset.left).toEqual(jasmine.any(Number))
})
})
describe('position', () => {
it('should return object with two properties top and left, both numbers', () => {
fixtureEl.innerHTML = '<div></div>'
const div = fixtureEl.querySelector('div')
const position = Manipulator.position(div)
expect(position).toBeDefined()
expect(position.top).toEqual(jasmine.any(Number))
expect(position.left).toEqual(jasmine.any(Number))
})
})
})