|
1 | | -import { buildBreadcrumb } from "../build-breadcrumbs" |
| 1 | +import { describe, expect, it, vi } from "vitest" |
2 | 2 | import type { SidebarSection } from "../sidebar" |
3 | 3 |
|
| 4 | +vi.mock("~/utils/split-slug-and-append-version", () => ({ |
| 5 | + splitSlugAndAppendVersion: (slug: string) => { |
| 6 | + const parts = slug.split("/").filter(Boolean) |
| 7 | + const version = "v1.0.0" |
| 8 | + |
| 9 | + if (parts.length === 2) { |
| 10 | + const [section, filename] = parts |
| 11 | + return { version, section, filename } |
| 12 | + } |
| 13 | + if (parts.length === 3) { |
| 14 | + const [section, subsection, filename] = parts |
| 15 | + return { version, section, subsection, filename } |
| 16 | + } |
| 17 | + |
| 18 | + throw new Error(`Bad slug in test: ${slug}`) |
| 19 | + }, |
| 20 | +})) |
| 21 | + |
| 22 | +import { buildBreadcrumb } from "../build-breadcrumbs" |
| 23 | + |
4 | 24 | type Doc = { slug: string; title: string } |
5 | | -const sec = (over: Partial<SidebarSection>) => ({ |
| 25 | +const makeDoc = (slug: string, title: string): Doc => ({ slug, title }) |
| 26 | + |
| 27 | +type MinimalSection = Pick<SidebarSection, "title" | "slug" | "documentationPages" | "subsections"> |
| 28 | +const makeSection = (overrides: Partial<MinimalSection> = {}) => ({ |
6 | 29 | title: "", |
7 | 30 | slug: "", |
8 | | - sectionId: "", |
9 | 31 | documentationPages: [], |
10 | 32 | subsections: [], |
11 | | - ...over, |
| 33 | + ...overrides, |
12 | 34 | }) |
13 | 35 |
|
14 | | -const doc = (slug: string, title: string): Doc => ({ slug, title }) |
15 | | - |
16 | | -describe("buildBreadcrumb test suite", () => { |
| 36 | +describe("buildBreadcrumb (versioned paths via splitSlugAndAppendVersion)", () => { |
17 | 37 | it("returns [] when pathname doesn't match any doc", () => { |
18 | 38 | const items = [ |
19 | | - sec({ |
| 39 | + makeSection({ |
20 | 40 | title: "Getting Started", |
21 | | - slug: "v1/started", |
22 | | - documentationPages: [doc("v1/started/intro", "Intro")], |
| 41 | + slug: "getting-started", |
| 42 | + documentationPages: [makeDoc("getting-started/intro", "Intro")], |
23 | 43 | }), |
24 | 44 | ] |
25 | | - |
26 | | - // biome-ignore lint/suspicious/noExplicitAny: we can use any in tests |
27 | | - const result = buildBreadcrumb(items as any, "/v1/started/unknown") |
28 | | - expect(result).toEqual([]) |
| 45 | + expect(buildBreadcrumb(items, "/v1.0.0/getting-started/unknown")).toEqual([]) |
29 | 46 | }) |
30 | 47 |
|
31 | 48 | it("returns [section, doc] for a top-level doc", () => { |
32 | 49 | const items = [ |
33 | | - sec({ |
| 50 | + makeSection({ |
34 | 51 | title: "Getting Started", |
35 | | - slug: "v1/started", |
36 | | - documentationPages: [doc("v1/started/intro", "Intro")], |
| 52 | + slug: "getting-started", |
| 53 | + documentationPages: [makeDoc("getting-started/intro", "Intro")], |
37 | 54 | }), |
38 | 55 | ] |
39 | | - |
40 | | - // biome-ignore lint/suspicious/noExplicitAny: we can use any in tests |
41 | | - const result = buildBreadcrumb(items as any, "/v1/started/intro") |
42 | | - expect(result).toEqual(["Getting Started", "Intro"]) |
| 56 | + expect(buildBreadcrumb(items, "/v1.0.0/getting-started/intro")).toEqual(["Getting Started", "Intro"]) |
43 | 57 | }) |
44 | 58 |
|
45 | 59 | it("returns full trail for a nested doc (root → sub → doc)", () => { |
46 | 60 | const items = [ |
47 | | - sec({ |
| 61 | + makeSection({ |
48 | 62 | title: "Configuration", |
49 | | - slug: "v1/configuration", |
| 63 | + slug: "configuration", |
50 | 64 | subsections: [ |
51 | | - sec({ |
| 65 | + makeSection({ |
52 | 66 | title: "Advanced", |
53 | | - slug: "v1/configuration/advanced", |
54 | | - documentationPages: [doc("v1/configuration/advanced/tuning", "Tuning")], |
55 | | - }), |
56 | | - ], |
57 | | - documentationPages: [doc("v1/configuration/setup", "Setup")], |
58 | | - }), |
59 | | - ] |
60 | | - |
61 | | - // biome-ignore lint/suspicious/noExplicitAny: we can use any in tests |
62 | | - const result = buildBreadcrumb(items as any, "/v1/configuration/advanced/tuning") |
63 | | - expect(result).toEqual(["Configuration", "Advanced", "Tuning"]) |
64 | | - }) |
65 | | - |
66 | | - it("stops at the first matching branch across multiple roots", () => { |
67 | | - const items = [ |
68 | | - sec({ |
69 | | - title: "Alpha", |
70 | | - slug: "v1/alpha", |
71 | | - documentationPages: [doc("v1/alpha/readme", "Readme")], |
72 | | - }), |
73 | | - sec({ |
74 | | - title: "Beta", |
75 | | - slug: "v1/beta", |
76 | | - subsections: [ |
77 | | - sec({ |
78 | | - title: "Deep", |
79 | | - slug: "v1/beta/deep", |
80 | | - documentationPages: [doc("v1/beta/deep/file", "File")], |
| 67 | + slug: "configuration/advanced", |
| 68 | + documentationPages: [makeDoc("configuration/advanced/tuning", "Tuning")], |
81 | 69 | }), |
82 | 70 | ], |
| 71 | + documentationPages: [makeDoc("configuration/setup", "Setup")], |
83 | 72 | }), |
84 | 73 | ] |
85 | | - |
86 | | - // biome-ignore lint/suspicious/noExplicitAny: we can use any in tests |
87 | | - const result1 = buildBreadcrumb(items as any, "/v1/alpha/readme") |
88 | | - expect(result1).toEqual(["Alpha", "Readme"]) |
89 | | - |
90 | | - // biome-ignore lint/suspicious/noExplicitAny: we can use any in tests |
91 | | - const result2 = buildBreadcrumb(items as any, "/v1/beta/deep/file") |
92 | | - expect(result2).toEqual(["Beta", "Deep", "File"]) |
| 74 | + expect(buildBreadcrumb(items, "/v1.0.0/configuration/advanced/tuning")).toEqual([ |
| 75 | + "Configuration", |
| 76 | + "Advanced", |
| 77 | + "Tuning", |
| 78 | + ]) |
93 | 79 | }) |
94 | 80 | }) |
0 commit comments