|
| 1 | +import { ResolveFn, 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 shadowedTitleResolver: ResolveFn<string> = (route) => |
| 13 | + route.data['shadowed']; |
| 14 | + |
| 15 | +const routes: Routes = [ |
| 16 | + { |
| 17 | + path: 'parent', |
| 18 | + component: GlobalRouterStoreTestParentComponent, |
| 19 | + data: { |
| 20 | + parent: 'parent-route-data', |
| 21 | + shadowed: 'parent-route-title', |
| 22 | + }, |
| 23 | + title: shadowedTitleResolver, |
| 24 | + children: [ |
| 25 | + { |
| 26 | + path: 'child', |
| 27 | + component: GlobalRouterStoreTestChildComponent, |
| 28 | + data: { |
| 29 | + child: 'child-route-data', |
| 30 | + shadowed: 'child-route-title', |
| 31 | + }, |
| 32 | + title: shadowedTitleResolver, |
| 33 | + children: [ |
| 34 | + { |
| 35 | + path: 'grandchild', |
| 36 | + component: GlobalRouterStoreTestGrandchildComponent, |
| 37 | + data: { |
| 38 | + grandchild: 'grandchild-route-data', |
| 39 | + shadowed: 'grandchild-route-title', |
| 40 | + }, |
| 41 | + title: shadowedTitleResolver, |
| 42 | + }, |
| 43 | + ], |
| 44 | + }, |
| 45 | + ], |
| 46 | + }, |
| 47 | +]; |
| 48 | + |
| 49 | +describe(`${GlobalRouterStore.name} nested route title`, () => { |
| 50 | + describe('Given three layers of routes with components and route title resolvers', () => { |
| 51 | + const paramsInheritanceStrategies: RouterConfigOptions['paramsInheritanceStrategy'][] = |
| 52 | + ['always', 'emptyOnly']; |
| 53 | + |
| 54 | + describe.each(paramsInheritanceStrategies)( |
| 55 | + ' And the "%s" route parameter inheritance strategy is used', |
| 56 | + (paramsInheritanceStrategy) => { |
| 57 | + it.each( |
| 58 | + [ |
| 59 | + GlobalRouterStoreTestParentComponent, |
| 60 | + GlobalRouterStoreTestChildComponent, |
| 61 | + GlobalRouterStoreTestGrandchildComponent, |
| 62 | + ].map((RoutedComponent) => ({ RoutedComponent })) |
| 63 | + )( |
| 64 | + ` And ${RouterStore.name} is injected at $RoutedComponent.name |
| 65 | + When the ${GlobalRouterStoreTestGrandchildComponent.name} route is activated |
| 66 | + Then the route title for the ${GlobalRouterStoreTestGrandchildComponent.name} route is emitted`, |
| 67 | + async ({ RoutedComponent }) => { |
| 68 | + expect.assertions(2); |
| 69 | + const { ngrxRouterStore, ngrxStore, routerStore } = |
| 70 | + await globalRouterStoreSetup({ |
| 71 | + navigateTo: '/parent/child/grandchild', |
| 72 | + paramsInheritanceStrategy, |
| 73 | + RoutedComponent, |
| 74 | + routes, |
| 75 | + }); |
| 76 | + |
| 77 | + const expectedTitle = 'grandchild-route-title'; |
| 78 | + await expect(firstValueFrom(routerStore.title$)).resolves.toBe( |
| 79 | + expectedTitle |
| 80 | + ); |
| 81 | + await expect( |
| 82 | + firstValueFrom(ngrxStore.select(ngrxRouterStore.selectTitle)) |
| 83 | + ).resolves.toBe( |
| 84 | + // NOTE(@LayZeeDK) Seems to be a bug in NgRx Router Store 15.0.0 |
| 85 | + undefined |
| 86 | + ); |
| 87 | + } |
| 88 | + ); |
| 89 | + |
| 90 | + it.each( |
| 91 | + [ |
| 92 | + GlobalRouterStoreTestParentComponent, |
| 93 | + GlobalRouterStoreTestChildComponent, |
| 94 | + ].map((RoutedComponent) => ({ |
| 95 | + RoutedComponent, |
| 96 | + })) |
| 97 | + )( |
| 98 | + ` And ${RouterStore.name} is injected at $RoutedComponent.name |
| 99 | + When the ${GlobalRouterStoreTestChildComponent.name} route is activated |
| 100 | + Then route title for the ${GlobalRouterStoreTestChildComponent.name} route is emitted`, |
| 101 | + async ({ RoutedComponent }) => { |
| 102 | + expect.assertions(2); |
| 103 | + const { ngrxRouterStore, ngrxStore, routerStore } = |
| 104 | + await globalRouterStoreSetup({ |
| 105 | + navigateTo: '/parent/child', |
| 106 | + paramsInheritanceStrategy, |
| 107 | + RoutedComponent, |
| 108 | + routes, |
| 109 | + }); |
| 110 | + |
| 111 | + const expectedTitle = 'child-route-title'; |
| 112 | + await expect(firstValueFrom(routerStore.title$)).resolves.toBe( |
| 113 | + expectedTitle |
| 114 | + ); |
| 115 | + await expect( |
| 116 | + firstValueFrom(ngrxStore.select(ngrxRouterStore.selectTitle)) |
| 117 | + ).resolves.toBe( |
| 118 | + // NOTE(@LayZeeDK) Seems to be a bug in NgRx Router Store 15.0.0 |
| 119 | + undefined |
| 120 | + ); |
| 121 | + } |
| 122 | + ); |
| 123 | + |
| 124 | + it(` And ${RouterStore.name} is injected at ${GlobalRouterStoreTestParentComponent.name} |
| 125 | + When the ${GlobalRouterStoreTestParentComponent.name} route is activated |
| 126 | + Then route title for the ${GlobalRouterStoreTestParentComponent.name} route is emitted`, async () => { |
| 127 | + expect.assertions(2); |
| 128 | + const { ngrxRouterStore, ngrxStore, routerStore } = |
| 129 | + await globalRouterStoreSetup({ |
| 130 | + navigateTo: '/parent', |
| 131 | + paramsInheritanceStrategy, |
| 132 | + RoutedComponent: GlobalRouterStoreTestParentComponent, |
| 133 | + routes, |
| 134 | + }); |
| 135 | + |
| 136 | + const expectedTitle = 'parent-route-title'; |
| 137 | + await expect(firstValueFrom(routerStore.title$)).resolves.toBe( |
| 138 | + expectedTitle |
| 139 | + ); |
| 140 | + await expect( |
| 141 | + firstValueFrom(ngrxStore.select(ngrxRouterStore.selectTitle)) |
| 142 | + ).resolves.toBe( |
| 143 | + // NOTE(@LayZeeDK) Seems to be a bug in NgRx Router Store 15.0.0 |
| 144 | + undefined |
| 145 | + ); |
| 146 | + }); |
| 147 | + } |
| 148 | + ); |
| 149 | + }); |
| 150 | +}); |
0 commit comments