1
- import { Component , DebugElement , ElementRef , OnInit , Type } from '@angular/core' ;
1
+ import { Component , DebugElement , ElementRef , OnInit , Type , NgZone } from '@angular/core' ;
2
2
import { ComponentFixture , TestBed } from '@angular/core/testing' ;
3
3
import { By } from '@angular/platform-browser' ;
4
4
import { BrowserAnimationsModule , NoopAnimationsModule } from '@angular/platform-browser/animations' ;
5
+ import { Router } from '@angular/router' ;
6
+ import { RouterTestingModule } from '@angular/router/testing' ;
5
7
import { fireEvent , FireFunction , FireObject , getQueriesForElement , prettyDOM } from '@testing-library/dom' ;
6
8
import { RenderOptions , RenderResult } from './models' ;
7
9
import { createSelectOptions , createType } from './user-events' ;
@@ -32,6 +34,7 @@ export async function render<T>(
32
34
componentProperties = { } ,
33
35
componentProviders = [ ] ,
34
36
excludeComponentDeclaration = false ,
37
+ routes,
35
38
} = renderOptions ;
36
39
37
40
const isTemplate = typeof templateOrComponent === 'string' ;
@@ -44,7 +47,7 @@ export async function render<T>(
44
47
45
48
TestBed . configureTestingModule ( {
46
49
declarations : [ ...declarations , ...componentDeclarations ] ,
47
- imports : addAutoImports ( imports ) ,
50
+ imports : addAutoImports ( { imports, routes } ) ,
48
51
providers : [ ...providers ] ,
49
52
schemas : [ ...schemas ] ,
50
53
} ) ;
@@ -80,6 +83,20 @@ export async function render<T>(
80
83
{ } as FireFunction & FireObject ,
81
84
) ;
82
85
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
+
83
100
return {
84
101
fixture,
85
102
container : fixture . nativeElement ,
@@ -89,6 +106,7 @@ export async function render<T>(
89
106
...eventsWithDetectChanges ,
90
107
type : createType ( eventsWithDetectChanges ) ,
91
108
selectOptions : createSelectOptions ( eventsWithDetectChanges ) ,
109
+ navigate,
92
110
} as any ;
93
111
}
94
112
@@ -168,10 +186,16 @@ function declareComponents({ isTemplate, wrapper, excludeComponentDeclaration, t
168
186
return [ templateOrComponent ] ;
169
187
}
170
188
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
+ } ;
175
199
176
- return [ ...imports , NoopAnimationsModule ] ;
200
+ return [ ...imports , ... animations ( ) , ... routing ( ) ] ;
177
201
}
0 commit comments