import { fireEvent } from '@testing-library/dom' ; import { Config , Instance } from '../setup' ; import { createDataTransfer , getSpaceUntilMaxLength , prepareInput , isEditable , readDataTransferFromClipboard , } from '../utils' ; export async function paste ( this , clipboardData ? ) { const doc = this [ Config ] . document ; const target = doc . activeElement ?? doc . body ; const data = ( typeof clipboardData === 'string' ? getClipboardDataFromString ( clipboardData ) : clipboardData ) ?? ( await readDataTransferFromClipboard ( doc ) . catch ( ( ) => { throw new Error ( '`userEvent.paste()` without `clipboardData` requires the `ClipboardAPI` to be available.' ) ; } ) ) ; return pasteImpl ( target , data ) ; } function pasteImpl ( target , clipboardData ) { fireEvent . paste ( target , { clipboardData , } ) ; if ( isEditable ( target ) ) { const data = clipboardData . getData ( 'text' ) . substr ( 0 , getSpaceUntilMaxLength ( target ) ) ; if ( data ) { prepareInput ( data , target , 'insertFromPaste' ) ?. commit ( ) ; } } } function getClipboardDataFromString ( text ) { const dt = createDataTransfer ( ) ; dt . setData ( 'text' , text ) ; return dt ; }