@@ -29,7 +29,7 @@ import { RenderComponentOptions, RenderTemplateOptions, RenderResult, ComponentO
29
29
import { getConfig } from './config' ;
30
30
31
31
const mountedFixtures = new Set < ComponentFixture < any > > ( ) ;
32
- const inject = TestBed . inject || TestBed . get ;
32
+ const safeInject = TestBed . inject || TestBed . get ;
33
33
34
34
export async function render < ComponentType > (
35
35
component : Type < ComponentType > ,
@@ -99,6 +99,17 @@ export async function render<SutType, WrapperType = SutType>(
99
99
100
100
const componentContainer = createComponentFixture ( sut , wrapper ) ;
101
101
102
+ const zone = safeInject ( NgZone ) ;
103
+ const router = safeInject ( Router ) ;
104
+
105
+ if ( typeof router ?. initialNavigation === 'function' ) {
106
+ if ( zone ) {
107
+ zone . run ( ( ) => router ?. initialNavigation ( ) ) ;
108
+ } else {
109
+ router ?. initialNavigation ( ) ;
110
+ }
111
+ }
112
+
102
113
let fixture : ComponentFixture < SutType > ;
103
114
let detectChanges : ( ) => void ;
104
115
@@ -120,17 +131,6 @@ export async function render<SutType, WrapperType = SutType>(
120
131
fixture . componentRef . injector . get ( ChangeDetectorRef ) . detectChanges ( ) ;
121
132
} ;
122
133
123
- const zone = inject ( NgZone ) ;
124
-
125
- const router = inject ( Router ) ;
126
- if ( typeof router ?. initialNavigation === 'function' ) {
127
- if ( zone ) {
128
- zone . run ( ( ) => router ?. initialNavigation ( ) ) ;
129
- } else {
130
- router ?. initialNavigation ( ) ;
131
- }
132
- }
133
-
134
134
const navigate = async ( elementOrPath : Element | string , basePath = '' ) : Promise < boolean > => {
135
135
const href = typeof elementOrPath === 'string' ? elementOrPath : elementOrPath . getAttribute ( 'href' ) ;
136
136
const [ path , params ] = ( basePath + href ) . split ( '?' ) ;
@@ -229,7 +229,7 @@ export async function render<SutType, WrapperType = SutType>(
229
229
230
230
async function createComponent < SutType > ( component : Type < SutType > ) : Promise < ComponentFixture < SutType > > {
231
231
/* Make sure angular application is initialized before creating component */
232
- await inject ( ApplicationInitStatus ) . donePromise ;
232
+ await safeInject ( ApplicationInitStatus ) . donePromise ;
233
233
return TestBed . createComponent ( component ) ;
234
234
}
235
235
0 commit comments