import Vue from 'vue' ; import { Wrapper } from '@vue/test-utils' ; import lolex from 'lolex' ; Vue . config . silent = true ; type MyTouchEvent = CustomEvent & { touches ? : any [ ] ; targetTouches ? : any [ ] ; changedTouches ? : any [ ] ; } ; param wrapper param eventName param x param y export function triggerTouch ( wrapper , eventName , x , y ) { const el = wrapper . element ? wrapper . element : wrapper ; const touch = { identifier : Date . now ( ) , target : el , pageX : x , pageY : y , clientX : x , clientY : y , radiusX : 2.5 , radiusY : 2.5 , rotationAngle : 10 , force : 0.5 , } ; const event = document . createEvent ( 'CustomEvent' ) ; event . initCustomEvent ( eventName , true , true , { } ) ; event . touches = [ touch ] ; event . targetTouches = [ touch ] ; event . changedTouches = [ touch ] ; el . dispatchEvent ( event ) ; } param el param startY param endY export function verticalDrag ( el , startY = 0 , endY ) { triggerTouch ( el , 'touchstart' , 0 , startY ) ; triggerTouch ( el , 'touchmove' , 0 , ( startY + endY ) / 4 ) ; triggerTouch ( el , 'touchmove' , 0 , ( startY + endY ) / 3 ) ; triggerTouch ( el , 'touchmove' , 0 , ( startY + endY ) / 2 ) ; triggerTouch ( el , 'touchmove' , 0 , endY ) ; triggerTouch ( el , 'touchend' , 0 , endY ) ; } param el param startX param endX export function horizontalDrag ( el , startX = 0 , endX ) { triggerTouch ( el , 'touchstart' , startX , 0 ) ; triggerTouch ( el , 'touchmove' , ( startX + endX ) / 4 , 0 ) ; triggerTouch ( el , 'touchmove' , ( startX + endX ) / 3 , 0 ) ; triggerTouch ( el , 'touchmove' , ( startX + endX ) / 2 , 0 ) ; triggerTouch ( el , 'touchmove' , endX , 0 ) ; triggerTouch ( el , 'touchend' , endX , 0 ) ; } param el param startY param endY export function slowVerticalDrag ( el , startY , endY ) { const clock = lolex . install ( { shouldAdvanceTime : true , } ) ; triggerTouch ( el , 'touchstart' , 0 , startY ) ; triggerTouch ( el , 'touchmove' , 0 , startY + ( endY - startY ) / 2 ) ; triggerTouch ( el , 'touchmove' , 0 , endY - 2 ) ; clock . tick ( 100 ) ; triggerTouch ( el , 'touchmove' , 0 , endY ) ; triggerTouch ( el , 'touchend' , 0 , endY ) ; clock . uninstall ( ) ; } param el param x param y export function dragAndHoldHelper ( el , x , y ) { triggerTouch ( el , 'touchstart' , 0 , 0 ) ; triggerTouch ( el , 'touchmove' , x / 4 , y / 4 ) ; triggerTouch ( el , 'touchmove' , x / 3 , y / 3 ) ; triggerTouch ( el , 'touchmove' , x / 2 , y / 2 ) ; triggerTouch ( el , 'touchmove' , x , y ) ; } param delay returns { Promise < any > } export function later ( delay ) { return new Promise ( resolve => { setTimeout ( resolve , delay ) ; } ) ; }