React Router v4 Redirect unit test Basically I’m making a shallow render of my component and verifying that if authenticated is rendering the redirect component otherwise the App one.
Here is the code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | function setup() { const enzymeWrapper = shallow(<AuthenticatedApp />); return { enzymeWrapper }; } describe("AuthenticatedApp component", () => { it("renders Redirect when user NOT autheticated", () => { authApi.isUserAuthenticated = jest.fn(() => false); const { enzymeWrapper } = setup(); expect(enzymeWrapper.find(Redirect)).toHaveLength(1); }); it("renders AppWithData when user autheticated", () => { authApi.isUserAuthenticated = jest.fn(() => true); const { enzymeWrapper } = setup(); expect(enzymeWrapper.find(AppWithData)).toHaveLength(1); }); }); |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.