-
Notifications
You must be signed in to change notification settings - Fork 273
/
Copy pathtext.ts
54 lines (46 loc) · 1.89 KB
/
text.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
import type { ReactTestInstance } from 'react-test-renderer';
import { findAll } from '../helpers/find-all';
import { isHostText } from '../helpers/host-component-names';
import { matchTextContent } from '../helpers/matchers/match-text-content';
import type { TextMatch, TextMatchOptions } from '../matches';
import type {
FindAllByQuery,
FindByQuery,
GetAllByQuery,
GetByQuery,
QueryAllByQuery,
QueryByQuery,
} from './make-queries';
import { makeQueries } from './make-queries';
import type { CommonQueryOptions } from './options';
type ByTextOptions = CommonQueryOptions & TextMatchOptions;
const queryAllByText = (instance: ReactTestInstance): QueryAllByQuery<TextMatch, ByTextOptions> =>
function queryAllByTextFn(text, options = {}) {
return findAll(instance, (node) => isHostText(node) && matchTextContent(node, text, options), {
...options,
matchDeepestOnly: true,
});
};
const getMultipleError = (text: TextMatch) => `Found multiple elements with text: ${String(text)}`;
const getMissingError = (text: TextMatch) => `Unable to find an element with text: ${String(text)}`;
const { getBy, getAllBy, queryBy, queryAllBy, findBy, findAllBy } = makeQueries(
queryAllByText,
getMissingError,
getMultipleError,
);
export type ByTextQueries = {
getByText: GetByQuery<TextMatch, ByTextOptions>;
getAllByText: GetAllByQuery<TextMatch, ByTextOptions>;
queryByText: QueryByQuery<TextMatch, ByTextOptions>;
queryAllByText: QueryAllByQuery<TextMatch, ByTextOptions>;
findByText: FindByQuery<TextMatch, ByTextOptions>;
findAllByText: FindAllByQuery<TextMatch, ByTextOptions>;
};
export const bindByTextQueries = (instance: ReactTestInstance): ByTextQueries => ({
getByText: getBy(instance),
getAllByText: getAllBy(instance),
queryByText: queryBy(instance),
queryAllByText: queryAllBy(instance),
findByText: findBy(instance),
findAllByText: findAllBy(instance),
});