/
form-tags.spec.js
570 lines (485 loc) · 19.6 KB
/
form-tags.spec.js
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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
import { mount } from '@vue/test-utils'
import { createContainer, waitNT, waitRAF } from '../../../tests/utils'
import { BFormTags } from './form-tags'
describe('form-tags', () => {
it('has expected default structure', async () => {
const wrapper = mount(BFormTags)
expect(wrapper.element.tagName).toBe('DIV')
expect(wrapper.classes()).toContain('b-form-tags')
expect(wrapper.classes()).toContain('form-control')
expect(wrapper.attributes('role')).toBe('group')
expect(wrapper.attributes('tabindex')).toBe('-1')
wrapper.destroy()
})
it('has tags when value is set', async () => {
const wrapper = mount(BFormTags, {
propsData: {
value: ['apple', 'orange']
}
})
expect(wrapper.element.tagName).toBe('DIV')
const $tags = wrapper.findAll('.b-form-tag')
expect($tags.length).toBe(2)
const $tag0 = $tags.at(0)
expect($tag0.attributes('title')).toEqual('apple')
expect($tag0.classes()).toContain('badge')
expect($tag0.classes()).toContain('badge-secondary')
expect($tag0.text()).toContain('apple')
expect($tag0.find('button.close').exists()).toBe(true)
const $tag1 = $tags.at(1)
expect($tag1.attributes('title')).toEqual('orange')
expect($tag1.classes()).toContain('badge')
expect($tag1.classes()).toContain('badge-secondary')
expect($tag1.text()).toContain('orange')
expect($tag1.find('button.close').exists()).toBe(true)
wrapper.destroy()
})
it('responds to changes in value prop', async () => {
const wrapper = mount(BFormTags, {
propsData: {
value: ['apple', 'orange']
}
})
expect(wrapper.element.tagName).toBe('DIV')
await wrapper.setProps({ value: ['pear'] })
expect(wrapper.vm.tags).toEqual(['pear'])
wrapper.destroy()
})
it('default slot has expected scope', async () => {
let scope
const wrapper = mount(BFormTags, {
propsData: {
value: ['apple', 'orange']
},
scopedSlots: {
default(props) {
scope = props
}
}
})
await waitNT(wrapper.vm)
await waitRAF()
expect(wrapper.element.tagName).toBe('DIV')
expect(scope).toBeDefined()
expect(typeof scope).toBe('object')
expect(Array.isArray(scope.tags)).toBe(true)
expect(scope.tags).toEqual(['apple', 'orange'])
expect(typeof scope.addTag).toBe('function')
expect(typeof scope.removeTag).toBe('function')
expect(typeof scope.disabled).toBe('boolean')
expect(scope.state).toEqual(null)
expect(typeof scope.tagRemoveLabel).toBe('string')
expect(scope.tagRemoveLabel).toBe('Remove tag')
expect(typeof scope.placeholder).toBe('string')
expect(scope.placeholder).toBe('Add tag...')
expect(typeof scope.tagVariant).toBe('string')
expect(scope.tagVariant).toBe('secondary')
expect(scope.inputAttrs).toEqual(wrapper.vm.computedInputAttrs)
expect(scope.inputHandlers).toEqual(wrapper.vm.computedInputHandlers)
wrapper.destroy()
})
it('has hidden inputs when name is set', async () => {
const wrapper = mount(BFormTags, {
propsData: {
value: ['apple', 'orange'],
name: 'foo'
}
})
expect(wrapper.element.tagName).toBe('DIV')
const $hidden = wrapper.findAll('input[type=hidden]')
expect($hidden.length).toBe(2)
expect($hidden.at(0).attributes('value')).toEqual('apple')
expect($hidden.at(0).attributes('name')).toEqual('foo')
expect($hidden.at(1).attributes('value')).toEqual('orange')
expect($hidden.at(1).attributes('name')).toEqual('foo')
wrapper.destroy()
})
it('adds new tags from user input', async () => {
const wrapper = mount(BFormTags, {
propsData: {
value: ['apple', 'orange']
}
})
expect(wrapper.element.tagName).toBe('DIV')
expect(wrapper.vm.tags).toEqual(['apple', 'orange'])
expect(wrapper.vm.newTag).toEqual('')
const $input = wrapper.find('input')
expect($input.exists()).toBe(true)
expect($input.element.value).toBe('')
expect($input.element.type).toBe('text')
$input.element.value = 'pear'
await $input.trigger('input')
expect(wrapper.vm.newTag).toEqual('pear')
expect(wrapper.vm.tags).toEqual(['apple', 'orange'])
await $input.trigger('change')
expect(wrapper.vm.newTag).toEqual('pear')
expect(wrapper.vm.tags).toEqual(['apple', 'orange'])
await wrapper.setProps({ addOnChange: true })
await $input.trigger('change')
expect(wrapper.vm.newTag).toEqual('')
expect(wrapper.vm.tags).toEqual(['apple', 'orange', 'pear'])
await wrapper.setProps({ addOnChange: false })
$input.element.value = 'peach'
await $input.trigger('input')
expect(wrapper.vm.newTag).toEqual('peach')
expect(wrapper.vm.tags).toEqual(['apple', 'orange', 'pear'])
await $input.trigger('keydown.enter')
expect(wrapper.vm.newTag).toEqual('')
expect(wrapper.vm.tags).toEqual(['apple', 'orange', 'pear', 'peach'])
wrapper.destroy()
})
it('removes tags when user clicks remove on tag', async () => {
const wrapper = mount(BFormTags, {
propsData: {
value: ['apple', 'orange', 'pear', 'peach']
}
})
expect(wrapper.element.tagName).toBe('DIV')
expect(wrapper.vm.tags).toEqual(['apple', 'orange', 'pear', 'peach'])
expect(wrapper.vm.newTag).toEqual('')
let $tags = wrapper.findAll('.badge')
expect($tags.length).toBe(4)
expect($tags.at(1).attributes('title')).toEqual('orange')
const $btn = $tags.at(1).find('button')
expect($btn.exists()).toBe(true)
await $btn.trigger('click')
expect(wrapper.vm.tags).toEqual(['apple', 'pear', 'peach'])
$tags = wrapper.findAll('.badge')
expect($tags.length).toBe(3)
expect($tags.at(1).attributes('title')).toEqual('pear')
wrapper.destroy()
})
it('adds new tags via separator', async () => {
const wrapper = mount(BFormTags, {
propsData: {
separator: ' ,;',
value: ['apple', 'orange']
}
})
expect(wrapper.element.tagName).toBe('DIV')
expect(wrapper.vm.tags).toEqual(['apple', 'orange'])
expect(wrapper.vm.newTag).toEqual('')
const $input = wrapper.find('input')
expect($input.exists()).toBe(true)
expect($input.element.value).toBe('')
$input.element.value = 'pear'
await $input.trigger('input')
expect(wrapper.vm.newTag).toEqual('pear')
expect(wrapper.vm.tags).toEqual(['apple', 'orange'])
$input.element.value = 'pear '
await $input.trigger('input')
expect(wrapper.vm.newTag).toEqual('')
expect(wrapper.vm.tags).toEqual(['apple', 'orange', 'pear'])
$input.element.value = 'peach; foo,bar apple pie'
await $input.trigger('input')
expect(wrapper.vm.newTag).toEqual('peach; foo,bar apple pie')
expect(wrapper.vm.tags).toEqual(['apple', 'orange', 'pear'])
$input.element.value = 'peach; foo,bar apple pie '
await $input.trigger('input')
expect(wrapper.vm.newTag).toEqual('apple ')
expect(wrapper.vm.tags).toEqual(['apple', 'orange', 'pear', 'peach', 'foo', 'bar', 'pie'])
wrapper.destroy()
})
it('tag validation works', async () => {
const wrapper = mount(BFormTags, {
propsData: {
separator: ' ',
tagValidator: tag => tag.length < 5,
value: ['one', 'two']
}
})
expect(wrapper.element.tagName).toBe('DIV')
expect(wrapper.vm.tags).toEqual(['one', 'two'])
expect(wrapper.vm.newTag).toEqual('')
const $input = wrapper.find('input')
expect($input.exists()).toBe(true)
expect($input.element.value).toBe('')
$input.element.value = 'tag'
await $input.trigger('input')
expect(wrapper.vm.tags).toEqual(['one', 'two'])
expect(wrapper.vm.newTag).toEqual('tag')
$input.element.value = 'tag '
await $input.trigger('input')
expect(wrapper.vm.tags).toEqual(['one', 'two', 'tag'])
expect(wrapper.vm.newTag).toEqual('')
$input.element.value = 'three four one four '
await $input.trigger('input')
expect(wrapper.vm.tags).toEqual(['one', 'two', 'tag', 'four'])
// No tags(s) were accepted so the input is left as is
expect(wrapper.vm.newTag).toEqual('three four one four ')
$input.element.value = ' three '
await $input.trigger('input')
expect(wrapper.vm.tags).toEqual(['one', 'two', 'tag', 'four'])
// No tags(s) were accepted so the input is left as is
expect(wrapper.vm.newTag).toEqual(' three ')
$input.element.value = ' three seven '
await $input.trigger('input')
expect(wrapper.vm.tags).toEqual(['one', 'two', 'tag', 'four'])
// No tags(s) were accepted so the input is left as is
expect(wrapper.vm.newTag).toEqual(' three seven ')
$input.element.value = ' three cat seven '
await $input.trigger('input')
expect(wrapper.vm.tags).toEqual(['one', 'two', 'tag', 'four', 'cat'])
// No tags(s) were accepted so the input is left as is
expect(wrapper.vm.newTag).toEqual('three seven ')
$input.element.value = ' '
await $input.trigger('input')
expect(wrapper.vm.newTag).toEqual(' ')
expect(wrapper.vm.tags).toEqual(['one', 'two', 'tag', 'four', 'cat'])
wrapper.destroy()
})
it('tag validation on input event works', async () => {
const wrapper = mount(BFormTags, {
propsData: {
separator: ' ',
tagValidator: tag => tag.length < 5,
validateOnInput: true,
value: ['one', 'two']
}
})
expect(wrapper.element.tagName).toBe('DIV')
expect(wrapper.vm.tags).toEqual(['one', 'two'])
expect(wrapper.vm.newTag).toEqual('')
expect(wrapper.vm.duplicateTags).toEqual([])
expect(wrapper.vm.invalidTags).toEqual([])
expect(wrapper.emitted('tag-state')).not.toBeDefined()
expect(wrapper.find('.invalid-feedback').exists()).toBe(false)
expect(wrapper.find('.form-text').exists()).toBe(false)
const $input = wrapper.find('input')
expect($input.exists()).toBe(true)
expect($input.element.value).toBe('')
$input.element.value = 'tag'
await $input.trigger('input')
expect(wrapper.vm.tags).toEqual(['one', 'two'])
expect(wrapper.vm.newTag).toEqual('tag')
expect(wrapper.vm.duplicateTags).toEqual([])
expect(wrapper.vm.invalidTags).toEqual([])
expect(wrapper.emitted('tag-state')).toBeDefined()
expect(wrapper.emitted('tag-state').length).toBe(1)
// Valid tags
expect(wrapper.emitted('tag-state')[0][0]).toEqual(['tag'])
// Invalid tags
expect(wrapper.emitted('tag-state')[0][1]).toEqual([])
// Duplicate tags
expect(wrapper.emitted('tag-state')[0][2]).toEqual([])
expect(wrapper.find('.invalid-feedback').exists()).toBe(false)
expect(wrapper.find('.form-text').exists()).toBe(false)
$input.element.value = 'tag '
await $input.trigger('input')
expect(wrapper.vm.tags).toEqual(['one', 'two', 'tag'])
expect(wrapper.vm.newTag).toEqual('')
expect(wrapper.vm.duplicateTags).toEqual([])
expect(wrapper.vm.invalidTags).toEqual([])
expect(wrapper.emitted('tag-state')).toBeDefined()
// No changes to tagsState data, so no emit
expect(wrapper.emitted('tag-state').length).toBe(1)
expect(wrapper.find('.invalid-feedback').exists()).toBe(false)
expect(wrapper.find('.form-text').exists()).toBe(false)
// Emulate a user typing the word `three` (but not finished)
$input.element.value = 'thr'
await $input.trigger('input')
expect(wrapper.vm.tags).toEqual(['one', 'two', 'tag'])
// Invalid tags are left in the input
expect(wrapper.vm.newTag).toEqual('thr')
expect(wrapper.vm.duplicateTags).toEqual([])
expect(wrapper.vm.invalidTags).toEqual([])
expect(wrapper.emitted('tag-state').length).toBe(2)
// Valid tags
expect(wrapper.emitted('tag-state')[1][0]).toEqual(['thr'])
// Invalid tags
expect(wrapper.emitted('tag-state')[1][1]).toEqual([])
// Duplicate tags
expect(wrapper.emitted('tag-state')[1][2]).toEqual([])
expect(wrapper.find('.invalid-feedback').exists()).toBe(false)
expect(wrapper.find('.form-text').exists()).toBe(false)
// Add next character
$input.element.value = 'thre'
await $input.trigger('input')
expect(wrapper.vm.tags).toEqual(['one', 'two', 'tag'])
// Invalid tags are left in the input
expect(wrapper.vm.newTag).toEqual('thre')
expect(wrapper.vm.duplicateTags).toEqual([])
expect(wrapper.vm.invalidTags).toEqual([])
expect(wrapper.emitted('tag-state').length).toBe(3)
// Valid tags
expect(wrapper.emitted('tag-state')[2][0]).toEqual(['thre'])
// Invalid tags
expect(wrapper.emitted('tag-state')[2][1]).toEqual([])
// Duplicate tags
expect(wrapper.emitted('tag-state')[2][2]).toEqual([])
expect(wrapper.find('.invalid-feedback').exists()).toBe(false)
expect(wrapper.find('.form-text').exists()).toBe(false)
// Add next character
$input.element.value = 'three'
await $input.trigger('input')
expect(wrapper.vm.tags).toEqual(['one', 'two', 'tag'])
// No tags(s) were accepted so the input is left as is
expect(wrapper.vm.newTag).toEqual('three')
expect(wrapper.vm.duplicateTags).toEqual([])
expect(wrapper.vm.invalidTags).toEqual(['three'])
expect(wrapper.emitted('tag-state').length).toBe(4)
// Valid tags
expect(wrapper.emitted('tag-state')[3][0]).toEqual([])
// Invalid tags
expect(wrapper.emitted('tag-state')[3][1]).toEqual(['three'])
// Duplicate tags
expect(wrapper.emitted('tag-state')[3][2]).toEqual([])
expect(wrapper.find('.invalid-feedback').exists()).toBe(true)
expect(wrapper.find('.form-text').exists()).toBe(false)
// Add next character
$input.element.value = 'three '
await $input.trigger('input')
expect(wrapper.vm.tags).toEqual(['one', 'two', 'tag'])
// No tags(s) were accepted so the input is left as is
expect(wrapper.vm.newTag).toEqual('three ')
expect(wrapper.vm.duplicateTags).toEqual([])
expect(wrapper.vm.invalidTags).toEqual(['three'])
// State hasn't changed, so event not emitted
expect(wrapper.emitted('tag-state').length).toBe(4)
$input.element.value = 'two'
await $input.trigger('input')
expect(wrapper.vm.tags).toEqual(['one', 'two', 'tag'])
// No tags(s) were accepted so the input is left as is
expect(wrapper.vm.newTag).toEqual('two')
expect(wrapper.vm.duplicateTags).toEqual(['two'])
expect(wrapper.vm.invalidTags).toEqual([])
expect(wrapper.emitted('tag-state').length).toBe(5)
// Valid tags
expect(wrapper.emitted('tag-state')[4][0]).toEqual([])
// Invalid tags
expect(wrapper.emitted('tag-state')[4][1]).toEqual([])
// Duplicate tags
expect(wrapper.emitted('tag-state')[4][2]).toEqual(['two'])
expect(wrapper.find('.invalid-feedback').exists()).toBe(false)
expect(wrapper.find('.form-text').exists()).toBe(true)
$input.element.value = ' three two '
await $input.trigger('input')
expect(wrapper.vm.tags).toEqual(['one', 'two', 'tag'])
// No tags(s) were accepted so the input is left as is
expect(wrapper.vm.newTag).toEqual(' three two ')
expect(wrapper.emitted('tag-state').length).toBe(6)
// Tags added
expect(wrapper.emitted('tag-state')[5][0]).toEqual([])
// Invalid tag
expect(wrapper.emitted('tag-state')[5][1]).toEqual(['three'])
// Duplicate tags
expect(wrapper.emitted('tag-state')[5][2]).toEqual(['two'])
expect(wrapper.find('.invalid-feedback').exists()).toBe(true)
expect(wrapper.find('.form-text').exists()).toBe(true)
await $input.trigger('input')
expect(wrapper.vm.tags).toEqual(['one', 'two', 'tag'])
// No tags(s) were accepted so the input is left as is
expect(wrapper.vm.newTag).toEqual(' three two ')
expect(wrapper.emitted('tag-state').length).toBe(6)
expect(wrapper.find('.invalid-feedback').exists()).toBe(true)
expect(wrapper.find('.form-text').exists()).toBe(true)
$input.element.value = ' '
await $input.trigger('input')
expect(wrapper.vm.newTag).toEqual(' ')
expect(wrapper.vm.tags).toEqual(['one', 'two', 'tag'])
expect(wrapper.emitted('tag-state').length).toBe(7)
expect(wrapper.vm.duplicateTags).toEqual([])
expect(wrapper.vm.invalidTags).toEqual([])
// Tags added
expect(wrapper.emitted('tag-state')[6][0]).toEqual([])
// Invalid tag
expect(wrapper.emitted('tag-state')[6][1]).toEqual([])
// Duplicate tags
expect(wrapper.emitted('tag-state')[6][2]).toEqual([])
expect(wrapper.find('.invalid-feedback').exists()).toBe(false)
expect(wrapper.find('.form-text').exists()).toBe(false)
wrapper.destroy()
})
it('adds new tags when add button clicked', async () => {
const wrapper = mount(BFormTags, {
propsData: {
value: ['apple', 'orange']
}
})
expect(wrapper.element.tagName).toBe('DIV')
expect(wrapper.vm.tags).toEqual(['apple', 'orange'])
expect(wrapper.vm.newTag).toEqual('')
const $input = wrapper.find('input')
expect($input.exists()).toBe(true)
expect($input.element.value).toBe('')
const $button = wrapper.find('button.b-form-tags-button')
expect($button.exists()).toBe(true)
expect($button.classes()).toContain('invisible')
$input.element.value = 'pear'
await $input.trigger('input')
expect(wrapper.vm.newTag).toEqual('pear')
expect(wrapper.vm.tags).toEqual(['apple', 'orange'])
expect($button.classes()).not.toContain('invisible')
await $button.trigger('click')
expect($button.classes()).toContain('invisible')
expect(wrapper.vm.newTag).toEqual('')
expect(wrapper.vm.tags).toEqual(['apple', 'orange', 'pear'])
wrapper.destroy()
})
it('focuses input when wrapper div clicked', async () => {
const wrapper = mount(BFormTags, {
attachTo: createContainer(),
propsData: {
value: ['apple', 'orange']
}
})
await waitNT(wrapper.vm)
await waitRAF()
expect(wrapper.element.tagName).toBe('DIV')
expect(wrapper.vm.tags).toEqual(['apple', 'orange'])
expect(wrapper.vm.newTag).toEqual('')
expect(wrapper.classes()).not.toContain('focus')
const $input = wrapper.find('input')
expect($input.exists()).toBe(true)
expect($input.element.value).toBe('')
expect(document.activeElement).not.toBe($input.element)
await wrapper.trigger('click')
expect(document.activeElement).toBe($input.element)
await $input.trigger('focusin')
expect(wrapper.classes()).toContain('focus')
$input.element.blur()
await waitNT(wrapper.vm)
await waitRAF()
expect(document.activeElement).not.toBe($input.element)
await $input.trigger('focusout')
expect(wrapper.classes()).not.toContain('focus')
wrapper.vm.focus()
await waitNT(wrapper.vm)
await waitRAF()
expect(document.activeElement).toBe($input.element)
await $input.trigger('focusin')
expect(wrapper.classes()).toContain('focus')
wrapper.vm.blur()
await waitNT(wrapper.vm)
await waitRAF()
expect(document.activeElement).not.toBe($input.element)
await $input.trigger('focusout')
expect(wrapper.classes()).not.toContain('focus')
wrapper.destroy()
})
it('autofocus works', async () => {
const wrapper = mount(BFormTags, {
attachTo: createContainer(),
propsData: {
autofocus: true,
value: ['apple', 'orange']
}
})
await waitNT(wrapper.vm)
await waitRAF()
expect(wrapper.element.tagName).toBe('DIV')
expect(wrapper.vm.tags).toEqual(['apple', 'orange'])
expect(wrapper.vm.newTag).toEqual('')
expect(wrapper.classes()).toContain('focus')
const $input = wrapper.find('input')
expect($input.exists()).toBe(true)
expect($input.element.value).toBe('')
expect(document.activeElement).toBe($input.element)
wrapper.vm.blur()
await waitNT(wrapper.vm)
await waitRAF()
expect(wrapper.classes()).not.toContain('focus')
expect(document.activeElement).not.toBe($input.element)
wrapper.destroy()
})
})