|
| 1 | +import { createRootRoute, createRoute, createRouter } from "@tanstack/react-router"; |
| 2 | + |
| 3 | +import { buildSitemapXml, collectSitemapEntries } from "./sitemap"; |
| 4 | + |
| 5 | +/** |
| 6 | + * Builds a real router from a real route tree - the same shape the app ships - |
| 7 | + * so the collector is exercised against the router's own path table rather than |
| 8 | + * a hand-written stand-in that could drift from it. |
| 9 | + */ |
| 10 | +const createTestRouter = () => { |
| 11 | + const rootRoute = createRootRoute(); |
| 12 | + |
| 13 | + const indexRoute = createRoute({ getParentRoute: () => rootRoute, path: "/" }); |
| 14 | + const pricingRoute = createRoute({ |
| 15 | + getParentRoute: () => rootRoute, |
| 16 | + path: "/pricing", |
| 17 | + staticData: { sitemap: { changeFrequency: "weekly", priority: 0.8 } }, |
| 18 | + }); |
| 19 | + const settingsRoute = createRoute({ |
| 20 | + getParentRoute: () => rootRoute, |
| 21 | + path: "/settings", |
| 22 | + staticData: { sitemap: false }, |
| 23 | + }); |
| 24 | + const postsRoute = createRoute({ getParentRoute: () => rootRoute, path: "/posts" }); |
| 25 | + const postRoute = createRoute({ getParentRoute: () => postsRoute, path: "/$postId" }); |
| 26 | + const feedRoute = createRoute({ |
| 27 | + getParentRoute: () => rootRoute, |
| 28 | + path: "/feed.xml", |
| 29 | + server: { handlers: { GET: () => new Response("") } }, |
| 30 | + }); |
| 31 | + |
| 32 | + return createRouter({ |
| 33 | + routeTree: rootRoute.addChildren([ |
| 34 | + indexRoute, |
| 35 | + pricingRoute, |
| 36 | + settingsRoute, |
| 37 | + postsRoute.addChildren([postRoute]), |
| 38 | + feedRoute, |
| 39 | + ]), |
| 40 | + }); |
| 41 | +}; |
| 42 | + |
| 43 | +describe("collectSitemapEntries", () => { |
| 44 | + it("lists every static page in the route tree", () => { |
| 45 | + // Arrange |
| 46 | + const router = createTestRouter(); |
| 47 | + |
| 48 | + // Act |
| 49 | + const entries = collectSitemapEntries(router.routesByPath); |
| 50 | + |
| 51 | + // Assert |
| 52 | + expect(entries.map((entry) => entry.path)).toStrictEqual(["/", "/pricing"]); |
| 53 | + }); |
| 54 | + |
| 55 | + it("keeps routes opted out with `staticData.sitemap: false` out", () => { |
| 56 | + // Arrange |
| 57 | + const router = createTestRouter(); |
| 58 | + |
| 59 | + // Act |
| 60 | + const entries = collectSitemapEntries(router.routesByPath); |
| 61 | + |
| 62 | + // Assert |
| 63 | + expect(entries.map((entry) => entry.path)).not.toContain("/settings"); |
| 64 | + }); |
| 65 | + |
| 66 | + it("skips dynamic routes and the layouts that wrap them", () => { |
| 67 | + // Arrange |
| 68 | + const router = createTestRouter(); |
| 69 | + |
| 70 | + // Act |
| 71 | + const paths = collectSitemapEntries(router.routesByPath).map((entry) => entry.path); |
| 72 | + |
| 73 | + // Assert |
| 74 | + expect(paths).not.toContain("/posts/$postId"); |
| 75 | + expect(paths).not.toContain("/posts"); |
| 76 | + }); |
| 77 | + |
| 78 | + it("skips routes that only answer with a server handler", () => { |
| 79 | + // Arrange |
| 80 | + const router = createTestRouter(); |
| 81 | + |
| 82 | + // Act |
| 83 | + const paths = collectSitemapEntries(router.routesByPath).map((entry) => entry.path); |
| 84 | + |
| 85 | + // Assert |
| 86 | + expect(paths).not.toContain("/feed.xml"); |
| 87 | + }); |
| 88 | + |
| 89 | + it("carries the route's own sitemap options over", () => { |
| 90 | + // Arrange |
| 91 | + const router = createTestRouter(); |
| 92 | + |
| 93 | + // Act |
| 94 | + const entries = collectSitemapEntries(router.routesByPath); |
| 95 | + |
| 96 | + // Assert |
| 97 | + expect(entries).toContainEqual({ path: "/pricing", changeFrequency: "weekly", priority: 0.8 }); |
| 98 | + }); |
| 99 | +}); |
| 100 | + |
| 101 | +describe("buildSitemapXml", () => { |
| 102 | + it("renders absolute URLs under the given origin", () => { |
| 103 | + // Act |
| 104 | + const xml = buildSitemapXml("https://example.com", [{ path: "/" }, { path: "/pricing" }]); |
| 105 | + |
| 106 | + // Assert |
| 107 | + expect(xml).toContain("<loc>https://example.com/</loc>"); |
| 108 | + expect(xml).toContain("<loc>https://example.com/pricing</loc>"); |
| 109 | + }); |
| 110 | + |
| 111 | + it("renders the optional per-entry hints", () => { |
| 112 | + // Act |
| 113 | + const xml = buildSitemapXml("https://example.com", [ |
| 114 | + { |
| 115 | + path: "/pricing", |
| 116 | + changeFrequency: "weekly", |
| 117 | + priority: 0.8, |
| 118 | + lastModified: new Date("2026-01-01T00:00:00.000Z"), |
| 119 | + }, |
| 120 | + ]); |
| 121 | + |
| 122 | + // Assert |
| 123 | + expect(xml).toContain("<lastmod>2026-01-01T00:00:00.000Z</lastmod>"); |
| 124 | + expect(xml).toContain("<changefreq>weekly</changefreq>"); |
| 125 | + expect(xml).toContain("<priority>0.8</priority>"); |
| 126 | + }); |
| 127 | + |
| 128 | + it("escapes characters that would break the document", () => { |
| 129 | + // Act |
| 130 | + const xml = buildSitemapXml("https://example.com", [{ path: "/search?q=a&b" }]); |
| 131 | + |
| 132 | + // Assert |
| 133 | + expect(xml).toContain("<loc>https://example.com/search?q=a&b</loc>"); |
| 134 | + }); |
| 135 | +}); |
0 commit comments