[英]React Router V6 - useNavigate() may be used only in the context of a <Router> component
I have installed react-router-domV6.我已经安装了 react-router-domV6。 I am having the above error when I run my very basic test using Jest-Enzyme:
当我使用 Jest-Enzyme 运行我的非常基本的测试时,我遇到了上述错误:
expect(shallow(<CustomerListTable customers={mockCustomers} />).length).toEqual(1);
I already came across this similar issue mentioned here , but I am already doing the provided answers.我已经遇到过这里提到的类似问题,但我已经在做提供的答案。 Even tried doing the
import { BrowserRouter as Router } from "react-router-dom";
甚至尝试
import { BrowserRouter as Router } from "react-router-dom";
. . What am I missing here?
我在这里想念什么?
index.js index.js
import React, { Suspense } from "react";
import ReactDOM from "react-dom";
import App from "./App";
import { StrictMode } from "react";
import { BrowserRouter } from "react-router-dom";
...
ReactDOM.render(
<StrictMode>
...
<BrowserRouter>
<AuthProvider>
<App />
</AuthProvider>
</BrowserRouter>
...
</StrictMode>,
document.getElementById("root")
);
App.js应用程序.js
import React from "react";
import { useRoutes } from "react-router-dom";
import Routing from "./routes";
import useAuth from "./hooks/useAuth";
import { CreateCustomTheme } from "./theme";
import { ThemeProvider } from "@mui/material/styles";
import CssBaseline from "@mui/material/CssBaseline";
import useSettings from "./hooks/useSettings";
import useScrollReset from "./hooks/useScrollReset";
import SplashScreen from "./components/splashScreen/SplashScreen";
const App = () => {
const content = useRoutes(Routing());
const { settings } = useSettings();
const auth = useAuth();
useScrollReset();
const theme = CreateCustomTheme({
direction: settings.direction,
responsiveFontSizes: settings.responsiveFontSizes,
roundedCorners: settings.roundedCorners,
theme: settings.theme,
});
return (
<ThemeProvider theme={theme}>
<CssBaseline />
{auth.isInitialized ? content : <SplashScreen />}
</ThemeProvider>
);
};
export default App;
It seems the CustomerListTable
component you are testing in isolation doesn't have access to a routing context it's expecting as when it's rendered in the app normally.您正在隔离测试的
CustomerListTable
组件似乎无法访问它所期望的路由上下文,就像它在应用程序中正常呈现时一样。 It's completely normal to need to wrap components when testing them with providers (redux, themes/styling, localization, routing , etc...) that are providing specific React Context values the component is accessing, typically via React hooks.使用提供组件正在访问的特定 React Context 值的提供程序(redux、主题/样式、本地化、路由等)测试组件时,需要包装组件是完全正常的,通常通过 React 挂钩。
For the unit test you should wrap the component with the providers it's expecting to have.对于单元测试,您应该将组件与它期望的提供者一起包装。
expect(shallow(
<BrowserRouter>
<CustomerListTable customers={mockCustomers} />
</BrowserRouter>
).length).toEqual(1);
It's been a long time since I've used Enzyme, so I'm not certain the shallow
render will still work, but if it doesn't then swap to the full mount
test renderer.自从我使用 Enzyme 已经很长时间了,所以我不确定
shallow
渲染是否仍然有效,但如果它不起作用,则切换到全mount
测试渲染器。
To fix this issue, you can direct mock your useNavigate() like this before all the imports for the file in which you are writing the test case:-要解决此问题,您可以在编写测试用例的文件的所有导入之前直接模拟您的 useNavigate() :-
const mockedUsedNavigate = jest.fn();
jest.mock('react-router-dom', () => ({
...jest.requireActual('react-router-dom'),
useNavigate: () => mockedUsedNavigate,
}));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.