-
Notifications
You must be signed in to change notification settings - Fork 255
Expand file tree
/
Copy pathDataTransfer.ts
More file actions
127 lines (105 loc) · 3.57 KB
/
DataTransfer.ts
File metadata and controls
127 lines (105 loc) · 3.57 KB
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
import {waitFor} from '@testing-library/dom'
import {createDataTransfer, getBlobFromDataTransferItem} from '#src/utils'
describe('create DataTransfer', () => {
test('plain string', async () => {
const dt = createDataTransfer(window)
dt.setData('text/plain', 'foo')
expect(dt.getData('text/plain')).toBe('foo')
const callback = mocks.fn()
dt.items[0].getAsString(callback)
await waitFor(() => expect(callback).toBeCalledWith('foo'))
})
test('multi format', async () => {
const dt = createDataTransfer(window)
dt.setData('text/plain', 'foo')
dt.setData('text/html', 'bar')
expect(dt.types).toEqual(['text/plain', 'text/html'])
expect(dt.getData('text/plain')).toBe('foo')
expect(dt.getData('text/html')).toBe('bar')
expect(dt.getData('text')).toBe('foo')
dt.clearData()
dt.setData('text', 'baz')
expect(dt.getData('text/plain')).toBe('baz')
expect(dt.getData('text')).toBe('baz')
})
test('overwrite item', async () => {
const dt = createDataTransfer(window)
dt.setData('text/plain', 'foo')
dt.setData('text/plain', 'bar')
expect(dt.types).toEqual(['text/plain'])
expect(dt.getData('text')).toBe('bar')
})
test('files operation', async () => {
const f0 = new File(['bar'], 'bar0.txt', {type: 'text/plain'})
const f1 = new File(['bar'], 'bar1.txt', {type: 'text/plain'})
const dt = createDataTransfer(window, [f0, f1])
dt.setData('text/html', 'foo')
expect(dt.types).toEqual(
expect.arrayContaining(
// TODO: Fix DataTransferStub
typeof window.DataTransfer === 'undefined'
? ['Files', 'text/html']
: ['text/html'],
),
)
expect(dt.files.length).toBe(2)
})
test('files item', async () => {
const f0 = new File(['bar'], 'bar0.txt', {type: 'text/plain'})
const dt = createDataTransfer(window)
dt.setData('text/html', 'foo')
dt.items.add(f0)
expect(dt.types).toEqual(
expect.arrayContaining(
// TODO: Fix DataTransferStub
typeof window.DataTransfer === 'undefined'
? ['text/html', 'text/plain']
: ['text/html', 'Files'],
),
)
expect(dt.items[0].getAsFile()).toBe(null)
expect(dt.items[1].getAsFile()).toBe(f0)
const callback = mocks.fn()
dt.items[1].getAsString(callback)
expect(callback).not.toBeCalled()
})
test('clear data', async () => {
const f0 = new File(['bar'], 'bar0.txt', {type: 'text/plain'})
const dt = createDataTransfer(window)
dt.setData('text/html', 'foo')
dt.items.add(f0)
dt.clearData('text/plain')
expect(dt.types).toEqual(
expect.arrayContaining(
// TODO: Fix DataTransferStub
typeof window.DataTransfer === 'undefined'
? ['text/html']
: ['text/html', 'Files'],
),
)
dt.clearData('text/html')
expect(dt.types).toEqual(
// TODO: Fix DataTransferStub
typeof window.DataTransfer === 'undefined' ? [] : ['Files'],
)
})
})
test('get Blob from DataTransfer', async () => {
const dt = createDataTransfer(window)
dt.items.add('foo', 'text/plain')
dt.items.add(new File(['bar'], 'bar.txt', {type: 'text/plain'}))
expect(await getBlobFromDataTransferItem(window, dt.items[0])).toHaveProperty(
'type',
'text/plain',
)
expect(
await getBlobFromDataTransferItem(window, dt.items[0]),
).not.toBeInstanceOf(File)
expect(await getBlobFromDataTransferItem(window, dt.items[1])).toHaveProperty(
'type',
'text/plain',
)
expect(await getBlobFromDataTransferItem(window, dt.items[1])).toBeInstanceOf(
File,
)
})