forked from testing-library/user-event
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpaste.ts
34 lines (31 loc) · 954 Bytes
/
paste.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
import {Config, Instance} from '../setup'
import {
createDataTransfer,
getActiveElement,
getWindow,
readDataTransferFromClipboard,
} from '../utils'
export async function paste(
this: Instance,
clipboardData?: DataTransfer | string,
) {
const doc = this[Config].document
const target = getActiveElement(doc) ?? doc.activeElement ?? doc.body
const dataTransfer: DataTransfer =
(typeof clipboardData === 'string'
? getClipboardDataFromString(doc, clipboardData)
: clipboardData) ??
(await readDataTransferFromClipboard(doc).catch(() => {
throw new Error(
'`userEvent.paste()` without `clipboardData` requires the `ClipboardAPI` to be available.',
)
}))
this.dispatchUIEvent(target, 'paste', {
clipboardData: dataTransfer,
})
}
function getClipboardDataFromString(doc: Document, text: string) {
const dt = createDataTransfer(getWindow(doc))
dt.setData('text', text)
return dt
}