Skip to content

Commit 901c23d

Browse files
jstoffanmergify[bot]
authored andcommitted
feat(versions): Add resin tracking for sidebar back button (#1309)
1 parent 0f810e4 commit 901c23d

File tree

6 files changed

+112
-47
lines changed

6 files changed

+112
-47
lines changed

src/elements/common/nav-button/BackButton.js

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,35 @@
55
*/
66

77
import * as React from 'react';
8+
import classNames from 'classnames';
89
import { FormattedMessage } from 'react-intl';
9-
import { withRouter } from 'react-router-dom';
10-
import type { RouterHistory } from 'react-router-dom';
10+
import { Route, type Location } from 'react-router-dom';
1111
import IconNavigateLeft from '../../../icons/general/IconNavigateLeft';
1212
import messages from '../messages';
1313
import PlainButton from '../../../components/plain-button';
1414
import './BackButton.scss';
1515

1616
type Props = {
17-
history: RouterHistory,
18-
path?: string,
17+
className?: string,
18+
to?: Location,
1919
};
2020

21-
const BackButton = ({ history, path }: Props) => (
22-
<PlainButton
23-
className="bdl-BackButton"
24-
onClick={() => (path ? history.push(path) : history.goBack())}
25-
type="button"
26-
>
27-
<IconNavigateLeft height={24} width={24} />
28-
<span className="accessibility-hidden">
29-
<FormattedMessage {...messages.back} />
30-
</span>
31-
</PlainButton>
21+
const BackButton = ({ className, to, ...rest }: Props) => (
22+
<Route>
23+
{({ history }) => (
24+
<PlainButton
25+
className={classNames('bdl-BackButton', className)}
26+
onClick={() => (to ? history.push(to) : history.goBack())}
27+
type="button"
28+
{...rest}
29+
>
30+
<IconNavigateLeft height={24} width={24} />
31+
<span className="accessibility-hidden">
32+
<FormattedMessage {...messages.back} />
33+
</span>
34+
</PlainButton>
35+
)}
36+
</Route>
3237
);
3338

34-
export default withRouter(BackButton);
39+
export default BackButton;
Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,43 @@
11
import React from 'react';
2-
import { shallow } from 'enzyme';
2+
import { MemoryRouter, Router } from 'react-router-dom';
3+
import { mount } from 'enzyme';
34
import { BackButton } from '..';
45

5-
jest.mock('react-router-dom', () => ({
6-
withRouter: Component => Component,
7-
}));
8-
96
describe('elements/common/nav-button/BackButton', () => {
10-
const history = { goBack: jest.fn() };
11-
const getWrapper = () => shallow(<BackButton history={history} />);
7+
const getWrapper = (props = {}) =>
8+
mount(
9+
<MemoryRouter initialEntries={['/start', '/test']}>
10+
<BackButton {...props} />
11+
</MemoryRouter>,
12+
);
13+
const getHistory = wrapper => wrapper.find(Router).prop('history');
1214

1315
test('should match its snapshot', () => {
1416
const wrapper = getWrapper();
17+
const button = wrapper.find(BackButton).first();
1518

16-
expect(wrapper).toMatchSnapshot();
19+
expect(button).toMatchSnapshot();
1720
});
1821

19-
test('should call history back on click', () => {
22+
test('should call history back on click if no path is defined', () => {
2023
const wrapper = getWrapper();
24+
const history = getHistory(wrapper);
25+
26+
history.goBack = jest.fn();
2127

2228
wrapper.simulate('click');
2329

2430
expect(history.goBack).toHaveBeenCalled();
2531
});
32+
33+
test('should call history.push on click if a path is defined', () => {
34+
const wrapper = getWrapper({ to: '/new' });
35+
const history = getHistory(wrapper);
36+
37+
history.push = jest.fn();
38+
39+
wrapper.simulate('click');
40+
41+
expect(history.push).toHaveBeenCalledWith('/new');
42+
});
2643
});
Lines changed: 59 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,63 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

33
exports[`elements/common/nav-button/BackButton should match its snapshot 1`] = `
4-
<PlainButton
5-
className="bdl-BackButton"
6-
onClick={[Function]}
7-
type="button"
8-
>
9-
<IconNavigateLeft
10-
height={24}
11-
width={24}
12-
/>
13-
<span
14-
className="accessibility-hidden"
15-
>
16-
<FormattedMessage
17-
defaultMessage="Back"
18-
id="be.back"
19-
/>
20-
</span>
21-
</PlainButton>
4+
<BackButton>
5+
<Route>
6+
<PlainButton
7+
className="bdl-BackButton"
8+
onClick={[Function]}
9+
type="button"
10+
>
11+
<button
12+
className="btn-plain bdl-BackButton"
13+
onClick={[Function]}
14+
type="button"
15+
>
16+
<IconNavigateLeft
17+
height={24}
18+
width={24}
19+
>
20+
<AccessibleSVG
21+
className="icon-navigate-left "
22+
focusable="false"
23+
height={24}
24+
viewBox="0 0 48 48"
25+
width={24}
26+
>
27+
<svg
28+
className="icon-navigate-left "
29+
focusable="false"
30+
height={24}
31+
role="presentation"
32+
viewBox="0 0 48 48"
33+
width={24}
34+
>
35+
<path
36+
d="M30.8,33.2L21.7,24l9.2-9.2L28,12L16,24l12,12L30.8,33.2z"
37+
fill="#494949"
38+
stroke="#DCDCDC"
39+
strokeMiterlimit="10"
40+
/>
41+
<path
42+
d="M0,0h48v48H0V0z"
43+
display="none"
44+
fill="none"
45+
/>
46+
</svg>
47+
</AccessibleSVG>
48+
</IconNavigateLeft>
49+
<span
50+
className="accessibility-hidden"
51+
>
52+
<FormattedMessage
53+
defaultMessage="Back"
54+
id="be.back"
55+
>
56+
<div />
57+
</FormattedMessage>
58+
</span>
59+
</button>
60+
</PlainButton>
61+
</Route>
62+
</BackButton>
2263
`;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ const VersionsSidebar = ({ error, isLoading, parentName, ...rest }: Props) => (
3636
data-resin-feature="versions"
3737
title={
3838
<React.Fragment>
39-
<BackButton path={`/${parentName}`} />
39+
<BackButton data-resin-target="back" to={`/${parentName}`} />
4040
<FormattedMessage {...messages.versionsTitle} />
4141
</React.Fragment>
4242
}

src/elements/content-sidebar/versions/__tests__/VersionsSidebar-test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ jest.mock('../../../common/nav-button', () => ({
99
}));
1010

1111
describe('elements/content-sidebar/versions/VersionsSidebar', () => {
12-
const getWrapper = (props = {}) => shallow(<VersionsSidebar {...props} />);
12+
const getWrapper = (props = {}) => shallow(<VersionsSidebar parentName="activity" {...props} />);
1313

1414
describe('render', () => {
1515
test('should show the versions list if no error prop is provided', () => {

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ exports[`elements/content-sidebar/versions/VersionsSidebar render should show an
88
title={
99
<React.Fragment>
1010
<BackButton
11-
path="/undefined"
11+
data-resin-target="back"
12+
to="/activity"
1213
/>
1314
<FormattedMessage
1415
defaultMessage="Version History"
@@ -44,7 +45,8 @@ exports[`elements/content-sidebar/versions/VersionsSidebar render should show th
4445
title={
4546
<React.Fragment>
4647
<BackButton
47-
path="/undefined"
48+
data-resin-target="back"
49+
to="/activity"
4850
/>
4951
<FormattedMessage
5052
defaultMessage="Version History"

0 commit comments

Comments
 (0)