Skip to content

Commit

Permalink
fix: Update getPathForNode() (#506)
Browse files Browse the repository at this point in the history
Small bug I noticed when working on
theopensystemslab/planx-new#3650
  • Loading branch information
DafyddLlyr authored Sep 12, 2024
1 parent 9f0c3b0 commit 88b6a4f
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/models/session/logic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ describe("getPathForNode", () => {
it("returns a path for a complex flow", () => {
const path = getPathForNode({ nodeId: "kTEuqpqCh2", flow });

expect(path).toHaveLength(57);
expect(path[56].id).toBe("_root");
expect(path).toHaveLength(58);
expect(path[57].id).toBe("_root");
expect(path[0].id).toBe("kTEuqpqCh2");

const pathIds = path.map(({ id }) => id);
Expand Down
11 changes: 5 additions & 6 deletions src/models/session/logic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,9 @@ const buildAnswerData = (crumb: Crumb, flow: FlowGraph) =>
}
}, {});

type GetPathForNode = (params: {
nodeId: string;
flow: OrderedFlow;
}) => { id: string; type: ComponentType | "_root" }[];
type Path = { id: string; type: ComponentType | "_root" }[];

type GetPathForNode = (params: { nodeId: string; flow: OrderedFlow }) => Path;

/**
* Return a "path" for a given node which represents it's placement relative to the root node
Expand All @@ -138,17 +137,17 @@ export const getPathForNode: GetPathForNode = ({ nodeId, flow }) => {
return acc;
}, {} as IndexedFlowGraph);

const path: ReturnType<GetPathForNode> = [];
const path: Path = [];

const traverseGraph = (currentNodeId: string) => {
const { id, type, parentId } = indexedFlow[currentNodeId];
path.push({ id, type });

if (parentId === "_root") {
path.push({ id: "_root", type: "_root" });
return;
}

path.push({ id, type });
traverseGraph(parentId);
};

Expand Down

0 comments on commit 88b6a4f

Please sign in to comment.