I have a code that goes like this:
function AppContainerAndroid() {
return <App />;
}
function AppContainerIOS() {
return (
<SafeAreaProvider testID="ios_area_provider">
<App />
</SafeAreaProvider>
);
}
function RenderProperContainer({renderProps}) {
return renderProps();
}
export default function AppContainer() {
return (
<RenderProperContainer
renderProps={() => {
if (Platform.OS === 'android') {
return <AppContainerAndroid />;
}
return <AppContainerIOS />;
}}
/>
);
}
and a test that looks like this.
it('should render android platform', () => {
setPlatform('android');
const {getByTestId} = render(<AppContainer />);
const element = getByTestId('ios_area_provider');
console.log(element);
});
When I run the test I get an error saying
Did some searching and most of the answers I get says it has something to do with the way I return the component. It appears that it does not allow conditional return because I already tried:
export default function AppContainer() {
if(Platform.OS === 'android') {
return <AppContainerAndroid />
}
return <AppContainerIOS />
}
and
export default function AppContainer() {
return Platform.OS === 'android' ? (
<AppContainerAndroid />
) : (
<AppContainerIOS />
);
}
and get the same error. Is there a way to handle this kind of scenario?
UPDATE: This is what's inside my <App />
for some reason somewhere in my react navigation is causing the issue because <WrapWithProvider><View /></WrapWithProvider>
works just fine, the test passed.
function App() {
return (
<WrapWithProvider>
<NavigationContainer>
<Stack.Navigator>
<Stack.Screen>
{() => <View />}
</Stack.Screen>
</Stack.Navigator>
</NavigationContainer>
</WrapWithProvider>
);
}