@@ -220,7 +220,7 @@ describe('Angular component life-cycle hooks', () => {
220
220
expect ( nameInitialized ) . toHaveBeenCalledWith ( 'Initial' ) ;
221
221
} ) ;
222
222
223
- it ( 'invokes ngOnChanges on initial render before ngOnInit' , async ( ) => {
223
+ it ( 'invokes ngOnChanges with componentProperties on initial render before ngOnInit' , async ( ) => {
224
224
const nameInitialized = jest . fn ( ) ;
225
225
const nameChanged = jest . fn ( ) ;
226
226
const componentProperties = { nameInitialized, nameChanged, name : 'Sarah' } ;
@@ -233,6 +233,23 @@ describe('Angular component life-cycle hooks', () => {
233
233
expect ( nameChanged ) . toHaveBeenCalledWith ( 'Sarah' , true ) ;
234
234
/// expect `nameChanged` to be called before `nameInitialized`
235
235
expect ( nameChanged . mock . invocationCallOrder [ 0 ] ) . toBeLessThan ( nameInitialized . mock . invocationCallOrder [ 0 ] ) ;
236
+ expect ( nameChanged ) . toHaveBeenCalledTimes ( 1 ) ;
237
+ } ) ;
238
+
239
+ it ( 'invokes ngOnChanges with componentInputs on initial render before ngOnInit' , async ( ) => {
240
+ const nameInitialized = jest . fn ( ) ;
241
+ const nameChanged = jest . fn ( ) ;
242
+ const componentInput = { nameInitialized, nameChanged, name : 'Sarah' } ;
243
+
244
+ const view = await render ( FixtureWithNgOnChangesComponent , { componentInputs : componentInput } ) ;
245
+
246
+ /// We wish to test the utility function from `render` here.
247
+ // eslint-disable-next-line testing-library/prefer-screen-queries
248
+ expect ( view . getByText ( 'Sarah' ) ) . toBeInTheDocument ( ) ;
249
+ expect ( nameChanged ) . toHaveBeenCalledWith ( 'Sarah' , true ) ;
250
+ /// expect `nameChanged` to be called before `nameInitialized`
251
+ expect ( nameChanged . mock . invocationCallOrder [ 0 ] ) . toBeLessThan ( nameInitialized . mock . invocationCallOrder [ 0 ] ) ;
252
+ expect ( nameChanged ) . toHaveBeenCalledTimes ( 1 ) ;
236
253
} ) ;
237
254
238
255
it ( 'does not invoke ngOnChanges when no properties are provided' , async ( ) => {
@@ -243,30 +260,39 @@ describe('Angular component life-cycle hooks', () => {
243
260
}
244
261
}
245
262
246
- await render ( TestFixtureComponent ) ;
263
+ const { fixture, detectChanges } = await render ( TestFixtureComponent ) ;
264
+ const spy = jest . spyOn ( fixture . componentInstance , 'ngOnChanges' ) ;
265
+
266
+ detectChanges ( ) ;
267
+
268
+ expect ( spy ) . not . toHaveBeenCalled ( ) ;
247
269
} ) ;
248
270
} ) ;
249
271
250
- test ( 'waits for angular app initialization before rendering components' , async ( ) => {
251
- const mock = jest . fn ( ) ;
252
-
253
- await render ( FixtureComponent , {
254
- providers : [
255
- {
256
- provide : APP_INITIALIZER ,
257
- useFactory : ( ) => mock ,
258
- multi : true ,
259
- } ,
260
- ] ,
261
- } ) ;
272
+ describe ( 'initializer' , ( ) => {
273
+ it ( 'waits for angular app initialization before rendering components' , async ( ) => {
274
+ const mock = jest . fn ( ) ;
275
+
276
+ await render ( FixtureComponent , {
277
+ providers : [
278
+ {
279
+ provide : APP_INITIALIZER ,
280
+ useFactory : ( ) => mock ,
281
+ multi : true ,
282
+ } ,
283
+ ] ,
284
+ } ) ;
262
285
263
- expect ( TestBed . inject ( ApplicationInitStatus ) . done ) . toBe ( true ) ;
264
- expect ( mock ) . toHaveBeenCalled ( ) ;
286
+ expect ( TestBed . inject ( ApplicationInitStatus ) . done ) . toBe ( true ) ;
287
+ expect ( mock ) . toHaveBeenCalled ( ) ;
288
+ } ) ;
265
289
} ) ;
266
290
267
- test ( 'gets the DebugElement' , async ( ) => {
268
- const view = await render ( FixtureComponent ) ;
291
+ describe ( 'DebugElement' , ( ) => {
292
+ it ( 'gets the DebugElement' , async ( ) => {
293
+ const view = await render ( FixtureComponent ) ;
269
294
270
- expect ( view . debugElement ) . not . toBeNull ( ) ;
271
- expect ( view . debugElement . componentInstance ) . toBeInstanceOf ( FixtureComponent ) ;
295
+ expect ( view . debugElement ) . not . toBeNull ( ) ;
296
+ expect ( view . debugElement . componentInstance ) . toBeInstanceOf ( FixtureComponent ) ;
297
+ } ) ;
272
298
} ) ;
0 commit comments