0
0
mirror of https://github.com/twbs/bootstrap.git synced 2024-12-01 13:24:25 +01:00
Bootstrap/js/tests/unit/dom/data.spec.js

132 lines
3.0 KiB
JavaScript
Raw Normal View History

import Data from '../../../src/dom/data'
2019-03-13 15:23:50 +01:00
/** Test helpers */
import { getFixture, clearFixture } from '../../helpers/fixture'
2019-03-13 15:23:50 +01:00
describe('Data', () => {
let fixtureEl
beforeAll(() => {
fixtureEl = getFixture()
})
afterEach(() => {
clearFixture()
})
describe('setData', () => {
2020-06-17 10:45:07 +02:00
it('should set data in an element by adding a bsKey attribute', () => {
2019-03-13 15:23:50 +01:00
fixtureEl.innerHTML = '<div></div>'
const div = fixtureEl.querySelector('div')
const data = {
test: 'bsData'
}
Data.setData(div, 'test', data)
2020-06-17 10:45:07 +02:00
expect(div.bsKey).toBeDefined()
2019-03-13 15:23:50 +01:00
})
it('should change data if something is already stored', () => {
fixtureEl.innerHTML = '<div></div>'
const div = fixtureEl.querySelector('div')
const data = {
test: 'bsData'
}
Data.setData(div, 'test', data)
data.test = 'bsData2'
Data.setData(div, 'test', data)
2020-06-17 10:45:07 +02:00
expect(div.bsKey).toBeDefined()
2019-03-13 15:23:50 +01:00
})
})
describe('getData', () => {
it('should return stored data', () => {
fixtureEl.innerHTML = '<div></div>'
const div = fixtureEl.querySelector('div')
const data = {
test: 'bsData'
}
Data.setData(div, 'test', data)
expect(Data.getData(div, 'test')).toEqual(data)
})
it('should return null on undefined element', () => {
expect(Data.getData(null)).toEqual(null)
expect(Data.getData(undefined)).toEqual(null)
})
it('should return null when an element have nothing stored', () => {
fixtureEl.innerHTML = '<div></div>'
const div = fixtureEl.querySelector('div')
expect(Data.getData(div, 'test')).toEqual(null)
})
it('should return null when an element have nothing stored with the provided key', () => {
fixtureEl.innerHTML = '<div></div>'
const div = fixtureEl.querySelector('div')
const data = {
test: 'bsData'
}
Data.setData(div, 'test', data)
expect(Data.getData(div, 'test2')).toEqual(null)
})
})
describe('removeData', () => {
it('should do nothing when an element have nothing stored', () => {
fixtureEl.innerHTML = '<div></div>'
const div = fixtureEl.querySelector('div')
Data.removeData(div, 'test')
expect().nothing()
})
it('should should do nothing if it\'s not a valid key provided', () => {
fixtureEl.innerHTML = '<div></div>'
const div = fixtureEl.querySelector('div')
const data = {
test: 'bsData'
}
Data.setData(div, 'test', data)
2020-06-17 10:45:07 +02:00
expect(div.bsKey).toBeDefined()
2019-03-13 15:23:50 +01:00
Data.removeData(div, 'test2')
2020-06-17 10:45:07 +02:00
expect(div.bsKey).toBeDefined()
2019-03-13 15:23:50 +01:00
})
it('should remove data if something is stored', () => {
fixtureEl.innerHTML = '<div></div>'
const div = fixtureEl.querySelector('div')
const data = {
test: 'bsData'
}
Data.setData(div, 'test', data)
2020-06-17 10:45:07 +02:00
expect(div.bsKey).toBeDefined()
2019-03-13 15:23:50 +01:00
Data.removeData(div, 'test')
2020-06-17 10:45:07 +02:00
expect(div.bsKey).toBeUndefined()
2019-03-13 15:23:50 +01:00
})
})
})