|
1 | 1 | import { describe, expect, it, assert } from "vitest"; |
2 | 2 | import { createContentRoutes } from "./routes"; |
3 | 3 | import { EmptyOutlet, SettingsWrapper } from "@/components/content"; |
4 | | -import { defineModule, defineResource, pass } from "@/resource"; |
| 4 | +import { defineModule, defineResource, pass, redirectTo } from "@/resource"; |
5 | 5 |
|
6 | 6 | const createMockResource = (path: string) => |
7 | 7 | defineResource({ |
@@ -160,6 +160,85 @@ describe("createContentRoutes", () => { |
160 | 160 | expect(childResource?.loader).toBeUndefined(); |
161 | 161 | }); |
162 | 162 |
|
| 163 | + it("creates index route with loader and dummy component for module without component but with guards", () => { |
| 164 | + const module = defineModule({ |
| 165 | + path: "redirect-module", |
| 166 | + meta: { title: "Redirect Module" }, |
| 167 | + guards: [() => redirectTo("/dashboard")], |
| 168 | + resources: [createMockResource("child")], |
| 169 | + }); |
| 170 | + |
| 171 | + const routes = createContentRoutes({ |
| 172 | + modules: [module], |
| 173 | + settingsResources: [], |
| 174 | + }); |
| 175 | + |
| 176 | + const moduleContainer = routes[1]; |
| 177 | + const moduleRoute = moduleContainer.children?.[0]; |
| 178 | + expect(moduleRoute?.path).toBe("redirect-module"); |
| 179 | + |
| 180 | + // Index route should exist with loader and dummy component |
| 181 | + const indexRoute = moduleRoute?.children?.[0]; |
| 182 | + expect(indexRoute?.index).toBe(true); |
| 183 | + expect(indexRoute?.loader).toBeDefined(); |
| 184 | + expect(typeof indexRoute?.loader).toBe("function"); |
| 185 | + expect(typeof indexRoute?.Component).toBe("function"); |
| 186 | + |
| 187 | + // Child resource should still be present |
| 188 | + const childRoute = moduleRoute?.children?.[1]; |
| 189 | + expect(childRoute?.path).toBe("child"); |
| 190 | + }); |
| 191 | + |
| 192 | + it("does not create index route for module without component and without guards", () => { |
| 193 | + // This case should throw in defineModule, but we test createRoute's behavior |
| 194 | + // by using a module with component (which produces index route) |
| 195 | + const module = defineModule({ |
| 196 | + path: "no-comp-no-guard", |
| 197 | + meta: { title: "Test" }, |
| 198 | + component: () => <div>Test</div>, |
| 199 | + resources: [createMockResource("child")], |
| 200 | + }); |
| 201 | + |
| 202 | + const routes = createContentRoutes({ |
| 203 | + modules: [module], |
| 204 | + settingsResources: [], |
| 205 | + }); |
| 206 | + |
| 207 | + const moduleContainer = routes[1]; |
| 208 | + const moduleRoute = moduleContainer.children?.[0]; |
| 209 | + const indexRoute = moduleRoute?.children?.[0]; |
| 210 | + expect(indexRoute?.index).toBe(true); |
| 211 | + expect(indexRoute?.Component).toBeDefined(); |
| 212 | + // No guards = no loader |
| 213 | + expect(indexRoute?.loader).toBeUndefined(); |
| 214 | + }); |
| 215 | + |
| 216 | + it("redirect guard loader returns a redirect response", async () => { |
| 217 | + const module = defineModule({ |
| 218 | + path: "redirect-module", |
| 219 | + meta: { title: "Redirect Module" }, |
| 220 | + guards: [() => redirectTo("/dashboard")], |
| 221 | + resources: [createMockResource("child")], |
| 222 | + }); |
| 223 | + |
| 224 | + const routes = createContentRoutes({ |
| 225 | + modules: [module], |
| 226 | + settingsResources: [], |
| 227 | + }); |
| 228 | + |
| 229 | + const moduleContainer = routes[1]; |
| 230 | + const moduleRoute = moduleContainer.children?.[0]; |
| 231 | + const indexRoute = moduleRoute?.children?.[0]; |
| 232 | + const loader = indexRoute?.loader; |
| 233 | + expect(typeof loader).toBe("function"); |
| 234 | + |
| 235 | + // Execute the loader - it should return a redirect |
| 236 | + const result = await (loader as Function)({} as never); |
| 237 | + expect(result).toBeInstanceOf(Response); |
| 238 | + expect((result as Response).status).toBe(302); |
| 239 | + expect((result as Response).headers.get("Location")).toBe("/dashboard"); |
| 240 | + }); |
| 241 | + |
163 | 242 | it("attaches loader to index route only for resource (no cascade to sub-resources)", () => { |
164 | 243 | const module = defineModule({ |
165 | 244 | path: "dashboard", |
|
0 commit comments