diff --git a/src/__tests__/render-basics.js b/src/__tests__/render-basics.js index d4517d2..c0ac4a7 100644 --- a/src/__tests__/render-basics.js +++ b/src/__tests__/render-basics.js @@ -43,3 +43,27 @@ test('Is able to make terminal input and view in-progress stdout', async () => { expect(await findByText('First option: Two')).toBeTruthy() }) + + +test('fireEvent works when bound', async () => { + const {fireEvent: fireEventLocal, findByText, cleanup} = await render('node', [ + resolve(__dirname, './execute-scripts/stdio-inquirer.js'), + ]) + + const instance = await findByText('First option') + + expect(instance).toBeTruthy() + + // Windows uses ">", Linux/MacOS use "❯" + expect(await findByText(/[❯>] One/)).toBeTruthy() + + cleanup(); + + fireEventLocal.down(instance) + + expect(await findByText(/[❯>] Two/)).toBeTruthy() + + fireEvent.enter(instance) + fireEvent.enter(instance) + cleanup(); +}) diff --git a/src/events.js b/src/events.js deleted file mode 100644 index fc1ef8a..0000000 --- a/src/events.js +++ /dev/null @@ -1,13 +0,0 @@ -import {eventMap} from './event-map' - -const fireEvent = Object.entries(eventMap).reduce( - (prev, [eventName, keyCode]) => { - prev[eventName] = instance => { - instance.stdin.write(keyCode) - } - return prev - }, - {}, -) - -export {fireEvent} diff --git a/src/events.ts b/src/events.ts new file mode 100644 index 0000000..b05bbce --- /dev/null +++ b/src/events.ts @@ -0,0 +1,34 @@ +import {eventMap} from './event-map' +import {TestInstance} from "../types/pure"; +import {ShiftArgs} from "../types/helpers"; + +type FireEventRecord = Record void>; + +type BoundFireEventRecord = Record< + string, + ShiftArgs +> + + +const fireEvent = Object.entries(eventMap).reduce( + (prev, [eventName, keyCode]) => { + prev[eventName] = (instance) => { + instance.stdin.write(keyCode) + } + return prev + }, + {} +) + +function getFireEventForElement ( + instance: TestInstance +) { + return { + fireEvent: Object.entries(fireEvent).reduce((prev, [eventName, eventFn]) => { + prev[eventName] = () => eventFn(instance) + return prev; + }, {}) + } +} + +export {fireEvent, getFireEventForElement} diff --git a/src/pure.ts b/src/pure.ts index 114e30c..7389cc3 100644 --- a/src/pure.ts +++ b/src/pure.ts @@ -9,6 +9,7 @@ import stripFinalNewline from 'strip-final-newline' import {RenderOptions, TestInstance} from '../types/pure' import {_runObservers} from './mutation-observer' import {getQueriesForElement} from './get-queries-for-instance' +import {getFireEventForElement} from './events' import {setCurrentInstance} from "./helpers"; async function render( @@ -86,6 +87,7 @@ async function render( stderr: exec.stderr, }, getQueriesForElement(execOutputAPI), + getFireEventForElement(execOutputAPI as unknown as TestInstance) ) } diff --git a/types/helpers.d.ts b/types/helpers.d.ts new file mode 100644 index 0000000..a96bb97 --- /dev/null +++ b/types/helpers.d.ts @@ -0,0 +1,5 @@ +// eslint-disable-next-line @typescript-eslint/no-explicit-any +export type Shift = Arr extends [any, ...infer Q] ? Q : never; + +// eslint-disable-next-line @typescript-eslint/no-explicit-any +export type ShiftArgs any> = (...props: Shift>) => ReturnType