From 5c8b1f071e256159baabc0ae148845ac5f4d4cc2 Mon Sep 17 00:00:00 2001 From: Andrew Jiang Date: Fri, 4 Oct 2024 15:30:39 -0400 Subject: [PATCH] update unit tests --- .../__test__/pruneNavigationTree.test.ts | 55 ++++++++++++++++++- .../navigation/utils/pruneNavigationTree.ts | 8 +-- .../src/navigation/utils/updatePointsTo.ts | 3 +- 3 files changed, 57 insertions(+), 9 deletions(-) diff --git a/packages/fdr-sdk/src/navigation/utils/__test__/pruneNavigationTree.test.ts b/packages/fdr-sdk/src/navigation/utils/__test__/pruneNavigationTree.test.ts index 092d2f69e5..2ec3a34327 100644 --- a/packages/fdr-sdk/src/navigation/utils/__test__/pruneNavigationTree.test.ts +++ b/packages/fdr-sdk/src/navigation/utils/__test__/pruneNavigationTree.test.ts @@ -59,7 +59,7 @@ describe("pruneNavigationTree", () => { hidden: undefined, overviewPageId: undefined, noindex: undefined, - pointsTo: undefined, + pointsTo: FernNavigation.Slug("root/page"), }); }); @@ -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")); @@ -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, }); }); @@ -214,7 +263,7 @@ describe("pruneNavigationTree", () => { icon: undefined, hidden: undefined, noindex: undefined, - pointsTo: undefined, + pointsTo: FernNavigation.Slug("root/page"), }); }); diff --git a/packages/fdr-sdk/src/navigation/utils/pruneNavigationTree.ts b/packages/fdr-sdk/src/navigation/utils/pruneNavigationTree.ts index 4ff3079a41..9f516f3ab0 100644 --- a/packages/fdr-sdk/src/navigation/utils/pruneNavigationTree.ts +++ b/packages/fdr-sdk/src/navigation/utils/pruneNavigationTree.ts @@ -23,7 +23,7 @@ function mutablePruneNavigationTree( 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, @@ -38,10 +38,8 @@ function mutablePruneNavigationTree( 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; } diff --git a/packages/fdr-sdk/src/navigation/utils/updatePointsTo.ts b/packages/fdr-sdk/src/navigation/utils/updatePointsTo.ts index 3e979adb9a..17b64fb1f5 100644 --- a/packages/fdr-sdk/src/navigation/utils/updatePointsTo.ts +++ b/packages/fdr-sdk/src/navigation/utils/updatePointsTo.ts @@ -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; } }); }