@angular/router
Version:
Angular - the routing library
1 lines • 15.5 kB
Source Map (JSON)
{"version":3,"file":"testing.mjs","sources":["../../../../../darwin_arm64-fastbuild-ST-199a4f3c4e20/bin/packages/router/testing/src/router_testing_module.ts","../../../../../darwin_arm64-fastbuild-ST-199a4f3c4e20/bin/packages/router/testing/src/router_testing_harness.ts"],"sourcesContent":["/**\n * @license\n * Copyright Google LLC All Rights Reserved.\n *\n * Use of this source code is governed by an MIT-style license that can be\n * found in the LICENSE file at https://angular.dev/license\n */\n\nimport {provideLocationMocks} from '@angular/common/testing';\nimport {ModuleWithProviders, NgModule} from '@angular/core';\nimport {\n ExtraOptions,\n NoPreloading,\n ROUTER_CONFIGURATION,\n RouterModule,\n ROUTES,\n Routes,\n withPreloading,\n ɵROUTER_PROVIDERS as ROUTER_PROVIDERS,\n} from '../../index';\n\n/**\n * @description\n *\n * Sets up the router to be used for testing.\n *\n * The modules sets up the router to be used for testing.\n * It provides spy implementations of `Location` and `LocationStrategy`.\n *\n * @usageNotes\n * ### Example\n *\n * ```ts\n * beforeEach(() => {\n * TestBed.configureTestingModule({\n * imports: [\n * RouterModule.forRoot(\n * [{path: '', component: BlankCmp}, {path: 'simple', component: SimpleCmp}]\n * )\n * ]\n * });\n * });\n * ```\n *\n * @publicApi\n * @deprecated Use `provideRouter` or `RouterModule`/`RouterModule.forRoot` instead.\n * This module was previously used to provide a helpful collection of test fakes,\n * most notably those for `Location` and `LocationStrategy`. These are generally not\n * required anymore, as `MockPlatformLocation` is provided in `TestBed` by default.\n * However, you can use them directly with `provideLocationMocks`.\n */\n@NgModule({\n exports: [RouterModule],\n providers: [\n ROUTER_PROVIDERS,\n provideLocationMocks(),\n withPreloading(NoPreloading).ɵproviders,\n {provide: ROUTES, multi: true, useValue: []},\n ],\n})\nexport class RouterTestingModule {\n static withRoutes(\n routes: Routes,\n config?: ExtraOptions,\n ): ModuleWithProviders<RouterTestingModule> {\n return {\n ngModule: RouterTestingModule,\n providers: [\n {provide: ROUTES, multi: true, useValue: routes},\n {provide: ROUTER_CONFIGURATION, useValue: config ? config : {}},\n ],\n };\n }\n}\n","/**\n * @license\n * Copyright Google LLC All Rights Reserved.\n *\n * Use of this source code is governed by an MIT-style license that can be\n * found in the LICENSE file at https://angular.dev/license\n */\n\nimport {\n Component,\n DebugElement,\n Injectable,\n Type,\n ViewChild,\n WritableSignal,\n signal,\n} from '@angular/core';\nimport {ComponentFixture, TestBed} from '@angular/core/testing';\nimport {Router, RouterOutlet, ɵafterNextNavigation as afterNextNavigation} from '../../index';\n\n@Injectable({providedIn: 'root'})\nexport class RootFixtureService {\n private fixture?: ComponentFixture<RootCmp>;\n private harness?: RouterTestingHarness;\n\n createHarness(): RouterTestingHarness {\n if (this.harness) {\n throw new Error('Only one harness should be created per test.');\n }\n this.harness = new RouterTestingHarness(this.getRootFixture());\n return this.harness;\n }\n\n private getRootFixture(): ComponentFixture<RootCmp> {\n if (this.fixture !== undefined) {\n return this.fixture;\n }\n this.fixture = TestBed.createComponent(RootCmp);\n this.fixture.detectChanges();\n return this.fixture;\n }\n}\n\n@Component({\n template: '<router-outlet [routerOutletData]=\"routerOutletData()\"></router-outlet>',\n imports: [RouterOutlet],\n})\nexport class RootCmp {\n @ViewChild(RouterOutlet) outlet?: RouterOutlet;\n readonly routerOutletData = signal<unknown>(undefined);\n}\n\n/**\n * A testing harness for the `Router` to reduce the boilerplate needed to test routes and routed\n * components.\n *\n * @publicApi\n */\nexport class RouterTestingHarness {\n /**\n * Creates a `RouterTestingHarness` instance.\n *\n * The `RouterTestingHarness` also creates its own root component with a `RouterOutlet` for the\n * purposes of rendering route components.\n *\n * Throws an error if an instance has already been created.\n * Use of this harness also requires `destroyAfterEach: true` in the `ModuleTeardownOptions`\n *\n * @param initialUrl The target of navigation to trigger before returning the harness.\n */\n static async create(initialUrl?: string): Promise<RouterTestingHarness> {\n const harness = TestBed.inject(RootFixtureService).createHarness();\n if (initialUrl !== undefined) {\n await harness.navigateByUrl(initialUrl);\n }\n return harness;\n }\n\n /**\n * Fixture of the root component of the RouterTestingHarness\n */\n public readonly fixture: ComponentFixture<{routerOutletData: WritableSignal<unknown>}>;\n\n /** @internal */\n constructor(fixture: ComponentFixture<{routerOutletData: WritableSignal<unknown>}>) {\n this.fixture = fixture;\n }\n\n /** Instructs the root fixture to run change detection. */\n detectChanges(): void {\n this.fixture.detectChanges();\n }\n /** The `DebugElement` of the `RouterOutlet` component. `null` if the outlet is not activated. */\n get routeDebugElement(): DebugElement | null {\n const outlet = (this.fixture.componentInstance as RootCmp).outlet;\n if (!outlet || !outlet.isActivated) {\n return null;\n }\n return this.fixture.debugElement.query((v) => v.componentInstance === outlet.component);\n }\n /** The native element of the `RouterOutlet` component. `null` if the outlet is not activated. */\n get routeNativeElement(): HTMLElement | null {\n return this.routeDebugElement?.nativeElement ?? null;\n }\n\n /**\n * Triggers a `Router` navigation and waits for it to complete.\n *\n * The root component with a `RouterOutlet` created for the harness is used to render `Route`\n * components. The root component is reused within the same test in subsequent calls to\n * `navigateForTest`.\n *\n * When testing `Routes` with a guards that reject the navigation, the `RouterOutlet` might not be\n * activated and the `activatedComponent` may be `null`.\n *\n * {@example router/testing/test/router_testing_harness_examples.spec.ts region='Guard'}\n *\n * @param url The target of the navigation. Passed to `Router.navigateByUrl`.\n * @returns The activated component instance of the `RouterOutlet` after navigation completes\n * (`null` if the outlet does not get activated).\n */\n async navigateByUrl(url: string): Promise<null | {}>;\n /**\n * Triggers a router navigation and waits for it to complete.\n *\n * The root component with a `RouterOutlet` created for the harness is used to render `Route`\n * components.\n *\n * {@example router/testing/test/router_testing_harness_examples.spec.ts region='RoutedComponent'}\n *\n * The root component is reused within the same test in subsequent calls to `navigateByUrl`.\n *\n * This function also makes it easier to test components that depend on `ActivatedRoute` data.\n *\n * {@example router/testing/test/router_testing_harness_examples.spec.ts region='ActivatedRoute'}\n *\n * @param url The target of the navigation. Passed to `Router.navigateByUrl`.\n * @param requiredRoutedComponentType After navigation completes, the required type for the\n * activated component of the `RouterOutlet`. If the outlet is not activated or a different\n * component is activated, this function will throw an error.\n * @returns The activated component instance of the `RouterOutlet` after navigation completes.\n */\n async navigateByUrl<T>(url: string, requiredRoutedComponentType: Type<T>): Promise<T>;\n async navigateByUrl<T>(url: string, requiredRoutedComponentType?: Type<T>): Promise<T | null> {\n const router = TestBed.inject(Router);\n let resolveFn!: () => void;\n const redirectTrackingPromise = new Promise<void>((resolve) => {\n resolveFn = resolve;\n });\n afterNextNavigation(TestBed.inject(Router), resolveFn);\n await router.navigateByUrl(url);\n await redirectTrackingPromise;\n this.fixture.detectChanges();\n const outlet = (this.fixture.componentInstance as RootCmp).outlet;\n // The outlet might not be activated if the user is testing a navigation for a guard that\n // rejects\n if (outlet && outlet.isActivated && outlet.activatedRoute.component) {\n const activatedComponent = outlet.component;\n if (\n requiredRoutedComponentType !== undefined &&\n !(activatedComponent instanceof requiredRoutedComponentType)\n ) {\n throw new Error(\n `Unexpected routed component type. Expected ${requiredRoutedComponentType.name} but got ${activatedComponent.constructor.name}`,\n );\n }\n return activatedComponent as T;\n } else {\n if (requiredRoutedComponentType !== undefined) {\n throw new Error(\n `Unexpected routed component type. Expected ${requiredRoutedComponentType.name} but the navigation did not activate any component.`,\n );\n }\n return null;\n }\n }\n}\n"],"names":["RouterTestingModule","withRoutes","routes","config","ngModule","providers","provide","ROUTES","multi","useValue","ROUTER_CONFIGURATION","deps","target","i0","ɵɵFactoryTarget","NgModule","ɵmod","ɵɵngDeclareNgModule","minVersion","version","ngImport","type","RouterModule","ɵinj","ɵɵngDeclareInjector","ROUTER_PROVIDERS","provideLocationMocks","withPreloading","NoPreloading","ɵproviders","imports","decorators","args","exports","RootFixtureService","fixture","harness","createHarness","Error","RouterTestingHarness","getRootFixture","undefined","TestBed","createComponent","RootCmp","detectChanges","Injectable","ɵprov","ɵɵngDeclareInjectable","providedIn","outlet","routerOutletData","signal","Component","ɵcmp","ɵɵngDeclareComponent","isStandalone","selector","viewQueries","propertyName","first","predicate","RouterOutlet","descendants","template","inputs","outputs","exportAs","ViewChild","create","initialUrl","inject","navigateByUrl","constructor","routeDebugElement","componentInstance","isActivated","debugElement","query","v","component","routeNativeElement","nativeElement","url","requiredRoutedComponentType","router","Router","resolveFn","redirectTrackingPromise","Promise","resolve","afterNextNavigation","activatedRoute","activatedComponent","name"],"mappings":";;;;;;;;;;;;;;;;;;;MA4DaA,mBAAmB,CAAA;AAC9B,EAAA,OAAOC,UAAUA,CACfC,MAAc,EACdC,MAAqB,EAAA;IAErB,OAAO;AACLC,MAAAA,QAAQ,EAAEJ,mBAAmB;AAC7BK,MAAAA,SAAS,EAAE,CACT;AAACC,QAAAA,OAAO,EAAEC,MAAM;AAAEC,QAAAA,KAAK,EAAE,IAAI;AAAEC,QAAAA,QAAQ,EAAEP;AAAO,OAAA,EAChD;AAACI,QAAAA,OAAO,EAAEI,oBAAoB;AAAED,QAAAA,QAAQ,EAAEN,MAAM,GAAGA,MAAM,GAAG;OAAG;KAElE;AACH;;;;;UAZWH,mBAAmB;AAAAW,IAAAA,IAAA,EAAA,EAAA;AAAAC,IAAAA,MAAA,EAAAC,EAAA,CAAAC,eAAA,CAAAC;AAAA,GAAA,CAAA;AAAnB,EAAA,OAAAC,IAAA,GAAAH,EAAA,CAAAI,mBAAA,CAAA;AAAAC,IAAAA,UAAA,EAAA,QAAA;AAAAC,IAAAA,OAAA,EAAA,mBAAA;AAAAC,IAAAA,QAAA,EAAAP,EAAA;AAAAQ,IAAAA,IAAA,EAAArB,mBAAmB;cARpBsB,YAAY;AAAA,GAAA,CAAA;AAQX,EAAA,OAAAC,IAAA,GAAAV,EAAA,CAAAW,mBAAA,CAAA;AAAAN,IAAAA,UAAA,EAAA,QAAA;AAAAC,IAAAA,OAAA,EAAA,mBAAA;AAAAC,IAAAA,QAAA,EAAAP,EAAA;AAAAQ,IAAAA,IAAA,EAAArB,mBAAmB;AAPnBK,IAAAA,SAAA,EAAA,CACToB,gBAAgB,EAChBC,oBAAoB,EAAE,EACtBC,cAAc,CAACC,Y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