1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
|
import Button from '../../src/button'
import EventHandler from '../../src/dom/event-handler'
/** Test helpers */
import {
getFixture,
clearFixture,
createEvent,
jQueryMock
} from '../helpers/fixture'
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)
})
it('should trigger input change event when toggled button has input field', done => {
fixtureEl.innerHTML = [
'<div class="btn-group" data-toggle="buttons">',
' <label class="btn btn-primary">',
' <input type="radio" id="radio" autocomplete="off"> Radio',
' </label>',
'</div>'
].join('')
const input = fixtureEl.querySelector('input')
const label = fixtureEl.querySelector('label')
input.addEventListener('change', () => {
expect().nothing()
done()
})
label.click()
})
it('should not trigger input change event when input already checked and button is active', () => {
fixtureEl.innerHTML = [
'<button type="button" class="btn btn-primary active" data-toggle="buttons">',
' <input type="radio" id="radio" autocomplete="off" checked> Radio',
'</button>'
].join('')
const button = fixtureEl.querySelector('button')
spyOn(EventHandler, 'trigger')
button.click()
expect(EventHandler.trigger).not.toHaveBeenCalled()
})
it('should remove active when an other radio button is clicked', () => {
fixtureEl.innerHTML = [
'<div class="btn-group btn-group-toggle" data-toggle="buttons">',
' <label class="btn btn-secondary active">',
' <input type="radio" name="options" id="option1" autocomplete="off" checked> Active',
' </label>',
' <label class="btn btn-secondary">',
' <input type="radio" name="options" id="option2" autocomplete="off"> Radio',
' </label>',
' <label class="btn btn-secondary">',
' <input type="radio" name="options" id="option3" autocomplete="off"> Radio',
' </label>',
'</div>'
].join('')
const option1 = fixtureEl.querySelector('#option1')
const option2 = fixtureEl.querySelector('#option2')
expect(option1.checked).toEqual(true)
expect(option1.parentElement.classList.contains('active')).toEqual(true)
const clickEvent = createEvent('click')
option2.dispatchEvent(clickEvent)
expect(option1.checked).toEqual(false)
expect(option1.parentElement.classList.contains('active')).toEqual(false)
expect(option2.checked).toEqual(true)
expect(option2.parentElement.classList.contains('active')).toEqual(true)
})
it('should do nothing if the child is not an input', () => {
fixtureEl.innerHTML = [
'<div class="btn-group btn-group-toggle" data-toggle="buttons">',
' <label class="btn btn-secondary active">',
' <span id="option1">el 1</span>',
' </label>',
' <label class="btn btn-secondary">',
' <span id="option2">el 2</span>',
' </label>',
' <label class="btn btn-secondary">',
' <span>el 3</span>',
' </label>',
'</div>'
].join('')
const option2 = fixtureEl.querySelector('#option2')
const clickEvent = createEvent('click')
option2.dispatchEvent(clickEvent)
expect().nothing()
})
it('should add focus class on focus event', () => {
fixtureEl.innerHTML = '<button class="btn" data-toggle="button"><input type="text" /></button>'
const btn = fixtureEl.querySelector('.btn')
const input = fixtureEl.querySelector('input')
const focusEvent = createEvent('focus')
input.dispatchEvent(focusEvent)
expect(btn.classList.contains('focus')).toEqual(true)
})
it('should not add focus class', () => {
fixtureEl.innerHTML = '<button data-toggle="button"><input type="text" /></button>'
const btn = fixtureEl.querySelector('button')
const input = fixtureEl.querySelector('input')
const focusEvent = createEvent('focus')
input.dispatchEvent(focusEvent)
expect(btn.classList.contains('focus')).toEqual(false)
})
it('should remove focus class on blur event', () => {
fixtureEl.innerHTML = '<button class="btn focus" data-toggle="button"><input type="text" /></button>'
const btn = fixtureEl.querySelector('.btn')
const input = fixtureEl.querySelector('input')
const focusEvent = createEvent('blur')
input.dispatchEvent(focusEvent)
expect(btn.classList.contains('focus')).toEqual(false)
})
it('should not remove focus class on blur event', () => {
fixtureEl.innerHTML = '<button class="focus" data-toggle="button"><input type="text" /></button>'
const btn = fixtureEl.querySelector('button')
const input = fixtureEl.querySelector('input')
const focusEvent = createEvent('blur')
input.dispatchEvent(focusEvent)
expect(btn.classList.contains('focus')).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)
})
it('should handle disabled attribute on non-button elements', () => {
fixtureEl.innerHTML = [
'<div class="btn-group disabled" data-toggle="buttons" aria-disabled="true" disabled>',
' <label class="btn btn-danger disabled" aria-disabled="true" disabled>',
' <input type="checkbox" aria-disabled="true" autocomplete="off" disabled class="disabled"/>',
' </label>',
'</div>'
].join('')
const btnGroupEl = fixtureEl.querySelector('.btn-group')
const btnDanger = fixtureEl.querySelector('.btn-danger')
const input = fixtureEl.querySelector('input')
const button = new Button(btnGroupEl)
button.toggle()
expect(btnDanger.hasAttribute('disabled')).toEqual(true)
expect(input.checked).toEqual(false)
})
})
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)
expect(Button.getInstance(btnEl)).toBeDefined()
button.dispose()
expect(Button.getInstance(btnEl)).toBeNull()
})
})
describe('jQueryInterface', () => {
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')
jQueryMock.fn.button = Button.jQueryInterface
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')
jQueryMock.fn.button = Button.jQueryInterface
jQueryMock.elements = [btnEl]
jQueryMock.fn.button.call(jQueryMock, 'toggle')
expect(Button.getInstance(btnEl)).toBeDefined()
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')
jQueryMock.fn.button = Button.jQueryInterface
jQueryMock.elements = [btnEl]
jQueryMock.fn.button.call(jQueryMock)
expect(Button.getInstance(btnEl)).toBeDefined()
expect(btnEl.classList.contains('active')).toEqual(false)
})
})
})
|