1
+ import * as React from 'react' ;
1
2
import { View } from 'react-native' ;
2
3
import TestRenderer from 'react-test-renderer' ;
3
4
import { configureInternal , getConfig } from '../config' ;
4
- import { getHostComponentNames } from '../helpers/host-component-names' ;
5
+ import {
6
+ getHostComponentNames ,
7
+ configureHostComponentNamesIfNeeded ,
8
+ } from '../helpers/host-component-names' ;
5
9
import * as within from '../within' ;
10
+ import { act , render } from '..' ;
6
11
7
12
const mockCreate = jest . spyOn ( TestRenderer , 'create' ) as jest . Mock ;
8
13
const mockGetQueriesForElements = jest . spyOn (
@@ -11,10 +16,48 @@ const mockGetQueriesForElements = jest.spyOn(
11
16
) as jest . Mock ;
12
17
13
18
describe ( 'getHostComponentNames' , ( ) => {
19
+ test ( 'returns host component names from internal config' , ( ) => {
20
+ configureInternal ( {
21
+ hostComponentNames : { text : 'banana' , textInput : 'banana' } ,
22
+ } ) ;
23
+
24
+ expect ( getHostComponentNames ( ) ) . toEqual ( {
25
+ text : 'banana' ,
26
+ textInput : 'banana' ,
27
+ } ) ;
28
+ } ) ;
29
+
30
+ test ( 'detects host component names if not present in internal config' , ( ) => {
31
+ expect ( getConfig ( ) . hostComponentNames ) . toBeUndefined ( ) ;
32
+
33
+ const hostComponentNames = getHostComponentNames ( ) ;
34
+
35
+ expect ( hostComponentNames ) . toEqual ( {
36
+ text : 'Text' ,
37
+ textInput : 'TextInput' ,
38
+ } ) ;
39
+ expect ( getConfig ( ) . hostComponentNames ) . toBe ( hostComponentNames ) ;
40
+ } ) ;
41
+
42
+ // Repro test for case when user indirectly triggers `getHostComponentNames` calls from
43
+ // explicit `act` wrapper.
44
+ // See: https://github.com/callstack/react-native-testing-library/issues/1302
45
+ // and https://github.com/callstack/react-native-testing-library/issues/1305
46
+ test ( 'does not throw when wrapped in act after render has been called' , ( ) => {
47
+ render ( < View /> ) ;
48
+ expect ( ( ) =>
49
+ act ( ( ) => {
50
+ getHostComponentNames ( ) ;
51
+ } )
52
+ ) . not . toThrow ( ) ;
53
+ } ) ;
54
+ } ) ;
55
+
56
+ describe ( 'configureHostComponentNamesIfNeeded' , ( ) => {
14
57
test ( 'updates internal config with host component names when they are not defined' , ( ) => {
15
58
expect ( getConfig ( ) . hostComponentNames ) . toBeUndefined ( ) ;
16
59
17
- getHostComponentNames ( ) ;
60
+ configureHostComponentNamesIfNeeded ( ) ;
18
61
19
62
expect ( getConfig ( ) . hostComponentNames ) . toEqual ( {
20
63
text : 'Text' ,
@@ -27,7 +70,7 @@ describe('getHostComponentNames', () => {
27
70
hostComponentNames : { text : 'banana' , textInput : 'banana' } ,
28
71
} ) ;
29
72
30
- getHostComponentNames ( ) ;
73
+ configureHostComponentNamesIfNeeded ( ) ;
31
74
32
75
expect ( getConfig ( ) . hostComponentNames ) . toEqual ( {
33
76
text : 'banana' ,
@@ -40,14 +83,14 @@ describe('getHostComponentNames', () => {
40
83
root : { type : View , children : [ ] , props : { } } ,
41
84
} ) ;
42
85
43
- expect ( ( ) => getHostComponentNames ( ) ) . toThrowErrorMatchingInlineSnapshot ( `
86
+ expect ( ( ) => configureHostComponentNamesIfNeeded ( ) )
87
+ . toThrowErrorMatchingInlineSnapshot ( `
44
88
"Trying to detect host component names triggered the following error:
45
89
46
90
Unable to find an element with testID: text
47
91
48
92
There seems to be an issue with your configuration that prevents React Native Testing Library from working correctly.
49
- Please check if you are using compatible versions of React Native and React Native Testing Library.
50
- "
93
+ Please check if you are using compatible versions of React Native and React Native Testing Library."
51
94
` ) ;
52
95
} ) ;
53
96
@@ -58,14 +101,14 @@ describe('getHostComponentNames', () => {
58
101
} ,
59
102
} ) ;
60
103
61
- expect ( ( ) => getHostComponentNames ( ) ) . toThrowErrorMatchingInlineSnapshot ( `
104
+ expect ( ( ) => configureHostComponentNamesIfNeeded ( ) )
105
+ . toThrowErrorMatchingInlineSnapshot ( `
62
106
"Trying to detect host component names triggered the following error:
63
107
64
108
getByTestId returned non-host component
65
109
66
110
There seems to be an issue with your configuration that prevents React Native Testing Library from working correctly.
67
- Please check if you are using compatible versions of React Native and React Native Testing Library.
68
- "
111
+ Please check if you are using compatible versions of React Native and React Native Testing Library."
69
112
` ) ;
70
113
} ) ;
71
114
} ) ;
0 commit comments