Skip to content

Commit cc05823

Browse files
thymikeeMattAgn
authored andcommitted
parametrize Queries
1 parent 6f5b08d commit cc05823

File tree

2 files changed

+32
-24
lines changed

2 files changed

+32
-24
lines changed

src/helpers/byTestId.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,11 @@ const {
3030
queryBy: queryByTestId,
3131
findBy: findByTestId,
3232
findAllBy: findAllByTestId,
33-
}: Queries = makeQueries(queryAllByTestId, getMissingError, getMultipleError);
33+
}: Queries<string | RegExp> = makeQueries(
34+
queryAllByTestId,
35+
getMissingError,
36+
getMultipleError
37+
);
3438

3539
export {
3640
getByTestId,

src/helpers/makeQueries.js

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,6 @@ import waitFor from '../waitFor';
33
import type { WaitForOptions } from '../waitFor';
44
import { ErrorWithStack } from './errors';
55

6-
// TODO: fix typing
7-
// is it always string | RegExp for every query?
8-
// what about options for each query?
9-
type QueryArg = string | RegExp;
10-
116
type QueryFunction<ArgType, ReturnType> = (
127
instance: ReactTestInstance
138
) => (args: ArgType) => ReturnType;
@@ -16,28 +11,37 @@ type FindQueryFunction<ArgType, ReturnType> = (
1611
instance: ReactTestInstance
1712
) => (args: ArgType, waitForOptions?: WaitForOptions) => Promise<ReturnType>;
1813

19-
type QueryAllByQuery = QueryFunction<QueryArg, Array<ReactTestInstance>>;
20-
type QueryByQuery = QueryFunction<QueryArg, null | ReactTestInstance>;
21-
22-
type GetAllByQuery = QueryFunction<QueryArg, Array<ReactTestInstance>>;
23-
type GetByQuery = QueryFunction<QueryArg, ReactTestInstance>;
24-
25-
type FindAllByQuery = FindQueryFunction<QueryArg, Array<ReactTestInstance>>;
26-
type FindByQuery = FindQueryFunction<QueryArg, ReactTestInstance>;
27-
28-
export type Queries = {
29-
getBy: GetByQuery,
30-
getAllBy: GetAllByQuery,
31-
queryBy: QueryByQuery,
32-
findBy: FindByQuery,
33-
findAllBy: FindAllByQuery,
14+
type QueryAllByQuery<QueryArg> = QueryFunction<
15+
QueryArg,
16+
Array<ReactTestInstance>
17+
>;
18+
type QueryByQuery<QueryArg> = QueryFunction<QueryArg, null | ReactTestInstance>;
19+
20+
type GetAllByQuery<QueryArg> = QueryFunction<
21+
QueryArg,
22+
Array<ReactTestInstance>
23+
>;
24+
type GetByQuery<QueryArg> = QueryFunction<QueryArg, ReactTestInstance>;
25+
26+
type FindAllByQuery<QueryArg> = FindQueryFunction<
27+
QueryArg,
28+
Array<ReactTestInstance>
29+
>;
30+
type FindByQuery<QueryArg> = FindQueryFunction<QueryArg, ReactTestInstance>;
31+
32+
export type Queries<QueryArg> = {
33+
getBy: GetByQuery<QueryArg>,
34+
getAllBy: GetAllByQuery<QueryArg>,
35+
queryBy: QueryByQuery<QueryArg>,
36+
findBy: FindByQuery<QueryArg>,
37+
findAllBy: FindAllByQuery<QueryArg>,
3438
};
3539

36-
export function makeQueries(
37-
queryAllByQuery: QueryAllByQuery,
40+
export function makeQueries<QueryArg>(
41+
queryAllByQuery: QueryAllByQuery<QueryArg>,
3842
getMissingError: (args: QueryArg) => string,
3943
getMultipleError: (args: QueryArg) => string
40-
): Queries {
44+
): Queries<QueryArg> {
4145
function getAllByQuery(instance: ReactTestInstance) {
4246
return function getAllFn(args: QueryArg) {
4347
const results = queryAllByQuery(instance)(args);

0 commit comments

Comments
 (0)