Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: cover nested route URL
Browse files Browse the repository at this point in the history
LayZeeDK committed Aug 30, 2024
1 parent ec7c959 commit 9d49464
Showing 2 changed files with 224 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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
);
});
}
);
});
});
Original file line number Diff line number Diff line change
@@ -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
);
});
}
);
});
});

0 comments on commit 9d49464

Please sign in to comment.