From b4a47df4cf59d358026f4a194a09080488ae2fa0 Mon Sep 17 00:00:00 2001 From: Arman Ozak Date: Sun, 21 Jun 2020 02:44:45 +0300 Subject: [PATCH] feat: avoid breaking change in route handler --- .../core/src/lib/handlers/routes.handler.ts | 30 +++++++++++++++---- .../core/src/lib/tests/routes.handler.spec.ts | 27 ++--------------- 2 files changed, 27 insertions(+), 30 deletions(-) diff --git a/npm/ng-packs/packages/core/src/lib/handlers/routes.handler.ts b/npm/ng-packs/packages/core/src/lib/handlers/routes.handler.ts index a5446c7101..10f95fa0c4 100644 --- a/npm/ng-packs/packages/core/src/lib/handlers/routes.handler.ts +++ b/npm/ng-packs/packages/core/src/lib/handlers/routes.handler.ts @@ -1,5 +1,6 @@ import { Injectable, Optional } from '@angular/core'; import { Router } from '@angular/router'; +import { ABP } from '../models'; import { RoutesService } from '../services/routes.service'; @Injectable({ @@ -11,15 +12,34 @@ export class RoutesHandler { } addRoutes() { - this.router?.config.forEach(({ path, data }) => { - if (!data) return; + this.router?.config.forEach(({ path = '', data }) => { + if (!data?.routes) return; - if (data.route) { - this.routes.add([{ path: '/' + path, ...data.route }]); + if (Array.isArray(data.routes)) { + this.routes.add(data.routes); return; } - if (data.routes) this.routes.add(data.routes); + const routes = flatRoutes([{ ...data.routes, path }], { path: '' }); + this.routes.add(routes); }); } } + +function flatRoutes(routes: RouteDef[], parent: any) { + if (!routes) return []; + + return routes.reduce((acc, route) => { + const current = { + ...route, + parentName: parent.name, + path: parent.path + '/' + route.path, + }; + + acc.push(current, ...flatRoutes(current.children, current)); + + return acc; + }, []); +} + +type RouteDef = ABP.Route & { children: RouteDef[] }; diff --git a/npm/ng-packs/packages/core/src/lib/tests/routes.handler.spec.ts b/npm/ng-packs/packages/core/src/lib/tests/routes.handler.spec.ts index e1e32d9221..777df39c60 100644 --- a/npm/ng-packs/packages/core/src/lib/tests/routes.handler.spec.ts +++ b/npm/ng-packs/packages/core/src/lib/tests/routes.handler.spec.ts @@ -3,36 +3,13 @@ import { RoutesHandler } from '../handlers'; import { RoutesService } from '../services'; describe('Routes Handler', () => { - describe('#add', () => { - it('should add routes from router config', () => { - const config = [ - { path: 'x' }, - { path: '', data: { route: { name: 'Foo' } } }, - { path: 'bar', data: { route: { name: 'Bar' } } }, - { data: { routes: [{ path: '/baz', name: 'Baz' }] } }, - ]; - const foo = [{ path: '/', name: 'Foo' }]; - const bar = [{ path: '/bar', name: 'Bar' }]; - const baz = [{ path: '/baz', name: 'Baz' }]; - - const routes = []; - const add = jest.fn(routes.push.bind(routes)); - const mockRoutesService = ({ add } as unknown) as RoutesService; - const mockRouter = ({ config } as unknown) as Router; - - const handler = new RoutesHandler(mockRoutesService, mockRouter); - - expect(add).toHaveBeenCalledTimes(3); - expect(routes).toEqual([foo, bar, baz]); - }); - }); describe('#add', () => { it('should add routes from router config', () => { const config = [ { path: 'x' }, { path: 'y', data: {} }, - { path: '', data: { route: { name: 'Foo' } } }, - { path: 'bar', data: { route: { name: 'Bar' } } }, + { path: '', data: { routes: { name: 'Foo' } } }, + { path: 'bar', data: { routes: { name: 'Bar' } } }, { data: { routes: [{ path: '/baz', name: 'Baz' }] } }, ]; const foo = [{ path: '/', name: 'Foo' }];