11import * as React from 'react' ;
2- import { TextStyle , View , ViewProps } from 'react-native' ;
2+ import { Text , TextProps , View , ViewProps } from 'react-native' ;
33import { render } from 'react-native-testing-library' ;
44
55import { getOverrides , WithOverrides } from './overrides' ;
66
7- interface ComponentTwoProps {
8- two ?: string ;
9- style ?: TextStyle ;
7+ interface CommonComponentProps extends TextProps {
8+ isDisabled : boolean ;
109}
1110
12- const ComponentTwo = ( props : ComponentTwoProps ) => {
13- return < > </ > ;
11+ const CommonComponent = ( props : CommonComponentProps ) => {
12+ const { isDisabled, ...textProps } = props ;
13+
14+ return < Text { ...textProps } /> ;
1415} ;
1516
16- interface ComponentOneBaseProps {
17- one ?: string ;
17+ interface RootProps extends ViewProps {
18+ children ?: React . ReactNode ;
1819}
1920
20- interface ComponentOneOverrides {
21- Root : ViewProps ;
22- ComponentTwo : ComponentTwoProps ;
23- }
21+ const StyledRoot = ( props : RootProps ) => {
22+ const { children, ...viewProps } = props ;
2423
25- type ComponentOneProps = WithOverrides <
26- ComponentOneBaseProps ,
27- ComponentOneOverrides
28- > ;
29-
30- const ComponentOne = ( props : ComponentOneProps ) => {
31- return < > </ > ;
24+ return < View { ...viewProps } > { children } </ View > ;
3225} ;
3326
3427interface TestComponentBaseProps {
35- zero ?: string ;
28+ isDisabled : boolean ;
3629}
3730
3831interface TestComponentOverrides {
39- Root : ViewProps ;
40- ComponentOne : ComponentOneProps ;
41- ComponentTwo : ComponentTwoProps ;
32+ Root : RootProps ;
33+ ComponentOne : CommonComponentProps ;
34+ ComponentTwo : CommonComponentProps ;
4235}
4336
4437type TestComponentProps = WithOverrides <
@@ -47,71 +40,68 @@ type TestComponentProps = WithOverrides<
4740> ;
4841
4942const TestComponent = ( props : TestComponentProps ) => {
50- const { overrides = { } , zero } = props ;
43+ const { overrides = { } , isDisabled } = props ;
5144
5245 const [ RootR , rootProps ] = getOverrides (
53- View ,
46+ StyledRoot ,
5447 props ,
5548 overrides . Root ,
5649 overrides . Root ,
5750 ) ;
5851 const [ ComponentOneR , componentOneProps ] = getOverrides (
59- ComponentOne ,
52+ CommonComponent ,
6053 props ,
6154 overrides . ComponentOne ,
6255 overrides . ComponentOne ,
6356 ) ;
6457 const [ ComponentTwoR , componentTwoProps ] = getOverrides (
65- ComponentTwo ,
58+ CommonComponent ,
6659 props ,
6760 overrides . ComponentTwo ,
6861 overrides . ComponentTwo ,
6962 ) ;
7063
7164 return (
7265 < RootR { ...rootProps } >
73- < ComponentOneR one = { zero } { ...componentOneProps } />
74- < ComponentTwoR two = { zero } { ...componentTwoProps } />
66+ < ComponentOneR isDisabled = { isDisabled } { ...componentOneProps } />
67+ < ComponentTwoR isDisabled = { isDisabled } { ...componentTwoProps } />
7568 </ RootR >
7669 ) ;
7770} ;
7871
79- const ReplaceComponentTwo = ( props : ComponentTwoProps ) => < > </ > ;
72+ const ReplaceComponentTwo = ( props : CommonComponentProps ) => < > </ > ;
8073
8174describe ( 'Overrides' , ( ) => {
8275 test ( 'Component is correctly type-checked and can render' , ( ) => {
83- render (
76+ const { getByTestId } = render (
8477 < TestComponent
78+ isDisabled = { true }
8579 overrides = { {
8680 Root : {
87- props : props => ( {
88- testID : props . zero ,
89- } ) ,
90- style : props => ( {
91- height : 1 ,
92- } ) ,
93- } ,
94- ComponentOne : {
9581 props : {
96- one : '1' ,
97- overrides : {
98- Root : {
99- style : {
100- height : 1 ,
101- } ,
102- } ,
103- } ,
82+ testID : 'ROOT' ,
10483 } ,
105- } ,
106- ComponentTwo : {
10784 style : {
108- fontSize : 1 ,
85+ backgroundColor : 'blue' ,
10986 } ,
87+ } ,
88+ ComponentOne : {
89+ props : ( { isDisabled } ) => ( {
90+ testID : isDisabled ? 'COMPONENT_ONE' : 'INVALID_COMPONENT' ,
91+ } ) ,
92+ style : ( { isDisabled } ) => ( {
93+ backgroundColor : isDisabled ? 'blue' : 'green' ,
94+ } ) ,
95+ } ,
96+ ComponentTwo : {
11097 component : ReplaceComponentTwo ,
11198 } ,
11299 } }
113100 /> ,
114101 ) ;
102+
103+ const componentOne = getByTestId ( 'COMPONENT_ONE' ) ;
104+ expect ( componentOne . props . style . backgroundColor ) . toBe ( 'blue' ) ;
115105 } ) ;
116106} ) ;
117107
0 commit comments