i start to learning react testing library a few days ago and i dont know how to make my tests works with react router v6. I have this two test below:
describe('testing login page', () => {
test('veryfy inputs and buttons in login page', () => {
render(
<MemoryRouter initialEntries={['/']}>
<Routes>
<Route path="/" element={<LoginPage />} />
</Routes>
</MemoryRouter>,
);
const userInput = screen.getByRole('textbox');
expect(userInput).toBeInTheDocument();
describe('testing Mine Page', () => {
test('check table components', () => {
render(
<MemoryRouter initialEntries={['/mina']}>
<Routes>
<Route path="/mina" element={<MineTable />} />
</Routes>
</MemoryRouter>,
);
const pageTitle = screen.getByRole('heading', { name: /mina/i });
expect(pageTitle).toBeInTheDocument();
});
Only the first test works, the second don't render the page MineTable, keeps rendering the last page Login. It's like the history of the first test render remains. Someone can help me? I want to test every page component of my application.
MineTablealone?MineTablecomponent have to do with any other test rendering theLoginPageor any other component?RoutesandRoutecomponents. You are using RTL so you can provide a wrapper to therendertest utility. Would you like an example?