Skip to content

Commit

Permalink
feat: added new fireEvent binding
Browse files Browse the repository at this point in the history
  • Loading branch information
crutchcorn committed Nov 23, 2021
1 parent 6d3ecb1 commit 853750e
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 13 deletions.
24 changes: 24 additions & 0 deletions src/__tests__/render-basics.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
})
13 changes: 0 additions & 13 deletions src/events.js

This file was deleted.

34 changes: 34 additions & 0 deletions src/events.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import {eventMap} from './event-map'
import {TestInstance} from "../types/pure";
import {ShiftArgs} from "../types/helpers";

type FireEventRecord = Record<string, (instance: TestInstance) => void>;

type BoundFireEventRecord = Record<
string,
ShiftArgs<FireEventRecord[string]>
>


const fireEvent = Object.entries(eventMap).reduce<FireEventRecord>(
(prev, [eventName, keyCode]) => {
prev[eventName] = (instance) => {
instance.stdin.write(keyCode)
}
return prev
},
{}
)

function getFireEventForElement (
instance: TestInstance
) {
return {
fireEvent: Object.entries(fireEvent).reduce<BoundFireEventRecord>((prev, [eventName, eventFn]) => {
prev[eventName] = () => eventFn(instance)
return prev;
}, {})
}
}

export {fireEvent, getFireEventForElement}
2 changes: 2 additions & 0 deletions src/pure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -86,6 +87,7 @@ async function render(
stderr: exec.stderr,
},
getQueriesForElement(execOutputAPI),
getFireEventForElement(execOutputAPI as unknown as TestInstance)
)
}

Expand Down
5 changes: 5 additions & 0 deletions types/helpers.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export type Shift<Arr extends any[]> = Arr extends [any, ...infer Q] ? Q : never;

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export type ShiftArgs<Fn extends (...args: any) => any> = (...props: Shift<Parameters<Fn>>) => ReturnType<Fn>

0 comments on commit 853750e

Please sign in to comment.