Skip to content

Commit edfb4c4

Browse files
committed
feat: added typing event and better types
1 parent 7e6345a commit edfb4c4

File tree

12 files changed

+151
-59
lines changed

12 files changed

+151
-59
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const inquirer = require('inquirer')
2+
3+
inquirer
4+
.prompt([
5+
{
6+
type: "input",
7+
name: "name",
8+
message: "What is your name?",
9+
},
10+
]);

src/__tests__/render-basics.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const {resolve} = require('path')
2+
const isRunning = require('is-running')
23
const {render} = require('../pure')
34
const {fireEvent} = require('../events')
4-
const isRunning = require('is-running')
55
const {waitFor} = require("../wait-for");
66

77
test('Should handle stderr outputs with rejection', async () => {
@@ -69,7 +69,6 @@ test('fireEvent works when bound', async () => {
6969
cleanup();
7070
})
7171

72-
7372
test('SigTerm works', async () => {
7473
const {findByText, cleanup} = await render('node', [
7574
resolve(__dirname, './execute-scripts/stdio-inquirer.js'),
@@ -86,3 +85,19 @@ test('SigTerm works', async () => {
8685
await waitFor(() => expect(isRunning(instance.pid)).toBeFalsy())
8786
})
8887

88+
test('input works', async () => {
89+
const {findByText, fireEvent: fireEventLocal, cleanup} = await render('node', [
90+
resolve(__dirname, './execute-scripts/stdio-inquirer-input.js'),
91+
])
92+
93+
expect(await findByText('What is your name?')).toBeTruthy()
94+
95+
fireEventLocal.type("Corbin");
96+
97+
expect(await findByText('Corbin')).toBeTruthy()
98+
99+
fireEventLocal.enter();
100+
101+
cleanup()
102+
})
103+

src/event-map.js

Lines changed: 0 additions & 11 deletions
This file was deleted.

src/event-map.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import kill from 'tree-kill';
2+
3+
import {TestInstance} from "../types/pure";
4+
5+
const eventMap = {
6+
down: (instance: TestInstance) => instance.stdin.write('\x1B\x5B\x42'),
7+
up: (instance: TestInstance) => instance.stdin.write('\x1B\x5B\x41'),
8+
enter: (instance: TestInstance) => instance.stdin.write('\x0D'),
9+
sigterm: (instance: TestInstance) => instance.pid && kill(instance.pid),
10+
sigkill: (instance: TestInstance) => instance.pid && kill(instance.pid, 'SIGKILL'),
11+
type: (instance: TestInstance, text: string) => instance.stdin.write(text)
12+
}
13+
14+
export {eventMap}

src/events.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import {eventMap} from './event-map'
2+
3+
const fireEvent = Object.entries(eventMap).reduce(
4+
(prev, [eventName, eventFn]) => {
5+
prev[eventName] = ((instance, ...props) => {
6+
eventFn(instance, ...(props))
7+
})
8+
return prev
9+
},
10+
{}
11+
)
12+
13+
function getFireEventForElement (
14+
instance
15+
) {
16+
return {
17+
fireEvent: Object.entries(fireEvent).reduce((prev, [eventName, eventFn]) => {
18+
prev[eventName] = (...props) => eventFn(instance, ...(props))
19+
return prev;
20+
}, {})
21+
}
22+
}
23+
24+
export {fireEvent, getFireEventForElement}

src/events.ts

Lines changed: 0 additions & 33 deletions
This file was deleted.

src/pure.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*/
77
import childProcess from 'child_process'
88
import stripFinalNewline from 'strip-final-newline'
9-
import {RenderOptions, TestInstance} from '../types/pure'
9+
import {RenderOptions, RenderResult, TestInstance} from '../types/pure'
1010
import {_runObservers} from './mutation-observer'
1111
import {getQueriesForElement} from './get-queries-for-instance'
1212
import {getFireEventForElement} from './events'
@@ -16,7 +16,7 @@ async function render(
1616
command: string,
1717
args: string[] = [],
1818
opts: Partial<RenderOptions> = {},
19-
): Promise<TestInstance> {
19+
): Promise<RenderResult> {
2020
const {cwd = __dirname} = opts
2121

2222
const exec = childProcess.spawn(command, args, {
@@ -88,8 +88,8 @@ async function render(
8888
pid: exec.pid
8989
},
9090
getQueriesForElement(execOutputAPI),
91-
getFireEventForElement(execOutputAPI as unknown as TestInstance)
92-
)
91+
getFireEventForElement(execOutputAPI)
92+
) as TestInstance as RenderResult
9393
}
9494

9595
export {render}

src/query-helpers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ import type {
77
WithSuggest,
88
Variant,
99
} from '../types'
10+
import {TestInstance} from "../types/pure";
1011
import {getSuggestedQuery} from './suggestions'
1112
import {waitFor} from './wait-for'
12-
import {TestInstance} from "../types/pure";
1313
import {getConfig} from "./config";
1414

1515
function getInstanceError(message: string | null, instance: TestInstance) {

types/events.d.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import {eventMap} from "../src/event-map";
2+
import {ShiftArgs} from "./helpers";
3+
import {TestInstance} from "./pure";
4+
5+
export const fireEvent = typeof eventMap;
6+
7+
export function getFireEventForElement (
8+
instance: TestInstance
9+
): {
10+
[key in keyof FireEventRecord]: ShiftArgs<FireEventRecord[key]>;
11+
};
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import {TestInstance} from "./pure";
2+
import * as queries from './queries'
3+
4+
export type BoundFunction<T> = T extends (
5+
container: TestInstance,
6+
...args: infer P
7+
) => infer R
8+
? (...args: P) => R
9+
: never
10+
11+
export type BoundFunctions<Q> = Q extends typeof queries
12+
? {
13+
getByText<T extends TestInstance = TestInstance>(
14+
...args: Parameters<BoundFunction<queries.GetByText<T>>>
15+
): ReturnType<queries.GetByText<T>>
16+
queryByText<T extends TestInstance = TestInstance>(
17+
...args: Parameters<BoundFunction<queries.QueryByText<T>>>
18+
): ReturnType<queries.QueryByText<T>>
19+
findByText<T extends TestInstance = TestInstance>(
20+
...args: Parameters<BoundFunction<queries.FindByText<T>>>
21+
): ReturnType<queries.FindByText<T>>
22+
} & {
23+
[P in keyof Q]: BoundFunction<Q[P]>
24+
}
25+
: {
26+
[P in keyof Q]: BoundFunction<Q[P]>
27+
}
28+
29+
export type Query = (
30+
container: TestInstance,
31+
...args: any[]
32+
) =>
33+
| Error
34+
| TestInstance
35+
| TestInstance[]
36+
| Promise<TestInstance[]>
37+
| Promise<TestInstance>
38+
| null
39+
40+
export interface Queries {
41+
[T: string]: Query
42+
}
43+
44+
export function getQueriesForInstance<T extends Queries = typeof queries>(
45+
instance: TestInstance,
46+
queriesToBind?: T,
47+
): BoundFunctions<T>

0 commit comments

Comments
 (0)