From dbae00195e114ac8b967201283962a7e2c0581b4 Mon Sep 17 00:00:00 2001 From: Dmitrij Kuba Date: Wed, 2 Jun 2021 14:28:27 +0300 Subject: [PATCH] feat(router): ability to provide custom route reuse strategy via DI for `RouterTestingModule` (#42434) For now it's not possible to provide custom route reuse strategy via DI for `RouterTestingModule`, only imperative instantiation. These changes makes it possible to provide custom route reuse strategy via DI. PR Close #42434 --- packages/router/test/integration.spec.ts | 11 +++++++++++ .../router/testing/src/router_testing_module.ts | 13 ++++++++++--- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/packages/router/test/integration.spec.ts b/packages/router/test/integration.spec.ts index 3a221459a5..b3bc41553b 100644 --- a/packages/router/test/integration.spec.ts +++ b/packages/router/test/integration.spec.ts @@ -5647,6 +5647,17 @@ describe('Integration', () => { } } + it('should be injectable', () => { + TestBed.configureTestingModule({ + imports: [RouterTestingModule], + providers: [{provide: RouteReuseStrategy, useClass: AttachDetachReuseStrategy}] + }); + + const router = TestBed.inject(Router); + + expect(router.routeReuseStrategy).toBeInstanceOf(AttachDetachReuseStrategy); + }); + it('should support attaching & detaching fragments', fakeAsync(inject([Router, Location], (router: Router, location: Location) => { const fixture = createRoot(router, RootCmp); diff --git a/packages/router/testing/src/router_testing_module.ts b/packages/router/testing/src/router_testing_module.ts index e7dc95e23a..a1ee2b000d 100644 --- a/packages/router/testing/src/router_testing_module.ts +++ b/packages/router/testing/src/router_testing_module.ts @@ -9,7 +9,7 @@ import {Location, LocationStrategy} from '@angular/common'; import {MockLocationStrategy, SpyLocation} from '@angular/common/testing'; import {Compiler, Injectable, Injector, ModuleWithProviders, NgModule, NgModuleFactory, NgModuleFactoryLoader, Optional} from '@angular/core'; -import {ChildrenOutletContexts, ExtraOptions, NoPreloading, PreloadingStrategy, provideRoutes, Route, Router, ROUTER_CONFIGURATION, RouterModule, ROUTES, Routes, UrlHandlingStrategy, UrlSerializer, ɵassignExtraOptionsToRouter as assignExtraOptionsToRouter, ɵflatten as flatten, ɵROUTER_PROVIDERS as ROUTER_PROVIDERS} from '@angular/router'; +import {ChildrenOutletContexts, ExtraOptions, NoPreloading, PreloadingStrategy, provideRoutes, Route, Router, ROUTER_CONFIGURATION, RouteReuseStrategy, RouterModule, ROUTES, Routes, UrlHandlingStrategy, UrlSerializer, ɵassignExtraOptionsToRouter as assignExtraOptionsToRouter, ɵflatten as flatten, ɵROUTER_PROVIDERS as ROUTER_PROVIDERS} from '@angular/router'; @@ -115,7 +115,8 @@ export function setupTestingRouter( export function setupTestingRouter( urlSerializer: UrlSerializer, contexts: ChildrenOutletContexts, location: Location, loader: NgModuleFactoryLoader, compiler: Compiler, injector: Injector, routes: Route[][], - opts?: ExtraOptions|UrlHandlingStrategy, urlHandlingStrategy?: UrlHandlingStrategy) { + opts?: ExtraOptions|UrlHandlingStrategy, urlHandlingStrategy?: UrlHandlingStrategy, + routeReuseStrategy?: RouteReuseStrategy) { const router = new Router( null!, urlSerializer, contexts, location, injector, loader, compiler, flatten(routes)); if (opts) { @@ -131,6 +132,11 @@ export function setupTestingRouter( if (urlHandlingStrategy) { router.urlHandlingStrategy = urlHandlingStrategy; } + + if (routeReuseStrategy) { + router.routeReuseStrategy = routeReuseStrategy; + } + return router; } @@ -170,7 +176,8 @@ export function setupTestingRouter( useFactory: setupTestingRouter, deps: [ UrlSerializer, ChildrenOutletContexts, Location, NgModuleFactoryLoader, Compiler, Injector, - ROUTES, ROUTER_CONFIGURATION, [UrlHandlingStrategy, new Optional()] + ROUTES, ROUTER_CONFIGURATION, [UrlHandlingStrategy, new Optional()], + [RouteReuseStrategy, new Optional()] ] }, {provide: PreloadingStrategy, useExisting: NoPreloading}, provideRoutes([])