forked from box/box-ui-elements
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwithRouterIfEnabled.test.js
More file actions
55 lines (47 loc) · 2.12 KB
/
withRouterIfEnabled.test.js
File metadata and controls
55 lines (47 loc) · 2.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// @flow
import * as React from 'react';
import { MemoryRouter } from 'react-router-dom';
import { render } from '../../../../test-utils/testing-library';
import withRouterIfEnabled from '../withRouterIfEnabled';
const TestComponent = (props: any) => {
const { history, location, match, routerDisabled } = props;
return (
<div
data-testid="test-component"
data-router-disabled={routerDisabled ? 'true' : undefined}
data-has-history={history ? 'true' : undefined}
data-has-location={location ? 'true' : undefined}
data-has-match={match ? 'true' : undefined}
/>
);
};
TestComponent.displayName = 'TestComponent';
const WithRouterIfEnabled = withRouterIfEnabled(TestComponent);
test('injects router props when wrapped in a Router', () => {
const { getByTestId } = render(
<MemoryRouter initialEntries={[{ pathname: '/foo' }]}>
<WithRouterIfEnabled />
</MemoryRouter>,
);
const el = getByTestId('test-component');
expect(el).toHaveAttribute('data-has-history', 'true');
expect(el).toHaveAttribute('data-has-location', 'true');
expect(el).toHaveAttribute('data-has-match', 'true');
expect(el).not.toHaveAttribute('data-router-disabled');
});
test('renders without Router and without router props (routerDisabled prop)', () => {
const { getByTestId } = render(<WithRouterIfEnabled routerDisabled />);
const el = getByTestId('test-component');
expect(el).not.toHaveAttribute('data-has-history');
expect(el).not.toHaveAttribute('data-has-location');
expect(el).not.toHaveAttribute('data-has-match');
expect(el).toHaveAttribute('data-router-disabled', 'true');
});
test('renders without Router and without router props (feature flag)', () => {
const features = { routerDisabled: { value: true } };
const { getByTestId } = render(<WithRouterIfEnabled features={features} />);
const el = getByTestId('test-component');
expect(el).not.toHaveAttribute('data-has-history');
expect(el).not.toHaveAttribute('data-has-location');
expect(el).not.toHaveAttribute('data-has-match');
});