React-router: Request: add example of <Redirect /> unit test to documentation.

Created on 6 Jun 2017  路  2Comments  路  Source: ReactTraining/react-router

Version

4.1.1

I'm trying to unit test a simple component that redirects the user to a location or the login page whether he has permissions or not.
I don't understand how to unit test it. I've asked the internet and stack overflow without success.
My simple component:

const AppContainer = ({ location }) =>
  (isUserAuthenticated()
    ? <AppWithData />
    : <Redirect
        to={{
          pathname: "/login",
          state: { from: location }
        }}
      />);

My attempt of unit test:

```
function setup() {
const enzymeWrapper = mount(


);

return {
enzymeWrapper
};
}

jest.mock("lib/authAPI", () => ({
isUserAuthenticated: jest.fn(() => false)
}));

describe("AppContainer component", () => {
it("renders redirect", () => {
const { enzymeWrapper } = setup();

expect(enzymeWrapper.find("<Redirect />")).toBe(true); // I know it's wrong but it's my best bet

});
});
```

Cheers
Luca

All 2 comments

If you have a documentation suggestion, a PR is really the only way of effectively discussing such things.

@timdorr Creating a PR to document something you cannot get to work doesn't make any sense. The example and explanation is a very clear and concise example, and @duvet86 explained that other avenues of assistance didn't pan out. Can you or another contributor provide some info to help this poor soul out?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ArthurRougier picture ArthurRougier  路  3Comments

ackvf picture ackvf  路  3Comments

Waquo picture Waquo  路  3Comments

misterwilliam picture misterwilliam  路  3Comments

hgezim picture hgezim  路  3Comments