Skip to content

Commit

Permalink
[preact-iso] Router: fix splat (/:x* and /:x+) matches being merged i…
Browse files Browse the repository at this point in the history
…nto props instead of params
  • Loading branch information
developit authored Aug 31, 2021
1 parent 942879a commit d503514
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 12 deletions.
2 changes: 1 addition & 1 deletion packages/preact-iso/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export const exec = (url, route, matches) => {
// normal/optional field:
if (flag >= '?' || flag === '') continue;
// rest (+/*) match:
matches[param] = url.slice(i).map(decodeURIComponent).join('/');
params[param] = url.slice(i).map(decodeURIComponent).join('/');
break;
}

Expand Down
49 changes: 38 additions & 11 deletions packages/preact-iso/test/match.test.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,60 @@
import { exec } from '../router.js';

function execPath(path, pattern) {
return exec(path, pattern, { path });
}

describe('match', () => {
it('Base route', () => {
const accurateResult = exec('/', '/', { path: '/' });
const accurateResult = execPath('/', '/');
expect(accurateResult).toEqual({ path: '/', params: {} });

const inaccurateResult = exec('/user/1', '/', { path: '/' });
const inaccurateResult = execPath('/user/1', '/');
expect(inaccurateResult).toEqual(undefined);
});

it('Param route', () => {
const accurateResult = exec('/user/2', '/user/:id', { path: '/' });
expect(accurateResult).toEqual({ path: '/', params: { id: '2' } });
const accurateResult = execPath('/user/2', '/user/:id');
expect(accurateResult).toEqual({ path: '/user/2', params: { id: '2' } });

const inaccurateResult = exec('/', '/user/:id', { path: '/' });
const inaccurateResult = execPath('/', '/user/:id');
expect(inaccurateResult).toEqual(undefined);
});

it('Optional param route', () => {
const accurateResult = exec('/user', '/user/:id?', { path: '/' });
expect(accurateResult).toEqual({ path: '/', params: { id: undefined } });
const accurateResult = execPath('/user', '/user/:id?');
expect(accurateResult).toEqual({ path: '/user', params: { id: undefined } });

const inaccurateResult = execPath('/', '/user/:id?');
expect(inaccurateResult).toEqual(undefined);
});

it('Optional rest param route "/:x*"', () => {
const accurateResult = execPath('/user', '/user/:id?');
expect(accurateResult).toEqual({ path: '/user', params: { id: undefined } });

const inaccurateResult = exec('/', '/user/:id?', { path: '/' });
const inaccurateResult = execPath('/', '/user/:id?');
expect(inaccurateResult).toEqual(undefined);
});

it('Rest param route "/:x+"', () => {
const matchedResult = execPath('/user/foo', '/user/:id+');
expect(matchedResult).toEqual({ path: '/user/foo', params: { id: 'foo' } });

const matchedResultWithSlash = execPath('/user/foo/bar', '/user/:id+');
expect(matchedResultWithSlash).toEqual({ path: '/user/foo/bar', params: { id: 'foo/bar' } });

const emptyResult = execPath('/user', '/user/:id+');
expect(emptyResult).toEqual(undefined);

const mismatchedResult = execPath('/', '/user/:id+');
expect(mismatchedResult).toEqual(undefined);
});

it('Handles leading/trailing slashes', () => {
const result = exec('/about-late/_SEGMENT1_/_SEGMENT2_/', '/about-late/:seg1/:seg2/', {});
const result = execPath('/about-late/_SEGMENT1_/_SEGMENT2_/', '/about-late/:seg1/:seg2/');
expect(result).toEqual({
path: '/about-late/_SEGMENT1_/_SEGMENT2_/',
params: {
seg1: '_SEGMENT1_',
seg2: '_SEGMENT2_'
Expand All @@ -36,10 +63,10 @@ describe('match', () => {
});

it('should not overwrite existing properties', () => {
const result = exec('/foo/bar', '/:path/:query', { path: '/' });
const result = exec('/foo/bar', '/:path/:query', { path: '/custom-path' });
expect(result).toEqual({
params: { path: 'foo', query: 'bar' },
path: '/'
path: '/custom-path'
});
});
});

0 comments on commit d503514

Please sign in to comment.