import { setImmediate as flushMicroTasks } from "timers" ; import { JsPsych } from "jspsych" ; export function dispatchEvent ( event ) { document . body . dispatchEvent ( event ) ; } export function keyDown ( key ) { dispatchEvent ( new KeyboardEvent ( "keydown" , { key } ) ) ; } export function keyUp ( key ) { dispatchEvent ( new KeyboardEvent ( "keyup" , { key } ) ) ; } export function pressKey ( key ) { keyDown ( key ) ; keyUp ( key ) ; } export function mouseDownMouseUpTarget ( target ) { target . dispatchEvent ( new MouseEvent ( "mousedown" , { bubbles : true } ) ) ; target . dispatchEvent ( new MouseEvent ( "mouseup" , { bubbles : true } ) ) ; } export function clickTarget ( target ) { target . dispatchEvent ( new MouseEvent ( "click" , { bubbles : true } ) ) ; } param x param y param container export function mouseMove ( x , y , container ) { const containerRect = container . getBoundingClientRect ( ) ; const eventInit = { clientX : containerRect . x + x , clientY : containerRect . y + y , bubbles : true , } ; container . dispatchEvent ( new MouseEvent ( "mousemove" , eventInit ) ) ; } param x param y param container export function mouseUp ( x , y , container ) { const containerRect = container . getBoundingClientRect ( ) ; const eventInit = { clientX : containerRect . x + x , clientY : containerRect . y + y , bubbles : true , } ; container . dispatchEvent ( new MouseEvent ( "mouseup" , eventInit ) ) ; } param x param y param container export function mouseDown ( x , y , container ) { const containerRect = container . getBoundingClientRect ( ) ; const eventInit = { clientX : containerRect . x + x , clientY : containerRect . y + y , bubbles : true , } ; container . dispatchEvent ( new MouseEvent ( "mousedown" , eventInit ) ) ; } /**
 * https://github.com/facebook/jest/issues/2157#issuecomment-279171856
 */ export function flushPromises ( ) { return new Promise ( ( resolve ) => flushMicroTasks ( resolve ) ) ; } param timeline param jsPsych returns export async function startTimeline ( timeline , jsPsych = { } ) { const jsPsychInstance = jsPsych instanceof JsPsych ? jsPsych : new JsPsych ( jsPsych ) ; let hasFinished = false ; const finished = jsPsychInstance . run ( timeline ) . then ( ( ) => { hasFinished = true ; } ) ; await flushPromises ( ) ; const displayElement = jsPsychInstance . getDisplayElement ( ) ; return { jsPsych : jsPsychInstance , displayElement , /** Shorthand for `jsPsych.getDisplayElement().innerHTML` */ getHTML : ( ) => displayElement . innerHTML , /** Shorthand for `jsPsych.data.get()` */ getData : ( ) => jsPsychInstance . data . get ( ) , expectFinished : async ( ) => { await flushPromises ( ) ; expect ( hasFinished ) . toBe ( true ) ; } , expectRunning : async ( ) => { await flushPromises ( ) ; expect ( hasFinished ) . toBe ( false ) ; } , /** A promise that is resolved when `jsPsych.run()` is done. */ finished , } ; } param timeline param simulation_mode param simulation_options param jsPsych returns export async function simulateTimeline ( timeline , simulation_mode ? , simulation_options = { } , jsPsych = { } ) { const jsPsychInstance = jsPsych instanceof JsPsych ? jsPsych : new JsPsych ( jsPsych ) ; let hasFinished = false ; const finished = jsPsychInstance . simulate ( timeline , simulation_mode , simulation_options ) . then ( ( ) => { hasFinished = true ; } ) ; await flushPromises ( ) ; const displayElement = jsPsychInstance . getDisplayElement ( ) ; return { jsPsych : jsPsychInstance , displayElement , /** Shorthand for `jsPsych.getDisplayElement().innerHTML` */ getHTML : ( ) => displayElement . innerHTML , /** Shorthand for `jsPsych.data.get()` */ getData : ( ) => jsPsychInstance . data . get ( ) , expectFinished : async ( ) => { await flushPromises ( ) ; expect ( hasFinished ) . toBe ( true ) ; } , expectRunning : async ( ) => { await flushPromises ( ) ; expect ( hasFinished ) . toBe ( false ) ; } , /** A promise that is resolved when `jsPsych.simulate()` is done. */ finished , } ; }