craigmiller160
craigmiller160

Reputation: 6263

Having trouble testing routes with React Router/Jest/Enzyme

So I've been trying to figure out how to write unit tests for a component of mine that conditionally renders components using React Router. My problem has been that I have never been able to get it to render anything other than the root route, no matter what I do.

To further investigate this, I put together a small piece of test code, which I'm including here:

it('testing', () => {
        const component = mount(
            <MemoryRouter initalEntries={ [ '/next' ] }>
                <Switch>
                    <Route path="/next" render={ () => <p>Next</p> } />
                    <Route path="/" exact render={ () => <p>Root</p> } />
                </Switch>
            </MemoryRouter>
        );

        console.log(component.debug());
    });

So, my expectation would be that this code would render <p>Next</p>, because I'm hitting the /next route. However, it will only ever render <p>Root</p>.

Upvotes: 0

Views: 606

Answers (1)

craigmiller160
craigmiller160

Reputation: 6263

So it turns out I misspelled "initialEntries", and that as the whole problem.

Upvotes: 3

Related Questions