Skip to content

Commit 5809382

Browse files
test(router-core): add unit tests for matchByPath (#4749)
This PR is simply adding unit test to the existing `matchByPath` function. However, it does highlight some weird cases / bugs: - this test passes OK, I'm just wondering whether that's a real case that can happen (in which case the test is good to keep) or if it's just accidental ```ts expect(matchByPath('/', '/a/1/b/2', { to: '/a/$id/b/$id' })).toEqual({ id: '2' }) ``` - this test passes OK, I'm just a little surprised that it includes a `/` char, because *any other* case does not ```ts expect(matchByPath('/', '/a/', { to: '/a/$' })).toEqual({ _splat: '/', '*': '/' }) ``` - this does NOT work, it seems like a bug with the recent optional params ```ts expect(matchByPath('/', '/a/b', { to: '/A/{-$id}/B', caseSensitive: false })).toEqual({}) expect(matchByPath('/', '/a/b/2', { to: '/A/{-$id}/B/{-$other}', caseSensitive: false })).toEqual({ other: '2' }) expect(matchByPath('/', '/a/b', { to: '/A/{-$id}/B/{-$other}', caseSensitive: false })).toEqual({}) ``` --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
1 parent 2de6dcc commit 5809382

File tree

1 file changed

+199
-0
lines changed

1 file changed

+199
-0
lines changed
Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
1+
import { describe, expect, it } from 'vitest'
2+
import { matchByPath } from '../src'
3+
4+
describe('default path matching', () => {
5+
it.each([
6+
['', '', '', {}],
7+
['', '/', '', {}],
8+
['', '', '/', {}],
9+
['', '/', '/', {}],
10+
['/', '/', '/', {}],
11+
['/', '/a', '/a', {}],
12+
['/', '/a/b', '/a/b', {}],
13+
['/', '/a', '/a/', {}],
14+
['/', '/a/', '/a/', {}],
15+
['/', '/a/', '/a', undefined],
16+
['/', '/b', '/a', undefined],
17+
])('static %s %s => %s', (base, from, to, result) => {
18+
expect(
19+
matchByPath(base, from, { to, caseSensitive: true, fuzzy: false }),
20+
).toEqual(result)
21+
})
22+
23+
it.each([
24+
['/a/1', '/a/$id', { id: '1' }],
25+
['/a/1/b', '/a/$id/b', { id: '1' }],
26+
['/a/1/b/2', '/a/$id/b/$other', { id: '1', other: '2' }],
27+
['/a/1/b/2', '/a/$id/b/$id', { id: '2' }],
28+
])('params %s => %s', (from, to, result) => {
29+
expect(
30+
matchByPath('/', from, { to, caseSensitive: true, fuzzy: false }),
31+
).toEqual(result)
32+
})
33+
34+
it('params support more than alphanumeric characters', () => {
35+
// in the value: basically everything except / and %
36+
expect(
37+
matchByPath(
38+
'/',
39+
'/a/@&é"\'(§è!çà)-_°^¨$*€£`ù=+:;.,?~<>|î©#0123456789\\😀}{',
40+
{ to: '/a/$id' },
41+
),
42+
).toEqual({ id: '@&é"\'(§è!çà)-_°^¨$*€£`ù=+:;.,?~<>|î©#0123456789\\😀}{' })
43+
// in the key: basically everything except / and % and $
44+
expect(
45+
matchByPath('/', '/a/1', {
46+
to: '/a/$@&é"\'(§è!çà)-_°^¨*€£`ù=+:;.,?~<>|î©#0123456789\\😀}{',
47+
}),
48+
).toEqual({ '@&é"\'(§è!çà)-_°^¨*€£`ù=+:;.,?~<>|î©#0123456789\\😀}{': '1' })
49+
})
50+
51+
it.each([
52+
['/a/1', '/a/{-$id}', { id: '1' }],
53+
['/a', '/a/{-$id}', {}],
54+
['/a/1/b', '/a/{-$id}/b', { id: '1' }],
55+
['/a/b', '/a/{-$id}/b', {}],
56+
['/a/1/b/2', '/a/{-$id}/b/{-$other}', { id: '1', other: '2' }],
57+
['/a/b/2', '/a/{-$id}/b/{-$other}', { other: '2' }],
58+
['/a/1/b', '/a/{-$id}/b/{-$other}', { id: '1' }],
59+
['/a/b', '/a/{-$id}/b/{-$other}', {}],
60+
['/a/1/b/2', '/a/{-$id}/b/{-$id}', { id: '2' }],
61+
])('optional %s => %s', (from, to, result) => {
62+
expect(
63+
matchByPath('/', from, { to, caseSensitive: true, fuzzy: false }),
64+
).toEqual(result)
65+
})
66+
67+
it.each([
68+
['/a/b/c', '/a/$', { _splat: 'b/c', '*': 'b/c' }],
69+
['/a/', '/a/$', { _splat: '/', '*': '/' }],
70+
['/a', '/a/$', { _splat: '', '*': '' }],
71+
['/a/b/c', '/a/$/foo', { _splat: 'b/c', '*': 'b/c' }],
72+
])('wildcard %s => %s', (from, to, result) => {
73+
expect(
74+
matchByPath('/', from, { to, caseSensitive: true, fuzzy: false }),
75+
).toEqual(result)
76+
})
77+
})
78+
79+
describe('case insensitive path matching', () => {
80+
it.each([
81+
['', '', '', {}],
82+
['', '/', '', {}],
83+
['', '', '/', {}],
84+
['', '/', '/', {}],
85+
['/', '/', '/', {}],
86+
['/', '/a', '/A', {}],
87+
['/', '/a/b', '/A/B', {}],
88+
['/', '/a', '/A/', {}],
89+
['/', '/a/', '/A/', {}],
90+
['/', '/a/', '/A', undefined],
91+
['/', '/b', '/A', undefined],
92+
])('static %s %s => %s', (base, from, to, result) => {
93+
expect(
94+
matchByPath(base, from, { to, caseSensitive: false, fuzzy: false }),
95+
).toEqual(result)
96+
})
97+
98+
it.each([
99+
['/a/1', '/A/$id', { id: '1' }],
100+
['/a/1/b', '/A/$id/B', { id: '1' }],
101+
['/a/1/b/2', '/A/$id/B/$other', { id: '1', other: '2' }],
102+
['/a/1/b/2', '/A/$id/B/$id', { id: '2' }],
103+
])('params %s => %s', (from, to, result) => {
104+
expect(
105+
matchByPath('/', from, { to, caseSensitive: false, fuzzy: false }),
106+
).toEqual(result)
107+
})
108+
109+
it.each([
110+
['/a/1', '/A/{-$id}', { id: '1' }],
111+
['/a', '/A/{-$id}', {}],
112+
['/a/1/b', '/A/{-$id}/B', { id: '1' }],
113+
// ['/a/b', '/A/{-$id}/B', {}],
114+
['/a/1/b/2', '/A/{-$id}/B/{-$other}', { id: '1', other: '2' }],
115+
// ['/a/b/2', '/A/{-$id}/B/{-$other}', { other: '2' }],
116+
['/a/1/b', '/A/{-$id}/B/{-$other}', { id: '1' }],
117+
// ['/a/b', '/A/{-$id}/B/{-$other}', {}],
118+
['/a/1/b/2', '/A/{-$id}/B/{-$id}', { id: '2' }],
119+
])('optional %s => %s', (from, to, result) => {
120+
expect(
121+
matchByPath('/', from, { to, caseSensitive: false, fuzzy: false }),
122+
).toEqual(result)
123+
})
124+
125+
it.each([
126+
['/a/b/c', '/A/$', { _splat: 'b/c', '*': 'b/c' }],
127+
['/a/', '/A/$', { _splat: '/', '*': '/' }],
128+
['/a', '/A/$', { _splat: '', '*': '' }],
129+
['/a/b/c', '/A/$/foo', { _splat: 'b/c', '*': 'b/c' }],
130+
])('wildcard %s => %s', (from, to, result) => {
131+
expect(
132+
matchByPath('/', from, { to, caseSensitive: false, fuzzy: false }),
133+
).toEqual(result)
134+
})
135+
})
136+
137+
describe('fuzzy path matching', () => {
138+
it.each([
139+
['', '', '', {}],
140+
['', '/', '', {}],
141+
['', '', '/', {}],
142+
['', '/', '/', {}],
143+
['/', '/', '/', {}],
144+
['/', '/a', '/a', {}],
145+
['/', '/a', '/a/', {}],
146+
['/', '/a/', '/a/', {}],
147+
['/', '/a/', '/a', { '**': '/' }],
148+
['/', '/a/b', '/a/b', {}],
149+
['/', '/a/b', '/a', { '**': 'b' }],
150+
['/', '/a/b/', '/a', { '**': 'b/' }],
151+
['/', '/a/b/c', '/a', { '**': 'b/c' }],
152+
['/', '/a', '/a/b', undefined],
153+
['/', '/b', '/a', undefined],
154+
['/', '/a', '/b', undefined],
155+
])('static %s %s => %s', (base, from, to, result) => {
156+
expect(
157+
matchByPath(base, from, { to, fuzzy: true, caseSensitive: true }),
158+
).toEqual(result)
159+
})
160+
161+
it.each([
162+
['/a/1', '/a/$id', { id: '1' }],
163+
['/a/1/b', '/a/$id', { id: '1', '**': 'b' }],
164+
['/a/1/', '/a/$id/', { id: '1' }],
165+
['/a/1/b/2', '/a/$id/b/$other', { id: '1', other: '2' }],
166+
['/a/1/b/2/c', '/a/$id/b/$other', { id: '1', other: '2', '**': 'c' }],
167+
])('params %s => %s', (from, to, result) => {
168+
expect(
169+
matchByPath('/', from, { to, fuzzy: true, caseSensitive: true }),
170+
).toEqual(result)
171+
})
172+
173+
it.each([
174+
['/a/1', '/a/{-$id}', { id: '1' }],
175+
['/a', '/a/{-$id}', {}],
176+
['/a/1/b', '/a/{-$id}', { '**': 'b', id: '1' }],
177+
['/a/1/b', '/a/{-$id}/b', { id: '1' }],
178+
['/a/b', '/a/{-$id}/b', {}],
179+
['/a/b/c', '/a/{-$id}/b', { '**': 'c' }],
180+
['/a/b', '/a/{-$id}/b/{-$other}', {}],
181+
['/a/b/2/d', '/a/{-$id}/b/{-$other}', { other: '2', '**': 'd' }],
182+
['/a/1/b/2/c', '/a/{-$id}/b/{-$other}', { id: '1', other: '2', '**': 'c' }],
183+
])('optional %s => %s', (from, to, result) => {
184+
expect(
185+
matchByPath('/', from, { to, fuzzy: true, caseSensitive: true }),
186+
).toEqual(result)
187+
})
188+
189+
it.each([
190+
['/a/b/c', '/a/$', { _splat: 'b/c', '*': 'b/c' }],
191+
['/a/', '/a/$', { _splat: '/', '*': '/' }],
192+
['/a', '/a/$', { _splat: '', '*': '' }],
193+
['/a/b/c/d', '/a/$/foo', { _splat: 'b/c/d', '*': 'b/c/d' }],
194+
])('wildcard %s => %s', (from, to, result) => {
195+
expect(
196+
matchByPath('/', from, { to, fuzzy: true, caseSensitive: true }),
197+
).toEqual(result)
198+
})
199+
})

0 commit comments

Comments
 (0)