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")
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');
This is basically how i got it working in the end:
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');