Skip to content

Commit 19d267d

Browse files
committed
fix: allow dynamic fireEvent, rename cleanup to clear
1 parent 748b31e commit 19d267d

File tree

4 files changed

+77
-30
lines changed

4 files changed

+77
-30
lines changed

src/__tests__/render-basics.js

Lines changed: 62 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ test('Is able to make terminal input and view in-progress stdout', async () => {
3535
resolve(__dirname, './execute-scripts/stdio-inquirer.js'),
3636
])
3737

38-
const {cleanup, findByText} = props;
38+
const {clear, findByText} = props;
3939

4040
const instance = await findByText('First option')
4141

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

47-
cleanup()
47+
clear()
4848

4949
fireEvent.down(instance)
5050

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

53-
cleanup()
53+
clear()
5454

5555
fireEvent.enter(instance)
5656

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

60+
test('Is able to use fireEvent as function', async () => {
61+
const props = await render('node', [
62+
resolve(__dirname, './execute-scripts/stdio-inquirer.js'),
63+
])
64+
65+
const {clear, findByText} = props;
66+
67+
const instance = await findByText('First option')
68+
69+
expect(instance).toBeTruthy()
70+
71+
// Windows uses ">", Linux/MacOS use "❯"
72+
expect(await findByText(/[>] One/)).toBeTruthy()
73+
74+
clear()
75+
76+
fireEvent(instance, 'down')
77+
78+
expect(await findByText(/[>] Two/)).toBeTruthy()
79+
80+
clear()
81+
82+
fireEvent(instance, 'enter')
83+
84+
expect(await findByText('First option: Two')).toBeTruthy()
85+
})
86+
6087
test('fireEvent works when bound', async () => {
61-
const {fireEvent: fireEventLocal, findByText, cleanup} = await render('node', [
88+
const {fireEvent: fireEventLocal, findByText, clear} = await render('node', [
6289
resolve(__dirname, './execute-scripts/stdio-inquirer.js'),
6390
])
6491

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

72-
cleanup();
99+
clear();
73100

74-
fireEventLocal.down(instance)
101+
fireEventLocal.down()
75102

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

78-
fireEvent.enter(instance)
79-
fireEvent.enter(instance)
80-
cleanup();
105+
fireEventLocal.enter()
106+
fireEventLocal.enter()
107+
clear();
108+
})
109+
110+
test('fireEvent works when bound and used as function', async () => {
111+
const {fireEvent: fireEventLocal, findByText, clear} = await render('node', [
112+
resolve(__dirname, './execute-scripts/stdio-inquirer.js'),
113+
])
114+
115+
const instance = await findByText('First option')
116+
117+
expect(instance).toBeTruthy()
118+
119+
// Windows uses ">", Linux/MacOS use "❯"
120+
expect(await findByText(/[>] One/)).toBeTruthy()
121+
122+
clear();
123+
124+
fireEventLocal('down')
125+
126+
expect(await findByText(/[>] Two/)).toBeTruthy()
127+
128+
fireEventLocal('enter')
129+
fireEventLocal('enter')
130+
clear();
81131
})
82132

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

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

92142
fireEvent.sigterm(instance);
93143

94-
cleanup()
95-
96144
await waitFor(() => expect(isRunning(instance.pid)).toBeFalsy())
97145
})
98146

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

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

110158
fireEventLocal.enter();
111159

112-
cleanup()
160+
clear()
113161
})
114162

src/events.js

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,26 @@
11
import {eventMap} from './event-map'
22

3-
/**
4-
* Silence TypeScript errors
5-
* @type {*}
6-
*/
7-
const fireEvent = Object.entries(eventMap).reduce(
8-
(prev, [eventName, eventFn]) => {
9-
prev[eventName] = ((instance, ...props) => {
3+
const fireEvent = (instance, event) => {
4+
fireEvent[event](instance);
5+
}
6+
7+
Object.entries(eventMap).forEach(
8+
([eventName, eventFn]) => {
9+
fireEvent[eventName] = ((instance, ...props) => {
1010
eventFn(instance, ...(props))
1111
})
12-
return prev
13-
},
14-
{}
12+
}
1513
)
1614

1715
function getFireEventForElement (
1816
instance
1917
) {
18+
const fireEventElement = event => fireEvent(instance, event);
19+
Object.entries(fireEvent).forEach(( [eventName, eventFn]) => {
20+
fireEventElement[eventName] = (...props) => eventFn(instance, ...(props))
21+
})
2022
return {
21-
fireEvent: Object.entries(fireEvent).reduce((prev, [eventName, eventFn]) => {
22-
prev[eventName] = (...props) => eventFn(instance, ...(props))
23-
return prev;
24-
}, {})
23+
fireEvent: fireEventElement
2524
}
2625
}
2726

src/pure.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ async function render(
3535
(resolve, reject) => (_readyPromiseInternals = {resolve, reject}),
3636
),
3737
// Clear buffer of stdout to do more accurate `t.regex` checks
38-
cleanup() {
38+
clear() {
3939
execOutputAPI.stdoutArr = []
4040
},
4141
// An array of strings gathered from stdout when unable to do

types/pure.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {getFireEventForElement} from "./events";
88
import {SpawnOptionsWithoutStdio} from "child_process";
99

1010
export interface TestInstance {
11-
cleanup(): void
11+
clear(): void
1212
stdoutArr: Array<string, Buffer>
1313
stdin: Writable
1414
stdout: Readable

0 commit comments

Comments
 (0)