I am trying to write a test where Drawer item goes from invisible to visible. The way I am implementing the visibility switch is by toggling the drawerItemStyle
prop on the Drawer item from display: "none"
to display: "flex"
. This works in an android emulator. However, when I render the Drawer navigator with react native testing library the DrawerItem is present even when the drawerItemStyle prop is set to display: "none"
.
With this:
<DrawerStack.Screen
name="Name"
component={Component}
options={{
title: "Title",
drawerItemStyle: {
display: "none",
},
}}
/>
This test passes:
const { getByText } = render(<DrawerNavigator />);
getByText("Title")
- Is it expected that setting display "none" will still render it in react-test-renderer?
- Is there a better way to toggle the visibility?
- Is there a better way to test the visibility?
Update: Solution Found
I had to do this:
const { container } = render(<Component />);
const drawerItemsProps = container .findAllByType(DrawerItem).map((e: any) => e.props);
const targetProps = drawerItemsProps[drawerItemsProps.findIndex((e: any) => e.label === "Title")];
const { style: { display }, } = targetProps;
expect(display).toEqual('none');