Skip to content

Commit 80c6ff6

Browse files
committed
test(form-tags): Add test cases for form tags
1 parent 600c129 commit 80c6ff6

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import {enableAutoUnmount, mount} from '@vue/test-utils'
2+
import BFormTags from './BFormTags.vue'
3+
import {afterEach, describe, expect, it} from 'vitest'
4+
5+
describe('form-tags', () => {
6+
enableAutoUnmount(afterEach)
7+
8+
it('has proper root element', () => {
9+
const wrapper = mount(BFormTags)
10+
11+
expect(wrapper.element.tagName).toBe('DIV')
12+
expect(wrapper.classes()).toContain('b-form-tags')
13+
expect(wrapper.classes()).toContain('form-control')
14+
})
15+
16+
it('has proper input element', () => {
17+
const wrapper = mount(BFormTags)
18+
19+
const $input = wrapper.find('input')
20+
21+
expect($input.exists()).toBe(true)
22+
expect($input.classes()).toContain('b-form-tags-input')
23+
})
24+
25+
it('adds new tag', async () => {
26+
const wrapper = mount(BFormTags)
27+
28+
const $input = wrapper.get('input')
29+
$input.element.value = 'test'
30+
await $input.trigger('input')
31+
32+
const $tagStateEmitted = wrapper.emitted('tag-state') as unknown[][]
33+
34+
expect($tagStateEmitted).toBeDefined()
35+
expect($tagStateEmitted[0][0]).toEqual(['test'])
36+
37+
await $input.trigger('keydown', {key: 'Enter'})
38+
39+
expect(wrapper.emitted('update:modelValue')).toBeDefined()
40+
})
41+
42+
it('renders tag slot', async () => {
43+
const wrapper = mount(BFormTags, {
44+
props: {
45+
modelValue: ['foo', 'bar'],
46+
},
47+
slots: {
48+
tag: `
49+
<template #tag="params">
50+
<button class="custom-tag" @click="params.removeTag">{{ params.tag }}</button>
51+
</template>
52+
`,
53+
},
54+
})
55+
56+
const $tags = wrapper.findAll('.custom-tag')
57+
expect($tags.length).toBe(2)
58+
59+
expect($tags[0].text()).toBe('foo')
60+
expect($tags[1].text()).toBe('bar')
61+
62+
await $tags[1].trigger('click')
63+
64+
const $emitted = wrapper.emitted('update:modelValue') as unknown[][]
65+
66+
expect($emitted.length).toBe(1)
67+
expect($emitted).toBeDefined()
68+
expect($emitted[0][0]).toEqual(['foo'])
69+
})
70+
})

0 commit comments

Comments
 (0)