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

132 lines
3.6 KiB
JavaScript
Raw Normal View History

import Button from '../../src/button'
2019-03-25 11:32:02 +01:00
/** Test helpers */
import {
getFixture,
clearFixture,
jQueryMock
} from '../helpers/fixture'
2019-03-25 11:32:02 +01:00
describe('Button', () => {
let fixtureEl
beforeAll(() => {
fixtureEl = getFixture()
})
afterEach(() => {
clearFixture()
})
describe('VERSION', () => {
it('should return plugin version', () => {
expect(Button.VERSION).toEqual(jasmine.any(String))
})
})
describe('data-api', () => {
it('should toggle active class on click', () => {
fixtureEl.innerHTML = [
'<button class="btn" data-toggle="button">btn</button>',
'<button class="btn testParent" data-toggle="button"><div class="test"></div></button>'
].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-toggle="button" aria-pressed="false"></button>'
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-toggle="button"></button>'
const btnEl = fixtureEl.querySelector('.btn')
const button = new Button(btnEl)
2019-07-28 15:24:46 +02:00
expect(Button.getInstance(btnEl)).toBeDefined()
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-toggle="button"></button>'
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-toggle="button"></button>'
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')
2019-07-28 15:24:46 +02:00
expect(Button.getInstance(btnEl)).toBeDefined()
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-toggle="button"></button>'
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)
2019-07-28 15:24:46 +02:00
expect(Button.getInstance(btnEl)).toBeDefined()
2019-03-25 11:32:02 +01:00
expect(btnEl.classList.contains('active')).toEqual(false)
})
})
})