Skip to content

Commit dc94116

Browse files
committed
add basic tests for createBranches
1 parent 8649ad2 commit dc94116

1 file changed

Lines changed: 50 additions & 2 deletions

File tree

test/route.spec.ts

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { vi } from 'vitest'
1+
import { vi } from "vitest";
22
import { createBranch, createBranches, createRoutes } from "../src/routing.js";
33
import type { RouteDefinition } from "../src/index.js";
44

@@ -174,7 +174,6 @@ describe("createRoutes should", () => {
174174
expect(match).not.toBeNull();
175175
expect(match.path).toBe("/foo/123/bar/solid.html");
176176
});
177-
178177
});
179178

180179
describe(`expand optional parameters`, () => {
@@ -564,4 +563,53 @@ describe("createBranches should", () => {
564563

565564
expect(branchPaths).toEqual(["/root/%E3%81%BB%E3%81%92/:ふが/*ぴよ"]);
566565
});
566+
567+
describe(`traverse slots`, () => {
568+
test("with no children", () => {
569+
const branches = createBranches({
570+
path: "root",
571+
children: {
572+
path: "nested",
573+
slots: { nestedSlot: {} }
574+
},
575+
slots: { rootSlot: {} }
576+
});
577+
578+
const root = branches[0].routes.find(r => r.originalPath === "root")!;
579+
expect(root.slots).toHaveProperty("rootSlot");
580+
const rootSlot = root.slots!.rootSlot;
581+
expect(rootSlot.length).toBe(1);
582+
expect(rootSlot[0].routes.length).toBe(1);
583+
expect(rootSlot[0].routes[0].pattern).toEqual("/root");
584+
585+
const nested = branches[0].routes.find(r => r.originalPath === "nested")!;
586+
expect(nested.slots).toHaveProperty("nestedSlot");
587+
const nestedSlot = nested.slots!.nestedSlot;
588+
expect(nestedSlot.length).toBe(1);
589+
expect(nestedSlot[0].routes.length).toBe(1);
590+
expect(nestedSlot[0].routes[0].pattern).toEqual("/root/nested");
591+
});
592+
593+
test("with children", () => {
594+
const branches = createBranches({
595+
path: "root",
596+
children: {
597+
path: "nested",
598+
slots: { nestedSlot: { children: [{ path: "one" }, { path: "two" }] } }
599+
},
600+
slots: { rootSlot: { children: [{ path: "one" }, { path: "two" }] } }
601+
});
602+
603+
const rootSlot = branches[0].routes.find(r => r.originalPath === "root")!.slots!.rootSlot;
604+
expect(rootSlot.length).toBe(2);
605+
expect(rootSlot[1].routes.length).toBe(2);
606+
expect(rootSlot[1].routes[1].pattern).toEqual("/root/two");
607+
608+
const nestedSlot = branches[0].routes.find(r => r.originalPath === "nested")!.slots!
609+
.nestedSlot;
610+
expect(nestedSlot.length).toBe(2);
611+
expect(nestedSlot[1].routes.length).toBe(2);
612+
expect(nestedSlot[1].routes[1].pattern).toEqual("/root/nested/two");
613+
});
614+
});
567615
});

0 commit comments

Comments
 (0)