mirror of
https://github.com/twbs/bootstrap.git
synced 2025-01-07 00:46:15 +01:00
ece1601227
* Revamp scrollspy to use IntersectionObserver * Add smooth scroll support * Update scrollspy.js/md * move functionality to method * Update scrollspy.js * Add SmoothScroll to docs example * Refactor Using `Maps` and smaller methods * Update scrollspy.md/js * Update scrollspy.spec.js * Support backwards compatibility * minor optimizations * Merge activation functionality * Update scrollspy.md * Update scrollspy.js * Rewording some of the documentation changes * Update scrollspy.js * Update scrollspy.md * tweaking calculation functionality & drop text that suggests, to deactivate target when wrapper is not visible * tweak calculation * Fix lint * Support scrollspy in body & tests * change doc example to a more valid solution Co-authored-by: XhmikosR <xhmikosr@gmail.com> Co-authored-by: Patrick H. Lauke <redux@splintered.co.uk>
109 lines
3.4 KiB
JavaScript
109 lines
3.4 KiB
JavaScript
import Manipulator from '../../../src/dom/manipulator'
|
|
import { clearFixture, getFixture } from '../../helpers/fixture'
|
|
|
|
describe('Manipulator', () => {
|
|
let fixtureEl
|
|
|
|
beforeAll(() => {
|
|
fixtureEl = getFixture()
|
|
})
|
|
|
|
afterEach(() => {
|
|
clearFixture()
|
|
})
|
|
|
|
describe('setDataAttribute', () => {
|
|
it('should set data attribute prefixed with bs', () => {
|
|
fixtureEl.innerHTML = '<div></div>'
|
|
|
|
const div = fixtureEl.querySelector('div')
|
|
|
|
Manipulator.setDataAttribute(div, 'key', 'value')
|
|
expect(div.getAttribute('data-bs-key')).toEqual('value')
|
|
})
|
|
|
|
it('should set data attribute in kebab case', () => {
|
|
fixtureEl.innerHTML = '<div></div>'
|
|
|
|
const div = fixtureEl.querySelector('div')
|
|
|
|
Manipulator.setDataAttribute(div, 'testKey', 'value')
|
|
expect(div.getAttribute('data-bs-test-key')).toEqual('value')
|
|
})
|
|
})
|
|
|
|
describe('removeDataAttribute', () => {
|
|
it('should only remove bs-prefixed data attribute', () => {
|
|
fixtureEl.innerHTML = '<div data-bs-key="value" data-key-bs="postfixed" data-key="value"></div>'
|
|
|
|
const div = fixtureEl.querySelector('div')
|
|
|
|
Manipulator.removeDataAttribute(div, 'key')
|
|
expect(div.getAttribute('data-bs-key')).toBeNull()
|
|
expect(div.getAttribute('data-key-bs')).toEqual('postfixed')
|
|
expect(div.getAttribute('data-key')).toEqual('value')
|
|
})
|
|
|
|
it('should remove data attribute in kebab case', () => {
|
|
fixtureEl.innerHTML = '<div data-bs-test-key="value"></div>'
|
|
|
|
const div = fixtureEl.querySelector('div')
|
|
|
|
Manipulator.removeDataAttribute(div, 'testKey')
|
|
expect(div.getAttribute('data-bs-test-key')).toBeNull()
|
|
})
|
|
})
|
|
|
|
describe('getDataAttributes', () => {
|
|
it('should return an empty object for null', () => {
|
|
expect(Manipulator.getDataAttributes(null)).toEqual({})
|
|
expect().nothing()
|
|
})
|
|
|
|
it('should get only bs-prefixed data attributes without bs namespace', () => {
|
|
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>'
|
|
|
|
const div = fixtureEl.querySelector('div')
|
|
|
|
expect(Manipulator.getDataAttributes(div)).toEqual({
|
|
toggle: 'tabs',
|
|
target: '#element'
|
|
})
|
|
})
|
|
})
|
|
|
|
describe('getDataAttribute', () => {
|
|
it('should only get bs-prefixed data attribute', () => {
|
|
fixtureEl.innerHTML = '<div data-bs-key="value" data-test-bs="postFixed" data-toggle="tab"></div>'
|
|
|
|
const div = fixtureEl.querySelector('div')
|
|
|
|
expect(Manipulator.getDataAttribute(div, 'key')).toEqual('value')
|
|
expect(Manipulator.getDataAttribute(div, 'test')).toBeNull()
|
|
expect(Manipulator.getDataAttribute(div, 'toggle')).toBeNull()
|
|
})
|
|
|
|
it('should get data attribute in kebab case', () => {
|
|
fixtureEl.innerHTML = '<div data-bs-test-key="value" ></div>'
|
|
|
|
const div = fixtureEl.querySelector('div')
|
|
|
|
expect(Manipulator.getDataAttribute(div, 'testKey')).toEqual('value')
|
|
})
|
|
|
|
it('should normalize data', () => {
|
|
fixtureEl.innerHTML = '<div data-bs-test="false" ></div>'
|
|
|
|
const div = fixtureEl.querySelector('div')
|
|
|
|
expect(Manipulator.getDataAttribute(div, 'test')).toBeFalse()
|
|
|
|
div.setAttribute('data-bs-test', 'true')
|
|
expect(Manipulator.getDataAttribute(div, 'test')).toBeTrue()
|
|
|
|
div.setAttribute('data-bs-test', '1')
|
|
expect(Manipulator.getDataAttribute(div, 'test')).toEqual(1)
|
|
})
|
|
})
|
|
})
|