From 9d494641e847023e0fe63f8fb21a14f59888b1a4 Mon Sep 17 00:00:00 2001 From: Lars Gyrup Brink Nielsen Date: Mon, 19 Aug 2024 22:35:06 +0200 Subject: [PATCH] test: cover nested route URL --- .../nested-route-url.spec.ts | 112 ++++++++++++++++++ .../nested-route-url.spec.ts | 112 ++++++++++++++++++ 2 files changed, 224 insertions(+) create mode 100644 packages/router-component-store/src/lib/global-router-store/nested-route-url.spec.ts create mode 100644 packages/router-component-store/src/lib/local-router-store/nested-route-url.spec.ts diff --git a/packages/router-component-store/src/lib/global-router-store/nested-route-url.spec.ts b/packages/router-component-store/src/lib/global-router-store/nested-route-url.spec.ts new file mode 100644 index 0000000..35d616f --- /dev/null +++ b/packages/router-component-store/src/lib/global-router-store/nested-route-url.spec.ts @@ -0,0 +1,112 @@ +import { RouterConfigOptions, Routes } from '@angular/router'; +import { firstValueFrom } from 'rxjs'; +import { RouterStore } from '../router-store'; +import { GlobalRouterStore } from './global-router-store'; +import { globalRouterStoreSetup } from './test-util/global-router-store-setup'; +import { + GlobalRouterStoreTestChildComponent, + GlobalRouterStoreTestGrandchildComponent, + GlobalRouterStoreTestParentComponent, +} from './test-util/global-router-store-test-components'; + +const routes: Routes = [ + { + path: 'parent', + component: GlobalRouterStoreTestParentComponent, + children: [ + { + path: 'child', + component: GlobalRouterStoreTestChildComponent, + children: [ + { + path: 'grandchild', + component: GlobalRouterStoreTestGrandchildComponent, + }, + ], + }, + ], + }, +]; + +const expectedUrls = { + parent: '/parent?query=param#fragment', + child: '/parent/child?query=param#fragment', + grandchild: '/parent/child/grandchild?query=param#fragment', +} as const; + +describe(`${GlobalRouterStore.name} nested route URL`, () => { + describe('Given three layers of routes with components', () => { + const paramsInheritanceStrategies: RouterConfigOptions['paramsInheritanceStrategy'][] = + ['always', 'emptyOnly']; + + describe.each(paramsInheritanceStrategies)( + ' And the "%s" route parameter inheritance strategy is used', + (paramsInheritanceStrategy) => { + it.each( + [ + GlobalRouterStoreTestParentComponent, + GlobalRouterStoreTestChildComponent, + GlobalRouterStoreTestGrandchildComponent, + ].map((RoutedComponent) => ({ RoutedComponent })) + )( + ` And ${RouterStore.name} is injected at $RoutedComponent.name + When the ${GlobalRouterStoreTestGrandchildComponent.name} route is activated + Then the full URL for the ${GlobalRouterStoreTestGrandchildComponent.name} route is emitted`, + async ({ RoutedComponent }) => { + expect.assertions(1); + const { routerStore } = await globalRouterStoreSetup({ + navigateTo: '/parent/child/grandchild?query=param#fragment', + paramsInheritanceStrategy, + RoutedComponent, + routes, + }); + + await expect(firstValueFrom(routerStore.url$)).resolves.toEqual( + expectedUrls.grandchild + ); + } + ); + + it.each( + [ + GlobalRouterStoreTestParentComponent, + GlobalRouterStoreTestChildComponent, + ].map((RoutedComponent) => ({ RoutedComponent })) + )( + ` And ${RouterStore.name} is injected at $RoutedComponent.name + When the ${GlobalRouterStoreTestChildComponent.name} route is activated + Then the full URL for the ${GlobalRouterStoreTestChildComponent.name} route is emitted`, + async ({ RoutedComponent }) => { + expect.assertions(1); + const { routerStore } = await globalRouterStoreSetup({ + navigateTo: '/parent/child?query=param#fragment', + paramsInheritanceStrategy, + RoutedComponent, + routes, + }); + + await expect(firstValueFrom(routerStore.url$)).resolves.toEqual( + expectedUrls.child + ); + } + ); + + it(` And ${RouterStore.name} is injected at ${GlobalRouterStoreTestParentComponent}.name + When the ${GlobalRouterStoreTestParentComponent.name} route is activated + Then full URL for the ${GlobalRouterStoreTestParentComponent.name} route is emitted`, async () => { + expect.assertions(1); + const { routerStore } = await globalRouterStoreSetup({ + navigateTo: '/parent?query=param#fragment', + paramsInheritanceStrategy, + RoutedComponent: GlobalRouterStoreTestParentComponent, + routes, + }); + + await expect(firstValueFrom(routerStore.url$)).resolves.toEqual( + expectedUrls.parent + ); + }); + } + ); + }); +}); diff --git a/packages/router-component-store/src/lib/local-router-store/nested-route-url.spec.ts b/packages/router-component-store/src/lib/local-router-store/nested-route-url.spec.ts new file mode 100644 index 0000000..a5ed9a5 --- /dev/null +++ b/packages/router-component-store/src/lib/local-router-store/nested-route-url.spec.ts @@ -0,0 +1,112 @@ +import { RouterConfigOptions, Routes } from '@angular/router'; +import { firstValueFrom } from 'rxjs'; +import { RouterStore } from '../router-store'; +import { LocalRouterStore } from './local-router-store'; +import { localRouterStoreSetup } from './test-util/local-router-store-setup'; +import { + LocalRouterStoreTestChildComponent, + LocalRouterStoreTestGrandchildComponent, + LocalRouterStoreTestParentComponent, +} from './test-util/local-router-store-test-components'; + +const routes: Routes = [ + { + path: 'parent', + component: LocalRouterStoreTestParentComponent, + children: [ + { + path: 'child', + component: LocalRouterStoreTestChildComponent, + children: [ + { + path: 'grandchild', + component: LocalRouterStoreTestGrandchildComponent, + }, + ], + }, + ], + }, +]; + +const expectedUrls = { + parent: '/parent?query=param#fragment', + child: '/parent/child?query=param#fragment', + grandchild: '/parent/child/grandchild?query=param#fragment', +} as const; + +describe(`${LocalRouterStore.name} nested route URL`, () => { + describe('Given three layers of routes with components', () => { + const paramsInheritanceStrategies: RouterConfigOptions['paramsInheritanceStrategy'][] = + ['always', 'emptyOnly']; + + describe.each(paramsInheritanceStrategies)( + ' And the "%s" route parameter inheritance strategy is used', + (paramsInheritanceStrategy) => { + it.each( + [ + LocalRouterStoreTestParentComponent, + LocalRouterStoreTestChildComponent, + LocalRouterStoreTestGrandchildComponent, + ].map((RoutedComponent) => ({ RoutedComponent })) + )( + ` And ${RouterStore.name} is injected at $RoutedComponent.name + When the ${LocalRouterStoreTestGrandchildComponent.name} route is activated + Then the full URL for the ${LocalRouterStoreTestGrandchildComponent.name} route is emitted`, + async ({ RoutedComponent }) => { + expect.assertions(1); + const { routerStore } = await localRouterStoreSetup({ + navigateTo: '/parent/child/grandchild?query=param#fragment', + paramsInheritanceStrategy, + RoutedComponent, + routes, + }); + + await expect(firstValueFrom(routerStore.url$)).resolves.toEqual( + expectedUrls.grandchild + ); + } + ); + + it.each( + [ + LocalRouterStoreTestParentComponent, + LocalRouterStoreTestChildComponent, + ].map((RoutedComponent) => ({ RoutedComponent })) + )( + ` And ${RouterStore.name} is injected at $RoutedComponent.name + When the ${LocalRouterStoreTestChildComponent.name} route is activated + Then the full URL for the ${LocalRouterStoreTestChildComponent.name} route is emitted`, + async ({ RoutedComponent }) => { + expect.assertions(1); + const { routerStore } = await localRouterStoreSetup({ + navigateTo: '/parent/child?query=param#fragment', + paramsInheritanceStrategy, + RoutedComponent, + routes, + }); + + await expect(firstValueFrom(routerStore.url$)).resolves.toEqual( + expectedUrls.child + ); + } + ); + + it(` And ${RouterStore.name} is injected at ${LocalRouterStoreTestParentComponent}.name + When the ${LocalRouterStoreTestParentComponent.name} route is activated + Then full URL for the ${LocalRouterStoreTestParentComponent.name} route is emitted`, async () => { + expect.assertions(1); + const { routerStore } = await localRouterStoreSetup({ + navigateTo: '/parent?query=param#fragment', + paramsInheritanceStrategy, + RoutedComponent: LocalRouterStoreTestParentComponent, + routes, + }); + + await expect(firstValueFrom(routerStore.url$)).resolves.toEqual( + expectedUrls.parent + ); + }); + } + ); + }); +});