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', () => {
|
|
|
|
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')
|
|
|
|
})
|
|
|
|
|
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')
|
|
|
|
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()
|
|
|
|
})
|
|
|
|
|
2019-10-31 06:58:09 +01:00
|
|
|
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')
|
|
|
|
|
2019-10-31 06:58:09 +01:00
|
|
|
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', () => {
|
2020-11-02 15:13:24 +01:00
|
|
|
expect(Manipulator.getDataAttributes(null)).toEqual({})
|
2019-03-22 08:10:03 +01:00
|
|
|
expect().nothing()
|
|
|
|
})
|
|
|
|
|
2020-07-22 21:33:11 +02:00
|
|
|
it('should get all data attributes, without bs prefixed as well', () => {
|
|
|
|
fixtureEl.innerHTML = '<div data-bs-toggle="tabs" data-bs-target="#element" data-another="value"></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
|
|
|
bsToggle: 'tabs',
|
|
|
|
bsTarget: '#element',
|
|
|
|
another: 'value',
|
|
|
|
toggle: 'tabs',
|
|
|
|
target: '#element'
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should remove just prefixed bs keyword from the attributes and override original attribute with bs prefixed', () => {
|
|
|
|
fixtureEl.innerHTML = '<div data-bs-toggle="tabs" data-toggle="override" data-target-bs="#element" data-in-bs-out="in-between"></div>'
|
|
|
|
|
|
|
|
const div = fixtureEl.querySelector('div')
|
|
|
|
|
|
|
|
expect(Manipulator.getDataAttributes(div)).toEqual({
|
|
|
|
bsToggle: 'tabs',
|
|
|
|
targetBs: '#element',
|
|
|
|
inBsOut: 'in-between',
|
|
|
|
toggle: 'tabs'
|
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()
|
|
|
|
})
|
|
|
|
|
2019-10-31 06:58:09 +01:00
|
|
|
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')
|
|
|
|
|
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', () => {
|
|
|
|
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))
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|