Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature]: Allow to pass in history to Router for testing #8342

Closed
dbartholomae opened this issue Nov 15, 2021 · 3 comments
Closed

[Feature]: Allow to pass in history to Router for testing #8342

dbartholomae opened this issue Nov 15, 2021 · 3 comments
Labels

Comments

@dbartholomae
Copy link

What is the new or updated feature that you are suggesting?

In v5 it was possible to pass in history for testing purposes.

Why should this feature be included?

Either allow this again or make another suggestion for how to best test isolated components that navigate away. E. g.:

const HomeButten = () => {
  const navigate = useNavigate()
  return <button onClick={navigate('/')}>Home</button>
}
import { createMemoryHistory } from "history";
import { render } from "@testing-library/react";
import userEvent from "@testing-library/user-event";

test('HomeButton when clicked navigates to root', () => {
  history = createMemoryHistory({ initialEntries: ['/foo'] });
  render(<Router history={mockHistory}><HomeButton /></Router>);
  userEvent.click(screen.getByText("Home"));
  expect(history.location.pathname).toBe("/");
})
@timdorr
Copy link
Member

timdorr commented Nov 15, 2021

See #7586, which will implement this.

This is also essentially a duplicate of #8264, but I'll keep it open since it comes at the problem from the other direction (read vs write)

@midanosi
Copy link

For the time being, see this stack overflow answer to get tests working again in v6: https://stackoverflow.com/a/69859510

@timdorr
Copy link
Member

timdorr commented Dec 10, 2021

This is now possible with HistoryRouter in 6.1.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants