/**
* @license Angular v16.2.12
* (c) 2010-2022 Google LLC. https://angular.io/
* License: MIT
*/
import { Location } from '@angular/common';
import { provideLocationMocks } from '@angular/common/testing';
import * as i0 from '@angular/core';
import { inject, Compiler, Injector, NgModule, Injectable, Component, ViewChild } from '@angular/core';
import { UrlSerializer, ChildrenOutletContexts, ROUTES, UrlHandlingStrategy, ROUTER_CONFIGURATION, RouteReuseStrategy, TitleStrategy, Router, RouterModule, ɵROUTER_PROVIDERS, withPreloading, NoPreloading, RouterOutlet, ɵafterNextNavigation } from '@angular/router';
import { TestBed } from '@angular/core/testing';
function isUrlHandlingStrategy(opts) {
// This property check is needed because UrlHandlingStrategy is an interface and doesn't exist at
// runtime.
return 'shouldProcessUrl' in opts;
}
function throwInvalidConfigError(parameter) {
throw new Error(`Parameter ${parameter} does not match the one available in the injector. ` +
'`setupTestingRouter` is meant to be used as a factory function with dependencies coming from DI.');
}
/**
* Router setup factory function used for testing.
*
* @publicApi
* @deprecated Use `provideRouter` or `RouterModule` instead.
*/
function setupTestingRouter(urlSerializer, contexts, location, compiler, injector, routes, opts, urlHandlingStrategy, routeReuseStrategy, titleStrategy) {
// Note: The checks below are to detect misconfigured providers and invalid uses of
// `setupTestingRouter`. This function is not used internally (neither in router code or anywhere
// in g3). It appears this function was exposed as publicApi by mistake and should not be used
// externally either. However, if it is, the documented intent is to be used as a factory function
// and parameter values should always match what's available in DI.
if (urlSerializer !== inject(UrlSerializer)) {
throwInvalidConfigError('urlSerializer');
}
if (contexts !== inject(ChildrenOutletContexts)) {
throwInvalidConfigError('contexts');
}
if (location !== inject(Location)) {
throwInvalidConfigError('location');
}
if (compiler !== inject(Compiler)) {
throwInvalidConfigError('compiler');
}
if (injector !== inject(Injector)) {
throwInvalidConfigError('injector');
}
if (routes !== inject(ROUTES)) {
throwInvalidConfigError('routes');
}
if (opts) {
// Handle deprecated argument ordering.
if (isUrlHandlingStrategy(opts)) {
if (opts !== inject(UrlHandlingStrategy)) {
throwInvalidConfigError('opts (UrlHandlingStrategy)');
}
}
else {
if (opts !== inject(ROUTER_CONFIGURATION)) {
throwInvalidConfigError('opts (ROUTER_CONFIGURATION)');
}
}
}
if (urlHandlingStrategy !== inject(UrlHandlingStrategy)) {
throwInvalidConfigError('urlHandlingStrategy');
}
if (routeReuseStrategy !== inject(RouteReuseStrategy)) {
throwInvalidConfigError('routeReuseStrategy');
}
if (titleStrategy !== inject(TitleStrategy)) {
throwInvalidConfigError('titleStrategy');
}
return new Router();
}
/**
* @description
*
* Sets up the router to be used for testing.
*
* The modules sets up the router to be used for testing.
* It provides spy implementations of `Location` and `LocationStrategy`.
*
* @usageNotes
* ### Example
*
* ```
* beforeEach(() => {
* TestBed.configureTestingModule({
* imports: [
* RouterModule.forRoot(
* [{path: '', component: BlankCmp}, {path: 'simple', component: SimpleCmp}]
* )
* ]
* });
* });
* ```
*
* @publicApi
*/
class RouterTestingModule {
static withRoutes(routes, config) {
return {
ngModule: RouterTestingModule,
providers: [
{ provide: ROUTES, multi: true, useValue: routes },
{ provide: ROUTER_CONFIGURATION, useValue: config ? config : {} },
]
};
}
static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: RouterTestingModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule }); }
static { this.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "16.2.12", ngImport: i0, type: RouterTestingModule, exports: [RouterModule] }); }
static { this.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: RouterTestingModule, providers: [
ɵROUTER_PROVIDERS,
provideLocationMocks(),
withPreloading(NoPreloading).ɵproviders,
{ provide: ROUTES, multi: true, useValue: [] },
], imports: [RouterModule] }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: RouterTestingModule, decorators: [{
type: NgModule,
args: [{
exports: [RouterModule],
providers: [
ɵROUTER_PROVIDERS,
provideLocationMocks(),
withPreloading(NoPreloading).ɵproviders,
{ provide: ROUTES, multi: true, useValue: [] },
]
}]
}] });
class RootFixtureService {
createHarness() {
if (this.harness) {
throw new Error('Only one harness should be created per test.');
}
this.harness = new RouterTestingHarness(this.getRootFixture());
return this.harness;
}
getRootFixture() {
if (this.fixture !== undefined) {
return this.fixture;
}
this.fixture = TestBed.createComponent(RootCmp);
this.fixture.detectChanges();
return this.fixture;
}
static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: RootFixtureService, deps: [], target: i0.ɵɵFactoryTarget.Injectable }); }
static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: RootFixtureService, providedIn: 'root' }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: RootFixtureService, decorators: [{
type: Injectable,
args: [{ providedIn: 'root' }]
}] });
class RootCmp {
static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: RootCmp, deps: [], target: i0.ɵɵFactoryTarget.Component }); }
static { this.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "16.2.12", type: RootCmp, isStandalone: true, selector: "ng-component", viewQueries: [{ propertyName: "outlet", first: true, predicate: RouterOutlet, descendants: true }], ngImport: i0, template: '', isInline: true, dependencies: [{ kind: "directive", type: RouterOutlet, selector: "router-outlet", inputs: ["name"], outputs: ["activate", "deactivate", "attach", "detach"], exportAs: ["outlet"] }] }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: RootCmp, decorators: [{
type: Component,
args: [{
standalone: true,
template: '',
imports: [RouterOutlet],
}]
}], propDecorators: { outlet: [{
type: ViewChild,
args: [RouterOutlet]
}] } });
/**
* A testing harness for the `Router` to reduce the boilerplate needed to test routes and routed
* components.
*
* @publicApi
*/
class RouterTestingHarness {
/**
* Creates a `RouterTestingHarness` instance.
*
* The `RouterTestingHarness` also creates its own root component with a `RouterOutlet` for the
* purposes of rendering route components.
*
* Throws an error if an instance has already been created.
* Use of this harness also requires `destroyAfterEach: true` in the `ModuleTeardownOptions`
*
* @param initialUrl The target of navigation to trigger before returning the harness.
*/
static async create(initialUrl) {
const harness = TestBed.inject(RootFixtureService).createHarness();
if (initialUrl !== undefined) {
await harness.navigateByUrl(initialUrl);
}
return harness;
}
/** @internal */
constructor(fixture) {
this.fixture = fixture;
}
/** Instructs the root fixture to run change detection. */
detectChanges() {
this.fixture.detectChanges();
}
/** The `DebugElement` of the `RouterOutlet` component. `null` if the outlet is not activated. */
get routeDebugElement() {
const outlet = this.fixture.componentInstance.outlet;
if (!outlet || !outlet.isActivated) {
return null;
}
return this.fixture.debugElement.query(v => v.componentInstance === outlet.component);
}
/** The native element of the `RouterOutlet` component. `null` if the outlet is not activated. */
get routeNativeElement() {
return this.routeDebugElement?.nativeElement ?? null;
}
async navigateByUrl(url, requiredRoutedComponentType) {
const router = TestBed.inject(Router);
let resolveFn;
const redirectTrackingPromise = new Promise(resolve => {
resolveFn = resolve;
});
ɵafterNextNavigation(TestBed.inject(Router), resolveFn);
await router.navigateByUrl(url);
await redirectTrackingPromise;
this.fixture.detectChanges();
const outlet = this.fixture.componentInstance.outlet;
// The outlet might not be activated if the user is testing a navigation for a guard that
// rejects
if (outlet && outlet.isActivated && outlet.activatedRoute.component) {
const activatedComponent = outlet.component;
if (requiredRoutedComponentType !== undefined &&
!(activatedComponent instanceof requiredRoutedComponentType)) {
throw new Error(`Unexpected routed component type. Expected ${requiredRoutedComponentType.name} but got ${activatedComponent.constructor.name}`);
}
return activatedComponent;
}
else {
if (requiredRoutedComponentType !== undefined) {
throw new Error(`Unexpected routed component type. Expected ${requiredRoutedComponentType.name} but the navigation did not activate any component.`);
}
return null;
}
}
}
/**
* @module
* @description
* Entry point for all public APIs of the router/testing package.
*/
/**
* @module
* @description
* Entry point for all public APIs of this package.
*/
// This file only reexports content of the `src` folder. Keep it that way.
// This file is not used to build this module. It is only used during editing
/**
* Generated bundle index. Do not edit.
*/
export { RouterTestingHarness, RouterTestingModule, setupTestingRouter };
//# sourceMappingURL=testing.mjs.map