|
1 | 1 | import * as React from 'react'; |
2 | | -import { shallow } from 'enzyme'; |
3 | 2 | import { createMemoryHistory } from 'history'; |
| 3 | +import { render, screen } from '@testing-library/react'; |
4 | 4 | import NavRouter from '../NavRouter'; |
5 | 5 | import withNavRouter from '../withNavRouter'; |
6 | | -import { WithNavRouterProps } from '../types'; |
7 | 6 |
|
8 | 7 | type Props = { |
9 | 8 | value?: string; |
10 | 9 | }; |
11 | 10 |
|
12 | | -describe('src/eleemnts/common/nav-router/withNavRouter', () => { |
13 | | - const TestComponent = ({ value }: Props) => <div>{`Test ${value}`}</div>; |
| 11 | +describe('src/elements/common/nav-router/withNavRouter', () => { |
| 12 | + const TestComponent = ({ value, location }: Props & { location?: { pathname: string } }) => ( |
| 13 | + <div>{`Test ${value} ${location?.pathname || ''}`}</div> |
| 14 | + ); |
14 | 15 | const WrappedComponent = withNavRouter(TestComponent); |
15 | 16 |
|
16 | | - const getWrapper = (props?: Props & WithNavRouterProps) => shallow(<WrappedComponent {...props} />); |
17 | | - |
18 | | - test('should wrap component with NavRouter', () => { |
19 | | - const wrapper = getWrapper(); |
20 | | - |
21 | | - expect(wrapper.find(NavRouter)).toBeTruthy(); |
22 | | - expect(wrapper.find(TestComponent)).toBeTruthy(); |
23 | | - }); |
24 | | - |
25 | | - test('should provide the appropriate props to NavRouter and the wrapped component', () => { |
| 17 | + const renderComponent = (props?: Props) => { |
26 | 18 | const history = createMemoryHistory(); |
27 | | - const initialEntries = ['foo']; |
28 | | - const value = 'foo'; |
29 | | - const wrapper = getWrapper({ |
30 | | - history, |
31 | | - initialEntries, |
32 | | - value, |
33 | | - }); |
34 | | - |
35 | | - const navRouter = wrapper.find(NavRouter); |
36 | | - expect(navRouter.prop('history')).toEqual(history); |
37 | | - expect(navRouter.prop('initialEntries')).toEqual(initialEntries); |
| 19 | + return render( |
| 20 | + <NavRouter history={history}> |
| 21 | + <WrappedComponent {...props} /> |
| 22 | + </NavRouter>, |
| 23 | + ); |
| 24 | + }; |
| 25 | + |
| 26 | + test('should render wrapped component with router context', () => { |
| 27 | + renderComponent({ value: 'test' }); |
| 28 | + expect(screen.getByText(/Test test/)).toBeInTheDocument(); |
| 29 | + }); |
38 | 30 |
|
39 | | - expect(wrapper.find(TestComponent).prop('value')).toEqual(value); |
| 31 | + test('should provide router props to wrapped component', () => { |
| 32 | + const history = createMemoryHistory({ initialEntries: ['/test-path'] }); |
| 33 | + render( |
| 34 | + <NavRouter history={history}> |
| 35 | + <WrappedComponent value="test" /> |
| 36 | + </NavRouter>, |
| 37 | + ); |
| 38 | + expect(screen.getByText(/Test test \/test-path/)).toBeInTheDocument(); |
40 | 39 | }); |
41 | 40 | }); |
0 commit comments