-
Notifications
You must be signed in to change notification settings - Fork 273
/
Copy pathlabel-text.ts
56 lines (49 loc) · 1.95 KB
/
label-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
55
56
import type { ReactTestInstance } from 'react-test-renderer';
import { findAll } from '../helpers/find-all';
import { matchAccessibilityLabel } from '../helpers/matchers/match-label-text';
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 ByLabelTextOptions = CommonQueryOptions & TextMatchOptions;
function queryAllByLabelText(instance: ReactTestInstance) {
return (text: TextMatch, queryOptions?: ByLabelTextOptions) => {
return findAll(
instance,
(node) => matchAccessibilityLabel(node, text, queryOptions),
queryOptions,
);
};
}
const getMultipleError = (labelText: TextMatch) =>
`Found multiple elements with accessibility label: ${String(labelText)} `;
const getMissingError = (labelText: TextMatch) =>
`Unable to find an element with accessibility label: ${String(labelText)}`;
const { getBy, getAllBy, queryBy, queryAllBy, findBy, findAllBy } = makeQueries(
queryAllByLabelText,
getMissingError,
getMultipleError,
);
export type ByLabelTextQueries = {
getByLabelText: GetByQuery<TextMatch, ByLabelTextOptions>;
getAllByLabelText: GetAllByQuery<TextMatch, ByLabelTextOptions>;
queryByLabelText: QueryByQuery<TextMatch, ByLabelTextOptions>;
queryAllByLabelText: QueryAllByQuery<TextMatch, ByLabelTextOptions>;
findByLabelText: FindByQuery<TextMatch, ByLabelTextOptions>;
findAllByLabelText: FindAllByQuery<TextMatch, ByLabelTextOptions>;
};
export const bindByLabelTextQueries = (instance: ReactTestInstance): ByLabelTextQueries => ({
getByLabelText: getBy(instance),
getAllByLabelText: getAllBy(instance),
queryByLabelText: queryBy(instance),
queryAllByLabelText: queryAllBy(instance),
findByLabelText: findBy(instance),
findAllByLabelText: findAllBy(instance),
});