Skip to content

Commit cf5d3d3

Browse files
author
MattAgn
committed
refactor: make single function makeQueries
1 parent 59a8490 commit cf5d3d3

File tree

2 files changed

+108
-101
lines changed

2 files changed

+108
-101
lines changed

src/helpers/byTestId.js

Lines changed: 18 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,13 @@
11
// @flow
2-
import {
3-
makeGetAllQuery,
4-
makeSingleQuery,
5-
makeGetQuery,
6-
makeFindAllQuery,
7-
makeFindQuery,
8-
} from './makeQueries';
2+
import { makeQueries } from './makeQueries';
93

104
const getNodeByTestId = (node, testID) => {
115
return typeof testID === 'string'
126
? testID === node.props.testID
137
: testID.test(node.props.testID);
148
};
159

16-
export const queryAllByTestId = (
10+
const queryAllByTestId = (
1711
instance: ReactTestInstance
1812
): ((testId: string | RegExp) => Array<ReactTestInstance>) =>
1913
function getAllByTestIdFn(testId) {
@@ -29,27 +23,19 @@ const getMultipleError = (testId) =>
2923
const getMissingError = (testId) =>
3024
`Unable to find an element with testID: ${String(testId)}`;
3125

32-
export const getAllByTestId = (
33-
instance: ReactTestInstance
34-
): ((testId: string | RegExp) => Array<ReactTestInstance>) =>
35-
makeGetAllQuery(queryAllByTestId, instance, getMissingError);
36-
37-
export const queryByTestId = (
38-
instance: ReactTestInstance
39-
): ((testId: string | RegExp) => ReactTestInstance | null) =>
40-
makeSingleQuery(queryAllByTestId, instance, getMultipleError);
41-
42-
export const getByTestId = (
43-
instance: ReactTestInstance
44-
): ((testId: string | RegExp) => ReactTestInstance) =>
45-
makeGetQuery(queryAllByTestId, instance, getMultipleError, getMissingError);
46-
47-
export const findAllByTestId = (
48-
instance: ReactTestInstance
49-
): ((testId: string | RegExp) => Promise<Array<ReactTestInstance>>) =>
50-
makeFindAllQuery(getAllByTestId, instance);
51-
52-
export const findByTestId = (
53-
instance: ReactTestInstance
54-
): ((testId: string | RegExp) => Promise<ReactTestInstance>) =>
55-
makeFindQuery(getByTestId, instance);
26+
const {
27+
getBy: getByTestId,
28+
getAllBy: getAllByTestId,
29+
queryBy: queryByTestId,
30+
findBy: findByTestId,
31+
findAllBy: findAllByTestId,
32+
} = makeQueries(queryAllByTestId, getMissingError, getMultipleError);
33+
34+
export {
35+
getByTestId,
36+
getAllByTestId,
37+
queryByTestId,
38+
findByTestId,
39+
findAllByTestId,
40+
queryAllByTestId,
41+
};

src/helpers/makeQueries.js

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

6-
type AllQuery = <T>(
6+
type QueryArg = string | RegExp;
7+
8+
type QueryFunction<ArgType, ReturnType> = (
79
instance: ReactTestInstance
8-
// $FlowFixMe - Property @@iterator is missing in T [1] but exists in $Iterable [2]
9-
) => (...args: T) => Array<ReactTestInstance>;
10-
11-
export function makeGetAllQuery<T>(
12-
allQuery: AllQuery,
13-
instance: ReactTestInstance,
14-
getMissingError: (args: T) => string
15-
): (...args: Array<T>) => Array<ReactTestInstance> {
16-
return function getAllFn(...args: Array<T>) {
17-
const results = allQuery(instance)(...args);
18-
19-
if (results.length === 0) {
20-
throw new ErrorWithStack(getMissingError(...args), getAllFn);
21-
}
22-
23-
return results;
24-
};
25-
}
10+
) => (args: ArgType) => ReturnType;
2611

27-
export function makeSingleQuery<T>(
28-
allQuery: AllQuery,
29-
instance: ReactTestInstance,
30-
getMultipleError: (args: T) => string
31-
): (...args: Array<T>) => null | ReactTestInstance {
32-
return function singleQueryFn(...args: Array<T>) {
33-
const results = allQuery(instance)(...args);
12+
type FindQueryFunction<ArgType, ReturnType> = (
13+
instance: ReactTestInstance
14+
) => (args: ArgType, waitForOptions?: WaitForOptions) => Promise<ReturnType>;
3415

35-
if (results.length > 1) {
36-
throw new ErrorWithStack(getMultipleError(...args), singleQueryFn);
37-
}
16+
type QueryAllByQuery = QueryFunction<QueryArg, Array<ReactTestInstance>>;
17+
type QueryByQuery = QueryFunction<QueryArg, null | ReactTestInstance>;
3818

39-
if (results.length === 0) {
40-
return null;
41-
}
19+
type GetAllByQuery = QueryFunction<QueryArg, Array<ReactTestInstance>>;
20+
type GetByQuery = QueryFunction<QueryArg, ReactTestInstance>;
4221

43-
return results[0];
44-
};
45-
}
22+
type FindAllByQuery = FindQueryFunction<QueryArg, Array<ReactTestInstance>>;
23+
type FindByQuery = FindQueryFunction<QueryArg, ReactTestInstance>;
4624

47-
export function makeGetQuery<T>(
48-
allQuery: AllQuery,
49-
instance: ReactTestInstance,
50-
getMultipleError: (args: T) => string,
51-
getMissingError: (args: T) => string
52-
): (...args: Array<T>) => ReactTestInstance {
53-
return function getFn(...args: Array<T>) {
54-
const results = allQuery(instance)(...args);
25+
type Queries = {
26+
getBy: GetByQuery,
27+
getAllBy: GetAllByQuery,
28+
queryBy: QueryByQuery,
29+
findBy: FindByQuery,
30+
findAllBy: FindAllByQuery,
31+
};
5532

56-
if (results.length > 1) {
57-
throw new ErrorWithStack(getMultipleError(...args), getFn);
58-
}
33+
export function makeQueries(
34+
queryAllByQuery: QueryAllByQuery,
35+
getMissingError: (args: QueryArg) => string,
36+
getMultipleError: (args: QueryArg) => string
37+
): Queries {
38+
function getAllByQuery(instance: ReactTestInstance) {
39+
return function getAllFn(args: QueryArg) {
40+
const results = queryAllByQuery(instance)(args);
5941

60-
if (results.length === 0) {
61-
throw new ErrorWithStack(getMissingError(...args), getFn);
62-
}
42+
if (results.length === 0) {
43+
throw new ErrorWithStack(getMissingError(args), getAllFn);
44+
}
6345

64-
return results[0];
65-
};
66-
}
46+
return results;
47+
};
48+
}
6749

68-
export function makeFindAllQuery<T>(
69-
getAllQuery: AllQuery,
70-
instance: ReactTestInstance
71-
): (
72-
args: T,
73-
waitForOptions?: WaitForOptions
74-
) => Promise<Array<ReactTestInstance>> {
75-
return function findAllFn(args: T, waitForOptions: WaitForOptions = {}) {
76-
return waitFor(() => getAllQuery(instance)(args), waitForOptions);
77-
};
78-
}
79-
export function makeFindQuery<T>(
80-
getQuery: (instance: ReactTestInstance) => (args: any) => ReactTestInstance,
81-
instance: ReactTestInstance
82-
): (args: T, waitForOptions?: WaitForOptions) => Promise<ReactTestInstance> {
83-
return function findFn(args: T, waitForOptions: WaitForOptions = {}) {
84-
return waitFor(() => getQuery(instance)(args), waitForOptions);
50+
function queryByQuery(instance: ReactTestInstance) {
51+
return function singleQueryFn(args: QueryArg) {
52+
const results = queryAllByQuery(instance)(args);
53+
54+
if (results.length > 1) {
55+
throw new ErrorWithStack(getMultipleError(args), singleQueryFn);
56+
}
57+
58+
if (results.length === 0) {
59+
return null;
60+
}
61+
62+
return results[0];
63+
};
64+
}
65+
66+
function getByQuery(instance: ReactTestInstance) {
67+
return function getFn(args: QueryArg) {
68+
const results = queryAllByQuery(instance)(args);
69+
70+
if (results.length > 1) {
71+
throw new ErrorWithStack(getMultipleError(args), getFn);
72+
}
73+
74+
if (results.length === 0) {
75+
throw new ErrorWithStack(getMissingError(args), getFn);
76+
}
77+
78+
return results[0];
79+
};
80+
}
81+
82+
function findAllByQuery(instance: ReactTestInstance) {
83+
return function findAllFn(
84+
args: QueryArg,
85+
waitForOptions?: WaitForOptions = {}
86+
) {
87+
return waitFor(() => getAllByQuery(instance)(args), waitForOptions);
88+
};
89+
}
90+
91+
function findByQuery(instance: ReactTestInstance) {
92+
return function findFn(
93+
args: QueryArg,
94+
waitForOptions?: WaitForOptions = {}
95+
) {
96+
return waitFor(() => getByQuery(instance)(args), waitForOptions);
97+
};
98+
}
99+
100+
return {
101+
getBy: getByQuery,
102+
getAllBy: getAllByQuery,
103+
queryBy: queryByQuery,
104+
findBy: findByQuery,
105+
findAllBy: findAllByQuery,
85106
};
86107
}

0 commit comments

Comments
 (0)