-
Notifications
You must be signed in to change notification settings - Fork 272
/
Copy pathtestId.ts
66 lines (59 loc) · 1.99 KB
/
testId.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import type { ReactTestInstance } from 'react-test-renderer';
import { matches, TextMatch } from '../matches';
import { makeQueries } from './makeQueries';
import type {
FindAllByQuery,
FindByQuery,
GetAllByQuery,
GetByQuery,
QueryAllByQuery,
QueryByQuery,
} from './makeQueries';
import type { TextMatchOptions } from './text';
const getNodeByTestId = (
node: ReactTestInstance,
testID: TextMatch,
options: TextMatchOptions = {}
) => {
const { exact, normalizer } = options;
return matches(testID, node.props.testID, normalizer, exact);
};
const queryAllByTestId = (
instance: ReactTestInstance
): ((
testId: TextMatch,
queryOptions?: TextMatchOptions
) => Array<ReactTestInstance>) =>
function queryAllByTestIdFn(testId, queryOptions) {
const results = instance
.findAll((node) => getNodeByTestId(node, testId, queryOptions))
.filter((element) => typeof element.type === 'string');
return results;
};
const getMultipleError = (testId: TextMatch) =>
`Found multiple elements with testID: ${String(testId)}`;
const getMissingError = (testId: TextMatch) =>
`Unable to find an element with testID: ${String(testId)}`;
const { getBy, getAllBy, queryBy, queryAllBy, findBy, findAllBy } = makeQueries(
queryAllByTestId,
getMissingError,
getMultipleError
);
export type ByTestIdQueries = {
getByTestId: GetByQuery<TextMatch, TextMatchOptions>;
getAllByTestId: GetAllByQuery<TextMatch, TextMatchOptions>;
queryByTestId: QueryByQuery<TextMatch, TextMatchOptions>;
queryAllByTestId: QueryAllByQuery<TextMatch, TextMatchOptions>;
findByTestId: FindByQuery<TextMatch, TextMatchOptions>;
findAllByTestId: FindAllByQuery<TextMatch, TextMatchOptions>;
};
export const bindByTestIdQueries = (
instance: ReactTestInstance
): ByTestIdQueries => ({
getByTestId: getBy(instance),
getAllByTestId: getAllBy(instance),
queryByTestId: queryBy(instance),
queryAllByTestId: queryAllBy(instance),
findByTestId: findBy(instance),
findAllByTestId: findAllBy(instance),
});