import { userEvent } from '@storybook/testing-library' ; export function delay ( ms ) { if ( ! ! global . test ) { return new Promise ( resolve => resolve ( undefined ) ) ; } else { return new Promise ( resolve => { return setTimeout ( resolve , ms ) ; } ) ; } } export async function mouseTo ( target , delay = 1700 ) { if ( ! ! global . test || ! target ) { return new Promise ( resolve => resolve ( undefined ) ) ; } else { return new Promise ( resolve => { let cursorEl = document . getElementById ( 'demoCursor' ) ; if ( ! cursorEl ) { cursorEl = document . createElement ( 'div' ) ; cursorEl . id = 'demoCursor' ; cursorEl . addEventListener ( 'transitionend' , ( ) => { if ( cursorEl ) { cursorEl . className = 'hide' ; } target . dispatchEvent ( new MouseEvent ( 'mouseover' , { 'view' : window , 'bubbles' : true , 'cancelable' : true } ) ) ; } , { capture : true } ) ; document . body . appendChild ( cursorEl ) ; } if ( ! target . getBoundingClientRect ) { console . log ( "target does not have getBoundingClientRect" , target ) ; return ; } const { left , top , width , height } = target . getBoundingClientRect ( ) ; const sTop = Math . round ( top + ( Math . min ( height / 2 , 50 ) ) ) + 'px' ; const sLeft = Math . round ( left + ( Math . min ( width / 2 , 50 ) ) ) + 'px' ; cursorEl . className = 'moving' ; cursorEl . style . top = sTop ; cursorEl . style . left = sLeft ; cursorEl . style . transitionDuration = `${ Math . round ( delay * .9 ) }ms` ; return setTimeout ( resolve , delay ) ; } ) ; } } export async function animatedUserEventClick ( target ) { await mouseTo ( target ) ; return userEvent . click ( target ) ; }