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

107 lines
2.5 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', () => {
const TEST_KEY = 'bs.test'
const UNKNOWN_KEY = 'bs.unknown'
const TEST_DATA = {
test: 'bsData'
}
2019-03-13 15:23:50 +01:00
let fixtureEl
let div
2019-03-13 15:23:50 +01:00
beforeAll(() => {
fixtureEl = getFixture()
})
beforeEach(() => {
fixtureEl.innerHTML = '<div></div>'
div = fixtureEl.querySelector('div')
})
2019-03-13 15:23:50 +01:00
afterEach(() => {
Data.remove(div, TEST_KEY)
2019-03-13 15:23:50 +01:00
clearFixture()
})
it('should return null for unknown elements', () => {
const data = { ...TEST_DATA }
2019-03-13 15:23:50 +01:00
Data.set(div, TEST_KEY, data)
2019-03-13 15:23:50 +01:00
expect(Data.get(null)).toBeNull()
expect(Data.get(undefined)).toBeNull()
expect(Data.get(document.createElement('div'), TEST_KEY)).toBeNull()
2019-03-13 15:23:50 +01:00
})
it('should return null for unknown keys', () => {
const data = { ...TEST_DATA }
2019-03-13 15:23:50 +01:00
Data.set(div, TEST_KEY, data)
2019-03-13 15:23:50 +01:00
expect(Data.get(div, null)).toBeNull()
expect(Data.get(div, undefined)).toBeNull()
expect(Data.get(div, UNKNOWN_KEY)).toBeNull()
})
2019-03-13 15:23:50 +01:00
it('should store data for an element with a given key and return it', () => {
const data = { ...TEST_DATA }
2019-03-13 15:23:50 +01:00
Data.set(div, TEST_KEY, data)
2019-03-13 15:23:50 +01:00
expect(Data.get(div, TEST_KEY)).toBe(data)
2019-03-13 15:23:50 +01:00
})
it('should overwrite data if something is already stored', () => {
const data = { ...TEST_DATA }
const copy = { ...data }
2019-03-13 15:23:50 +01:00
Data.set(div, TEST_KEY, data)
Data.set(div, TEST_KEY, copy)
2019-03-13 15:23:50 +01:00
expect(Data.get(div, TEST_KEY)).not.toBe(data)
expect(Data.get(div, TEST_KEY)).toBe(copy)
})
2019-03-13 15:23:50 +01:00
it('should do nothing when an element have nothing stored', () => {
Data.remove(div, TEST_KEY)
2019-03-13 15:23:50 +01:00
expect().nothing()
})
2019-03-13 15:23:50 +01:00
it('should remove nothing for an unknown key', () => {
const data = { ...TEST_DATA }
2019-03-13 15:23:50 +01:00
Data.set(div, TEST_KEY, data)
Data.remove(div, UNKNOWN_KEY)
2019-03-13 15:23:50 +01:00
expect(Data.get(div, TEST_KEY)).toBe(data)
})
2019-03-13 15:23:50 +01:00
it('should remove data for a given key', () => {
const data = { ...TEST_DATA }
2019-03-13 15:23:50 +01:00
Data.set(div, TEST_KEY, data)
Data.remove(div, TEST_KEY)
2019-03-13 15:23:50 +01:00
expect(Data.get(div, TEST_KEY)).toBeNull()
})
2019-03-13 15:23:50 +01:00
it('should console.error a message if called with multiple keys', () => {
/* eslint-disable no-console */
console.error = jasmine.createSpy('console.error')
2019-03-13 15:23:50 +01:00
const data = { ...TEST_DATA }
const copy = { ...data }
2019-03-13 15:23:50 +01:00
Data.set(div, TEST_KEY, data)
Data.set(div, UNKNOWN_KEY, copy)
2019-03-13 15:23:50 +01:00
expect(console.error).toHaveBeenCalled()
expect(Data.get(div, UNKNOWN_KEY)).toBe(null)
2019-03-13 15:23:50 +01:00
})
})