Trying to learn testing. Using testing-library, Jest, and React-Router v6, and Typescript. I'm trying to figure out how to test a link. I've been looking all over the place for a solution and I can't find one. Using React-Router v6. Code looks like the following (link is just a regular element with an href) just want to make sure the user gets to the new page (in this case the login page from the forgot password page).
//omitted imports but imported all appropriate items from below
describe('ForgotPassword', () => {
test('User can navigate to login screen', async () => {
render(
<MemoryRouter initialEntries={['/forgot-password' ]}>
<ForgotPassword />
</MemoryRouter>)
userEvent.click(screen.getByRole('link', { name: 'Back to Login' }))
await waitFor(() => {
expect(screen.getByRole('heading', { name: 'Login' })).toBeInTheDocument()
})
})
//also tried:
describe('ForgotPassword', () => {
test('User can navigate to login screen', async () => {
render(
<MemoryRouter initialEntries={['/forgot-password' ]}>
<Routes>
<Route path='/forgot-password' component={<ForgotPassword />} />
<Route path='/login' component={<Login />} />
<Routes>
</MemoryRouter>)
userEvent.click(screen.getByRole('link', { name: 'Back to Login' }))
await waitFor(() => {
expect(screen.getByRole('heading', { name: 'Login' })).toBeInTheDocument()
})
})
//also tried the following:
const history = createMemoryHistory({ initialEntries: ['/home'] });
const { getByText } = render(
<Router history={history}>
<ButtonLogin />
</Router>
);
got a TS error: Property 'history' does not exist on type 'IntrinsicAttributes & RouterProps'.
//also tried using fireEvent instead of userEvent