-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
104 lines (83 loc) · 2.77 KB
/
test.js
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import t from 'libtap';
import * as pathRouter from '@cfware/path-router';
const {createRouter, setExactRoute, setSubRouter, findRoute, executeRoute} = pathRouter;
t.same(Object.keys(pathRouter).sort(), [
'createRouter',
'setExactRoute',
'setSubRouter',
'findRoute',
'executeRoute'
].sort());
t.test('empty route returns undefined', async t => {
const router = createRouter();
t.type(findRoute(router, ''));
t.type(findRoute(router, 'page'));
t.type(findRoute(router, 'path/page'));
});
t.test('exact route replacement', async t => {
const info = {
fn1: 0,
fn2: 0
};
const fn1 = () => info.fn1++;
const fn2 = () => info.fn2++;
const router = createRouter();
setExactRoute(router, 'path', fn1);
executeRoute(router, 'path');
t.same(info, {fn1: 1, fn2: 0});
setExactRoute(router, 'path', fn2);
executeRoute(router, 'path');
t.same(info, {fn1: 1, fn2: 1});
});
t.test('default route match', async t => {
const info = {
hit: 0,
fn1: 0
};
const defaultRoute = () => info.hit++;
const fn1 = () => info.fn1++;
const router = createRouter(defaultRoute);
setExactRoute(router, 'path', fn1);
executeRoute(router, 'path/');
t.same(info, {hit: 1, fn1: 0});
});
t.test('longest subrouter match', async t => {
const info = {
hit: 0,
router1: 0,
router2: 0
};
const router = createRouter(() => info.hit++);
setSubRouter(router, 'path/sub', createRouter(() => info.router1++));
setSubRouter(router, 'path', createRouter(() => info.router2++));
executeRoute(router, '');
t.same(info, {hit: 1, router1: 0, router2: 0});
executeRoute(router, 'path');
t.same(info, {hit: 1, router1: 0, router2: 1});
executeRoute(router, 'path/su');
t.same(info, {hit: 1, router1: 0, router2: 2});
executeRoute(router, 'path/sub');
t.same(info, {hit: 1, router1: 1, router2: 2});
executeRoute(router, 'path/subby');
t.same(info, {hit: 1, router1: 2, router2: 2});
executeRoute(router, 'unknown');
t.same(info, {hit: 2, router1: 2, router2: 2});
setSubRouter(router, 'broken', createRouter());
executeRoute(router, 'broken');
t.same(info, {hit: 3, router1: 2, router2: 2});
});
t.test('fallthrough to shorter subrouter match', async t => {
const info = {
hit: 0,
router: 0
};
const router = createRouter(() => info.hit++);
setSubRouter(router, 'path/sub', createRouter());
setSubRouter(router, 'path', createRouter(() => info.router++));
executeRoute(router, 'path/sub');
t.same(info, {hit: 0, router: 1});
executeRoute(router, 'path/sub/sub');
t.same(info, {hit: 0, router: 2});
executeRoute(router, 'path');
t.same(info, {hit: 0, router: 3});
});