Skip to content

Commit

Permalink
fix(routes): fix nested dynamic routes path (#305)
Browse files Browse the repository at this point in the history
  • Loading branch information
yassernasc authored Mar 21, 2023
1 parent 4054d84 commit 8abb61c
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
43 changes: 43 additions & 0 deletions packages/router/src/lib/routes.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,49 @@ describe('routes', () => {
});
});

describe('a nested dynamic route', () => {
const files: Files = {
'/app/routes/categories.[categoryId].products.[productId].ts': () =>
Promise.resolve({ default: RouteComponent }),
};

const routes = getRoutes(files);
const route: ModuleRoute = routes[0];

it('should have a path', () => {
expect(route.path).toBe('categories/:categoryId/products/:productId');
});

it('should have a pathMatch set to prefix', () => {
expect(route.pathMatch).toBe('prefix');
});

it('should have a _module property', () => {
expect(route._module).toBeDefined();

expect(typeof route._module).toBe('function');
});

it('should have a loadChildren property', () => {
expect(route.loadChildren).toBeDefined();

expect(typeof route.loadChildren).toBe('function');
});

it('should return an array of one route config from the loadChildren property', async () => {
expect(route.loadChildren).toBeDefined();

const routes = (await route.loadChildren()) as Route[];

expect(routes.length).toBe(1);

const innerRoute = routes.shift();

expect(innerRoute.path).toBe('');
expect(innerRoute.component).toBe(RouteComponent);
});
});

describe('an index route', () => {
const files: Files = {
'/app/routes/index.ts': () =>
Expand Down
4 changes: 3 additions & 1 deletion packages/router/src/lib/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ export function getRoutes(
.filter(Boolean);

segments.reduce((parent, segment, index) => {
const path = segment.replace(/index|^\(.*?\)$/g, '').replace('.', '/');
const path = segment
.replace(/index|^\(.*?\)$/g, '')
.replace(/\./g, '/');
const isIndex = !path;
const isCatchall = path === '**';
const pathMatch = isIndex ? 'full' : 'prefix';
Expand Down

0 comments on commit 8abb61c

Please sign in to comment.