Skip to content
Closed
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
14 changes: 9 additions & 5 deletions __tests__/src/components/SidebarIndexTableOfContents.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,16 @@ import manifestVersion3 from '../../fixtures/version-3/structures.json';
*/
function createWrapper(props) {
const manifest = Utils.parseManifest(props.manifest ? props.manifest : manifestVersion2);
const containerElement = document.createElement('div');
const mockRef = { current: containerElement };
return render(
<SidebarIndexTableOfContents
id="something"
classes={{}}
treeStructure={props.treeStructure ? props.treeStructure : manifest.getDefaultTree()}
visibleNodeIds={props.visibleNodeIds ? props.visibleNodeIds : []}
expandedNodeIds={props.expandedNodeIds ? props.expandedNodeIds : []}
containerRef={props.containerRef}
containerRef={mockRef}
nodeIdToScrollTo={props.nodeIdToScrollTo}
{...props}
/>,
Expand All @@ -33,10 +35,13 @@ function createWrapper(props) {
* write a reasonable test for it)
*/
function createInteractiveWrapper({ manifest = manifestVersion3, ...props }) {
const containerElement = document.createElement('div');
const mockRef = { current: containerElement };
return render(
<ConnectedSidebarIndexTableOfContents
id="something"
windowId="a"
containerRef={mockRef}
{...props}
/>,
{
Expand All @@ -59,7 +64,7 @@ function createInteractiveWrapper({ manifest = manifestVersion3, ...props }) {
);
}

describe('SidebarIndexTableOfContents', () => {
describe('SidebarIndexTableOfContents', async () => {
let setCanvas;

beforeEach(() => {
Expand Down Expand Up @@ -100,6 +105,7 @@ describe('SidebarIndexTableOfContents', () => {
},
});
expect(screen.getByRole('treeitem')).toBeInTheDocument();
unmount();
});

it('accepts missing nodes property for tree structure and tree nodes', () => {
Expand Down Expand Up @@ -242,9 +248,7 @@ describe('SidebarIndexTableOfContents', () => {
expect(store.getState().windows.a.canvasId).toEqual('http://foo.test/1/canvas/c9');

const leafNode3 = screen.getAllByRole('treeitem')[4];
act(() => {
leafNode3.focus();
});
leafNode3.focus();
await user.keyboard('{Enter}');
expect(store.getState().windows.a.canvasId).toEqual('http://foo.test/1/canvas/c10');
});
Expand Down
36 changes: 29 additions & 7 deletions src/components/SidebarIndexTableOfContents.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { useState, useEffect } from 'react';
import PropTypes from 'prop-types';
import { alpha, styled } from '@mui/material/styles';
import { TreeView } from '@mui/x-tree-view/TreeView';
Expand Down Expand Up @@ -44,16 +45,37 @@ function deepFind(treeNode, id) {
}

/** Wrap <ScrollTo> to remove the nodeId prop required for MUI's TreeView */
const ScrollToForTreeItem = ({ children, nodeId, ...props }) => (
<ScrollTo
{...props}
>
{ children }
</ScrollTo>
);
function ScrollToForTreeItem(
{
children, nodeId, containerRef, ...props
},
) {
const [containerReady, setContainerReady] = useState(false);

useEffect(() => {
if (containerRef && containerRef.current) {
setContainerReady(true);
}
}, [containerRef]);

if (containerReady) {
return (
<ScrollTo
containerRef={containerRef}
{...props}
>
{children}
</ScrollTo>
);
}
return null;
}
ScrollToForTreeItem.propTypes = {
children: PropTypes.node.isRequired,
containerRef: PropTypes.oneOfType([
PropTypes.func,
PropTypes.shape({ current: PropTypes.instanceOf(Element) }),
]).isRequired,
nodeId: PropTypes.string.isRequired,
};

Expand Down