1
1
import { mount } from '@vue/test-utils'
2
2
import SearchInput from '@/SearchInput.vue'
3
3
import { fieldType } from '@/SearchInput.types'
4
+ import { nextTick } from 'vue'
4
5
5
6
const createWrapper = ( opts ?: Record < string , unknown > ) => {
6
7
return mount ( SearchInput , opts )
@@ -26,7 +27,7 @@ describe('SearchInput.vue', () => {
26
27
it ( 'should render a search icon' , async ( ) => {
27
28
const wrapper = createWrapper ( )
28
29
29
- const div = await wrapper . find ( 'div.search-icon search' )
30
+ const div = await wrapper . find ( 'div.search-icon. search' )
30
31
31
32
expect ( div ) . toBeTruthy ( )
32
33
} )
@@ -51,4 +52,52 @@ describe('SearchInput.vue', () => {
51
52
52
53
expect ( onClick ) . toHaveBeenCalled ( )
53
54
} )
55
+
56
+ it ( 'sets the value' , async ( ) => {
57
+ const wrapper = createWrapper ( )
58
+ const input = wrapper . find ( 'input' )
59
+
60
+ await input . setValue ( 'test_value' )
61
+
62
+ expect ( input . element . value ) . toBe ( 'test_value' )
63
+ } )
64
+
65
+ it ( 'emits the updated value' , async ( ) => {
66
+ const wrapper = createWrapper ( )
67
+ const input = wrapper . find ( 'input' )
68
+
69
+ await input . setValue ( 'test_value' )
70
+
71
+ expect ( wrapper . emitted ( ) [ 'update:modelValue' ] [ 0 ] ) . toEqual ( [ 'test_value' ] )
72
+ } )
73
+
74
+ it ( 'clears the value when the clear icon is clicked' , async ( ) => {
75
+ const wrapper = createWrapper ( {
76
+ props : {
77
+ modelValue : 'test'
78
+ }
79
+ } )
80
+
81
+ const clearIcon = await wrapper . find ( 'div.search-icon.clear' )
82
+
83
+ await clearIcon . trigger ( 'mousedown' )
84
+
85
+ expect ( wrapper . emitted ( ) [ 'update:modelValue' ] [ 0 ] ) . toEqual ( [ '' ] )
86
+ } )
87
+
88
+ it ( 'esc key clears the value' , async ( ) => {
89
+ const wrapper = createWrapper ( {
90
+ props : {
91
+ modelValue : 'test'
92
+ }
93
+ } )
94
+
95
+ const input = wrapper . find ( 'input' )
96
+
97
+ await input . trigger ( 'keydown' , {
98
+ key : 'Escape'
99
+ } )
100
+
101
+ expect ( wrapper . emitted ( ) [ 'update:modelValue' ] [ 0 ] ) . toEqual ( [ '' ] )
102
+ } )
54
103
} )
0 commit comments