Skip to content

Commit

Permalink
fix: get flatten routes which nested level more than 3 (#6555)
Browse files Browse the repository at this point in the history
* fix: get flatten routes which nested level more than 3

* fix: lint
  • Loading branch information
ClarkXia authored Oct 10, 2023
1 parent 6f18c3d commit aa29b37
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/wicked-trainers-relate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@ice/app': patch
---

fix: get flatten routes which nested level more than 3
6 changes: 3 additions & 3 deletions packages/ice/src/utils/getRoutePaths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ import formatPath from './formatPath.js';
*/
export default function getRoutePaths(routes: NestedRouteManifest[], parentPath = ''): string[] {
let pathList = [];

routes.forEach(route => {
const routePath = formatPath(path.join('/', parentPath.replace(/^\//, ''), route.path || ''));
if (route.children) {
pathList = pathList.concat(getRoutePaths(route.children, route.path));
pathList = pathList.concat(getRoutePaths(route.children, routePath));
} else {
pathList.push(formatPath(path.join('/', parentPath, route.path || '')));
pathList.push(routePath);
}
});

Expand Down
88 changes: 88 additions & 0 deletions packages/ice/tests/getRoutePaths.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import { expect, it, describe } from 'vitest';
import getRoutePaths from '../src/utils/getRoutePaths.js';

describe('getRoutePaths', () => {
it('index route with layout', () => {
const routeManifest = [
{
children: [
{},
],
},
];
// @ts-ignore for mock routeManifest.
const routePaths = getRoutePaths(routeManifest);
expect(routePaths).toEqual(['/']);
});
it('nested level 1', () => {
const routeManifest = [
{
children: [
{
path: 'a',
},
{
path: 'b',
},
],
},
];
// @ts-ignore for mock routeManifest.
const routePaths = getRoutePaths(routeManifest);
expect(routePaths).toEqual(['/a', '/b']);
});

it('nested level 2', () => {
const routeManifest = [
{
children: [
{
path: 'a',
children: [
{
path: 'a1',
},
{
path: 'a2',
},
],
},
{
path: 'b',
},
],
},
];
// @ts-ignore for mock routeManifest.
const routePaths = getRoutePaths(routeManifest, '/');
expect(routePaths).toEqual(['/a/a1', '/a/a2', '/b']);
});

it('nested level 3', () => {
const routeManifest = [
{
children: [
{
path: 'a',
children: [
{
path: 'a1',
children: [
{
path: 'a11',
},
],
},
],
},
{
path: 'b',
},
],
},
];
// @ts-ignore for mock routeManifest.
const routePaths = getRoutePaths(routeManifest);
expect(routePaths).toEqual(['/a/a1/a11', '/b']);
});
});

0 comments on commit aa29b37

Please sign in to comment.