1- import { Component , DebugElement , ElementRef , OnInit , Type } from '@angular/core' ;
1+ import { Component , DebugElement , ElementRef , OnInit , Type , NgZone } from '@angular/core' ;
22import { ComponentFixture , TestBed } from '@angular/core/testing' ;
33import { By } from '@angular/platform-browser' ;
44import { BrowserAnimationsModule , NoopAnimationsModule } from '@angular/platform-browser/animations' ;
5+ import { Router } from '@angular/router' ;
6+ import { RouterTestingModule } from '@angular/router/testing' ;
57import { fireEvent , FireFunction , FireObject , getQueriesForElement , prettyDOM } from '@testing-library/dom' ;
68import { RenderOptions , RenderResult } from './models' ;
79import { createSelectOptions , createType } from './user-events' ;
@@ -32,6 +34,7 @@ export async function render<T>(
3234 componentProperties = { } ,
3335 componentProviders = [ ] ,
3436 excludeComponentDeclaration = false ,
37+ routes,
3538 } = renderOptions ;
3639
3740 const isTemplate = typeof templateOrComponent === 'string' ;
@@ -44,7 +47,7 @@ export async function render<T>(
4447
4548 TestBed . configureTestingModule ( {
4649 declarations : [ ...declarations , ...componentDeclarations ] ,
47- imports : addAutoImports ( imports ) ,
50+ imports : addAutoImports ( { imports, routes } ) ,
4851 providers : [ ...providers ] ,
4952 schemas : [ ...schemas ] ,
5053 } ) ;
@@ -80,6 +83,20 @@ export async function render<T>(
8083 { } as FireFunction & FireObject ,
8184 ) ;
8285
86+ let router = routes ? ( TestBed . get < Router > ( Router ) as Router ) : null ;
87+ const zone = TestBed . get < NgZone > ( NgZone ) as NgZone ;
88+
89+ async function navigate ( elementOrPath : Element | string , basePath = '' ) {
90+ if ( ! router ) {
91+ router = TestBed . get < Router > ( Router ) as Router ;
92+ }
93+
94+ const href = typeof elementOrPath === 'string' ? elementOrPath : elementOrPath . getAttribute ( 'href' ) ;
95+
96+ await zone . run ( ( ) => router . navigate ( [ basePath + href ] ) ) ;
97+ fixture . detectChanges ( ) ;
98+ }
99+
83100 return {
84101 fixture,
85102 container : fixture . nativeElement ,
@@ -89,6 +106,7 @@ export async function render<T>(
89106 ...eventsWithDetectChanges ,
90107 type : createType ( eventsWithDetectChanges ) ,
91108 selectOptions : createSelectOptions ( eventsWithDetectChanges ) ,
109+ navigate,
92110 } as any ;
93111}
94112
@@ -168,10 +186,16 @@ function declareComponents({ isTemplate, wrapper, excludeComponentDeclaration, t
168186 return [ templateOrComponent ] ;
169187}
170188
171- function addAutoImports ( imports : any [ ] ) {
172- if ( imports . indexOf ( NoopAnimationsModule ) > - 1 || imports . indexOf ( BrowserAnimationsModule ) > - 1 ) {
173- return imports ;
174- }
189+ function addAutoImports ( { imports, routes } : Pick < RenderOptions < any > , 'imports' | 'routes' > ) {
190+ const animations = ( ) => {
191+ const animationIsDefined =
192+ imports . indexOf ( NoopAnimationsModule ) > - 1 || imports . indexOf ( BrowserAnimationsModule ) > - 1 ;
193+ return animationIsDefined ? [ ] : [ NoopAnimationsModule ] ;
194+ } ;
195+
196+ const routing = ( ) => {
197+ return routes ? [ RouterTestingModule . withRoutes ( routes ) ] : [ ] ;
198+ } ;
175199
176- return [ ...imports , NoopAnimationsModule ] ;
200+ return [ ...imports , ... animations ( ) , ... routing ( ) ] ;
177201}
0 commit comments