-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrouter.test.ts
55 lines (44 loc) · 2 KB
/
router.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import { expect, test } from "bun:test";
import Router, { RouterNode } from "./router.ts";
const router = new Router();
test("Register multiple static get routes", () => {
router.Get("/users", handler);
router.Get("/users/profile", handler);
router.Get("/users/profile/update", handler);
router.Get("/users/profile/settings", handler);
router.Get("/videos/recommended", handler);
expect(router.getRoot().path).toBe("/");
expect(router.getRoot().children[0].path).toBe("users");
expect(router.getRoot().children[0].children[0].path).toBe("profile");
expect(router.getRoot().children[0].children[0].children[0].path).toBe("update");
expect(router.getRoot().children[0].children[0].children[1].path).toBe("settings");
expect(router.getRoot().children[1].path).toBe("videos");
});
test("Register multiple static get routes with new intermediate paths", () => {
router.Get("/users", handler);
router.Get("/users/profile/update", handler);
expect(router.getRoot().path).toBe("/");
expect(router.getRoot().children[0].path).toBe("users");
expect(router.getRoot().children[0].children[0].path).toBe("profile");
expect(router.getRoot().children[0].children[0].children[0].path).toBe("update");
expect(router.getRoot().children[0].handler).toBe(handler);
expect(router.getRoot().children[0].children[0].handler).toBeEmpty();
});
test("Register static get routes and match routes", () => {
router.Get("/", homeHandler);
router.Get("/user/profile", profileHandler);
expect(router.getMatch("/")).toBeInstanceOf(RouterNode);
expect(router.getMatch("/")?.handler).toBe(homeHandler);
expect(router.getMatch("/video")).toBeNull();
expect(router.getMatch("/user/profile")).toBeInstanceOf(RouterNode);
expect(router.getMatch("/user/profile")?.handler).toBe(profileHandler);
});
function profileHandler() {
console.log('profileHandler');
}
function homeHandler() {
console.log('homeHandler');
}
function handler() {
console.log('handler');
}