Skip to content

Commit 263dc1a

Browse files
committed
chore: improve tests
1 parent 8473762 commit 263dc1a

File tree

2 files changed

+81
-25
lines changed

2 files changed

+81
-25
lines changed

src/queries/__tests__/predicate.test.tsx

Lines changed: 60 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,15 @@ import { ReactTestInstance } from 'react-test-renderer';
44
import { render } from '../..';
55

66
test('getByPredicate returns only native elements', () => {
7-
const testIdPredicate = (testID: string) => (node: ReactTestInstance) => {
8-
return node.props.testID === testID;
7+
const testIdPredicate = (testID: string) => (element: ReactTestInstance) => {
8+
return element.props.testID === testID;
9+
};
10+
11+
const textInputPredicate = function matchTextInput(
12+
element: ReactTestInstance
13+
) {
14+
// @ts-expect-error - ReatTestInstance type is missing host element typing
15+
return element.type === 'TextInput';
916
};
1017

1118
const { getByPredicate, getAllByPredicate } = render(
@@ -27,10 +34,56 @@ test('getByPredicate returns only native elements', () => {
2734
expect(getAllByPredicate(testIdPredicate('view'))).toHaveLength(1);
2835
expect(getAllByPredicate(testIdPredicate('button'))).toHaveLength(1);
2936

30-
expect(() => getByPredicate(testIdPredicate('myComponent'))).toThrowError(
31-
/^Unable to find an element matching predicate: /i
32-
);
33-
expect(() => getAllByPredicate(testIdPredicate('myComponent'))).toThrowError(
34-
/^Unable to find an element matching predicate: /i
37+
expect(getByPredicate(textInputPredicate)).toBeTruthy();
38+
});
39+
40+
test('getByPredicate error messages', () => {
41+
function hasStylePredicate(element: ReactTestInstance) {
42+
return element.props.style !== undefined;
43+
}
44+
45+
const textInputPredicate = function textInputPredicate(
46+
element: ReactTestInstance
47+
) {
48+
// @ts-expect-error - ReatTestInstance type is missing host element typing
49+
return element.type === 'TextInput';
50+
};
51+
52+
const testIdPredicate = (testID: string) => (element: ReactTestInstance) => {
53+
return element.props.testID === testID;
54+
};
55+
56+
const { getByPredicate, getAllByPredicate } = render(
57+
<View>
58+
<Text testID="text">Text</Text>
59+
</View>
3560
);
61+
62+
expect(() => getByPredicate(hasStylePredicate))
63+
.toThrowErrorMatchingInlineSnapshot(`
64+
"Unable to find an element matching predicate: function hasStylePredicate(element) {
65+
return element.props.style !== undefined;
66+
}"
67+
`);
68+
69+
expect(() => getByPredicate(textInputPredicate))
70+
.toThrowErrorMatchingInlineSnapshot(`
71+
"Unable to find an element matching predicate: function textInputPredicate(element) {
72+
// @ts-expect-error - ReatTestInstance type is missing host element typing
73+
return element.type === 'TextInput';
74+
}"
75+
`);
76+
77+
expect(() => getByPredicate(testIdPredicate('myComponent')))
78+
.toThrowErrorMatchingInlineSnapshot(`
79+
"Unable to find an element matching predicate: element => {
80+
return element.props.testID === testID;
81+
}"
82+
`);
83+
expect(() => getAllByPredicate(testIdPredicate('myComponent')))
84+
.toThrowErrorMatchingInlineSnapshot(`
85+
"Unable to find an element matching predicate: element => {
86+
return element.props.testID === testID;
87+
}"
88+
`);
3689
});

src/queries/predicate.ts

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import type { ReactTestInstance } from 'react-test-renderer';
2+
import { isHostElement } from '../helpers/component-tree';
3+
import { findAll } from '../helpers/findAll';
24
import { makeQueries } from './makeQueries';
35
import type {
46
FindAllByQuery,
@@ -8,24 +10,25 @@ import type {
810
QueryAllByQuery,
911
QueryByQuery,
1012
} from './makeQueries';
13+
import { CommonQueryOptions } from './options';
1114

1215
type PredicateFn = (instance: ReactTestInstance) => boolean;
13-
// Not used so far
14-
type PredicateQueryOptions = void;
15-
16-
const queryAllByPredicate = (
17-
instance: ReactTestInstance
18-
): ((
19-
predicate: PredicateFn,
20-
options?: PredicateQueryOptions
21-
) => Array<ReactTestInstance>) =>
22-
function queryAllByTestIdFn(predicate) {
23-
const results = instance.findAll(
24-
(node) => typeof node.type === 'string' && predicate(node)
16+
type ByPredicateQueryOptions = CommonQueryOptions;
17+
18+
function queryAllByPredicate(instance: ReactTestInstance) {
19+
return function queryAllByPredicateFn(
20+
predicate: PredicateFn,
21+
options?: ByPredicateQueryOptions
22+
): Array<ReactTestInstance> {
23+
const results = findAll(
24+
instance,
25+
(node) => isHostElement(node) && predicate(node),
26+
options
2527
);
2628

2729
return results;
2830
};
31+
}
2932

3033
const getMultipleError = (predicate: PredicateFn) =>
3134
`Found multiple elements matching predicate: ${predicate}`;
@@ -40,12 +43,12 @@ const { getBy, getAllBy, queryBy, queryAllBy, findBy, findAllBy } = makeQueries(
4043
);
4144

4245
export type ByTestIdQueries = {
43-
getByPredicate: GetByQuery<PredicateFn, PredicateQueryOptions>;
44-
getAllByPredicate: GetAllByQuery<PredicateFn, PredicateQueryOptions>;
45-
queryByPredicate: QueryByQuery<PredicateFn, PredicateQueryOptions>;
46-
queryAllByPredicate: QueryAllByQuery<PredicateFn, PredicateQueryOptions>;
47-
findByPredicate: FindByQuery<PredicateFn, PredicateQueryOptions>;
48-
findAllByPredicate: FindAllByQuery<PredicateFn, PredicateQueryOptions>;
46+
getByPredicate: GetByQuery<PredicateFn, ByPredicateQueryOptions>;
47+
getAllByPredicate: GetAllByQuery<PredicateFn, ByPredicateQueryOptions>;
48+
queryByPredicate: QueryByQuery<PredicateFn, ByPredicateQueryOptions>;
49+
queryAllByPredicate: QueryAllByQuery<PredicateFn, ByPredicateQueryOptions>;
50+
findByPredicate: FindByQuery<PredicateFn, ByPredicateQueryOptions>;
51+
findAllByPredicate: FindAllByQuery<PredicateFn, ByPredicateQueryOptions>;
4952
};
5053

5154
export const bindByPredicateQueries = (

0 commit comments

Comments
 (0)