Skip to content

Commit

Permalink
fix(router): allow dynamic routes in the root route array (#381)
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonroberts authored May 10, 2023
1 parent 0993e02 commit c5ef38e
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 3 deletions.
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 @@ -161,6 +161,49 @@ describe('routes', () => {
});
});

describe('a root dynamic route', () => {
const files: Files = {
'/app/pages/[productId].[partId].page.ts': () =>
Promise.resolve({ default: RouteComponent }),
};

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

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

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
3 changes: 0 additions & 3 deletions packages/router/src/lib/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,6 @@ export function getRoutes(
const insert = /^\w|\//.test(path) && !isCatchall ? 'unshift' : 'push';

if (root) {
const dynamic = path.startsWith(':');
if (dynamic) return parent;

const last = segments.length === 1;
if (last) {
const newRoute = {
Expand Down

0 comments on commit c5ef38e

Please sign in to comment.