[英]how to write jest tests that use useAuthContext
Hi I'm not familiar with react but I want to write jest unit tests that use useAuthContext, it was widely used in modules made by my teammates.嗨,我不熟悉反应,但我想编写使用 useAuthContext 的开玩笑单元测试,它被广泛用于我的队友制作的模块中。 But each time I tried to render anything that uses useAuthContext.js I cannot render and it throwing me error of this error
但是每次我尝试渲染任何使用 useAuthContext.js 的东西时,我都无法渲染,它会抛出这个错误
I wonder if there is anyway that I pass this context or how can i write tests for the rendering?我想知道我是否通过了这个上下文或者我如何为渲染编写测试? I tried several solutions but none seem to work well.
我尝试了几种解决方案,但似乎没有一个效果很好。 Any help is appreciated, thanks!
任何帮助表示赞赏,谢谢!
App.test.js应用程序测试.js
import React from "react";
import { render, screen } from "@testing-library/react";
import App from "./App";
import ReactDOM from 'react-dom';
import ShallowRenderer from 'react-shallow-renderer';
let realUseContext;
let useContextMock;
// Setup mock
beforeEach(() => {
realUseContext = React.useContext;
useContextMock = React.useContext = jest.fn();
});
// Cleanup mock
afterEach(() => {
React.useContext = realUseContext;
});
test("mock hook", () => {
const element = new ShallowRenderer().render(
<App />
);
});
test("render without failure", () => {
const div = document.createElement("div");
ReactDOM.render(<App />, div);
});
test("renders the nav bar", () => {
render(<App />);
const navbar = screen.getByTestId("navbar");
expect(navbar).toBeInTheDocument();
});
App.js应用程序.js
import { BrowserRouter, Routes, Route, Navigate } from 'react-router-dom'
import { useAuthContext } from './hooks/useAuthContext'
// pages & components
import Home from './pages/Home'
import Login from './pages/Login'
import Signup from './pages/Signup'
import LostPassword from './pages/LostPassword'
import WaitTimes from './pages/WaitTimes'
import Navbar from './components/Navbar'
function App() {
const { user } = useAuthContext()
return (
<div className="App">
<BrowserRouter>
<Navbar />
<div className="pages">
<Routes>
<Route
path="/"
element={user ? <Home /> : <Navigate to="/login" />}
/>
<Route
path="/login"
element={!user ? <Login /> : <Navigate to="/" />}
/>
<Route
path="/signup"
element={!user ? <Signup /> : <Navigate to="/" />}
/>
<Route
path="/lost"
element={!user ? <LostPassword /> : <Navigate to="/" />}
/>
<Route
path="/wait"
element={!user ? <WaitTimes /> : <Navigate to="/" />}
/>
</Routes>
</div>
</BrowserRouter>
</div>
);
}
export default App;
useAuthContext.js使用AuthContext.js
import { AuthContext } from "../context/AuthContext"
import { useContext } from "react"
export const useAuthContext = () => {
const context = useContext(AuthContext)
if(!context) {
throw Error('useAuthContext must be used inside an AuthContextProvider')
}
return context
}
According to https://reactjs.org/docs/context.html#contextprovider you need to wrap and search on AuthContextProvider
on the app.根据https://reactjs.org/docs/context.html#contextprovider你需要在应用程序上包装和搜索
AuthContextProvider
。 It will be similar to the below just need to fill the value.它会类似于下面只需要填写值。
render(
<AuthContextProvider value={}>
<App />
</AuthContextProvider>
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.