@@ -220,7 +220,7 @@ describe('Angular component life-cycle hooks', () => {
220220 expect ( nameInitialized ) . toHaveBeenCalledWith ( 'Initial' ) ;
221221 } ) ;
222222
223- it ( 'invokes ngOnChanges on initial render before ngOnInit' , async ( ) => {
223+ it ( 'invokes ngOnChanges with componentProperties on initial render before ngOnInit' , async ( ) => {
224224 const nameInitialized = jest . fn ( ) ;
225225 const nameChanged = jest . fn ( ) ;
226226 const componentProperties = { nameInitialized, nameChanged, name : 'Sarah' } ;
@@ -233,6 +233,23 @@ describe('Angular component life-cycle hooks', () => {
233233 expect ( nameChanged ) . toHaveBeenCalledWith ( 'Sarah' , true ) ;
234234 /// expect `nameChanged` to be called before `nameInitialized`
235235 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 ) ;
236253 } ) ;
237254
238255 it ( 'does not invoke ngOnChanges when no properties are provided' , async ( ) => {
@@ -243,30 +260,39 @@ describe('Angular component life-cycle hooks', () => {
243260 }
244261 }
245262
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 ( ) ;
247269 } ) ;
248270} ) ;
249271
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+ } ) ;
262285
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+ } ) ;
265289} ) ;
266290
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 ) ;
269294
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+ } ) ;
272298} ) ;
0 commit comments