|
| 1 | +import { RouterConfigOptions, Routes } from '@angular/router'; |
| 2 | +import { firstValueFrom } from 'rxjs'; |
| 3 | +import { RouterStore } from '../router-store'; |
| 4 | +import { GlobalRouterStore } from './global-router-store'; |
| 5 | +import { globalRouterStoreSetup } from './test-util/global-router-store-setup'; |
| 6 | +import { |
| 7 | + GlobalRouterStoreTestChildComponent, |
| 8 | + GlobalRouterStoreTestGrandchildComponent, |
| 9 | + GlobalRouterStoreTestParentComponent, |
| 10 | +} from './test-util/global-router-store-test-components'; |
| 11 | + |
| 12 | +const routes: Routes = [ |
| 13 | + { |
| 14 | + path: 'parent', |
| 15 | + component: GlobalRouterStoreTestParentComponent, |
| 16 | + children: [ |
| 17 | + { |
| 18 | + path: 'child', |
| 19 | + component: GlobalRouterStoreTestChildComponent, |
| 20 | + children: [ |
| 21 | + { |
| 22 | + path: 'grandchild', |
| 23 | + component: GlobalRouterStoreTestGrandchildComponent, |
| 24 | + }, |
| 25 | + ], |
| 26 | + }, |
| 27 | + ], |
| 28 | + }, |
| 29 | +]; |
| 30 | + |
| 31 | +const expectedUrls = { |
| 32 | + parent: '/parent?query=param#fragment', |
| 33 | + child: '/parent/child?query=param#fragment', |
| 34 | + grandchild: '/parent/child/grandchild?query=param#fragment', |
| 35 | +} as const; |
| 36 | + |
| 37 | +describe(`${GlobalRouterStore.name} nested route URL`, () => { |
| 38 | + describe('Given three layers of routes with components', () => { |
| 39 | + const paramsInheritanceStrategies: RouterConfigOptions['paramsInheritanceStrategy'][] = |
| 40 | + ['always', 'emptyOnly']; |
| 41 | + |
| 42 | + describe.each(paramsInheritanceStrategies)( |
| 43 | + ' And the "%s" route parameter inheritance strategy is used', |
| 44 | + (paramsInheritanceStrategy) => { |
| 45 | + it.each( |
| 46 | + [ |
| 47 | + GlobalRouterStoreTestParentComponent, |
| 48 | + GlobalRouterStoreTestChildComponent, |
| 49 | + GlobalRouterStoreTestGrandchildComponent, |
| 50 | + ].map((RoutedComponent) => ({ RoutedComponent })) |
| 51 | + )( |
| 52 | + ` And ${RouterStore.name} is injected at $RoutedComponent.name |
| 53 | + When the ${GlobalRouterStoreTestGrandchildComponent.name} route is activated |
| 54 | + Then the full URL for the ${GlobalRouterStoreTestGrandchildComponent.name} route is emitted`, |
| 55 | + async ({ RoutedComponent }) => { |
| 56 | + expect.assertions(1); |
| 57 | + const { routerStore } = await globalRouterStoreSetup({ |
| 58 | + navigateTo: '/parent/child/grandchild?query=param#fragment', |
| 59 | + paramsInheritanceStrategy, |
| 60 | + RoutedComponent, |
| 61 | + routes, |
| 62 | + }); |
| 63 | + |
| 64 | + await expect(firstValueFrom(routerStore.url$)).resolves.toEqual( |
| 65 | + expectedUrls.grandchild |
| 66 | + ); |
| 67 | + } |
| 68 | + ); |
| 69 | + |
| 70 | + it.each( |
| 71 | + [ |
| 72 | + GlobalRouterStoreTestParentComponent, |
| 73 | + GlobalRouterStoreTestChildComponent, |
| 74 | + ].map((RoutedComponent) => ({ RoutedComponent })) |
| 75 | + )( |
| 76 | + ` And ${RouterStore.name} is injected at $RoutedComponent.name |
| 77 | + When the ${GlobalRouterStoreTestChildComponent.name} route is activated |
| 78 | + Then the full URL for the ${GlobalRouterStoreTestChildComponent.name} route is emitted`, |
| 79 | + async ({ RoutedComponent }) => { |
| 80 | + expect.assertions(1); |
| 81 | + const { routerStore } = await globalRouterStoreSetup({ |
| 82 | + navigateTo: '/parent/child?query=param#fragment', |
| 83 | + paramsInheritanceStrategy, |
| 84 | + RoutedComponent, |
| 85 | + routes, |
| 86 | + }); |
| 87 | + |
| 88 | + await expect(firstValueFrom(routerStore.url$)).resolves.toEqual( |
| 89 | + expectedUrls.child |
| 90 | + ); |
| 91 | + } |
| 92 | + ); |
| 93 | + |
| 94 | + it(` And ${RouterStore.name} is injected at ${GlobalRouterStoreTestParentComponent}.name |
| 95 | + When the ${GlobalRouterStoreTestParentComponent.name} route is activated |
| 96 | + Then full URL for the ${GlobalRouterStoreTestParentComponent.name} route is emitted`, async () => { |
| 97 | + expect.assertions(1); |
| 98 | + const { routerStore } = await globalRouterStoreSetup({ |
| 99 | + navigateTo: '/parent?query=param#fragment', |
| 100 | + paramsInheritanceStrategy, |
| 101 | + RoutedComponent: GlobalRouterStoreTestParentComponent, |
| 102 | + routes, |
| 103 | + }); |
| 104 | + |
| 105 | + await expect(firstValueFrom(routerStore.url$)).resolves.toEqual( |
| 106 | + expectedUrls.parent |
| 107 | + ); |
| 108 | + }); |
| 109 | + } |
| 110 | + ); |
| 111 | + }); |
| 112 | +}); |
0 commit comments