diff --git a/.changeset/wicked-trainers-relate.md b/.changeset/wicked-trainers-relate.md new file mode 100644 index 0000000000..c24e909cd5 --- /dev/null +++ b/.changeset/wicked-trainers-relate.md @@ -0,0 +1,5 @@ +--- +'@ice/app': patch +--- + +fix: get flatten routes which nested level more than 3 diff --git a/packages/ice/src/utils/getRoutePaths.ts b/packages/ice/src/utils/getRoutePaths.ts index ab4c860f6f..602e2c2dc2 100644 --- a/packages/ice/src/utils/getRoutePaths.ts +++ b/packages/ice/src/utils/getRoutePaths.ts @@ -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); } }); diff --git a/packages/ice/tests/getRoutePaths.test.ts b/packages/ice/tests/getRoutePaths.test.ts new file mode 100644 index 0000000000..f924836859 --- /dev/null +++ b/packages/ice/tests/getRoutePaths.test.ts @@ -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']); + }); +});