@@ -4,8 +4,15 @@ import { ReactTestInstance } from 'react-test-renderer';
4
4
import { render } from '../..' ;
5
5
6
6
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' ;
9
16
} ;
10
17
11
18
const { getByPredicate, getAllByPredicate } = render (
@@ -27,10 +34,56 @@ test('getByPredicate returns only native elements', () => {
27
34
expect ( getAllByPredicate ( testIdPredicate ( 'view' ) ) ) . toHaveLength ( 1 ) ;
28
35
expect ( getAllByPredicate ( testIdPredicate ( 'button' ) ) ) . toHaveLength ( 1 ) ;
29
36
30
- expect ( ( ) => getByPredicate ( testIdPredicate ( 'myComponent' ) ) ) . toThrowError (
31
- / ^ U n a b l e t o f i n d a n e l e m e n t m a t c h i n g p r e d i c a t e : / i
32
- ) ;
33
- expect ( ( ) => getAllByPredicate ( testIdPredicate ( 'myComponent' ) ) ) . toThrowError (
34
- / ^ U n a b l e t o f i n d a n e l e m e n t m a t c h i n g p r e d i c a t e : / 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 >
35
60
) ;
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
+ ` ) ;
36
89
} ) ;
0 commit comments