-
Notifications
You must be signed in to change notification settings - Fork 239
/
Clipboard.ts
57 lines (50 loc) 路 1.9 KB
/
Clipboard.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
import {
attachClipboardStubToView,
createClipboardItem,
detachClipboardStubFromView,
readBlobText,
resetClipboardStubOnView,
} from '#src/utils'
describe('read from and write to clipboard', () => {
beforeEach(() => {
attachClipboardStubToView(window)
})
test('read and write text', async () => {
await window.navigator.clipboard.writeText('foo')
await expect(window.navigator.clipboard.readText()).resolves.toBe('foo')
})
test('read and write item', async () => {
const items = [
createClipboardItem(window, new Blob(['foo'], {type: 'text/plain'})),
createClipboardItem(window, new Blob(['bar'], {type: 'text/html'})),
createClipboardItem(window, new Blob(['PNG'], {type: 'image/png'})),
createClipboardItem(
window,
new Blob(['baz1'], {type: 'text/plain'}),
new Blob(['baz2'], {type: 'text/html'}),
),
]
expect(items[0]).toHaveProperty('types', ['text/plain'])
expect(items[3]).toHaveProperty('types', ['text/plain', 'text/html'])
await expect(items[3].getType('text/html')).resolves.toBeInstanceOf(Blob)
await expect(
readBlobText(await items[3].getType('text/html'), FileReader),
).resolves.toBe('baz2')
await expect(items[3].getType('image/png')).rejects.toThrowError()
await window.navigator.clipboard.write(items)
await expect(window.navigator.clipboard.read()).resolves.toEqual(items)
await expect(window.navigator.clipboard.readText()).resolves.toBe(
'foobarbaz1',
)
})
test('reset clipboard', async () => {
await window.navigator.clipboard.writeText('foo')
resetClipboardStubOnView(window)
await expect(window.navigator.clipboard.readText()).resolves.toBe('')
})
test('detach clipboard', async () => {
expect(window.navigator.clipboard).not.toBe(undefined)
detachClipboardStubFromView(window)
expect(window.navigator.clipboard).toBe(undefined)
})
})