2019-10-02 11:43:54 +02:00
|
|
|
import Button from '../../src/button'
|
2019-03-25 11:32:02 +01:00
|
|
|
|
|
|
|
/** Test helpers */
|
|
|
|
import {
|
|
|
|
getFixture,
|
|
|
|
clearFixture,
|
|
|
|
jQueryMock
|
2019-10-02 11:43:54 +02:00
|
|
|
} 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 = [
|
2020-07-22 21:33:11 +02:00
|
|
|
'<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', () => {
|
2020-07-22 21:33:11 +02:00
|
|
|
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', () => {
|
2020-07-22 21:33:11 +02:00
|
|
|
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)
|
|
|
|
|
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', () => {
|
2020-07-22 21:33:11 +02:00
|
|
|
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', () => {
|
2020-07-22 21:33:11 +02:00
|
|
|
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')
|
|
|
|
|
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', () => {
|
2020-07-22 21:33:11 +02:00
|
|
|
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)
|
|
|
|
|
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)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|