I am trying to mock useSearchParams
using vitest
. I have tried a couple of methods but couldn’t find a solution.
The sample code can be found here:
https://codesandbox.io/p/sandbox/laughing-bas-m7ygo8
import { useEffect, useState } from "react";
import { useSearchParams } from "react-router-dom";
function App() {
const [searchParams, setSearchParams] = useSearchParams();
const [search, setSearch] = useState("");
const [direction, setDirection] = useState("");
const sortParams = searchParams.get("sortBy");
useEffect(() => {
setDirection(sortParams || "");
}, [sortParams]);
const handleSearchChange = (event: React.ChangeEvent<HTMLInputElement>) => {
const val = event.target.value;
setSearch(val);
setSearchParams((prev) => {
prev.set("search", val);
return prev;
});
};
const handleSortChange = (val: string) => {
setDirection(val);
setSearchParams((prev) => {
prev.set("sortBy", val);
return prev;
});
};
return (
<div>
<p>Hello</p>
<input type="search" value={search} onChange={handleSearchChange} />
<button onClick={() => handleSortChange("asc")}>Sort</button>
{direction}
</div>
);
}
export default App;
import { render, screen } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { describe, expect, it, vi } from "vitest";
import { useSearchParams } from "react-router-dom";
import App from "./App";
vi.mock("react-router-dom", () => ({
useSearchParams: vi.fn(),
}));
describe("Simple working test", () => {
it("url should update when user clicks sort button", () => {
render(<App />);
const button = screen.getByRole("button");
userEvent.click(button);
expect(screen.getByText(/Hello/i)).toBeInTheDocument();
});
});
Can anyone please help me here? Thanks 🙂
3
Answers
Remove the mock :
Instead use:
render(<App />, { wrapper: BrowserRouter });
You should not mock
useSearchParams
. Instead, wrap your component in aMemoryRouter
for testing:If you know what you doing and there is no other way for you to test it, try to mock it like this:
And then you can test against
mockUseSearchParams
.For example: