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

184 lines
5.5 KiB
JavaScript
Raw Normal View History

import Button from '../../src/button'
import { getFixture, clearFixture, jQueryMock } from '../helpers/fixture'
2019-03-25 11:32:02 +01:00
describe('Button', () => {
let fixtureEl
beforeAll(() => {
fixtureEl = getFixture()
})
afterEach(() => {
clearFixture()
})
it('should take care of element either passed as a CSS selector or DOM element', () => {
fixtureEl.innerHTML = '<button data-bs-toggle="button">Placeholder</button>'
const buttonEl = fixtureEl.querySelector('[data-bs-toggle="button"]')
const buttonBySelector = new Button('[data-bs-toggle="button"]')
const buttonByElement = new Button(buttonEl)
expect(buttonBySelector._element).toEqual(buttonEl)
expect(buttonByElement._element).toEqual(buttonEl)
})
2019-03-25 11:32:02 +01:00
describe('VERSION', () => {
it('should return plugin version', () => {
expect(Button.VERSION).toEqual(jasmine.any(String))
})
})
describe('DATA_KEY', () => {
it('should return plugin data key', () => {
expect(Button.DATA_KEY).toEqual('bs.button')
})
})
2019-03-25 11:32:02 +01:00
describe('data-api', () => {
it('should toggle active class on click', () => {
fixtureEl.innerHTML = [
'<button class="btn" data-bs-toggle="button">btn</button>',
'<button class="btn testParent" data-bs-toggle="button"><div class="test"></div></button>'
2019-03-25 11:32:02 +01:00
].join('')
const btn = fixtureEl.querySelector('.btn')
const divTest = fixtureEl.querySelector('.test')
const btnTestParent = fixtureEl.querySelector('.testParent')
expect(btn.classList.contains('active')).toEqual(false)
btn.click()
expect(btn.classList.contains('active')).toEqual(true)
btn.click()
expect(btn.classList.contains('active')).toEqual(false)
divTest.click()
expect(btnTestParent.classList.contains('active')).toEqual(true)
})
})
describe('toggle', () => {
it('should toggle aria-pressed', () => {
fixtureEl.innerHTML = '<button class="btn" data-bs-toggle="button" aria-pressed="false"></button>'
2019-03-25 11:32:02 +01:00
const btnEl = fixtureEl.querySelector('.btn')
const button = new Button(btnEl)
expect(btnEl.getAttribute('aria-pressed')).toEqual('false')
expect(btnEl.classList.contains('active')).toEqual(false)
button.toggle()
expect(btnEl.getAttribute('aria-pressed')).toEqual('true')
expect(btnEl.classList.contains('active')).toEqual(true)
})
})
describe('dispose', () => {
it('should dispose a button', () => {
fixtureEl.innerHTML = '<button class="btn" data-bs-toggle="button"></button>'
2019-03-25 11:32:02 +01:00
const btnEl = fixtureEl.querySelector('.btn')
const button = new Button(btnEl)
expect(Button.getInstance(btnEl)).not.toBeNull()
2019-03-25 11:32:02 +01:00
button.dispose()
2019-07-28 15:24:46 +02:00
expect(Button.getInstance(btnEl)).toBeNull()
2019-03-25 11:32:02 +01:00
})
})
2019-07-28 15:24:46 +02:00
describe('jQueryInterface', () => {
2019-03-25 11:32:02 +01:00
it('should handle config passed and toggle existing button', () => {
fixtureEl.innerHTML = '<button class="btn" data-bs-toggle="button"></button>'
2019-03-25 11:32:02 +01:00
const btnEl = fixtureEl.querySelector('.btn')
const button = new Button(btnEl)
spyOn(button, 'toggle')
2019-07-28 15:24:46 +02:00
jQueryMock.fn.button = Button.jQueryInterface
2019-03-25 11:32:02 +01:00
jQueryMock.elements = [btnEl]
jQueryMock.fn.button.call(jQueryMock, 'toggle')
expect(button.toggle).toHaveBeenCalled()
})
it('should create new button instance and call toggle', () => {
fixtureEl.innerHTML = '<button class="btn" data-bs-toggle="button"></button>'
2019-03-25 11:32:02 +01:00
const btnEl = fixtureEl.querySelector('.btn')
2019-07-28 15:24:46 +02:00
jQueryMock.fn.button = Button.jQueryInterface
2019-03-25 11:32:02 +01:00
jQueryMock.elements = [btnEl]
jQueryMock.fn.button.call(jQueryMock, 'toggle')
expect(Button.getInstance(btnEl)).not.toBeNull()
2019-03-25 11:32:02 +01:00
expect(btnEl.classList.contains('active')).toEqual(true)
})
it('should just create a button instance without calling toggle', () => {
fixtureEl.innerHTML = '<button class="btn" data-bs-toggle="button"></button>'
2019-03-25 11:32:02 +01:00
const btnEl = fixtureEl.querySelector('.btn')
2019-07-28 15:24:46 +02:00
jQueryMock.fn.button = Button.jQueryInterface
2019-03-25 11:32:02 +01:00
jQueryMock.elements = [btnEl]
jQueryMock.fn.button.call(jQueryMock)
expect(Button.getInstance(btnEl)).not.toBeNull()
2019-03-25 11:32:02 +01:00
expect(btnEl.classList.contains('active')).toEqual(false)
})
})
2019-09-04 16:58:29 +02:00
describe('getInstance', () => {
it('should return button instance', () => {
fixtureEl.innerHTML = '<div></div>'
const div = fixtureEl.querySelector('div')
const button = new Button(div)
expect(Button.getInstance(div)).toEqual(button)
expect(Button.getInstance(div)).toBeInstanceOf(Button)
2019-09-04 16:58:29 +02:00
})
it('should return null when there is no button instance', () => {
fixtureEl.innerHTML = '<div></div>'
const div = fixtureEl.querySelector('div')
expect(Button.getInstance(div)).toEqual(null)
})
})
describe('getOrCreateInstance', () => {
it('should return button instance', () => {
fixtureEl.innerHTML = '<div></div>'
const div = fixtureEl.querySelector('div')
const button = new Button(div)
expect(Button.getOrCreateInstance(div)).toEqual(button)
expect(Button.getInstance(div)).toEqual(Button.getOrCreateInstance(div, {}))
expect(Button.getOrCreateInstance(div)).toBeInstanceOf(Button)
})
it('should return new instance when there is no button instance', () => {
fixtureEl.innerHTML = '<div></div>'
const div = fixtureEl.querySelector('div')
expect(Button.getInstance(div)).toEqual(null)
expect(Button.getOrCreateInstance(div)).toBeInstanceOf(Button)
})
})
2019-03-25 11:32:02 +01:00
})