From 2d1909ae4bf194ab0dfcefb5cff7454a60fc061c Mon Sep 17 00:00:00 2001 From: Andrew Jiang Date: Thu, 3 Oct 2024 15:05:18 -0400 Subject: [PATCH] add test --- .../__test__/withVersionSwitcherInfo.test.ts | 31 ++++++++++++++++ .../src/server/withVersionSwitcherInfo.ts | 36 +++++++++++++------ 2 files changed, 57 insertions(+), 10 deletions(-) create mode 100644 packages/ui/docs-bundle/src/server/__test__/withVersionSwitcherInfo.test.ts diff --git a/packages/ui/docs-bundle/src/server/__test__/withVersionSwitcherInfo.test.ts b/packages/ui/docs-bundle/src/server/__test__/withVersionSwitcherInfo.test.ts new file mode 100644 index 0000000000..02772700c2 --- /dev/null +++ b/packages/ui/docs-bundle/src/server/__test__/withVersionSwitcherInfo.test.ts @@ -0,0 +1,31 @@ +import { FernNavigation } from "@fern-api/fdr-sdk"; +import { getNodesUnderCurrentVersionAscending } from "../withVersionSwitcherInfo"; + +describe("getNodesUnderCurrentVersionAscending", () => { + it("should return nodes under the current version in ascending order", () => { + const { version, nodes } = getNodesUnderCurrentVersionAscending< + { type: FernNavigation.NavigationNode["type"]; id: string }, + { type: FernNavigation.VersionNode["type"]; id: string } + >({ type: "page", id: "a" }, [ + { type: "version", id: "d" }, + { type: "tab", id: "c" }, + { type: "section", id: "b" }, + ]); + + expect(version).toEqual({ type: "version", id: "d" }); + expect(nodes.map((node) => node.id)).toEqual(["a", "b", "c"]); + }); + + it("should return nothing if a version node is not found", () => { + const { version, nodes } = getNodesUnderCurrentVersionAscending< + { type: FernNavigation.NavigationNode["type"]; id: string }, + { type: FernNavigation.VersionNode["type"]; id: string } + >({ type: "page", id: "a" }, [ + { type: "tab", id: "c" }, + { type: "section", id: "b" }, + ]); + + expect(version).toBeUndefined(); + expect(nodes.map((node) => node.id)).toEqual([]); + }); +}); diff --git a/packages/ui/docs-bundle/src/server/withVersionSwitcherInfo.ts b/packages/ui/docs-bundle/src/server/withVersionSwitcherInfo.ts index 1929fc1a70..1c6fc404aa 100644 --- a/packages/ui/docs-bundle/src/server/withVersionSwitcherInfo.ts +++ b/packages/ui/docs-bundle/src/server/withVersionSwitcherInfo.ts @@ -9,7 +9,7 @@ interface WithVersionSwitcherInfoArgs { node: FernNavigation.NavigationNodeWithMetadata; /** - * The parents of the current node. + * The parents of the current node, in descending order. */ parents: readonly FernNavigation.NavigationNode[]; @@ -114,25 +114,41 @@ export function withVersionSwitcherInfo({ * * ascending order = from the current node to its ancestors * - * @internal + * @internal visibleForTesting */ -export function getNodesUnderCurrentVersionAscending( - node: FernNavigation.NavigationNode, - parents: readonly FernNavigation.NavigationNode[], +export function getNodesUnderCurrentVersionAscending< + NODE extends { type: FernNavigation.NavigationNode["type"] } = FernNavigation.NavigationNode, + VERSION_NODE extends { type: FernNavigation.VersionNode["type"] } = FernNavigation.VersionNode, +>( + node: NODE, + + /** + * The parents of the current node should be in descending order. + */ + parents: readonly NODE[], ): { - version: FernNavigation.VersionNode | undefined; - nodes: FernNavigation.NavigationNode[]; + version: VERSION_NODE | undefined; + nodes: NODE[]; } { - const currentVersionIndex = parents.findIndex(FernNavigation.isVersionNode); + const currentVersionIndex = parents.findIndex((node) => node.type === "version"); // if the current node is not under a version, return an empty array if (currentVersionIndex < 0) { return { version: undefined, nodes: [] }; } - const version = parents[currentVersionIndex] as FernNavigation.VersionNode; + const version = parents[currentVersionIndex]; + if (version == null || version.type !== "version") { + return { version: undefined, nodes: [] }; + } parents = parents.slice(currentVersionIndex + 1); - return { version, nodes: [...parents, node].reverse() }; + return { + // this is safe because of the checks above + version: version as unknown as VERSION_NODE, + + // reverse the array to get the nodes in ascending order + nodes: [...parents, node].reverse(), + }; }