Open jnizet opened 1 month ago
afterNextRender
runs after the application synchronization happens. With ZoneJS, this happens synchronously after the click
because the handler is called inside the angular zone. There are two ways to resolve this for your test:
NgZone.run
:
TestBed.inject(NgZone).run(() => {
button.click();
fixture.detectChanges();
});
const fixture = TestBed.createComponent(AppComponent);
fixture.autoDetectChanges();
Thank you @atscott.
The same applies to inputs signals
Which @angular/* package(s) are the source of the bug?
core
Is this a regression?
No
Description
Given the following component:
I expect that clicking the button will make the input appear and give it the focus. And it indeed does that, but not in a unit test.
When running the following unit test, all the expectations pass except the last one, and the console logs show that the length of the view children inside the
afterNextRender
callback is 0.The same problem occurs with
@ViewChildren
orviewChild
is being used.Please provide a link to a minimal reproduction of the bug
https://stackblitz.com/edit/stackblitz-starters-kkjfwf?file=repro%2Fsrc%2Fapp%2Fapp.component.spec.ts,repro%2Fsrc%2Fapp%2Fapp.component.ts
Please provide the exception or error you saw
Please provide the environment you discovered this bug in (run
ng version
)Anything else?
To run the repro, here are the commands to run in the terminal: