form-radio-group.spec.js
14.3 KB
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
import { mount } from '@vue/test-utils'
import { waitNT } from '../../../tests/utils'
import { BFormRadioGroup } from './form-radio-group'
import { BFormRadio } from './form-radio'
describe('form-radio-group', () => {
// --- Structure, class and attributes tests ---
it('default has structure <div></div>', async () => {
const wrapper = mount(BFormRadioGroup)
expect(wrapper).toBeDefined()
expect(wrapper.element.tagName).toBe('DIV')
const children = wrapper.element.children
expect(children.length).toEqual(0)
wrapper.destroy()
})
it('default has no classes on wrapper other than focus ring', async () => {
const wrapper = mount(BFormRadioGroup)
expect(wrapper.classes().length).toEqual(1)
expect(wrapper.classes()).toContain('bv-no-focus-ring')
wrapper.destroy()
})
it('default has auto ID set', async () => {
const wrapper = mount(BFormRadioGroup, {
attachTo: document.body
})
await waitNT(wrapper.vm)
// Auto ID not generated until after mount
expect(wrapper.attributes('id')).toBeDefined()
wrapper.destroy()
})
it('default has tabindex set to -1', async () => {
const wrapper = mount(BFormRadioGroup)
expect(wrapper.attributes('tabindex')).toBeDefined()
expect(wrapper.attributes('tabindex')).toBe('-1')
wrapper.destroy()
})
it('default does not have aria-required set', async () => {
const wrapper = mount(BFormRadioGroup)
expect(wrapper.attributes('aria-required')).toBeUndefined()
wrapper.destroy()
})
it('default does not have aria-invalid set', async () => {
const wrapper = mount(BFormRadioGroup)
expect(wrapper.attributes('aria-invalid')).toBeUndefined()
wrapper.destroy()
})
it('default has attribute role=radiogroup', async () => {
const wrapper = mount(BFormRadioGroup)
expect(wrapper.attributes('role')).toBeDefined()
expect(wrapper.attributes('role')).toBe('radiogroup')
wrapper.destroy()
})
it('default has user provided ID', async () => {
const wrapper = mount(BFormRadioGroup, {
attachTo: document.body,
propsData: {
id: 'test'
}
})
expect(wrapper.attributes('id')).toBeDefined()
expect(wrapper.attributes('id')).toBe('test')
wrapper.destroy()
})
it('default has class was-validated when validated=true', async () => {
const wrapper = mount(BFormRadioGroup, {
attachTo: document.body,
propsData: {
validated: true
}
})
expect(wrapper.classes()).toBeDefined()
expect(wrapper.classes()).toContain('was-validated')
wrapper.destroy()
})
it('default has attribute aria-invalid=true when state=false', async () => {
const wrapper = mount(BFormRadioGroup, {
attachTo: document.body,
propsData: {
state: false
}
})
expect(wrapper.attributes('aria-invalid')).toBeDefined()
expect(wrapper.attributes('aria-invalid')).toBe('true')
wrapper.destroy()
})
it('default does not have attribute aria-invalid when state=true', async () => {
const wrapper = mount(BFormRadioGroup, {
attachTo: document.body,
propsData: {
state: true
}
})
expect(wrapper.attributes('aria-invalid')).toBeUndefined()
wrapper.destroy()
})
it('default does not have attribute aria-invalid when state=null', async () => {
const wrapper = mount(BFormRadioGroup, {
attachTo: document.body,
propsData: {
state: null
}
})
expect(wrapper.attributes('aria-invalid')).toBeUndefined()
wrapper.destroy()
})
it('default has attribute aria-invalid=true when aria-invalid=true', async () => {
const wrapper = mount(BFormRadioGroup, {
attachTo: document.body,
propsData: {
ariaInvalid: true
}
})
expect(wrapper.attributes('aria-invalid')).toBeDefined()
expect(wrapper.attributes('aria-invalid')).toBe('true')
wrapper.destroy()
})
it('default has attribute aria-invalid=true when aria-invalid="true"', async () => {
const wrapper = mount(BFormRadioGroup, {
attachTo: document.body,
propsData: {
ariaInvalid: 'true'
}
})
expect(wrapper.attributes('aria-invalid')).toBeDefined()
expect(wrapper.attributes('aria-invalid')).toBe('true')
wrapper.destroy()
})
it('default has attribute aria-invalid=true when aria-invalid=""', async () => {
const wrapper = mount(BFormRadioGroup, {
attachTo: document.body,
propsData: {
ariaInvalid: ''
}
})
expect(wrapper.attributes('aria-invalid')).toBeDefined()
expect(wrapper.attributes('aria-invalid')).toBe('true')
wrapper.destroy()
})
it('has radios with input validation class "is-valid" when `state` is `true`', async () => {
const wrapper = mount(BFormRadioGroup, {
attachTo: document.body,
propsData: {
options: ['one', 'two', 'three'],
checked: '',
state: true
}
})
const $radios = wrapper.findAll('input[type=radio]')
expect($radios.length).toBe(3)
expect($radios.wrappers.every(c => c.classes().includes('is-valid'))).toBe(true)
expect($radios.wrappers.every(c => c.classes().includes('is-invalid'))).toBe(false)
wrapper.destroy()
})
it('has radios with input validation class "is-invalid" when `state` is `false`', async () => {
const wrapper = mount(BFormRadioGroup, {
attachTo: document.body,
propsData: {
options: ['one', 'two', 'three'],
checked: '',
state: false
}
})
const $radios = wrapper.findAll('input[type=radio]')
expect($radios.length).toBe(3)
expect($radios.wrappers.every(c => c.classes().includes('is-valid'))).toBe(false)
expect($radios.wrappers.every(c => c.classes().includes('is-invalid'))).toBe(true)
wrapper.destroy()
})
it('has radios with no input validation class when `state` is `null`', async () => {
const wrapper = mount(BFormRadioGroup, {
attachTo: document.body,
propsData: {
options: ['one', 'two', 'three'],
checked: '',
state: null
}
})
const $radios = wrapper.findAll('input[type=radio]')
expect($radios.length).toBe(3)
expect($radios.wrappers.every(c => c.classes().includes('is-valid'))).toBe(false)
expect($radios.wrappers.every(c => c.classes().includes('is-invalid'))).toBe(false)
wrapper.destroy()
})
// --- Button mode structure ---
it('button mode has classes button-group and button-group-toggle', async () => {
const wrapper = mount(BFormRadioGroup, {
attachTo: document.body,
propsData: {
buttons: true
}
})
expect(wrapper.classes()).toBeDefined()
expect(wrapper.classes().length).toBe(3)
expect(wrapper.classes()).toContain('btn-group')
expect(wrapper.classes()).toContain('btn-group-toggle')
expect(wrapper.classes()).toContain('bv-no-focus-ring')
wrapper.destroy()
})
it('button mode has classes button-group-vertical and button-group-toggle when stacked=true', async () => {
const wrapper = mount(BFormRadioGroup, {
attachTo: document.body,
propsData: {
buttons: true,
stacked: true
}
})
expect(wrapper.classes()).toBeDefined()
expect(wrapper.classes().length).toBe(3)
expect(wrapper.classes()).toContain('btn-group-vertical')
expect(wrapper.classes()).toContain('btn-group-toggle')
expect(wrapper.classes()).toContain('bv-no-focus-ring')
wrapper.destroy()
})
it('button mode has size class when size prop set', async () => {
const wrapper = mount(BFormRadioGroup, {
attachTo: document.body,
propsData: {
buttons: true,
size: 'lg'
}
})
expect(wrapper.classes()).toBeDefined()
expect(wrapper.classes().length).toBe(4)
expect(wrapper.classes()).toContain('btn-group')
expect(wrapper.classes()).toContain('btn-group-toggle')
expect(wrapper.classes()).toContain('btn-group-lg')
expect(wrapper.classes()).toContain('bv-no-focus-ring')
wrapper.destroy()
})
it('button mode has size class when size prop set and stacked', async () => {
const wrapper = mount(BFormRadioGroup, {
attachTo: document.body,
propsData: {
buttons: true,
stacked: true,
size: 'lg'
}
})
expect(wrapper.classes()).toBeDefined()
expect(wrapper.classes().length).toBe(4)
expect(wrapper.classes()).toContain('btn-group-vertical')
expect(wrapper.classes()).toContain('btn-group-toggle')
expect(wrapper.classes()).toContain('btn-group-lg')
expect(wrapper.classes()).toContain('bv-no-focus-ring')
wrapper.destroy()
})
it('button mode button-variant works', async () => {
const App = {
render(h) {
return h(
BFormRadioGroup,
{
props: {
checked: '',
buttons: true,
buttonVariant: 'primary'
}
},
[
h(BFormRadio, { props: { value: 'one' } }, 'button 1'),
h(BFormRadio, { props: { value: 'two' } }, 'button 2'),
h(BFormRadio, { props: { value: 'three', buttonVariant: 'danger' } }, 'button 3')
]
)
}
}
const wrapper = mount(App, {
attachTo: document.body
})
expect(wrapper).toBeDefined()
await waitNT(wrapper.vm)
// Find all the labels with .btn class
const btns = wrapper.findAll('label.btn')
expect(btns).toBeDefined()
expect(btns.length).toBe(3)
// Expect them to have the correct variant classes
expect(btns.at(0).classes()).toContain('btn-primary')
expect(btns.at(1).classes()).toContain('btn-primary')
expect(btns.at(2).classes()).toContain('btn-danger')
wrapper.destroy()
})
// --- Functionality testing ---
it('has radios via options array', async () => {
const wrapper = mount(BFormRadioGroup, {
attachTo: document.body,
propsData: {
options: ['one', 'two', 'three'],
checked: ''
}
})
expect(wrapper.vm.isRadioGroup).toEqual(true)
expect(wrapper.vm.localChecked).toEqual('')
const radios = wrapper.findAll('input')
expect(radios.length).toBe(3)
expect(radios.wrappers.every(c => c.element.matches('input[type=radio]'))).toBe(true)
wrapper.destroy()
})
it('has radios via options array which respect disabled', async () => {
const wrapper = mount(BFormRadioGroup, {
attachTo: document.body,
propsData: {
options: [{ text: 'one' }, { text: 'two' }, { text: 'three', disabled: true }],
checked: ''
}
})
expect(wrapper.classes()).toBeDefined()
const radios = wrapper.findAll('input')
expect(radios.length).toBe(3)
expect(wrapper.vm.localChecked).toEqual('')
expect(radios.wrappers.every(c => c.element.matches('input[type=radio]'))).toBe(true)
expect(radios.at(0).attributes('disabled')).toBeUndefined()
expect(radios.at(1).attributes('disabled')).toBeUndefined()
expect(radios.at(2).attributes('disabled')).toBeDefined()
wrapper.destroy()
})
it('has radios with attribute required when prop required set', async () => {
const wrapper = mount(BFormRadioGroup, {
attachTo: document.body,
propsData: {
options: ['one', 'two', 'three'],
checked: '',
required: true
}
})
// We need `$nextTick()` here since auto generated name is
// computed in a `$nextTick()` on mount
await waitNT(wrapper.vm)
expect(wrapper.classes()).toBeDefined()
const radios = wrapper.findAll('input')
expect(radios.length).toBe(3)
expect(wrapper.vm.localChecked).toEqual('')
expect(radios.wrappers.every(c => c.find('input[type=radio]'))).toBe(true)
expect(radios.wrappers.every(c => c.find('input[required]'))).toBe(true)
expect(radios.wrappers.every(c => c.find('input[aria-required="true"]'))).toBe(true)
wrapper.destroy()
})
it('emits change event when radio clicked', async () => {
const wrapper = mount(BFormRadioGroup, {
attachTo: document.body,
propsData: {
options: ['one', 'two', 'three'],
checked: ''
}
})
expect(wrapper.classes()).toBeDefined()
const radios = wrapper.findAll('input')
expect(radios.length).toBe(3)
expect(wrapper.vm.localChecked).toEqual('')
await radios.at(0).trigger('click')
expect(wrapper.vm.localChecked).toEqual('one')
expect(wrapper.emitted('change')).toBeDefined()
expect(wrapper.emitted('change').length).toBe(1)
expect(wrapper.emitted('change')[0][0]).toEqual('one')
expect(wrapper.emitted('input')).toBeDefined()
expect(wrapper.emitted('input').length).toBe(1)
expect(wrapper.emitted('input')[0][0]).toEqual('one')
await radios.at(2).trigger('click')
expect(wrapper.vm.localChecked).toEqual('three')
expect(wrapper.emitted('change').length).toBe(2)
expect(wrapper.emitted('change')[1][0]).toEqual('three')
expect(wrapper.emitted('input').length).toBe(2)
expect(wrapper.emitted('input')[1][0]).toEqual('three')
await radios.at(0).trigger('click')
expect(wrapper.vm.localChecked).toEqual('one')
expect(wrapper.emitted('change').length).toBe(3)
expect(wrapper.emitted('change')[2][0]).toEqual('one')
expect(wrapper.emitted('input').length).toBe(3)
expect(wrapper.emitted('input')[2][0]).toEqual('one')
wrapper.destroy()
})
it('radios reflect group checked v-model', async () => {
const wrapper = mount(BFormRadioGroup, {
attachTo: document.body,
propsData: {
options: ['one', 'two', 'three'],
checked: 'two'
}
})
expect(wrapper.classes()).toBeDefined()
const radios = wrapper.findAll('input')
expect(radios.length).toBe(3)
expect(wrapper.vm.localChecked).toEqual('two')
expect(radios.wrappers.every(w => w.attributes('type') === 'radio')).toBe(true)
expect(radios.at(0).element.checked).toBe(false)
expect(radios.at(1).element.checked).toBe(true)
expect(radios.at(2).element.checked).toBe(false)
await wrapper.setProps({ checked: 'three' })
await waitNT(wrapper.vm)
await waitNT(wrapper.vm)
expect(wrapper.vm.localChecked).toEqual('three')
expect(radios.wrappers.every(w => w.attributes('type') === 'radio')).toBe(true)
expect(radios.at(0).element.checked).toBe(false)
expect(radios.at(1).element.checked).toBe(false)
expect(radios.at(2).element.checked).toBe(true)
wrapper.destroy()
})
})