如何在響應路由器v4中測試組件? 我嘗試使用jest和酶對重定向進行單元測試時失敗。React Router v4重定向單元測試
我的組件:
const AppContainer = ({ location }) =>
(isUserAuthenticated()
? <AppWithData />
: <Redirect
to={{
pathname: "/login",
state: { from: location }
}}
/>);
我試圖對它進行測試:
function setup() {
const enzymeWrapper = mount(
<MemoryRouter initialEntries={["/"]}>
<AppContainer />
</MemoryRouter>
);
return {
enzymeWrapper
};
}
jest.mock("lib/authAPI",() => ({
isUserAuthenticated: jest.fn(() => false)
}));
describe("AppContainer component",() => {
it("renders redirect",() => {
const { enzymeWrapper } = setup();
expect(enzymeWrapper.find("<Redirect></Redirect>")).toBe(true);
});
});