1+ import * as React from 'react' ;
12import { View } from 'react-native' ;
23import TestRenderer from 'react-test-renderer' ;
34import { configureInternal , getConfig } from '../config' ;
4- import { getHostComponentNames } from '../helpers/host-component-names' ;
5+ import {
6+ getHostComponentNames ,
7+ configureHostComponentNamesIfNeeded ,
8+ } from '../helpers/host-component-names' ;
59import * as within from '../within' ;
10+ import { act , render } from '..' ;
611
712const mockCreate = jest . spyOn ( TestRenderer , 'create' ) as jest . Mock ;
813const mockGetQueriesForElements = jest . spyOn (
@@ -11,10 +16,48 @@ const mockGetQueriesForElements = jest.spyOn(
1116) as jest . Mock ;
1217
1318describe ( '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' , ( ) => {
1457 test ( 'updates internal config with host component names when they are not defined' , ( ) => {
1558 expect ( getConfig ( ) . hostComponentNames ) . toBeUndefined ( ) ;
1659
17- getHostComponentNames ( ) ;
60+ configureHostComponentNamesIfNeeded ( ) ;
1861
1962 expect ( getConfig ( ) . hostComponentNames ) . toEqual ( {
2063 text : 'Text' ,
@@ -27,7 +70,7 @@ describe('getHostComponentNames', () => {
2770 hostComponentNames : { text : 'banana' , textInput : 'banana' } ,
2871 } ) ;
2972
30- getHostComponentNames ( ) ;
73+ configureHostComponentNamesIfNeeded ( ) ;
3174
3275 expect ( getConfig ( ) . hostComponentNames ) . toEqual ( {
3376 text : 'banana' ,
@@ -40,14 +83,14 @@ describe('getHostComponentNames', () => {
4083 root : { type : View , children : [ ] , props : { } } ,
4184 } ) ;
4285
43- expect ( ( ) => getHostComponentNames ( ) ) . toThrowErrorMatchingInlineSnapshot ( `
86+ expect ( ( ) => configureHostComponentNamesIfNeeded ( ) )
87+ . toThrowErrorMatchingInlineSnapshot ( `
4488 "Trying to detect host component names triggered the following error:
4589
4690 Unable to find an element with testID: text
4791
4892 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."
5194 ` ) ;
5295 } ) ;
5396
@@ -58,14 +101,14 @@ describe('getHostComponentNames', () => {
58101 } ,
59102 } ) ;
60103
61- expect ( ( ) => getHostComponentNames ( ) ) . toThrowErrorMatchingInlineSnapshot ( `
104+ expect ( ( ) => configureHostComponentNamesIfNeeded ( ) )
105+ . toThrowErrorMatchingInlineSnapshot ( `
62106 "Trying to detect host component names triggered the following error:
63107
64108 getByTestId returned non-host component
65109
66110 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."
69112 ` ) ;
70113 } ) ;
71114} ) ;
0 commit comments