Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
abvthecity committed Oct 3, 2024
1 parent 157da86 commit 2d1909a
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -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([]);
});
});
36 changes: 26 additions & 10 deletions packages/ui/docs-bundle/src/server/withVersionSwitcherInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[];

Expand Down Expand Up @@ -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(),
};
}

0 comments on commit 2d1909a

Please sign in to comment.