Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show nearby decisions #67

Merged
merged 4 commits into from
Mar 13, 2024
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: 0 additions & 22 deletions src/components/Nodes/BoolNode/BoolNode.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -94,26 +94,4 @@ describe('BoolNode', () => {
expect(noButton).toHaveClass(/selected/i);
expect(yesButton).not.toHaveClass(/selected/i);
});
test('clicking yes/no toggles the visibility of the children nodes', async () => {
const user = userEvent.setup();
const primaryId = '1';
const yesId = '2';
const noId = '3';

render(
<TestComponent
primaryId={primaryId}
yesId={yesId}
noId={noId}
overwrites={{
id: primaryId,
data: { label: 'question', yesId: yesId, noId: noId, children: [yesId, noId] },
}}
/>
);
await user.click(screen.getByRole('button', { name: /yes/i }));
expect(useTreeStore.getState().tree[yesId].hidden).toBe(false);
await user.click(screen.getByRole('button', { name: /no/i }));
expect(useTreeStore.getState().tree[yesId].hidden).toBe(true);
});
});
22 changes: 17 additions & 5 deletions src/components/Nodes/BoolNode/BoolNode.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,22 @@ export const BoolNode = ({
id,
...props
}: NodeProps<BoolNodeData>) => {
const { showNode, hideNode } = useTreeStore();
const { showNode, showChildren, hideNiblings, hideDescendants } = useTreeStore();
const [selected, setSelected] = useState<'yes' | 'no' | undefined>(undefined);

const handleYes = () => {
showNode(yesId, { parentId: id });
hideNode(noId);
showChildren(yesId);
hideNiblings(id);
hideDescendants(noId);
setSelected('yes');
};

const handleNo = () => {
showNode(noId, { parentId: id });
hideNode(yesId);
showChildren(noId);
hideNiblings(id);
hideDescendants(yesId);
setSelected('no');
};

Expand All @@ -39,10 +43,18 @@ export const BoolNode = ({
<span>{label}</span>
</div>
<div className={styles.boolNodeOptions}>
<button onClick={handleYes} className={selected === 'yes' ? styles.selected : ''}>
<button
onClick={handleYes}
className={selected === 'yes' ? styles.selected : ''}
data-testid={`${id}-yes-button`}
>
Yes
</button>
<button onClick={handleNo} className={selected === 'no' ? styles.selected : ''}>
<button
onClick={handleNo}
className={selected === 'no' ? styles.selected : ''}
data-testid={`${id}-no-button`}
>
No
</button>
</div>
Expand Down
188 changes: 109 additions & 79 deletions src/components/Tree/Tree.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,38 +6,92 @@ import { ReactFlowProvider } from 'reactflow';
import { DecisionTree, PositionUnawareDecisionTree } from 'store';
import { afterEach, describe, expect, test } from 'vitest';

afterEach(() => cleanup());
// parent
// / \
// yesChild noChild
// / \ / \
// yesYesChild yesNoChild noYesChild noNoChild

const TestComponent = ({ tree }: { tree?: PositionUnawareDecisionTree }) => {
const myTree = tree || {
['1']: {
id: '1',
const PARENT_ID = '1';
const YES_CHILD_ID = '2';
const NO_CHILD_ID = '3';
const YES_YES_ID = '4';
const YES_NO_ID = '5';
const NO_YES_ID = '6';
const NO_NO_ID = '7';

interface createTestTreeOptions {
showIds?: Array<string>;
}

const createTestPositionUnawareDecisionTree = (
options?: createTestTreeOptions
): PositionUnawareDecisionTree => {
return {
[PARENT_ID]: {
id: PARENT_ID,
data: {
label: 'this is a question?',
yesId: '2',
noId: '3',
label: 'parent',
yesId: YES_CHILD_ID,
noId: NO_CHILD_ID,
children: [],
},
position: { x: 0, y: 0 },
type: 'BoolNode',
hidden: false,
},
['2']: {
id: '2',
data: { label: 'this is an answer?', children: [] },
position: { x: 0, y: 0 },
[YES_CHILD_ID]: {
id: YES_CHILD_ID,
data: {
label: 'yes child',
children: [YES_YES_ID, YES_NO_ID],
yesId: YES_YES_ID,
noId: YES_NO_ID,
},
type: 'BoolNode',
hidden: !options?.showIds?.includes(YES_CHILD_ID),
},
[NO_CHILD_ID]: {
id: NO_CHILD_ID,
data: {
label: 'no child',
children: [NO_NO_ID, NO_YES_ID],
noId: NO_NO_ID,
yesId: NO_YES_ID,
},
type: 'BoolNode',
hidden: !options?.showIds?.includes(NO_CHILD_ID),
},
[YES_YES_ID]: {
id: YES_YES_ID,
data: { label: 'yes grandchild', children: [] },
type: 'default',
hidden: false,
hidden: !options?.showIds?.includes(YES_YES_ID),
},
['3']: {
id: '3',
data: { label: 'this is an answer?', children: [] },
position: { x: 0, y: 0 },
[YES_NO_ID]: {
id: YES_NO_ID,
data: { label: 'no grandchild', children: [] },
type: 'default',
hidden: false,
hidden: !options?.showIds?.includes(YES_NO_ID),
},
[NO_NO_ID]: {
id: NO_NO_ID,
data: { label: 'yes grandchild', children: [] },
type: 'default',
hidden: !options?.showIds?.includes(NO_NO_ID),
},
[NO_YES_ID]: {
id: NO_YES_ID,
data: { label: 'no grandchild', children: [] },
type: 'default',
hidden: !options?.showIds?.includes(NO_YES_ID),
},
};
const { nodes, edges, onClick } = useDecisionTree(myTree);
};

afterEach(() => cleanup());

const TestComponent = ({ tree }: { tree?: PositionUnawareDecisionTree }) => {
const { nodes, edges, onClick } = useDecisionTree(tree);
return <Tree nodes={nodes} edges={edges} onClick={onClick} />;
};

Expand Down Expand Up @@ -147,65 +201,6 @@ describe('Tree Component', () => {
fireEvent.click(screen.queryByTestId(`node-${siblingWithChild2}`)!);
expect(screen.queryByTestId(`node-${grandchildId}`)).not.toBeInTheDocument();
});
test('hides all descendants of expanded nodes on click', () => {
const parentId = '1';
const childId = '2';
const grandchildId = '3';
const greatGrandchildId = '4';
const tree: DecisionTree = {
[parentId]: {
id: parentId,
data: {
label: 'this is a question?',
children: ['2'],
expanded: true,
},
position: { x: 0, y: 0 },
type: 'default',
hidden: false,
},
[childId]: {
id: childId,
data: {
label: 'this is an answer?',
children: ['3'],
expanded: true,
},
position: { x: 0, y: 0 },
type: 'default',
hidden: false,
},
[grandchildId]: {
id: grandchildId,
data: { label: 'this is an answer?', children: ['4'] },
position: { x: 0, y: 0 },
type: 'default',
hidden: false,
},
[greatGrandchildId]: {
id: greatGrandchildId,
data: { label: 'this is an answer?', children: [] },
position: { x: 0, y: 0 },
type: 'default',
hidden: false,
},
};
render(
<ReactFlowProvider>
<TestComponent tree={tree} />
</ReactFlowProvider>
);
[parentId, childId, grandchildId, greatGrandchildId].forEach((nodeId: string) => {
expect(screen.queryByTestId(`node-${nodeId}`)).toBeInTheDocument();
});
fireEvent.click(screen.queryByTestId(`node-${childId}`)!);
[parentId, childId].forEach((nodeId) => {
expect(screen.queryByTestId(`node-${nodeId}`)).toBeInTheDocument();
});
[grandchildId, greatGrandchildId].forEach((nodeId) => {
expect(screen.queryByTestId(`node-${nodeId}`)).not.toBeInTheDocument();
});
});
test('ignores yes/no clicks', () => {
const parentId = '1';
const childId = '2';
Expand Down Expand Up @@ -246,4 +241,39 @@ describe('Tree Component', () => {
expect(screen.queryByTestId(`node-${nodeId}`)).toBeInTheDocument();
});
});
describe('Bool node selection', () => {
test('opens the options children', () => {
const myTree = createTestPositionUnawareDecisionTree();
render(
<ReactFlowProvider>
<TestComponent tree={myTree} />
</ReactFlowProvider>
);
[YES_CHILD_ID, YES_YES_ID, YES_NO_ID].forEach((nodeId: string) => {
expect(screen.queryByTestId(`node-${nodeId}`)).not.toBeInTheDocument();
});
fireEvent.click(screen.queryByTestId(`${PARENT_ID}-yes-button`)!);
[YES_CHILD_ID, YES_YES_ID, YES_NO_ID].forEach((nodeId: string) => {
expect(screen.queryByTestId(`node-${nodeId}`)).toBeInTheDocument();
});
});
test('closes the descendants the selected options siblings', () => {
const myTree = createTestPositionUnawareDecisionTree({
showIds: [YES_CHILD_ID, NO_CHILD_ID, YES_YES_ID, YES_NO_ID],
});

render(
<ReactFlowProvider>
<TestComponent tree={myTree} />
</ReactFlowProvider>
);
[YES_CHILD_ID, NO_CHILD_ID, YES_YES_ID, YES_NO_ID].forEach((nodeId: string) => {
expect(screen.queryByTestId(`node-${nodeId}`)).toBeInTheDocument();
});
fireEvent.click(screen.queryByTestId(`${NO_CHILD_ID}-yes-button`)!);
[YES_YES_ID, YES_NO_ID].forEach((nodeId: string) => {
expect(screen.queryByTestId(`node-${nodeId}`)).not.toBeInTheDocument();
});
});
});
});
Loading