Issue with testing react native navigation

Hello,

I have a problem with my tests in react-native with Jest and React native testing library.

I have a react native application built with expo with a Home screen with one button. Using a stack navigator, this button can enable users to access Screen1. I want to test the following scenario :
“When the user clicks on the “go to screen1” button, he is sent to Screen1.”

So I did this :

it(
  'should take user to Screen1 when go-to-screen1 button is pressed',
  async () => {
    const navigate = jest.fn();
    const { getByTestId } = render(<HomeScreen navigation={{ navigate }} />);
    fireEvent.press(getByTestId('go-to-screen1-btn'));
    waitFor(() => expect(getByTestId('screen1')).toBeTruthy());
  },
);

this test succeeds. So in order to check, I made this test fail :

it(
  'should take user to Screen1 when go-to-screen1 button is pressed',
  async () => {
    const navigate = jest.fn();
    const { getByTestId } = render(<HomeScreen navigation={{ navigate }} />);
    fireEvent.press(getByTestId('go-to-screen1-btn'));
    waitFor(() => expect(getByTestId('go-to-screen1-btn')).toBeTruthy());
  },
);

This should fail as the button to go to Screen1 is not on Screen1. But it succeeds.

Here is a github repo with a react native app which reproduces the issue I describe : here.

Can someone help me with this please ? :pray:

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.