diff --git a/src/utilities/content-tree-enhancers.mjs b/src/utilities/content-tree-enhancers.mjs index 12f3ed24bed6..f65ea262fa38 100644 --- a/src/utilities/content-tree-enhancers.mjs +++ b/src/utilities/content-tree-enhancers.mjs @@ -144,7 +144,7 @@ export function restructure(item, options) { if (item.children) { for (const child of item.children) restructure(child, options); - item.children.filter(filter); + item.children = item.children.filter(filter); item.children.sort(sort); } diff --git a/src/utilities/content-tree-enhancers.test.mjs b/src/utilities/content-tree-enhancers.test.mjs new file mode 100644 index 000000000000..dfb7d12da696 --- /dev/null +++ b/src/utilities/content-tree-enhancers.test.mjs @@ -0,0 +1,57 @@ +// eslint-disable-next-line import/no-extraneous-dependencies +import { describe, expect } from "@jest/globals"; +import { restructure } from "./content-tree-enhancers.mjs"; + +describe("restructure", () => { + it("applies filter result back to children array", () => { + const originalChildren = [ + { + type: "directory", + path: "src/content/guides", + title: "Guides", + }, + { + type: "directory", + path: "src/content/api", + title: "API", + }, + ]; + + const root = { + type: "directory", + path: "src/content", + children: originalChildren, + }; + + restructure(root, { dir: "src/content" }); + + // Filter creates a new array; restructure must assign that result back. + expect(root.children).not.toBe(originalChildren); + expect(root.children).toHaveLength(2); + }); + + it("sorts children after restructuring", () => { + const root = { + type: "directory", + path: "src/content", + children: [ + { + type: "directory", + path: "src/content/guides", + title: "Guides", + sort: 20, + }, + { + type: "directory", + path: "src/content/api", + title: "API", + sort: 10, + }, + ], + }; + + restructure(root, { dir: "src/content" }); + + expect(root.children.map((item) => item.title)).toEqual(["API", "Guides"]); + }); +});