Skip to content

Commit 6311e90

Browse files
feat(router): withRouterIfEnabled - fixed tests, made sure history is treated as optional in affected components
1 parent 875194d commit 6311e90

File tree

5 files changed

+51
-61
lines changed

5 files changed

+51
-61
lines changed

src/elements/common/routing/__tests__/withRouterIfEnabled.test.js

Lines changed: 44 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -4,62 +4,52 @@ import { MemoryRouter } from 'react-router-dom';
44
import { render } from '../../../../test-utils/testing-library';
55
import withRouterIfEnabled from '../withRouterIfEnabled';
66

7-
describe('elements/common/routing/withRouterIfEnabled', () => {
8-
const TestComponent = (props: any) => {
9-
const { history, location, match, routerDisabled } = props;
10-
return (
11-
// Expose which props were injected
12-
<div
13-
data-testid="test-component"
14-
data-router-disabled={routerDisabled ? 'true' : undefined}
15-
data-has-history={history ? 'true' : undefined}
16-
data-has-location={location ? 'true' : undefined}
17-
data-has-match={match ? 'true' : undefined}
18-
/>
19-
);
20-
};
21-
TestComponent.displayName = 'TestComponent';
22-
23-
const WithRouterIfEnabled = withRouterIfEnabled(TestComponent);
24-
25-
describe('router enabled (default)', () => {
26-
test('injects router props when wrapped in a Router', () => {
27-
const { getByTestId } = render(
28-
<MemoryRouter initialEntries={[{ pathname: '/foo' }]}>
29-
<WithRouterIfEnabled />
30-
</MemoryRouter>,
31-
);
32-
33-
const el = getByTestId('test-component');
34-
expect(el).toHaveAttribute('data-has-history', 'true');
35-
expect(el).toHaveAttribute('data-has-location', 'true');
36-
expect(el).toHaveAttribute('data-has-match', 'true');
37-
expect(el).not.toHaveAttribute('data-router-disabled');
38-
});
39-
});
7+
const TestComponent = (props: any) => {
8+
const { history, location, match, routerDisabled } = props;
9+
return (
10+
<div
11+
data-testid="test-component"
12+
data-router-disabled={routerDisabled ? 'true' : undefined}
13+
data-has-history={history ? 'true' : undefined}
14+
data-has-location={location ? 'true' : undefined}
15+
data-has-match={match ? 'true' : undefined}
16+
/>
17+
);
18+
};
19+
TestComponent.displayName = 'TestComponent';
20+
21+
const WithRouterIfEnabled = withRouterIfEnabled(TestComponent);
22+
23+
test('injects router props when wrapped in a Router', () => {
24+
const { getByTestId } = render(
25+
<MemoryRouter initialEntries={[{ pathname: '/foo' }]}>
26+
<WithRouterIfEnabled />
27+
</MemoryRouter>,
28+
);
29+
30+
const el = getByTestId('test-component');
31+
expect(el).toHaveAttribute('data-has-history', 'true');
32+
expect(el).toHaveAttribute('data-has-location', 'true');
33+
expect(el).toHaveAttribute('data-has-match', 'true');
34+
expect(el).not.toHaveAttribute('data-router-disabled');
35+
});
4036

41-
describe('router disabled via prop', () => {
42-
test('renders without Router and without router props', () => {
43-
const { getByTestId } = render(<WithRouterIfEnabled routerDisabled />);
44-
const el = getByTestId('test-component');
45-
expect(el).not.toHaveAttribute('data-has-history');
46-
expect(el).not.toHaveAttribute('data-has-location');
47-
expect(el).not.toHaveAttribute('data-has-match');
48-
expect(el).toHaveAttribute('data-router-disabled', 'true');
49-
});
50-
});
37+
test('renders without Router and without router props (routerDisabled prop)', () => {
38+
const { getByTestId } = render(<WithRouterIfEnabled routerDisabled />);
39+
const el = getByTestId('test-component');
40+
expect(el).not.toHaveAttribute('data-has-history');
41+
expect(el).not.toHaveAttribute('data-has-location');
42+
expect(el).not.toHaveAttribute('data-has-match');
43+
expect(el).toHaveAttribute('data-router-disabled', 'true');
44+
});
5145

52-
describe('router disabled via feature flag', () => {
53-
test('renders without Router and without router props when feature is enabled', () => {
54-
const { getByTestId } = render(<WithRouterIfEnabled />, {
55-
wrapperProps: { features: { routerDisabled: { value: true } } },
56-
});
46+
test('renders without Router and without router props (feature flag)', () => {
47+
const features = { routerDisabled: { value: true } };
48+
const { getByTestId } = render(<WithRouterIfEnabled features={features} />);
5749

58-
const el = getByTestId('test-component');
59-
expect(el).not.toHaveAttribute('data-has-history');
60-
expect(el).not.toHaveAttribute('data-has-location');
61-
expect(el).not.toHaveAttribute('data-has-match');
62-
});
63-
});
50+
const el = getByTestId('test-component');
51+
expect(el).not.toHaveAttribute('data-has-history');
52+
expect(el).not.toHaveAttribute('data-has-location');
53+
expect(el).not.toHaveAttribute('data-has-match');
6454
});
6555

src/elements/common/routing/withRouterIfEnabled.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ import { withRouter } from 'react-router-dom';
44
import { isFeatureEnabled } from '../feature-checking';
55

66
export default function withRouterIfEnabled(Wrapped: React.ComponentType<any>) {
7-
const WrappedWithRouter = withRouter(Wrapped);
87

98
const WithRouterIfEnabled = (props: any) => {
109
const routerDisabled =
1110
props?.routerDisabled === true || isFeatureEnabled(props?.features, 'routerDisabled.value');
1211

13-
const Component = routerDisabled ? Wrapped : WrappedWithRouter;
12+
const Component = routerDisabled ? Wrapped : withRouter(Wrapped);
13+
1414
return <Component {...props} />;
1515
};
1616

src/elements/content-sidebar/AddTaskButton.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import type { ElementsXhrError } from '../../common/types/api';
1212
import type { InternalSidebarNavigation, InternalSidebarNavigationHandler } from '../common/types/SidebarNavigation';
1313

1414
type Props = {|
15-
history: RouterHistory,
15+
history?: RouterHistory,
1616
internalSidebarNavigation?: InternalSidebarNavigation,
1717
internalSidebarNavigationHandler?: InternalSidebarNavigationHandler,
1818
isDisabled: boolean,
@@ -55,7 +55,7 @@ class AddTaskButton extends React.Component<Props, State> {
5555
},
5656
true,
5757
);
58-
} else {
58+
} else if (history) {
5959
history.replace({ state: { open: true } });
6060
}
6161

src/elements/content-sidebar/__tests__/__snapshots__/ActivitySidebar.test.js.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ exports[`elements/content-sidebar/ActivitySidebar render() should render the act
44
<SidebarContent
55
actions={
66
<React.Fragment>
7-
<withRouter(AddTaskButton)
7+
<withRouterIfEnabled(AddTaskButton)
88
isDisabled={false}
99
onTaskModalClose={[Function]}
1010
taskFormProps={

src/elements/content-sidebar/versions/VersionsSidebarContainer.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ type Props = {
3737
features: FeatureConfig,
3838
fileId: string,
3939
hasSidebarInitialized?: boolean,
40-
history: RouterHistory,
40+
history?: RouterHistory,
4141
internalSidebarNavigation?: InternalSidebarNavigation,
4242
internalSidebarNavigationHandler?: InternalSidebarNavigationHandler,
4343
match: Match,
@@ -284,7 +284,7 @@ class VersionsSidebarContainer extends React.Component<Props, State> {
284284
delete navigationUpdate.versionId;
285285
}
286286
internalSidebarNavigationHandler(navigationUpdate);
287-
} else {
287+
} else if (history) {
288288
history.push(generatePath(match.path, { ...match.params, versionId }));
289289
}
290290
};

0 commit comments

Comments
 (0)