/* eslint-disable regexp/no-useless-non-capturing-group */ /* eslint-disable regexp/no-useless-escape */ /* eslint-disable no-useless-escape */ /* eslint-disable regexp/no-empty-group */ import { describe, expect, test } from "vitest"; import type { RouteMatch } from "../../router/findMatch.ts"; import type { NotLocalizedEndRoute, NotLocalizedSegmentRoute, } from "../../routes/index.ts"; import { createRouterBuilder } from "../createRouterBuilder.ts"; describe("blog", () => { const ref = Symbol("ref"); const builder = createRouterBuilder() .add("/", ref) .addSegment("/post", (segmentBuilder) => { segmentBuilder.defaultRoute(ref, "postList"); segmentBuilder.add("/:id.:slug", ref, "postView"); segmentBuilder.addSegment("/search", (subSegmentBuilder) => { subSegmentBuilder.defaultRoute("refsearch", "search"); subSegmentBuilder.add("/:term", ref, "search-results"); }); segmentBuilder.add("{/:tag}/:date{_:slug}", ref, "postWithTag"); }); const router = builder.createRouter(); describe("routes", () => { const routes = builder.getRoutes(); test("length", () => { expect(routes.length).toBe(2); }); test("home", () => { const homeRoute = routes[0] as NotLocalizedEndRoute; expect(homeRoute.path.path).toBe("/"); }); describe("post", () => { const postRouterRoute = routes[1] as NotLocalizedSegmentRoute; const routePath = postRouterRoute.path; test("path", () => { expect(routePath).toHaveProperty("path", "/post"); expect(routePath).toHaveProperty("completePath", "/post"); expect(routePath.regExp).toEqual(/^(?:\/post)(?=\/|$)/); }); describe("nested routes", () => { const { nestedRoutes } = postRouterRoute; test("/post should have 3 nested routes", () => { expect(nestedRoutes.length).toBe(3); }); test("first nested route", () => { const nestedRoute = nestedRoutes[0] as NotLocalizedSegmentRoute< any, any >; expect(nestedRoute.path.path).toBe("/:id.:slug"); expect(nestedRoute.path.completePath).toBe("/post/:id.:slug"); expect(nestedRoute.path.regExp).toEqual( /^(?:\/([^\/]+)\.([^\/\.]+|\.))$/, ); }); test("third nested route", () => { const nestedRoute = nestedRoutes[2] as NotLocalizedSegmentRoute< any, any >; expect(nestedRoute.path.path).toBe("{/:tag}/:date{_:slug}"); expect(nestedRoute.path.completePath).toBe( "/post{/:tag}/:date{_:slug}", ); expect(nestedRoute.path.regExp).toEqual( /^(?:\/([^\/]+)\/([^\/]+)_([^\/_]+|_)|\/([^\/]+)\/([^\/]+)|\/([^\/]+)_([^\/_]+|_)|\/([^\/]+))$/, ); }); }); test("default route", () => { const defaultRoute = postRouterRoute.defaultRoute!; expect(defaultRoute).toBeDefined(); expect(defaultRoute.path).toHaveProperty("completePath", "/post"); expect(defaultRoute.path).toHaveProperty("namedParams", []); expect(defaultRoute.path).toHaveProperty("path", ""); expect(defaultRoute.path).toHaveProperty("regExp", /^(?:)$/); expect(defaultRoute.path).toHaveProperty("toPath"); }); }); test("postView", () => { const rrPostView = router.get("postView") as NotLocalizedEndRoute< any, any >; expect(rrPostView.path.namedParams).toEqual(["id", "slug"]); expect( rrPostView.path.toPath({ id: "001", slug: "The-First-Post" }), ).toBe("/post/001.The-First-Post"); }); }); describe("find", () => { test("postList", () => { const path = "/post"; const match = router.find(path) as RouteMatch; expect(match).toHaveProperty("path", path); expect(match).toHaveProperty("route", router.get("postList")); expect(match.namedParams).toBe(undefined); }); test("postView", () => { const path = "/post/001.The-First-Post"; const match = router.find(path) as RouteMatch; expect(match).toHaveProperty("path", path); expect(match.namedParams).toEqual( new Map([ ["id", "001"], ["slug", "The-First-Post"], ]), ); }); test("search", () => { const path = "/post/search"; const match = router.find(path) as RouteMatch; expect(match).toHaveProperty("path", path); const route: any = match.route; expect(route.path.completePath).toBe( (router.get("search") as NotLocalizedEndRoute).path .completePath, ); expect(match.ref).toBe("refsearch"); }); test("search", () => { const path = "/post/search/searchedterm"; const match = router.find(path) as RouteMatch; expect(match).toHaveProperty("path", path); expect(match).toHaveProperty("route", router.get("search-results")); expect(match.namedParams).toEqual(new Map([["term", "searchedterm"]])); }); }); });