Repository navigation
Expand file tree
/
Copy pathutils.spec.js
More file actions
120 lines (102 loc) · 3.32 KB
/
Copy pathutils.spec.js
File metadata and controls
120 lines (102 loc) · 3.32 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
import { describe, test, expect } from 'vitest';
import { matches } from './utils.js';
/**
* Helper to create a static route segment
* @param {string} content
* @returns {{ rest: boolean, dynamic: boolean, content: string }}
*/
function static_segment(content) {
return { rest: false, dynamic: false, content };
}
/**
* Helper to create a dynamic route segment
* @param {string} content
* @returns {{ rest: boolean, dynamic: boolean, content: string }}
*/
function dynamic_segment(content) {
return { rest: false, dynamic: true, content };
}
/**
* Helper to create a rest route segment
* @param {string} content
* @returns {{ rest: boolean, dynamic: boolean, content: string }}
*/
function rest_segment(content) {
return { rest: true, dynamic: true, content };
}
describe('matches', () => {
test('two identical static routes match', () => {
expect(
matches(
[static_segment('blog'), static_segment('post')],
[static_segment('blog'), static_segment('post')]
)
).toBe(true);
});
test('static segment mismatch returns false', () => {
expect(matches([static_segment('blog')], [static_segment('about')])).toBe(false);
});
test('dynamic segment matches any static segment', () => {
expect(matches([static_segment('blog')], [dynamic_segment('[slug]')])).toBe(true);
});
test('rest segment at end matches everything', () => {
expect(
matches([static_segment('blog'), static_segment('post')], [rest_segment('[...rest]')])
).toBe(true);
});
test('rest-only route matches any route', () => {
expect(
matches(
[static_segment('a'), static_segment('b'), static_segment('c')],
[rest_segment('[...rest]')]
)
).toBe(true);
});
test('rest segment matches remaining segments', () => {
expect(
matches(
[static_segment('blog'), static_segment('2024'), static_segment('post')],
[static_segment('blog'), rest_segment('[...rest]')]
)
).toBe(true);
});
test('empty segments (index routes)', () => {
expect(matches([], [])).toBe(false);
});
test('mismatched lengths without rest return false', () => {
expect(
matches([static_segment('blog'), static_segment('post')], [static_segment('blog')])
).toBe(false);
});
test('rest with following segments', () => {
expect(
matches(
[static_segment('a'), static_segment('b'), static_segment('page')],
[rest_segment('[...rest]'), static_segment('page')]
)
).toBe(true);
});
test('rest with following segments that do not match', () => {
expect(
matches(
[static_segment('a'), static_segment('b'), static_segment('other')],
[rest_segment('[...rest]'), static_segment('page')]
)
).toBe(false);
});
test('a has trailing rest and b is shorter', () => {
expect(matches([rest_segment('[...rest]')], [static_segment('blog')])).toBe(true);
});
test('a is longer without rest in b', () => {
expect(matches([static_segment('a'), static_segment('b')], [static_segment('a')])).toBe(false);
});
test('b is longer without rest in a', () => {
expect(matches([static_segment('a')], [static_segment('a'), static_segment('b')])).toBe(false);
});
test('dynamic in a matches static in b', () => {
expect(matches([dynamic_segment('[id]')], [static_segment('blog')])).toBe(true);
});
test('both dynamic segments match', () => {
expect(matches([dynamic_segment('[id]')], [dynamic_segment('[slug]')])).toBe(true);
});
});