-
Notifications
You must be signed in to change notification settings - Fork 252
/
Copy pathsetFiles.ts
69 lines (49 loc) · 1.83 KB
/
setFiles.ts
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
import {createFileList, setFiles} from '#src/utils'
import {setup} from '#testHelpers'
test('set files', () => {
const {element} = setup<HTMLInputElement & {type: 'file'}>(
`<input type="file"/>`,
)
const list = createFileList(window, [new File(['foo'], 'foo.txt')])
setFiles(element, list)
expect(element).toHaveProperty('files', list)
expect(element).toHaveValue('C:\\fakepath\\foo.txt')
})
test('switching type resets value', () => {
const {element} = setup<HTMLInputElement>(`<input type="text"/>`)
element.type = 'file'
expect(element).toHaveValue('')
const list = createFileList(window, [new File(['foo'], 'foo.txt')])
setFiles(element as HTMLInputElement & {type: 'file'}, list)
element.type = 'file'
expect(element).toHaveValue('C:\\fakepath\\foo.txt')
element.type = 'text'
expect(element).toHaveValue('')
expect(element).toHaveProperty('type', 'text')
})
test('setting value resets `files`', () => {
const {element} = setup<HTMLInputElement & {type: 'file'}>(
`<input type="file"/>`,
)
const list = createFileList(window, [new File(['foo'], 'foo.txt')])
setFiles(element, list)
// Everything but an empty string throws an error in the browser
// TODO: Research why this behavior is inconsistent
// expect(() => {
// element.value = 'foo'
// }).toThrow()
expect(element).toHaveProperty('files', list)
element.value = ''
expect(element).toHaveProperty('files', expect.objectContaining({length: 0}))
})
test('is save to call multiple times', () => {
const {element} = setup<HTMLInputElement & {type: 'file'}>(
`<input type="file"/>`,
)
const list = createFileList(window, [new File(['foo'], 'foo.txt')])
setFiles(element, list)
setFiles(element, list)
expect(element).toHaveValue('C:\\fakepath\\foo.txt')
element.value = ''
expect(element).toHaveValue('')
})