Skip to content

Commit df3e39c

Browse files
Merge pull request #824 from preactjs/iso-fix-splat
2 parents b04d8d7 + 048b15f commit df3e39c

File tree

2 files changed

+39
-12
lines changed

2 files changed

+39
-12
lines changed

router.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export const exec = (url, route, matches) => {
4545
// normal/optional field:
4646
if (flag >= '?' || flag === '') continue;
4747
// rest (+/*) match:
48-
matches[param] = url.slice(i).map(decodeURIComponent).join('/');
48+
params[param] = url.slice(i).map(decodeURIComponent).join('/');
4949
break;
5050
}
5151

test/match.test.js

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,60 @@
11
import { exec } from '../router.js';
22

3+
function execPath(path, pattern) {
4+
return exec(path, pattern, { path });
5+
}
6+
37
describe('match', () => {
48
it('Base route', () => {
5-
const accurateResult = exec('/', '/', { path: '/' });
9+
const accurateResult = execPath('/', '/');
610
expect(accurateResult).toEqual({ path: '/', params: {} });
711

8-
const inaccurateResult = exec('/user/1', '/', { path: '/' });
12+
const inaccurateResult = execPath('/user/1', '/');
913
expect(inaccurateResult).toEqual(undefined);
1014
});
1115

1216
it('Param route', () => {
13-
const accurateResult = exec('/user/2', '/user/:id', { path: '/' });
14-
expect(accurateResult).toEqual({ path: '/', params: { id: '2' } });
17+
const accurateResult = execPath('/user/2', '/user/:id');
18+
expect(accurateResult).toEqual({ path: '/user/2', params: { id: '2' } });
1519

16-
const inaccurateResult = exec('/', '/user/:id', { path: '/' });
20+
const inaccurateResult = execPath('/', '/user/:id');
1721
expect(inaccurateResult).toEqual(undefined);
1822
});
1923

2024
it('Optional param route', () => {
21-
const accurateResult = exec('/user', '/user/:id?', { path: '/' });
22-
expect(accurateResult).toEqual({ path: '/', params: { id: undefined } });
25+
const accurateResult = execPath('/user', '/user/:id?');
26+
expect(accurateResult).toEqual({ path: '/user', params: { id: undefined } });
27+
28+
const inaccurateResult = execPath('/', '/user/:id?');
29+
expect(inaccurateResult).toEqual(undefined);
30+
});
31+
32+
it('Optional rest param route "/:x*"', () => {
33+
const accurateResult = execPath('/user', '/user/:id?');
34+
expect(accurateResult).toEqual({ path: '/user', params: { id: undefined } });
2335

24-
const inaccurateResult = exec('/', '/user/:id?', { path: '/' });
36+
const inaccurateResult = execPath('/', '/user/:id?');
2537
expect(inaccurateResult).toEqual(undefined);
2638
});
2739

40+
it('Rest param route "/:x+"', () => {
41+
const matchedResult = execPath('/user/foo', '/user/:id+');
42+
expect(matchedResult).toEqual({ path: '/user/foo', params: { id: 'foo' } });
43+
44+
const matchedResultWithSlash = execPath('/user/foo/bar', '/user/:id+');
45+
expect(matchedResultWithSlash).toEqual({ path: '/user/foo/bar', params: { id: 'foo/bar' } });
46+
47+
const emptyResult = execPath('/user', '/user/:id+');
48+
expect(emptyResult).toEqual(undefined);
49+
50+
const mismatchedResult = execPath('/', '/user/:id+');
51+
expect(mismatchedResult).toEqual(undefined);
52+
});
53+
2854
it('Handles leading/trailing slashes', () => {
29-
const result = exec('/about-late/_SEGMENT1_/_SEGMENT2_/', '/about-late/:seg1/:seg2/', {});
55+
const result = execPath('/about-late/_SEGMENT1_/_SEGMENT2_/', '/about-late/:seg1/:seg2/');
3056
expect(result).toEqual({
57+
path: '/about-late/_SEGMENT1_/_SEGMENT2_/',
3158
params: {
3259
seg1: '_SEGMENT1_',
3360
seg2: '_SEGMENT2_'
@@ -36,10 +63,10 @@ describe('match', () => {
3663
});
3764

3865
it('should not overwrite existing properties', () => {
39-
const result = exec('/foo/bar', '/:path/:query', { path: '/' });
66+
const result = exec('/foo/bar', '/:path/:query', { path: '/custom-path' });
4067
expect(result).toEqual({
4168
params: { path: 'foo', query: 'bar' },
42-
path: '/'
69+
path: '/custom-path'
4370
});
4471
});
4572
});

0 commit comments

Comments
 (0)