Skip to content

Commit

Permalink
feat: getPathForFlow() (#463)
Browse files Browse the repository at this point in the history
## What does this PR do?
- Generates a path for a node, which is required for search
- Relies on #462
- Implemented in
theopensystemslab/planx-new#3460
  • Loading branch information
DafyddLlyr authored Jul 29, 2024
1 parent a322165 commit 0336fd4
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 2 deletions.
28 changes: 26 additions & 2 deletions src/models/session/logic.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { OrderedBreadcrumbs, OrderedFlow } from "../../types";
import { sortBreadcrumbs, sortFlow } from "./logic";
import { type OrderedBreadcrumbs, type OrderedFlow } from "../../types";
import { getPathForNode, sortBreadcrumbs, sortFlow } from "./logic";
import * as complex from "./mocks/complex-flow-breadcrumbs";
import * as large from "./mocks/large-real-life-flow";
import * as sectioned from "./mocks/section-flow-breadcrumbs";
Expand Down Expand Up @@ -75,6 +75,30 @@ describe("sortBreadcrumbs", () => {
});
});

describe("getPathForNode", () => {
const flow = sortFlow(large.flow);
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[0].id).toBe("kTEuqpqCh2");

const pathIds = path.map(({ id }) => id);
const uniquePathIds = [...new Set(pathIds)];

// All nodes in path are unique
expect(pathIds.length).toEqual(uniquePathIds.length);
});

test("it returns a path for a complex flow in a reasonable amount of time", () => {
expectReasonableExecutionTime(
() => getPathForNode({ nodeId: "kTEuqpqCh2", flow }),
2000,
);
});
});

function expectReasonableExecutionTime<T>(fn: () => T, timeout: number): T {
const testStartTime = new Date().getTime();
const out = fn();
Expand Down
35 changes: 35 additions & 0 deletions src/models/session/logic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type {
Crumb,
DataObject,
FlowGraph,
IndexedFlowGraph,
IndexedNode,
Node,
NodeId,
Expand Down Expand Up @@ -121,3 +122,37 @@ const buildAnswerData = (crumb: Crumb, flow: FlowGraph) =>
);
}
}, {});

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

/**
* Return a "path" for a given node which represents it's placement relative to the root node
* A path begins with the provided node, and ends at the root (inclusive)
*/
export const getPathForNode: GetPathForNode = ({ nodeId, flow }) => {
const indexedFlow = flow.reduce((acc, indexedNode) => {
acc[indexedNode.id] = indexedNode;
return acc;
}, {} as IndexedFlowGraph);

const path: ReturnType<GetPathForNode> = [];

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

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

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

traverseGraph(nodeId);

return path;
};

0 comments on commit 0336fd4

Please sign in to comment.