-
-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathrouter-match.test.js
More file actions
128 lines (103 loc) · 3.85 KB
/
router-match.test.js
File metadata and controls
128 lines (103 loc) · 3.85 KB
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import { test } from 'uvu';
import * as assert from 'uvu/assert';
import { exec } from '../../src/router.js';
function execPath(path, pattern, opts) {
return exec(path, pattern, { path, query: {}, params: {}, ...(opts || {}) });
}
test('Base route', () => {
const accurateResult = execPath('/', '/');
assert.equal(accurateResult, { path: '/', params: {}, query: {} });
const inaccurateResult = execPath('/user/1', '/');
assert.equal(inaccurateResult, undefined);
});
test('Param route', () => {
const accurateResult = execPath('/user/2', '/user/:id');
assert.equal(accurateResult, { path: '/user/2', params: { id: '2' }, id: '2', query: {} });
const inaccurateResult = execPath('/', '/user/:id');
assert.equal(inaccurateResult, undefined);
});
test('Param rest segment', () => {
const accurateResult = execPath('/user/foo', '/user/*');
assert.equal(accurateResult, { path: '/user/foo', params: {}, query: {}, rest: '/foo' });
const accurateResult2 = execPath('/user/foo/bar/baz', '/user/*');
assert.equal(accurateResult2, { path: '/user/foo/bar/baz', params: {}, query: {}, rest: '/foo/bar/baz' });
const inaccurateResult = execPath('/user', '/user/*');
assert.equal(inaccurateResult, undefined);
});
test('Param route with rest segment', () => {
const accurateResult = execPath('/user/2/foo', '/user/:id/*');
assert.equal(accurateResult, { path: '/user/2/foo', params: { id: '2' }, id: '2', query: {}, rest: '/foo' });
const accurateResult2 = execPath('/user/2/foo/bar/bob', '/user/:id/*');
assert.equal(accurateResult2, {
path: '/user/2/foo/bar/bob',
params: { id: '2' },
id: '2',
query: {},
rest: '/foo/bar/bob'
});
const inaccurateResult = execPath('/', '/user/:id/*');
assert.equal(inaccurateResult, undefined);
});
test('Optional param route', () => {
const accurateResult = execPath('/user', '/user/:id?');
assert.equal(accurateResult, { path: '/user', params: { id: undefined }, id: undefined, query: {} });
const inaccurateResult = execPath('/', '/user/:id?');
assert.equal(inaccurateResult, undefined);
});
test('Optional rest param route "/:x*"', () => {
const matchedResult = execPath('/user/foo', '/user/:id*');
assert.equal(matchedResult, { path: '/user/foo', params: { id: 'foo' }, id: 'foo', query: {} });
const matchedResultWithSlash = execPath('/user/foo/bar', '/user/:id*');
assert.equal(matchedResultWithSlash, {
path: '/user/foo/bar',
params: { id: 'foo/bar' },
id: 'foo/bar',
query: {}
});
const emptyResult = execPath('/user', '/user/:id*');
assert.equal(emptyResult, {
path: '/user',
params: { id: undefined },
id: undefined,
query: {}
});
const inaccurateResult = execPath('/', '/user/:id*');
assert.equal(inaccurateResult, undefined);
});
test('Rest param route "/:x+"', () => {
const matchedResult = execPath('/user/foo', '/user/:id+');
assert.equal(matchedResult, { path: '/user/foo', params: { id: 'foo' }, id: 'foo', query: {} });
const matchedResultWithSlash = execPath('/user/foo/bar', '/user/:id+');
assert.equal(matchedResultWithSlash, {
path: '/user/foo/bar',
params: { id: 'foo/bar' },
id: 'foo/bar',
query: {}
});
const emptyResult = execPath('/user', '/user/:id+');
assert.equal(emptyResult, undefined);
const mismatchedResult = execPath('/', '/user/:id+');
assert.equal(mismatchedResult, undefined);
});
test('Handles leading/trailing slashes', () => {
const result = execPath('/about-late/_SEGMENT1_/_SEGMENT2_/', '/about-late/:seg1/:seg2/');
assert.equal(result, {
path: '/about-late/_SEGMENT1_/_SEGMENT2_/',
params: {
seg1: '_SEGMENT1_',
seg2: '_SEGMENT2_'
},
seg1: '_SEGMENT1_',
seg2: '_SEGMENT2_',
query: {}
});
});
test('should not overwrite existing properties', () => {
const result = execPath('/foo/bar', '/:path/:query', { path: '/custom-path' });
assert.equal(result, {
params: { path: 'foo', query: 'bar' },
path: '/custom-path',
query: {}
});
});
test.run();