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