2016-07-07 21:13:32 +00:00
|
|
|
/**
|
|
|
|
|
* @license
|
2020-05-19 19:08:49 +00:00
|
|
|
* Copyright Google LLC All Rights Reserved.
|
2016-07-07 21:13:32 +00:00
|
|
|
*
|
|
|
|
|
* Use of this source code is governed by an MIT-style license that can be
|
2024-09-20 15:23:15 +00:00
|
|
|
* found in the LICENSE file at https://angular.dev/license
|
2016-07-07 21:13:32 +00:00
|
|
|
*/
|
|
|
|
|
|
2022-10-05 23:16:49 +00:00
|
|
|
import {Location} from '@angular/common';
|
|
|
|
|
import {provideLocationMocks} from '@angular/common/testing';
|
2022-11-23 23:07:35 +00:00
|
|
|
import {Compiler, inject, Injector, ModuleWithProviders, NgModule} from '@angular/core';
|
2024-02-07 16:00:01 +00:00
|
|
|
import {
|
|
|
|
|
ChildrenOutletContexts,
|
|
|
|
|
ExtraOptions,
|
|
|
|
|
NoPreloading,
|
|
|
|
|
Route,
|
|
|
|
|
Router,
|
|
|
|
|
ROUTER_CONFIGURATION,
|
|
|
|
|
RouteReuseStrategy,
|
|
|
|
|
RouterModule,
|
|
|
|
|
ROUTES,
|
|
|
|
|
Routes,
|
|
|
|
|
TitleStrategy,
|
|
|
|
|
UrlHandlingStrategy,
|
|
|
|
|
UrlSerializer,
|
|
|
|
|
withPreloading,
|
|
|
|
|
ɵROUTER_PROVIDERS as ROUTER_PROVIDERS,
|
|
|
|
|
} from '@angular/router';
|
2021-06-14 20:43:04 +00:00
|
|
|
|
2024-02-07 16:00:01 +00:00
|
|
|
function isUrlHandlingStrategy(
|
|
|
|
|
opts: ExtraOptions | UrlHandlingStrategy,
|
|
|
|
|
): opts is UrlHandlingStrategy {
|
2017-11-29 00:57:10 +00:00
|
|
|
// This property check is needed because UrlHandlingStrategy is an interface and doesn't exist at
|
|
|
|
|
// runtime.
|
|
|
|
|
return 'shouldProcessUrl' in opts;
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-23 23:07:35 +00:00
|
|
|
function throwInvalidConfigError(parameter: string): never {
|
|
|
|
|
throw new Error(
|
2024-02-07 16:00:01 +00:00
|
|
|
`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.',
|
|
|
|
|
);
|
2022-11-23 23:07:35 +00:00
|
|
|
}
|
|
|
|
|
|
2016-07-07 21:13:32 +00:00
|
|
|
/**
|
2018-04-05 10:38:57 +00:00
|
|
|
* @description
|
|
|
|
|
*
|
2018-04-05 10:51:21 +00:00
|
|
|
* Sets up the router to be used for testing.
|
|
|
|
|
*
|
2018-04-05 10:38:57 +00:00
|
|
|
* The modules sets up the router to be used for testing.
|
2021-09-24 21:20:25 +00:00
|
|
|
* It provides spy implementations of `Location` and `LocationStrategy`.
|
2018-04-05 10:38:57 +00:00
|
|
|
*
|
2018-09-20 13:50:32 +00:00
|
|
|
* @usageNotes
|
2018-04-05 10:38:57 +00:00
|
|
|
* ### Example
|
2016-07-07 21:13:32 +00:00
|
|
|
*
|
|
|
|
|
* ```
|
|
|
|
|
* beforeEach(() => {
|
2020-05-27 13:44:47 +00:00
|
|
|
* TestBed.configureTestingModule({
|
2016-10-06 17:22:39 +00:00
|
|
|
* imports: [
|
2023-03-14 20:31:54 +00:00
|
|
|
* RouterModule.forRoot(
|
2018-01-03 21:45:17 +00:00
|
|
|
* [{path: '', component: BlankCmp}, {path: 'simple', component: SimpleCmp}]
|
2016-08-19 23:01:59 +00:00
|
|
|
* )
|
|
|
|
|
* ]
|
2016-07-07 21:13:32 +00:00
|
|
|
* });
|
|
|
|
|
* });
|
|
|
|
|
* ```
|
|
|
|
|
*
|
2018-10-19 17:25:11 +00:00
|
|
|
* @publicApi
|
2024-02-15 19:29:51 +00:00
|
|
|
* @deprecated Use `provideRouter` or `RouterModule`/`RouterModule.forRoot` instead.
|
|
|
|
|
* This module was previously used to provide a helpful collection of test fakes,
|
|
|
|
|
* most notably those for `Location` and `LocationStrategy`. These are generally not
|
|
|
|
|
* required anymore, as `MockPlatformLocation` is provided in `TestBed` by default.
|
|
|
|
|
* However, you can use them directly with `provideLocationMocks`.
|
2016-07-07 21:13:32 +00:00
|
|
|
*/
|
2016-07-18 10:50:31 +00:00
|
|
|
@NgModule({
|
|
|
|
|
exports: [RouterModule],
|
2016-07-07 21:13:32 +00:00
|
|
|
providers: [
|
2021-09-30 22:43:10 +00:00
|
|
|
ROUTER_PROVIDERS,
|
2022-10-05 23:16:49 +00:00
|
|
|
provideLocationMocks(),
|
2022-08-01 17:38:23 +00:00
|
|
|
withPreloading(NoPreloading).ɵproviders,
|
2022-10-27 18:23:11 +00:00
|
|
|
{provide: ROUTES, multi: true, useValue: []},
|
2024-02-07 16:00:01 +00:00
|
|
|
],
|
2016-07-07 21:13:32 +00:00
|
|
|
})
|
2016-07-20 18:39:31 +00:00
|
|
|
export class RouterTestingModule {
|
2024-02-07 16:00:01 +00:00
|
|
|
static withRoutes(
|
|
|
|
|
routes: Routes,
|
|
|
|
|
config?: ExtraOptions,
|
|
|
|
|
): ModuleWithProviders<RouterTestingModule> {
|
2017-11-29 00:57:10 +00:00
|
|
|
return {
|
|
|
|
|
ngModule: RouterTestingModule,
|
|
|
|
|
providers: [
|
2022-10-27 18:23:11 +00:00
|
|
|
{provide: ROUTES, multi: true, useValue: routes},
|
2017-11-29 00:57:10 +00:00
|
|
|
{provide: ROUTER_CONFIGURATION, useValue: config ? config : {}},
|
2024-02-07 16:00:01 +00:00
|
|
|
],
|
2017-11-29 00:57:10 +00:00
|
|
|
};
|
2016-08-19 23:01:59 +00:00
|
|
|
}
|
2016-07-07 21:13:32 +00:00
|
|
|
}
|