2019-10-02 11:43:54 +02:00
|
|
|
import Data from '../../../src/dom/data'
|
|
|
|
import { getFixture, clearFixture } from '../../helpers/fixture'
|
2019-03-13 15:23:50 +01:00
|
|
|
|
|
|
|
describe('Data', () => {
|
2021-03-02 15:55:44 +01:00
|
|
|
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
|
2021-03-02 15:55:44 +01:00
|
|
|
let div
|
2019-03-13 15:23:50 +01:00
|
|
|
|
|
|
|
beforeAll(() => {
|
|
|
|
fixtureEl = getFixture()
|
|
|
|
})
|
|
|
|
|
2021-03-02 15:55:44 +01:00
|
|
|
beforeEach(() => {
|
|
|
|
fixtureEl.innerHTML = '<div></div>'
|
|
|
|
div = fixtureEl.querySelector('div')
|
|
|
|
})
|
|
|
|
|
2019-03-13 15:23:50 +01:00
|
|
|
afterEach(() => {
|
2021-03-02 15:55:44 +01:00
|
|
|
Data.remove(div, TEST_KEY)
|
2019-03-13 15:23:50 +01:00
|
|
|
clearFixture()
|
|
|
|
})
|
|
|
|
|
2021-03-02 15:55:44 +01:00
|
|
|
it('should return null for unknown elements', () => {
|
|
|
|
const data = { ...TEST_DATA }
|
2019-03-13 15:23:50 +01:00
|
|
|
|
2021-03-02 15:55:44 +01:00
|
|
|
Data.set(div, TEST_KEY, data)
|
2019-03-13 15:23:50 +01:00
|
|
|
|
2021-03-02 15:55:44 +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
|
|
|
})
|
|
|
|
|
2021-03-02 15:55:44 +01:00
|
|
|
it('should return null for unknown keys', () => {
|
|
|
|
const data = { ...TEST_DATA }
|
2019-03-13 15:23:50 +01:00
|
|
|
|
2021-03-02 15:55:44 +01:00
|
|
|
Data.set(div, TEST_KEY, data)
|
2019-03-13 15:23:50 +01:00
|
|
|
|
2021-03-02 15:55:44 +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
|
|
|
|
2021-03-02 15:55:44 +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
|
|
|
|
2021-03-02 15:55:44 +01:00
|
|
|
Data.set(div, TEST_KEY, data)
|
2019-03-13 15:23:50 +01:00
|
|
|
|
2021-10-14 17:16:54 +02:00
|
|
|
expect(Data.get(div, TEST_KEY)).toEqual(data)
|
2019-03-13 15:23:50 +01:00
|
|
|
})
|
|
|
|
|
2021-03-02 15:55:44 +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
|
|
|
|
2021-03-02 15:55:44 +01:00
|
|
|
Data.set(div, TEST_KEY, data)
|
|
|
|
Data.set(div, TEST_KEY, copy)
|
2019-03-13 15:23:50 +01:00
|
|
|
|
2021-10-14 17:16:54 +02:00
|
|
|
// Using `toBe` since spread creates a shallow copy
|
2021-03-02 15:55:44 +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
|
|
|
|
2021-03-02 15:55:44 +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
|
|
|
|
2021-03-02 15:55:44 +01:00
|
|
|
expect().nothing()
|
|
|
|
})
|
2019-03-13 15:23:50 +01:00
|
|
|
|
2021-03-02 15:55:44 +01:00
|
|
|
it('should remove nothing for an unknown key', () => {
|
|
|
|
const data = { ...TEST_DATA }
|
2019-03-13 15:23:50 +01:00
|
|
|
|
2021-03-02 15:55:44 +01:00
|
|
|
Data.set(div, TEST_KEY, data)
|
|
|
|
Data.remove(div, UNKNOWN_KEY)
|
2019-03-13 15:23:50 +01:00
|
|
|
|
2021-10-14 17:16:54 +02:00
|
|
|
expect(Data.get(div, TEST_KEY)).toEqual(data)
|
2021-03-02 15:55:44 +01:00
|
|
|
})
|
2019-03-13 15:23:50 +01:00
|
|
|
|
2021-03-02 15:55:44 +01:00
|
|
|
it('should remove data for a given key', () => {
|
|
|
|
const data = { ...TEST_DATA }
|
2019-03-13 15:23:50 +01:00
|
|
|
|
2021-03-02 15:55:44 +01:00
|
|
|
Data.set(div, TEST_KEY, data)
|
|
|
|
Data.remove(div, TEST_KEY)
|
2019-03-13 15:23:50 +01:00
|
|
|
|
2021-03-02 15:55:44 +01:00
|
|
|
expect(Data.get(div, TEST_KEY)).toBeNull()
|
|
|
|
})
|
2019-03-13 15:23:50 +01:00
|
|
|
|
2021-10-08 11:32:11 +02:00
|
|
|
/* eslint-disable no-console */
|
2021-03-02 15:55:44 +01:00
|
|
|
it('should console.error a message if called with multiple keys', () => {
|
|
|
|
console.error = jasmine.createSpy('console.error')
|
2019-03-13 15:23:50 +01:00
|
|
|
|
2021-03-02 15:55:44 +01:00
|
|
|
const data = { ...TEST_DATA }
|
|
|
|
const copy = { ...data }
|
2019-03-13 15:23:50 +01:00
|
|
|
|
2021-03-02 15:55:44 +01:00
|
|
|
Data.set(div, TEST_KEY, data)
|
|
|
|
Data.set(div, UNKNOWN_KEY, copy)
|
2019-03-13 15:23:50 +01:00
|
|
|
|
2021-03-02 15:55:44 +01:00
|
|
|
expect(console.error).toHaveBeenCalled()
|
2021-10-14 17:16:54 +02:00
|
|
|
expect(Data.get(div, UNKNOWN_KEY)).toBeNull()
|
2019-03-13 15:23:50 +01:00
|
|
|
})
|
2021-10-08 11:32:11 +02:00
|
|
|
/* eslint-enable no-console */
|
2019-03-13 15:23:50 +01:00
|
|
|
})
|