lundi 30 novembre 2020

How do you integrate the log in status in your react test?

I have a React App with protected routes for example /post which I access via the 'link' with name: go to post. Now I am testing this route with:

test('Post links to the correct route [registered]', () => {
        render(<MemoryRouter><App /></MemoryRouter>);
        const link = screen.getByRole('link', { name: /go to post/i });
        userEvent.click(link);
        expect(screen.getByRole('heading', { name: /log in/i })).toBeInTheDocument();

to see if it redirects me to the /login page. It does, but I am wondering what is the best set up for a test with the log in status true for this route? Do I just sign up in the beginning of the test() function?

Aucun commentaire:

Enregistrer un commentaire