Skip to content

Commit

Permalink
update unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
abvthecity committed Oct 4, 2024
1 parent 8b5abbe commit 5c8b1f0
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ describe("pruneNavigationTree", () => {
hidden: undefined,
overviewPageId: undefined,
noindex: undefined,
pointsTo: undefined,
pointsTo: FernNavigation.Slug("root/page"),
});
});

Expand Down Expand Up @@ -88,7 +88,7 @@ describe("pruneNavigationTree", () => {
hidden: undefined,
overviewPageId: undefined,
noindex: undefined,
pointsTo: undefined,
pointsTo: FernNavigation.Slug("root/page"),
};

const result = pruneNavigationTree(root, (node) => node.id !== FernNavigation.NodeId("page"));
Expand Down Expand Up @@ -153,6 +153,55 @@ describe("pruneNavigationTree", () => {
icon: undefined,
hidden: undefined,
noindex: undefined,
pointsTo: FernNavigation.Slug("root/page"),
});
});

it("should not prune section even if children are pruned", () => {
const root: FernNavigation.NavigationNode = {
type: "section",
id: FernNavigation.NodeId("root"),
slug: FernNavigation.Slug("root"),
title: "Root",
overviewPageId: FernNavigation.PageId("overview.mdx"), // this is a visitable page
children: [
{
type: "page",
id: FernNavigation.NodeId("page"),
slug: FernNavigation.Slug("root/page"),
title: "Page",
pageId: FernNavigation.PageId("page.mdx"),
canonicalSlug: undefined,
icon: undefined,
hidden: undefined,
noindex: undefined,
},
],
collapsed: undefined,
canonicalSlug: undefined,
icon: undefined,
hidden: undefined,
noindex: undefined,
pointsTo: undefined,
};

const result = pruneNavigationTree(root, (node) => node.id !== "page");

// structuredClone should duplicate the object
expect(result === root).toBe(false);

expect(result).toStrictEqual({
type: "section",
id: FernNavigation.NodeId("root"),
slug: FernNavigation.Slug("root"),
overviewPageId: FernNavigation.PageId("overview.mdx"), // this is a visitable page
title: "Root",
children: [], // children is empty, but the section is still there because it has an overview page
collapsed: undefined,
canonicalSlug: undefined,
icon: undefined,
hidden: undefined,
noindex: undefined,
pointsTo: undefined,
});
});
Expand Down Expand Up @@ -214,7 +263,7 @@ describe("pruneNavigationTree", () => {
icon: undefined,
hidden: undefined,
noindex: undefined,
pointsTo: undefined,
pointsTo: FernNavigation.Slug("root/page"),
});
});

Expand Down
8 changes: 3 additions & 5 deletions packages/fdr-sdk/src/navigation/utils/pruneNavigationTree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ function mutablePruneNavigationTree<ROOT extends FernNavigation.NavigationNode>(
root: ROOT,
keep: (node: FernNavigation.NavigationNode) => boolean,
): ROOT | undefined {
const [result, deleted] = prunetree(root, {
const [result] = prunetree(root, {
predicate: keep,
getChildren: FernNavigation.getChildren,
getPointer: (node) => node.id,
Expand All @@ -38,10 +38,8 @@ function mutablePruneNavigationTree<ROOT extends FernNavigation.NavigationNode>(
return undefined;
}

if (deleted.size > 0) {
// since the tree has been pruned, we need to update the pointsTo property
mutableUpdatePointsTo(result);
}
// since the tree has been pruned, we need to update the pointsTo property
mutableUpdatePointsTo(result);

return result;
}
3 changes: 2 additions & 1 deletion packages/fdr-sdk/src/navigation/utils/updatePointsTo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import { followRedirect } from "./followRedirect";
export function mutableUpdatePointsTo(input: FernNavigation.NavigationNode): void {
FernNavigation.traverseDF(input, (node) => {
if (FernNavigation.hasPointsTo(node)) {
node.pointsTo = followRedirect(node);
const pointsTo = followRedirect(node);
node.pointsTo = node.slug === pointsTo ? undefined : pointsTo;
}
});
}

0 comments on commit 5c8b1f0

Please sign in to comment.