How to set correspondence path with MemoryRouter and Jest? (no place or story)

please forgive me if there is a duplicate.

I know that MemoryRouter has initialEntries and initialIndex, so you can set the path, etc. for "location" and "history". However, “match” is not updated ... I need to install “match” for my Jest application applications and tests.

When i try

<MemoryRouter initialEntries={['/hello']} initialIndex={0}>
      <Hello store={store} />
</MemoryRouter>

I get

match: { path: '/', url: '/', params: {} ... },
location: { path: '/hello', pathname: '/', ... },
history: { ..., location: { path: '/hello', pathname: '/', ... }}

I wonder if there is a way to establish a match. Thanks in advance.

+9
source share
2 answers

You can wrap your component with a <Hello store={store} />component <Route ... />, for example:

import React from 'react';
import { MemoryRouter, Route } from 'react-router-dom';

// later in tests

<MemoryRouter initialEntries={['/home']}>      
  <Route component={props => <HomeComponent {...props} />} path="/home" /> 
</MemoryRouter>

match props.

+12

, :

const props = {
location: { pathname: 'https://products/1' }
}

beforeAll(() => {
    store = mockStore(initialState);
    wrapper = mount(<Provider store={store}>
    <Router >
        <ProductDetails {...props} />
    </Router>
</Provider>)
})
0

Source: https://habr.com/ru/post/1683348/


All Articles