1
1
import { describe , expect , it , vi } from 'vitest'
2
- import { shallowRef } from 'vue'
2
+ import { nextTick , shallowRef } from 'vue'
3
3
import { useFileDialog } from './index'
4
4
5
5
class DataTransferMock {
@@ -59,17 +59,29 @@ describe('useFileDialog', () => {
59
59
expect ( input . click ) . toBeCalled ( )
60
60
} )
61
61
62
- it ( 'should work with input element passed as template ref' , ( ) => {
63
- const inputEl = document . createElement ( 'input' )
64
- inputEl . click = vi . fn ( )
62
+ it ( 'should work with input element passed as template ref' , async ( ) => {
63
+ const inputEl1 = document . createElement ( 'input' )
64
+ inputEl1 . click = vi . fn ( )
65
+ const input = shallowRef < HTMLInputElement > ( inputEl1 )
66
+ const { open } = useFileDialog ( { input } )
67
+
68
+ expect ( inputEl1 . click ) . toHaveBeenCalledTimes ( 0 )
69
+ open ( )
70
+ expect ( inputEl1 . type ) . toBe ( 'file' )
71
+ expect ( inputEl1 . click ) . toHaveBeenCalledTimes ( 1 )
65
72
66
- const inputRef = shallowRef < HTMLInputElement > ( inputEl )
73
+ const inputEl2 = document . createElement ( 'input' )
74
+ inputEl2 . click = vi . fn ( )
67
75
68
- const { open } = useFileDialog ( { input : inputRef } )
76
+ input . value = inputEl2
77
+ await nextTick ( )
78
+
79
+ expect ( inputEl2 . type ) . toBe ( 'file' )
80
+ expect ( inputEl2 . click ) . toHaveBeenCalledTimes ( 0 )
69
81
70
82
open ( )
71
- expect ( inputEl . type ) . toBe ( 'file' )
72
- expect ( inputEl . click ) . toHaveBeenCalled ( )
83
+
84
+ expect ( inputEl2 . click ) . toHaveBeenCalledTimes ( 1 )
73
85
} )
74
86
75
87
it ( 'should trigger onchange and update files when file is selected' , async ( ) => {
@@ -93,4 +105,127 @@ describe('useFileDialog', () => {
93
105
expect ( files . value ?. [ 0 ] ) . toEqual ( file )
94
106
expect ( changeHandler ) . toHaveBeenCalledWith ( files . value )
95
107
} )
108
+
109
+ it ( 'should work with ref value for multiple option' , async ( ) => {
110
+ const input = document . createElement ( 'input' )
111
+ input . click = vi . fn ( )
112
+
113
+ const multipleRef = shallowRef ( true )
114
+
115
+ const { open } = useFileDialog ( {
116
+ input,
117
+ multiple : multipleRef ,
118
+ } )
119
+
120
+ expect ( input . multiple ) . toBe ( true )
121
+
122
+ open ( )
123
+
124
+ expect ( input . multiple ) . toBe ( true )
125
+
126
+ multipleRef . value = false
127
+ await nextTick ( )
128
+
129
+ expect ( input . multiple ) . toBe ( false )
130
+
131
+ open ( )
132
+
133
+ expect ( input . multiple ) . toBe ( false )
134
+ } )
135
+
136
+ it ( 'should work with ref value for accept option' , async ( ) => {
137
+ const input = document . createElement ( 'input' )
138
+ input . click = vi . fn ( )
139
+
140
+ const acceptRef = shallowRef ( 'image/*' )
141
+
142
+ const { open } = useFileDialog ( {
143
+ input,
144
+ accept : acceptRef ,
145
+ } )
146
+
147
+ expect ( input . accept ) . toBe ( 'image/*' )
148
+
149
+ open ( )
150
+
151
+ expect ( input . accept ) . toBe ( 'image/*' )
152
+
153
+ acceptRef . value = 'video/*'
154
+ await nextTick ( )
155
+
156
+ expect ( input . accept ) . toBe ( 'video/*' )
157
+
158
+ open ( )
159
+
160
+ expect ( input . accept ) . toBe ( 'video/*' )
161
+ } )
162
+
163
+ it ( 'should work with ref value for directory option' , async ( ) => {
164
+ const input = document . createElement ( 'input' )
165
+ input . click = vi . fn ( )
166
+
167
+ const directoryRef = shallowRef ( true )
168
+
169
+ const { open } = useFileDialog ( {
170
+ input,
171
+ directory : directoryRef ,
172
+ } )
173
+
174
+ expect ( input . webkitdirectory ) . toBe ( true )
175
+
176
+ open ( )
177
+
178
+ expect ( input . webkitdirectory ) . toBe ( true )
179
+
180
+ directoryRef . value = false
181
+ await nextTick ( )
182
+
183
+ expect ( input . webkitdirectory ) . toBe ( false )
184
+
185
+ open ( )
186
+
187
+ expect ( input . webkitdirectory ) . toBe ( false )
188
+ } )
189
+
190
+ it ( 'should work with ref value for reset option' , ( ) => {
191
+ const input = document . createElement ( 'input' )
192
+ input . click = vi . fn ( )
193
+
194
+ const resetRef = shallowRef ( true )
195
+
196
+ const { open } = useFileDialog ( {
197
+ input,
198
+ reset : resetRef ,
199
+ } )
200
+ open ( )
201
+
202
+ expect ( input . click ) . toHaveBeenCalled ( ) // Assuming reset does not change input attributes
203
+ } )
204
+
205
+ it ( 'should work with ref value for capture option' , async ( ) => {
206
+ const input = document . createElement ( 'input' )
207
+ input . click = vi . fn ( )
208
+
209
+ const captureRef = shallowRef ( 'user' )
210
+
211
+ const { open } = useFileDialog ( {
212
+ input,
213
+ capture : captureRef ,
214
+ } )
215
+
216
+ expect ( input . capture ) . toBe ( 'user' )
217
+
218
+ open ( )
219
+
220
+ expect ( input . capture ) . toBe ( 'user' )
221
+
222
+ captureRef . value = 'environment'
223
+ await nextTick ( )
224
+
225
+ expect ( input . capture ) . toBe ( 'environment' )
226
+
227
+ open ( )
228
+
229
+ expect ( input . capture ) . toBe ( 'environment' )
230
+ } )
96
231
} )
0 commit comments