Skip to content

Commit

Permalink
fix: allow dynamic fireEvent, rename cleanup to clear
Browse files Browse the repository at this point in the history
  • Loading branch information
crutchcorn committed Nov 26, 2021
1 parent 748b31e commit 19d267d
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 30 deletions.
76 changes: 62 additions & 14 deletions src/__tests__/render-basics.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ test('Is able to make terminal input and view in-progress stdout', async () => {
resolve(__dirname, './execute-scripts/stdio-inquirer.js'),
])

const {cleanup, findByText} = props;
const {clear, findByText} = props;

const instance = await findByText('First option')

Expand All @@ -44,21 +44,48 @@ test('Is able to make terminal input and view in-progress stdout', async () => {
// Windows uses ">", Linux/MacOS use "❯"
expect(await findByText(/[❯>] One/)).toBeTruthy()

cleanup()
clear()

fireEvent.down(instance)

expect(await findByText(/[❯>] Two/)).toBeTruthy()

cleanup()
clear()

fireEvent.enter(instance)

expect(await findByText('First option: Two')).toBeTruthy()
})

test('Is able to use fireEvent as function', async () => {
const props = await render('node', [
resolve(__dirname, './execute-scripts/stdio-inquirer.js'),
])

const {clear, findByText} = props;

const instance = await findByText('First option')

expect(instance).toBeTruthy()

// Windows uses ">", Linux/MacOS use "❯"
expect(await findByText(/[❯>] One/)).toBeTruthy()

clear()

fireEvent(instance, 'down')

expect(await findByText(/[❯>] Two/)).toBeTruthy()

clear()

fireEvent(instance, 'enter')

expect(await findByText('First option: Two')).toBeTruthy()
})

test('fireEvent works when bound', async () => {
const {fireEvent: fireEventLocal, findByText, cleanup} = await render('node', [
const {fireEvent: fireEventLocal, findByText, clear} = await render('node', [
resolve(__dirname, './execute-scripts/stdio-inquirer.js'),
])

Expand All @@ -69,19 +96,42 @@ test('fireEvent works when bound', async () => {
// Windows uses ">", Linux/MacOS use "❯"
expect(await findByText(/[❯>] One/)).toBeTruthy()

cleanup();
clear();

fireEventLocal.down(instance)
fireEventLocal.down()

expect(await findByText(/[❯>] Two/)).toBeTruthy()

fireEvent.enter(instance)
fireEvent.enter(instance)
cleanup();
fireEventLocal.enter()
fireEventLocal.enter()
clear();
})

test('fireEvent works when bound and used as function', async () => {
const {fireEvent: fireEventLocal, findByText, clear} = 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()

clear();

fireEventLocal('down')

expect(await findByText(/[❯>] Two/)).toBeTruthy()

fireEventLocal('enter')
fireEventLocal('enter')
clear();
})

test('SigTerm works', async () => {
const {findByText, cleanup} = await render('node', [
const {findByText} = await render('node', [
resolve(__dirname, './execute-scripts/stdio-inquirer.js'),
])

Expand All @@ -91,13 +141,11 @@ test('SigTerm works', async () => {

fireEvent.sigterm(instance);

cleanup()

await waitFor(() => expect(isRunning(instance.pid)).toBeFalsy())
})

test('input works', async () => {
const {findByText, fireEvent: fireEventLocal, cleanup} = await render('node', [
const {findByText, fireEvent: fireEventLocal, clear} = await render('node', [
resolve(__dirname, './execute-scripts/stdio-inquirer-input.js'),
])

Expand All @@ -109,6 +157,6 @@ test('input works', async () => {

fireEventLocal.enter();

cleanup()
clear()
})

27 changes: 13 additions & 14 deletions src/events.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,26 @@
import {eventMap} from './event-map'

/**
* Silence TypeScript errors
* @type {*}
*/
const fireEvent = Object.entries(eventMap).reduce(
(prev, [eventName, eventFn]) => {
prev[eventName] = ((instance, ...props) => {
const fireEvent = (instance, event) => {
fireEvent[event](instance);
}

Object.entries(eventMap).forEach(
([eventName, eventFn]) => {
fireEvent[eventName] = ((instance, ...props) => {
eventFn(instance, ...(props))
})
return prev
},
{}
}
)

function getFireEventForElement (
instance
) {
const fireEventElement = event => fireEvent(instance, event);
Object.entries(fireEvent).forEach(( [eventName, eventFn]) => {
fireEventElement[eventName] = (...props) => eventFn(instance, ...(props))
})
return {
fireEvent: Object.entries(fireEvent).reduce((prev, [eventName, eventFn]) => {
prev[eventName] = (...props) => eventFn(instance, ...(props))
return prev;
}, {})
fireEvent: fireEventElement
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/pure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ async function render(
(resolve, reject) => (_readyPromiseInternals = {resolve, reject}),
),
// Clear buffer of stdout to do more accurate `t.regex` checks
cleanup() {
clear() {
execOutputAPI.stdoutArr = []
},
// An array of strings gathered from stdout when unable to do
Expand Down
2 changes: 1 addition & 1 deletion types/pure.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {getFireEventForElement} from "./events";
import {SpawnOptionsWithoutStdio} from "child_process";

export interface TestInstance {
cleanup(): void
clear(): void
stdoutArr: Array<string, Buffer>
stdin: Writable
stdout: Readable
Expand Down

0 comments on commit 19d267d

Please sign in to comment.