|
| 1 | +import { relative, route, configRoutesToRouteManifest } from "../config/routes"; |
| 2 | + |
| 3 | +describe("relative() route ID generation - Fix #12325", () => { |
| 4 | + describe("Verify updated behavior", () => { |
| 5 | + it("When using relative(), route IDs are generated based on relative paths", () => { |
| 6 | + const { route, layout, index } = relative("app/routes"); |
| 7 | + |
| 8 | + const routes = [ |
| 9 | + layout("_layout.tsx", [ |
| 10 | + route("home", "home.tsx"), |
| 11 | + route("about", "about.tsx"), |
| 12 | + index("_index.tsx"), |
| 13 | + ]), |
| 14 | + ]; |
| 15 | + |
| 16 | + const appDirectory = process.cwd(); |
| 17 | + const manifest = configRoutesToRouteManifest(appDirectory, routes); |
| 18 | + |
| 19 | + const layoutRoute = Object.values(manifest).find((r) => |
| 20 | + r.file.includes("_layout"), |
| 21 | + ); |
| 22 | + const homeRoute = Object.values(manifest).find((r) => |
| 23 | + r.file.includes("home"), |
| 24 | + ); |
| 25 | + const indexRoute = Object.values(manifest).find((r) => |
| 26 | + r.file.includes("_index"), |
| 27 | + ); |
| 28 | + |
| 29 | + console.log("Layout ID:", layoutRoute?.id); |
| 30 | + console.log("Home ID:", homeRoute?.id); |
| 31 | + console.log("Index ID:", indexRoute?.id); |
| 32 | + |
| 33 | + expect(layoutRoute?.id).toBe("app/routes/_layout"); |
| 34 | + expect(homeRoute?.id).toBe("app/routes/home"); |
| 35 | + expect(indexRoute?.id).toBe("app/routes/_index"); |
| 36 | + |
| 37 | + expect(layoutRoute?.id).not.toContain(process.cwd()); |
| 38 | + expect(homeRoute?.id).not.toContain(process.cwd()); |
| 39 | + expect(indexRoute?.id).not.toContain(process.cwd()); |
| 40 | + }); |
| 41 | + |
| 42 | + it("Even when using normal route(), relative path IDs are still generated", () => { |
| 43 | + const routes = [route("home", "app/routes/home.tsx")]; |
| 44 | + |
| 45 | + const appDirectory = process.cwd(); |
| 46 | + const manifest = configRoutesToRouteManifest(appDirectory, routes); |
| 47 | + |
| 48 | + const homeRoute = Object.values(manifest)[0]; |
| 49 | + |
| 50 | + console.log("Normal route ID:", homeRoute.id); |
| 51 | + |
| 52 | + expect(homeRoute.id).toBe("app/routes/home"); |
| 53 | + expect(homeRoute.id).not.toContain(process.cwd()); |
| 54 | + }); |
| 55 | + |
| 56 | + it("If an ID is explicitly specified, it is preserved as is", () => { |
| 57 | + const { route } = relative("app/routes"); |
| 58 | + |
| 59 | + const routes = [route("home", "home.tsx", { id: "custom-home-id" })]; |
| 60 | + |
| 61 | + const appDirectory = process.cwd(); |
| 62 | + const manifest = configRoutesToRouteManifest(appDirectory, routes); |
| 63 | + const homeRoute = Object.values(manifest)[0]; |
| 64 | + |
| 65 | + expect(homeRoute.id).toBe("custom-home-id"); |
| 66 | + }); |
| 67 | + |
| 68 | + it("Nested relative paths are handled correctly", () => { |
| 69 | + const { route, layout } = relative("app/routes/admin"); |
| 70 | + |
| 71 | + const routes = [ |
| 72 | + layout("_layout.tsx", [ |
| 73 | + route("users", "users.tsx"), |
| 74 | + route("settings", "settings.tsx"), |
| 75 | + ]), |
| 76 | + ]; |
| 77 | + |
| 78 | + const appDirectory = process.cwd(); |
| 79 | + const manifest = configRoutesToRouteManifest(appDirectory, routes); |
| 80 | + |
| 81 | + const layoutRoute = Object.values(manifest).find((r) => |
| 82 | + r.file.includes("_layout"), |
| 83 | + ); |
| 84 | + const usersRoute = Object.values(manifest).find((r) => |
| 85 | + r.file.includes("users"), |
| 86 | + ); |
| 87 | + |
| 88 | + expect(layoutRoute?.id).toBe("app/routes/admin/_layout"); |
| 89 | + expect(usersRoute?.id).toBe("app/routes/admin/users"); |
| 90 | + }); |
| 91 | + |
| 92 | + it("Route IDs returned from useMatches() should be relative paths", () => { |
| 93 | + const { route, layout, index } = relative("app/routes"); |
| 94 | + const routes = [ |
| 95 | + layout("root.tsx", [index("_index.tsx"), route("about", "about.tsx")]), |
| 96 | + ]; |
| 97 | + |
| 98 | + const manifest = configRoutesToRouteManifest(process.cwd(), routes); |
| 99 | + const matchIds = Object.keys(manifest); |
| 100 | + |
| 101 | + console.log("Route IDs that useMatches() will return:", matchIds); |
| 102 | + |
| 103 | + expect(matchIds).toEqual([ |
| 104 | + "app/routes/root", |
| 105 | + "app/routes/_index", |
| 106 | + "app/routes/about", |
| 107 | + ]); |
| 108 | + }); |
| 109 | + }); |
| 110 | + |
| 111 | + describe("Various signature tests", () => { |
| 112 | + it("All route() overloads work correctly", () => { |
| 113 | + const { route } = relative("app/routes"); |
| 114 | + |
| 115 | + const r1 = route("path1", "file1.tsx"); |
| 116 | + expect(r1.id).toBe("app/routes/file1"); |
| 117 | + |
| 118 | + const r2 = route("path2", "file2.tsx", [route("child", "child.tsx")]); |
| 119 | + expect(r2.id).toBe("app/routes/file2"); |
| 120 | + |
| 121 | + const r3 = route("path3", "file3.tsx", { caseSensitive: true }); |
| 122 | + expect(r3.id).toBe("app/routes/file3"); |
| 123 | + |
| 124 | + const r4 = route("path4", "file4.tsx", { index: true }, [ |
| 125 | + route("child", "child.tsx"), |
| 126 | + ]); |
| 127 | + expect(r4.id).toBe("app/routes/file4"); |
| 128 | + }); |
| 129 | + |
| 130 | + it("All layout() overloads work correctly", () => { |
| 131 | + const { layout } = relative("app/routes"); |
| 132 | + |
| 133 | + const l1 = layout("layout1.tsx"); |
| 134 | + expect(l1.id).toBe("app/routes/layout1"); |
| 135 | + |
| 136 | + const l2 = layout("layout2.tsx", [route("child", "child.tsx")]); |
| 137 | + expect(l2.id).toBe("app/routes/layout2"); |
| 138 | + |
| 139 | + const l3 = layout("layout3.tsx", { id: "custom" }); |
| 140 | + expect(l3.id).toBe("custom"); |
| 141 | + |
| 142 | + const l4 = layout("layout4.tsx", {}, [route("child", "child.tsx")]); |
| 143 | + expect(l4.id).toBe("app/routes/layout4"); |
| 144 | + }); |
| 145 | + }); |
| 146 | +}); |
0 commit comments