diff --git a/libs/angular-webcomponents/jest.config.ts b/libs/angular-webcomponents/jest.config.ts index 95bf190d..4ab6e2d4 100644 --- a/libs/angular-webcomponents/jest.config.ts +++ b/libs/angular-webcomponents/jest.config.ts @@ -14,6 +14,9 @@ export default { ], }, transformIgnorePatterns: ['node_modules/(?!.*\\.mjs$)'], + moduleNameMapper: { + '^d3-(.*)$': `d3-$1/dist/d3-$1`, + }, snapshotSerializers: [ 'jest-preset-angular/build/serializers/no-ng-attributes', 'jest-preset-angular/build/serializers/ng-snapshot', diff --git a/libs/angular-webcomponents/src/lib/utils/webcomponent-bootstrap.utils.spec.ts b/libs/angular-webcomponents/src/lib/utils/webcomponent-bootstrap.utils.spec.ts new file mode 100644 index 00000000..e69de29b diff --git a/libs/angular-webcomponents/src/lib/utils/webcomponent-router-initializer.spec.ts b/libs/angular-webcomponents/src/lib/utils/webcomponent-router-initializer.spec.ts new file mode 100644 index 00000000..e1b8c312 --- /dev/null +++ b/libs/angular-webcomponents/src/lib/utils/webcomponent-router-initializer.spec.ts @@ -0,0 +1,75 @@ +import { TestBed } from '@angular/core/testing' +import * as utils from './webcomponent-router-initializer.utils' +import { of } from 'rxjs' +import { AppStateService } from '@onecx/portal-integration-angular' + +describe('webcomponent router initializer', () => { + beforeEach(async () => { + await TestBed.configureTestingModule({ + declarations: [], + providers: [AppStateService], + }).compileComponents() + + jest.clearAllMocks() + }) + + describe('initializeRouter', () => { + it('should add mfeInfo to routes', async () => { + const routes = [ + { + data: { + myProp: 'value', + }, + path: 'first', + }, + { + data: { + fromRoutesConfig: 'passing value', + }, + path: 'second', + }, + ] + const router = { + config: routes, + resetConfig: jest.fn(), + } as any + const appStateService = TestBed.inject(AppStateService) + jest.spyOn(appStateService.currentMfe$, 'asObservable').mockReturnValue( + of({ + mfeInfoProp: 'mockedMfeInfo', + }) as any + ) + jest.spyOn(router, 'resetConfig') + + expect(router.resetConfig).toHaveBeenCalledTimes(0) + + await utils.initializeRouter(router, appStateService)() + + expect(router.resetConfig).toHaveBeenCalledTimes(1) + expect(routes).toEqual([ + { + data: { + myProp: 'value', + mfeInfo: { + mfeInfoProp: 'mockedMfeInfo', + }, + }, + path: 'first', + }, + { + data: { + fromRoutesConfig: 'passing value', + mfeInfo: { + mfeInfoProp: 'mockedMfeInfo', + }, + }, + path: 'second', + }, + { + path: '**', + children: [], + }, + ]) + }) + }) +}) diff --git a/libs/angular-webcomponents/src/lib/utils/webcomponent-router.utils.spec.ts b/libs/angular-webcomponents/src/lib/utils/webcomponent-router.utils.spec.ts new file mode 100644 index 00000000..7347b1d5 --- /dev/null +++ b/libs/angular-webcomponents/src/lib/utils/webcomponent-router.utils.spec.ts @@ -0,0 +1,259 @@ +import { TestBed } from '@angular/core/testing' +import * as router_utils from './webcomponent-router.utils' + +describe('webcomponent router utils', () => { + beforeEach(async () => { + await TestBed.configureTestingModule({ + declarations: [], + }).compileComponents() + + jest.clearAllMocks() + }) + + describe('startsWith', () => { + it('should consume base href and prefix only', () => { + const emptyMatcher = router_utils.startsWith('') + expect( + emptyMatcher( + [{ path: 'admin' } as any, { path: 'help' } as any], + {} as any, + { + data: { + mfeInfo: { + baseHref: '/admin/help', + }, + }, + } as any + ) + ).toEqual({ + consumed: [{ path: 'admin' } as any, { path: 'help' } as any], + }) + expect( + emptyMatcher( + [{ path: 'admin' } as any, { path: 'help' } as any, { path: 'item' } as any], + {} as any, + { + data: { + mfeInfo: { + baseHref: '/admin/help', + }, + }, + } as any + ) + ).toEqual({ + consumed: [{ path: 'admin' }, { path: 'help' }], + }) + + const singlePathMatcher = router_utils.startsWith('subPath') + expect( + singlePathMatcher( + [{ path: 'admin' } as any, { path: 'help' } as any], + {} as any, + { + data: { + mfeInfo: { + baseHref: '/admin/help', + }, + }, + } as any + ) + ).toBeNull() + expect( + singlePathMatcher( + [{ path: 'admin' } as any, { path: 'help' } as any, { path: 'item' } as any], + {} as any, + { + data: { + mfeInfo: { + baseHref: '/admin/help', + }, + }, + } as any + ) + ).toBeNull() + expect( + singlePathMatcher( + [{ path: 'admin' } as any, { path: 'help' } as any, { path: 'subPath' } as any], + {} as any, + { + data: { + mfeInfo: { + baseHref: '/admin/help', + }, + }, + } as any + ) + ).toEqual({ + consumed: [{ path: 'admin' } as any, { path: 'help' } as any, { path: 'subPath' } as any], + }) + expect( + singlePathMatcher( + [{ path: 'admin' } as any, { path: 'help' } as any, { path: 'subPath' } as any, { path: 'nested' } as any], + {} as any, + { + data: { + mfeInfo: { + baseHref: '/admin/help', + }, + }, + } as any + ) + ).toEqual({ + consumed: [{ path: 'admin' } as any, { path: 'help' } as any, { path: 'subPath' } as any], + }) + + const complexPathMatcher = router_utils.startsWith('my/nested/subPath') + expect( + complexPathMatcher( + [{ path: 'admin' } as any, { path: 'help' } as any], + {} as any, + { + data: { + mfeInfo: { + baseHref: '/admin/help', + }, + }, + } as any + ) + ).toBeNull() + expect( + complexPathMatcher( + [{ path: 'admin' } as any, { path: 'help' } as any, { path: 'item' } as any], + {} as any, + { + data: { + mfeInfo: { + baseHref: '/admin/help', + }, + }, + } as any + ) + ).toBeNull() + expect( + complexPathMatcher( + [ + { path: 'admin' } as any, + { path: 'help' } as any, + { path: 'my' } as any, + { path: 'nested' } as any, + { path: 'subPath' } as any, + ], + {} as any, + { + data: { + mfeInfo: { + baseHref: '/admin/help', + }, + }, + } as any + ) + ).toEqual({ + consumed: [ + { path: 'admin' } as any, + { path: 'help' } as any, + { path: 'my' } as any, + { path: 'nested' } as any, + { path: 'subPath' } as any, + ], + }) + expect( + complexPathMatcher( + [ + { path: 'admin' } as any, + { path: 'help' } as any, + { path: 'my' } as any, + { path: 'nested' } as any, + { path: 'subPath' } as any, + { path: 'nested2' } as any, + ], + {} as any, + { + data: { + mfeInfo: { + baseHref: '/admin/help', + }, + }, + } as any + ) + ).toEqual({ + consumed: [ + { path: 'admin' } as any, + { path: 'help' } as any, + { path: 'my' } as any, + { path: 'nested' } as any, + { path: 'subPath' } as any, + ], + }) + }) + }) + + describe('sliceBaseHref', () => { + it('should remove base href from url', () => { + expect( + router_utils.sliceBaseHref( + { + data: { + mfeInfo: { + baseHref: '/admin/help', + }, + }, + } as any, + ['admin' as any, 'help' as any] + ) + ).toEqual([]) + expect( + router_utils.sliceBaseHref( + { + data: { + mfeInfo: { + baseHref: '/admin/help', + }, + }, + } as any, + ['admin' as any, 'help' as any, 'item' as any] + ) + ).toEqual(['item' as any]) + expect( + router_utils.sliceBaseHref( + { + data: { + mfeInfo: { + baseHref: 'admin/help', + }, + }, + } as any, + ['admin' as any, 'help' as any, 'item' as any] + ) + ).toEqual(['item' as any]) + expect( + router_utils.sliceBaseHref( + { + data: { + mfeInfo: { + baseHref: 'admin/help/', + }, + }, + } as any, + ['admin' as any, 'help' as any, 'item' as any] + ) + ).toEqual(['item' as any]) + }) + + it('should warn if base href is not defined', () => { + expect(router_utils.sliceBaseHref({} as any, ['admin' as any, 'help' as any])).toEqual([ + 'admin' as any, + 'help' as any, + ]) + }) + }) + + describe('baseHrefSegmentAmount', () => { + it('should return correct base href segment amount', () => { + expect(router_utils.baseHrefSegmentAmount(['admin' as any, 'help' as any], [])).toEqual(2) + expect(router_utils.baseHrefSegmentAmount([], [])).toEqual(0) + expect( + router_utils.baseHrefSegmentAmount(['admin' as any, 'help' as any, 'item' as any], ['item' as any]) + ).toEqual(2) + }) + }) +}) diff --git a/libs/angular-webcomponents/src/lib/utils/webcomponent-router.utils.ts b/libs/angular-webcomponents/src/lib/utils/webcomponent-router.utils.ts index 618dd209..e9e72e39 100644 --- a/libs/angular-webcomponents/src/lib/utils/webcomponent-router.utils.ts +++ b/libs/angular-webcomponents/src/lib/utils/webcomponent-router.utils.ts @@ -19,6 +19,7 @@ export function sliceBaseHref(route: Route, url: UrlSegment[]): UrlSegment[] { console.warn( 'mfeInfo was not provided for route. initializeRouter function is required to be registered as app initializer.' ) + return url } const baseHrefSegmentAmount = mfeBaseHref.split('/').filter((value) => value).length