Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions src/elements/content-sidebar/SidebarPanels.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import * as React from 'react';
import flow from 'lodash/flow';
import { Redirect, Route, Switch } from 'react-router-dom';
import { matchPath, Redirect, Route, Switch, type Location } from 'react-router-dom';
import SidebarUtils from './SidebarUtils';
import withSidebarAnnotations from './withSidebarAnnotations';
import { withAnnotatorContext } from '../common/annotator-context';
Expand Down Expand Up @@ -47,6 +47,7 @@ type Props = {
hasSkills: boolean,
hasVersions: boolean,
isOpen: boolean,
location: Location,
metadataSidebarProps: MetadataSidebarProps,
onAnnotationSelect?: Function,
onVersionChange?: Function,
Expand Down Expand Up @@ -87,6 +88,8 @@ const LoadableVersionsSidebar = SidebarUtils.getAsyncSidebarContent(
MARK_NAME_JS_LOADING_VERSIONS,
);

const SIDEBAR_PATH_VERSIONS = '/:sidebar(activity|details)/versions/:versionId?';

class SidebarPanels extends React.Component<Props, State> {
activitySidebar: ElementRefType = React.createRef();

Expand All @@ -102,6 +105,21 @@ class SidebarPanels extends React.Component<Props, State> {
this.setState({ isInitialized: true });
}

componentDidUpdate(prevProps: Props): void {
const { location, onVersionChange } = this.props;
const { location: prevLocation } = prevProps;

// Reset the current version id if the wrapping versions route is no longer active
if (onVersionChange && this.getVersionsMatchPath(prevLocation) && !this.getVersionsMatchPath(location)) {
onVersionChange(null);
}
}

getVersionsMatchPath = (location: Location) => {
const { pathname } = location;
return matchPath(pathname, SIDEBAR_PATH_VERSIONS);
};

/**
* Refreshes the contents of the active sidebar
* @returns {void}
Expand Down Expand Up @@ -250,7 +268,7 @@ class SidebarPanels extends React.Component<Props, State> {
)}
{hasVersions && (
<Route
path="/:sidebar(activity|details)/versions/:versionId?"
path={SIDEBAR_PATH_VERSIONS}
render={({ match }) => (
<LoadableVersionsSidebar
fileId={fileId}
Expand Down
63 changes: 51 additions & 12 deletions src/elements/content-sidebar/__tests__/SidebarPanels.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,23 @@ jest.mock('../SidebarUtils');
describe('elements/content-sidebar/SidebarPanels', () => {
const getWrapper = ({ path = '/', ...rest } = {}) =>
mount(
<MemoryRouter initialEntries={[path]} keyLength={0}>
<SidebarPanels
file={{ id: '1234' }}
hasActivity
hasDetails
hasMetadata
hasSkills
hasVersions
isOpen
{...rest}
/>
</MemoryRouter>,
<SidebarPanels
file={{ id: '1234' }}
hasActivity
hasDetails
hasMetadata
hasSkills
hasVersions
isOpen
{...rest}
/>,
{

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Curious, what was the reason for this change?

@Machoper Machoper Aug 10, 2023

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

setProps() can only be called on the root, which was MemoryRouter in the previous setup. So, this is more of a workaround to call setProps() on SidebarPanels instead and trigger componentDidUpdate().

wrappingComponent: MemoryRouter,
wrappingComponentProps: {
initialEntries: [path],
keyLength: 0,
},
},
);

describe('render', () => {
Expand Down Expand Up @@ -144,4 +149,38 @@ describe('elements/content-sidebar/SidebarPanels', () => {
expect(instance.versionsSidebar.current.refresh).toHaveBeenCalledWith();
});
});

describe('componentDidUpdate', () => {
const onVersionChange = jest.fn();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just to confirm, the call counts are reset between the tests right?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think so. We have clearMocks: true in jest.config.js


test.each([
['/activity/versions/123', '/activity/versions/456'],
['/activity/versions/123', '/details/versions/456'],
['/activity/versions', '/activity/versions/123'],
['/activity/versions', '/details/versions'],
])('should not reset the current version if the versions route is still active', (prevPathname, pathname) => {
const wrapper = getWrapper({ location: { pathname: prevPathname }, onVersionChange });
wrapper.setProps({ location: { pathname } });
expect(onVersionChange).not.toBeCalled();
});

test.each([true, false])('should not reset the current version if the sidebar is toggled', isOpen => {
const wrapper = getWrapper({ isOpen, location: { pathname: '/details/versions/123' }, onVersionChange });
wrapper.setProps({ isOpen: !isOpen });
expect(onVersionChange).not.toBeCalled();
});

test.each([
['/activity/versions/123', '/metadata'],
['/activity/versions/123', '/activity'],
['/activity/versions', '/metadata'],
['/details/versions/123', '/metadata'],
['/details/versions/123', '/details'],
['/details/versions', '/metadata'],
])('should reset the current version if the versions route is no longer active', (prevPathname, pathname) => {
const wrapper = getWrapper({ location: { pathname: prevPathname }, onVersionChange });
wrapper.setProps({ location: { pathname } });
expect(onVersionChange).toBeCalledWith(null);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,6 @@ class VersionsSidebarContainer extends React.Component<Props, State> {
}
}

componentWillUnmount() {
// Reset the current version id since the wrapping route is no longer active
this.props.onVersionChange(null);
}

handleActionDelete = (versionId: string): Promise<void> => {
this.setState({ isLoading: true });

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,6 @@ describe('elements/content-sidebar/versions/VersionsSidebarContainer', () => {
});
});

describe('componentWillUnmount', () => {
test('should forward verison id reset to the parent component', () => {
const onVersionChange = jest.fn();
const wrapper = getWrapper({ onVersionChange });

wrapper.instance().componentWillUnmount();

expect(onVersionChange).toBeCalledWith(null);
});
});

describe('componentDidMount', () => {
test('should call onLoad after a successful fetchData() call', async () => {
const onLoad = jest.fn();
Expand Down