-
Notifications
You must be signed in to change notification settings - Fork 14
/
navigator-clipboard.js
54 lines (50 loc) · 1.91 KB
/
navigator-clipboard.js
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
import {clipboardRead, clipboardWrite, apply, isPolyfilled, isSupported} from '../lib/navigator-clipboard.js'
describe('navigator clipboard', () => {
it('has standard isSupported, isPolyfilled, apply API', () => {
expect(isSupported).to.be.a('function')
expect(isPolyfilled).to.be.a('function')
expect(apply).to.be.a('function')
expect(isSupported()).to.be.a('boolean')
expect(isPolyfilled()).to.equal(false)
})
describe('read', () => {
it('read returns array of 1 clipboard entry with plaintext of readText value', async () => {
navigator.clipboard.readText = () => Promise.resolve('foo')
const arr = await clipboardRead()
expect(arr).to.have.lengthOf(1)
expect(arr[0]).to.be.an.instanceof(globalThis.ClipboardItem)
expect(arr[0].types).to.eql(['text/plain'])
expect(await (await arr[0].getType('text/plain')).text()).to.eql('foo')
})
})
describe('write', () => {
it('unpacks text/plain content to writeText', async () => {
const calls = []
navigator.clipboard.writeText = (...args) => calls.push(args)
await clipboardWrite([
new globalThis.ClipboardItem({
'foo/bar': 'horrible',
'text/plain': Promise.resolve('foo'),
}),
])
expect(calls).to.have.lengthOf(1)
expect(calls[0]).to.eql(['foo'])
})
it('accepts multiple clipboard items, picking the first', async () => {
const calls = []
navigator.clipboard.writeText = (...args) => calls.push(args)
await clipboardWrite([
new globalThis.ClipboardItem({
'foo/bar': 'horrible',
'text/plain': Promise.resolve('multiple-pass'),
}),
new globalThis.ClipboardItem({
'foo/bar': 'multiple-fail',
'text/plain': Promise.resolve('multiple-fail'),
}),
])
expect(calls).to.have.lengthOf(1)
expect(calls[0]).to.eql(['multiple-pass'])
})
})
})