UNPKG

one

Version:

One is a new React Framework that makes Vite serve both native and web.

155 lines 6.97 kB
var import_vitest = require("vitest"), import_createHandleRequest = require("./createHandleRequest.cjs"); import_vitest.vi.mock("./vite/getManifest", () => ({ getManifest: () => ({ pageRoutes: [{ file: "app/[slug].tsx", page: "/[slug]", namedRegex: "^/(?<nxtPslug>[^/]+?)(?:/)?$", routeKeys: { nxtPslug: "slug" }, type: "ssr", middlewares: [] }, { file: "app/index.tsx", page: "/", namedRegex: "^/(?:/)?$", routeKeys: {}, type: "ssr", middlewares: [] }], apiRoutes: [] }) })); function createRequest(path) { return new Request(`http://localhost:3000${path}`, { headers: { host: "localhost:3000" } }); } (0, import_vitest.describe)("createHandleRequest", () => { const mockHandlers = { handlePage: import_vitest.vi.fn().mockResolvedValue("<html></html>") }; (0, import_vitest.beforeEach)(() => { import_vitest.vi.clearAllMocks(); }), (0, import_vitest.describe)("static file extension filtering", () => { (0, import_vitest.it)("should skip paths with file extensions (favicon.ico)", async () => { const { handler } = (0, import_createHandleRequest.createHandleRequest)(mockHandlers, { routerRoot: "/app" }), result = await handler(createRequest("/favicon.ico")); (0, import_vitest.expect)(result).toBeNull(), (0, import_vitest.expect)(mockHandlers.handlePage).not.toHaveBeenCalled(); }), (0, import_vitest.it)("should skip paths with file extensions (logo.png)", async () => { const { handler } = (0, import_createHandleRequest.createHandleRequest)(mockHandlers, { routerRoot: "/app" }), result = await handler(createRequest("/logo.png")); (0, import_vitest.expect)(result).toBeNull(), (0, import_vitest.expect)(mockHandlers.handlePage).not.toHaveBeenCalled(); }), (0, import_vitest.it)("should skip paths with file extensions (styles.css)", async () => { const { handler } = (0, import_createHandleRequest.createHandleRequest)(mockHandlers, { routerRoot: "/app" }), result = await handler(createRequest("/styles.css")); (0, import_vitest.expect)(result).toBeNull(), (0, import_vitest.expect)(mockHandlers.handlePage).not.toHaveBeenCalled(); }), (0, import_vitest.it)("should skip paths with file extensions (robots.txt)", async () => { const { handler } = (0, import_createHandleRequest.createHandleRequest)(mockHandlers, { routerRoot: "/app" }), result = await handler(createRequest("/robots.txt")); (0, import_vitest.expect)(result).toBeNull(), (0, import_vitest.expect)(mockHandlers.handlePage).not.toHaveBeenCalled(); }), (0, import_vitest.it)("should skip nested paths with file extensions", async () => { const { handler } = (0, import_createHandleRequest.createHandleRequest)(mockHandlers, { routerRoot: "/app" }), result = await handler(createRequest("/assets/images/logo.png")); (0, import_vitest.expect)(result).toBeNull(), (0, import_vitest.expect)(mockHandlers.handlePage).not.toHaveBeenCalled(); }), (0, import_vitest.it)("should skip extensions 2-4 chars like .xyz", async () => { const { handler } = (0, import_createHandleRequest.createHandleRequest)(mockHandlers, { routerRoot: "/app" }), result = await handler(createRequest("/somefile.xyz")); (0, import_vitest.expect)(result).toBeNull(), (0, import_vitest.expect)(mockHandlers.handlePage).not.toHaveBeenCalled(); }), (0, import_vitest.it)("should NOT skip extensions longer than 4 chars (routes with dots in names)", async () => { const { handler } = (0, import_createHandleRequest.createHandleRequest)(mockHandlers, { routerRoot: "/app" }); await handler(createRequest("/route.normal")), (0, import_vitest.expect)(mockHandlers.handlePage).toHaveBeenCalled(); }), (0, import_vitest.it)("should NOT skip 5+ char extensions like .woff2", async () => { const { handler } = (0, import_createHandleRequest.createHandleRequest)(mockHandlers, { routerRoot: "/app" }); await handler(createRequest("/font.woff2")), (0, import_vitest.expect)(mockHandlers.handlePage).toHaveBeenCalled(); }), (0, import_vitest.it)("should NOT skip loader paths ending with _vxrn_loader.js", async () => { const mockHandlersWithLoader = { handlePage: import_vitest.vi.fn().mockResolvedValue("<html></html>"), handleLoader: import_vitest.vi.fn().mockResolvedValue("loader data") }, { handler } = (0, import_createHandleRequest.createHandleRequest)(mockHandlersWithLoader, { routerRoot: "/app" }); await handler(createRequest("/assets/my-page_123_vxrn_loader.js")), (0, import_vitest.expect)(mockHandlersWithLoader.handleLoader).toHaveBeenCalled(); }); }), (0, import_vitest.describe)("dynamic route matching", () => { (0, import_vitest.it)("should match dynamic routes for regular paths without extensions", async () => { const { handler } = (0, import_createHandleRequest.createHandleRequest)(mockHandlers, { routerRoot: "/app" }); await handler(createRequest("/my-page")), (0, import_vitest.expect)(mockHandlers.handlePage).toHaveBeenCalled(); }), (0, import_vitest.it)("should match index route", async () => { const { handler } = (0, import_createHandleRequest.createHandleRequest)(mockHandlers, { routerRoot: "/app" }); await handler(createRequest("/")), (0, import_vitest.expect)(mockHandlers.handlePage).toHaveBeenCalled(); }), (0, import_vitest.it)("should match paths with hyphens", async () => { const { handler } = (0, import_createHandleRequest.createHandleRequest)(mockHandlers, { routerRoot: "/app" }); await handler(createRequest("/my-awesome-page")), (0, import_vitest.expect)(mockHandlers.handlePage).toHaveBeenCalled(); }); }), (0, import_vitest.describe)("special paths", () => { (0, import_vitest.it)("should skip __vxrnhmr path", async () => { const { handler } = (0, import_createHandleRequest.createHandleRequest)(mockHandlers, { routerRoot: "/app" }), result = await handler(createRequest("/__vxrnhmr")); (0, import_vitest.expect)(result).toBeNull(); }), (0, import_vitest.it)("should skip paths starting with /@", async () => { const { handler } = (0, import_createHandleRequest.createHandleRequest)(mockHandlers, { routerRoot: "/app" }), result = await handler(createRequest("/@fs/some/path")); (0, import_vitest.expect)(result).toBeNull(); }); }); });