|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import type { Page } from "../types/model"; |
| 3 | +import { flattenDocs } from "./flattenDocs"; |
| 4 | + |
| 5 | +describe("flattenDocs", () => { |
| 6 | + const createMockPage = ( |
| 7 | + route: string, |
| 8 | + title: string, |
| 9 | + children: Page[] = [], |
| 10 | + ): Page => ({ |
| 11 | + route, |
| 12 | + title, |
| 13 | + description: `Description for ${title}`, |
| 14 | + part: null, |
| 15 | + outline: [], |
| 16 | + body: { |
| 17 | + kind: "html", |
| 18 | + content: `<p>${title} content</p>`, |
| 19 | + }, |
| 20 | + children, |
| 21 | + }); |
| 22 | + |
| 23 | + it("空の配列を渡すと空の結果を返す", () => { |
| 24 | + const [flattenedPages, pagePaths] = flattenDocs([]); |
| 25 | + |
| 26 | + expect(flattenedPages).toEqual([]); |
| 27 | + expect(pagePaths).toEqual([]); |
| 28 | + }); |
| 29 | + |
| 30 | + it("子を持たない単一ページを平坦化する", () => { |
| 31 | + const page = createMockPage("/docs/", "Documentation"); |
| 32 | + const [flattenedPages, pagePaths] = flattenDocs([page]); |
| 33 | + |
| 34 | + expect(flattenedPages).toEqual([page]); |
| 35 | + expect(pagePaths).toEqual([[page]]); |
| 36 | + }); |
| 37 | + |
| 38 | + it("複数の子を持たないページを平坦化する", () => { |
| 39 | + const page1 = createMockPage("/docs/", "Documentation"); |
| 40 | + const page2 = createMockPage("/tutorial/", "Tutorial"); |
| 41 | + const [flattenedPages, pagePaths] = flattenDocs([page1, page2]); |
| 42 | + |
| 43 | + expect(flattenedPages).toEqual([page1, page2]); |
| 44 | + expect(pagePaths).toEqual([[page1], [page2]]); |
| 45 | + }); |
| 46 | + |
| 47 | + it("子を持つページを平坦化する", () => { |
| 48 | + const childPage = createMockPage("/docs/tutorial/", "Tutorial"); |
| 49 | + const parentPage = createMockPage("/docs/", "Documentation", [childPage]); |
| 50 | + const [flattenedPages, pagePaths] = flattenDocs([parentPage]); |
| 51 | + |
| 52 | + expect(flattenedPages).toEqual([parentPage, childPage]); |
| 53 | + expect(pagePaths).toEqual([[parentPage], [parentPage, childPage]]); |
| 54 | + }); |
| 55 | + |
| 56 | + it("複数階層のページを平坦化する", () => { |
| 57 | + const grandChildPage = createMockPage("/docs/tutorial/basics/", "Basics"); |
| 58 | + const childPage = createMockPage("/docs/tutorial/", "Tutorial", [ |
| 59 | + grandChildPage, |
| 60 | + ]); |
| 61 | + const parentPage = createMockPage("/docs/", "Documentation", [childPage]); |
| 62 | + const [flattenedPages, pagePaths] = flattenDocs([parentPage]); |
| 63 | + |
| 64 | + expect(flattenedPages).toEqual([parentPage, childPage, grandChildPage]); |
| 65 | + expect(pagePaths).toEqual([ |
| 66 | + [parentPage], |
| 67 | + [parentPage, childPage], |
| 68 | + [parentPage, childPage, grandChildPage], |
| 69 | + ]); |
| 70 | + }); |
| 71 | + |
| 72 | + it("複数の子を持つページを平坦化する", () => { |
| 73 | + const child1 = createMockPage("/docs/tutorial/", "Tutorial"); |
| 74 | + const child2 = createMockPage("/docs/reference/", "Reference"); |
| 75 | + const parentPage = createMockPage("/docs/", "Documentation", [ |
| 76 | + child1, |
| 77 | + child2, |
| 78 | + ]); |
| 79 | + const [flattenedPages, pagePaths] = flattenDocs([parentPage]); |
| 80 | + |
| 81 | + expect(flattenedPages).toEqual([parentPage, child1, child2]); |
| 82 | + expect(pagePaths).toEqual([ |
| 83 | + [parentPage], |
| 84 | + [parentPage, child1], |
| 85 | + [parentPage, child2], |
| 86 | + ]); |
| 87 | + }); |
| 88 | + |
| 89 | + it("複雑な階層構造を平坦化する", () => { |
| 90 | + // docs/ |
| 91 | + // ├── tutorial/ |
| 92 | + // │ ├── basics/ |
| 93 | + // │ └── advanced/ |
| 94 | + // └── reference/ |
| 95 | + // └── functions/ |
| 96 | + const basics = createMockPage("/docs/tutorial/basics/", "Basics"); |
| 97 | + const advanced = createMockPage("/docs/tutorial/advanced/", "Advanced"); |
| 98 | + const tutorial = createMockPage("/docs/tutorial/", "Tutorial", [ |
| 99 | + basics, |
| 100 | + advanced, |
| 101 | + ]); |
| 102 | + |
| 103 | + const functions = createMockPage("/docs/reference/functions/", "Functions"); |
| 104 | + const reference = createMockPage("/docs/reference/", "Reference", [ |
| 105 | + functions, |
| 106 | + ]); |
| 107 | + |
| 108 | + const docs = createMockPage("/docs/", "Documentation", [ |
| 109 | + tutorial, |
| 110 | + reference, |
| 111 | + ]); |
| 112 | + |
| 113 | + const [flattenedPages, pagePaths] = flattenDocs([docs]); |
| 114 | + |
| 115 | + expect(flattenedPages).toEqual([ |
| 116 | + docs, |
| 117 | + tutorial, |
| 118 | + basics, |
| 119 | + advanced, |
| 120 | + reference, |
| 121 | + functions, |
| 122 | + ]); |
| 123 | + expect(pagePaths).toEqual([ |
| 124 | + [docs], |
| 125 | + [docs, tutorial], |
| 126 | + [docs, tutorial, basics], |
| 127 | + [docs, tutorial, advanced], |
| 128 | + [docs, reference], |
| 129 | + [docs, reference, functions], |
| 130 | + ]); |
| 131 | + }); |
| 132 | + |
| 133 | + it("複数のルートページを持つ階層構造を平坦化する", () => { |
| 134 | + const tutorialChild = createMockPage("/tutorial/basics/", "Basics"); |
| 135 | + const tutorial = createMockPage("/tutorial/", "Tutorial", [tutorialChild]); |
| 136 | + |
| 137 | + const docsChild = createMockPage("/docs/reference/", "Reference"); |
| 138 | + const docs = createMockPage("/docs/", "Documentation", [docsChild]); |
| 139 | + |
| 140 | + const [flattenedPages, pagePaths] = flattenDocs([tutorial, docs]); |
| 141 | + |
| 142 | + expect(flattenedPages).toEqual([tutorial, tutorialChild, docs, docsChild]); |
| 143 | + expect(pagePaths).toEqual([ |
| 144 | + [tutorial], |
| 145 | + [tutorial, tutorialChild], |
| 146 | + [docs], |
| 147 | + [docs, docsChild], |
| 148 | + ]); |
| 149 | + }); |
| 150 | + |
| 151 | + it("パス情報が正しく設定される", () => { |
| 152 | + const grandChild = createMockPage("/a/b/c/", "C"); |
| 153 | + const child = createMockPage("/a/b/", "B", [grandChild]); |
| 154 | + const parent = createMockPage("/a/", "A", [child]); |
| 155 | + |
| 156 | + const [flattenedPages, pagePaths] = flattenDocs([parent]); |
| 157 | + |
| 158 | + expect(pagePaths[0]).toEqual([parent]); |
| 159 | + expect(pagePaths[1]).toEqual([parent, child]); |
| 160 | + expect(pagePaths[2]).toEqual([parent, child, grandChild]); |
| 161 | + |
| 162 | + expect(pagePaths[0][0].route).toBe("/a/"); |
| 163 | + expect(pagePaths[1][1].route).toBe("/a/b/"); |
| 164 | + expect(pagePaths[2][2].route).toBe("/a/b/c/"); |
| 165 | + }); |
| 166 | +}); |
0 commit comments