Skip to content

Commit

Permalink
Simplify TreeViewHeader and fix more test data
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonleenaylor committed Sep 16, 2022
1 parent 6073627 commit c0c5c85
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 37 deletions.
20 changes: 4 additions & 16 deletions src/components/TreeView/TreeViewHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,18 +61,6 @@ export function TreeViewHeader(props: TreeHeaderProps) {

// exported for unit testing only
export function useTreeNavigation(props: TreeHeaderProps) {
function getDomainTreeNode(
domain: SemanticDomain | undefined
): SemanticDomainTreeNode | undefined {
if (!domain) {
return undefined;
}

const domainTreeNode: SemanticDomainTreeNode | undefined = undefined;

return domainTreeNode;
}

function getRightBrother(props: TreeHeaderProps): SemanticDomain | undefined {
return props.currentDomain.next;
}
Expand All @@ -87,22 +75,22 @@ export function useTreeNavigation(props: TreeHeaderProps) {
let domain: SemanticDomainTreeNode | undefined;
switch (event.key) {
case Key.ArrowLeft:
domain = getDomainTreeNode(getLeftBrother(props));
domain = getLeftBrother(props);
break;
case Key.ArrowRight:
domain = getDomainTreeNode(getRightBrother(props));
domain = getRightBrother(props);
break;
case Key.ArrowUp:
if (props.currentDomain.parent !== undefined) {
domain = getDomainTreeNode(props.currentDomain.parent);
domain = props.currentDomain.parent;
}
break;
case Key.ArrowDown:
if (
props.currentDomain.children &&
props.currentDomain.children.length === 1
) {
domain = getDomainTreeNode(props.currentDomain.children[0]);
domain = props.currentDomain.children[0];
}
break;
}
Expand Down
43 changes: 22 additions & 21 deletions src/components/TreeView/tests/MockSemanticDomain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,22 @@ domMap[mapIds.head] = {
...newSemanticDomainTreeNode("", "head"),
children: [parent],
};
domMap[parent.id] = { ...parent, parent: domMap[mapIds.head] };
parent.parent = domMap[mapIds.head];
domMap[parent.id] = parent;

// Following children
for (let i = 0; i < 3; i++) {
const id = "1." + i;
const subdom: SemanticDomainTreeNode = {
...newSemanticDomainTreeNode(id, `kid${i}`),
};
parent.children?.push(subdom);
domMap[parent.id].children?.push(subdom);
domMap[id] = {
...subdom,
children: [],
parent: parent,
previous: i > 0 ? domMap["1." + (i - 1)] : undefined,
};
subdom.previous = i > 0 ? domMap["1." + (i - 1)] : undefined;
parent.children?.push(subdom);
domMap[id] = subdom;
}

// now set the 'next' props for the first two children
domMap[mapIds.firstKid].next = domMap[mapIds.middleKid];
domMap[mapIds.middleKid].next = domMap[mapIds.lastKid];
Expand All @@ -53,7 +53,7 @@ for (let i = 0; i < 4; i++) {
const subdom: SemanticDomainTreeNode = {
...newSemanticDomainTreeNode(id, `evenData${i}`),
};
domMap[dom0!.id].children?.push(newSemanticDomainTreeNode(id));
domMap[dom0!.id].children!.push(newSemanticDomainTreeNode(id));
domMap[id] = { ...subdom, parent: parent };
}

Expand All @@ -64,32 +64,33 @@ for (let i = 0; i < 3; i++) {
const subdom: SemanticDomainTreeNode = {
...newSemanticDomainTreeNode(id, `oddData${i}`),
};
domMap[dom1!.id]?.children?.push(newSemanticDomainTreeNode(id));
domMap[dom1!.id]?.children!.push(newSemanticDomainTreeNode(id));
domMap[id] = { ...subdom, parent: parent };
}

// Give the last subdomain one subdomain with total depth of 5
const dom2 = parent.children?.[2];
const dom2 = parent.children![2] as SemanticDomainTreeNode;
let id = mapIds.depth3;
const dom20 = {
const dom20: SemanticDomainTreeNode = {
...newSemanticDomainTreeNode(id, "depth=3"),
description: "so lonely...",
children: [],
};
domMap[dom2!.id]?.children?.push(newSemanticDomainTreeNode(id));
domMap[id] = { ...dom20, parent: parent };
dom2.children!.push(newSemanticDomainTreeNode(id));
dom20.parent = dom2;
domMap[id] = dom20;

id = mapIds.depth4;
const dom200: SemanticDomainTreeNode = {
...newSemanticDomainTreeNode(id, "depth=4"),
children: [],
};
domMap[dom20.id].children?.push(newSemanticDomainTreeNode(id));
domMap[id] = { ...dom200, parent: dom20 };
dom20.children!.push(dom200);
dom200.parent = dom20;
domMap[id] = dom200;
id = mapIds.depth5;
dom200.children?.push({
...newSemanticDomainTreeNode(id, "depth=5"),
});
domMap[dom200.id]?.children?.push(newSemanticDomainTreeNode(id));
domMap[id] = { ...dom200?.children?.[0]!, parent: dom200 };
const domDepth5 = newSemanticDomainTreeNode(id, "depth=5");
domDepth5.parent = dom200;
domMap[id] = domDepth5;
export const jsonDomain = parent;

export default domMap;
2 changes: 2 additions & 0 deletions src/components/TreeView/tests/TreeViewHeader.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ describe("TreeViewHeader", () => {
it("left arrow moves to left sibling", () => {
render(<TreeViewHeader {...twoBrothersManyKids} />);
simulateKey(Key.ArrowLeft);
expect(MOCK_ANIMATE).toHaveBeenCalled();
expect(MOCK_ANIMATE).toHaveBeenCalledWith(domMap[mapIds.firstKid]);
});
it("left arrow does nothing when no left sibling", () => {
Expand All @@ -95,6 +96,7 @@ describe("TreeViewHeader", () => {
it("up arrow moves to parent domain", () => {
render(<TreeViewHeader {...twoBrothersManyKids} />);
simulateKey(Key.ArrowUp);
expect(MOCK_ANIMATE).toHaveBeenCalled();
expect(MOCK_ANIMATE).toHaveBeenCalledWith(domMap[mapIds.parent]);
});
it("down arrow does nothing when multiple kids", () => {
Expand Down

0 comments on commit c0c5c85

Please sign in to comment.