2019-10-02 11:43:54 +02:00
|
|
|
import Manipulator from '../../../src/dom/manipulator'
|
2019-03-22 08:10:03 +01:00
|
|
|
|
|
|
|
/** Test helpers */
|
2019-10-02 11:43:54 +02:00
|
|
|
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', () => {
|
2020-11-20 10:13:13 +01:00
|
|
|
it('should set data attribute prefixed with bs', () => {
|
2019-03-22 08:10:03 +01:00
|
|
|
fixtureEl.innerHTML = '<div></div>'
|
|
|
|
|
|
|
|
const div = fixtureEl.querySelector('div')
|
|
|
|
|
|
|
|
Manipulator.setDataAttribute(div, 'key', 'value')
|
2020-11-20 10:13:13 +01:00
|
|
|
expect(div.getAttribute('data-bs-key')).toEqual('value')
|
2019-03-22 08:10:03 +01:00
|
|
|
})
|
|
|
|
|
2019-10-31 06:58:09 +01:00
|
|
|
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')
|
|
|
|
|
2019-10-31 06:58:09 +01:00
|
|
|
Manipulator.setDataAttribute(div, 'testKey', 'value')
|
2020-11-20 10:13:13 +01:00
|
|
|
expect(div.getAttribute('data-bs-test-key')).toEqual('value')
|
2019-03-22 08:10:03 +01:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('removeDataAttribute', () => {
|
2020-11-20 10:13:13 +01:00
|
|
|
it('should only remove bs-prefixed data attribute', () => {
|
|
|
|
fixtureEl.innerHTML = '<div data-bs-key="value" data-key-bs="postfixed" data-key="value"></div>'
|
2019-03-22 08:10:03 +01:00
|
|
|
|
|
|
|
const div = fixtureEl.querySelector('div')
|
|
|
|
|
|
|
|
Manipulator.removeDataAttribute(div, 'key')
|
2020-11-20 10:13:13 +01:00
|
|
|
expect(div.getAttribute('data-bs-key')).toBeNull()
|
|
|
|
expect(div.getAttribute('data-key-bs')).toEqual('postfixed')
|
|
|
|
expect(div.getAttribute('data-key')).toEqual('value')
|
2019-03-22 08:10:03 +01:00
|
|
|
})
|
|
|
|
|
2019-10-31 06:58:09 +01:00
|
|
|
it('should remove data attribute in kebab case', () => {
|
2020-11-20 10:13:13 +01:00
|
|
|
fixtureEl.innerHTML = '<div data-bs-test-key="value"></div>'
|
2019-03-22 08:10:03 +01:00
|
|
|
|
|
|
|
const div = fixtureEl.querySelector('div')
|
|
|
|
|
2019-10-31 06:58:09 +01:00
|
|
|
Manipulator.removeDataAttribute(div, 'testKey')
|
2020-11-20 10:13:13 +01:00
|
|
|
expect(div.getAttribute('data-bs-test-key')).toBeNull()
|
2019-03-22 08:10:03 +01:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('getDataAttributes', () => {
|
2020-11-20 10:13:13 +01:00
|
|
|
it('should return an empty object for null', () => {
|
2020-11-02 15:13:24 +01:00
|
|
|
expect(Manipulator.getDataAttributes(null)).toEqual({})
|
2019-03-22 08:10:03 +01:00
|
|
|
expect().nothing()
|
|
|
|
})
|
|
|
|
|
2020-11-20 10:13:13 +01:00
|
|
|
it('should get only bs-prefixed data attributes without bs namespace', () => {
|
2020-11-11 07:37:04 +01:00
|
|
|
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({
|
2020-07-22 21:33:11 +02:00
|
|
|
toggle: 'tabs',
|
|
|
|
target: '#element'
|
|
|
|
})
|
|
|
|
})
|
2019-03-22 08:10:03 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
describe('getDataAttribute', () => {
|
2020-11-20 10:13:13 +01:00
|
|
|
it('should only get bs-prefixed data attribute', () => {
|
|
|
|
fixtureEl.innerHTML = '<div data-bs-key="value" data-test-bs="postFixed" data-toggle="tab"></div>'
|
2019-03-22 08:10:03 +01:00
|
|
|
|
|
|
|
const div = fixtureEl.querySelector('div')
|
|
|
|
|
2020-11-20 10:13:13 +01:00
|
|
|
expect(Manipulator.getDataAttribute(div, 'key')).toEqual('value')
|
2019-03-22 08:10:03 +01:00
|
|
|
expect(Manipulator.getDataAttribute(div, 'test')).toBeNull()
|
2020-11-20 10:13:13 +01:00
|
|
|
expect(Manipulator.getDataAttribute(div, 'toggle')).toBeNull()
|
2019-03-22 08:10:03 +01:00
|
|
|
})
|
|
|
|
|
2019-10-31 06:58:09 +01:00
|
|
|
it('should get data attribute in kebab case', () => {
|
2020-11-20 10:13:13 +01:00
|
|
|
fixtureEl.innerHTML = '<div data-bs-test-key="value" ></div>'
|
2019-03-22 08:10:03 +01:00
|
|
|
|
|
|
|
const div = fixtureEl.querySelector('div')
|
|
|
|
|
2019-10-31 06:58:09 +01:00
|
|
|
expect(Manipulator.getDataAttribute(div, 'testKey')).toEqual('value')
|
2019-03-22 08:10:03 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
it('should normalize data', () => {
|
2020-11-20 10:13:13 +01:00
|
|
|
fixtureEl.innerHTML = '<div data-bs-test="false" ></div>'
|
2019-03-22 08:10:03 +01:00
|
|
|
|
|
|
|
const div = fixtureEl.querySelector('div')
|
|
|
|
|
|
|
|
expect(Manipulator.getDataAttribute(div, 'test')).toEqual(false)
|
|
|
|
|
2020-11-20 10:13:13 +01:00
|
|
|
div.setAttribute('data-bs-test', 'true')
|
2019-03-22 08:10:03 +01:00
|
|
|
expect(Manipulator.getDataAttribute(div, 'test')).toEqual(true)
|
|
|
|
|
2020-11-20 10:13:13 +01:00
|
|
|
div.setAttribute('data-bs-test', '1')
|
2019-03-22 08:10:03 +01:00
|
|
|
expect(Manipulator.getDataAttribute(div, 'test')).toEqual(1)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('offset', () => {
|
2020-11-20 10:13:13 +01:00
|
|
|
it('should return an object with two properties top and left, both numbers', () => {
|
2019-03-22 08:10:03 +01:00
|
|
|
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', () => {
|
2020-11-20 10:13:13 +01:00
|
|
|
it('should return an object with two properties top and left, both numbers', () => {
|
2019-03-22 08:10:03 +01:00
|
|
|
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))
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|