I am trying to test a component and there is an <img> tag but it is a child of an element that has an *ngIf. Even when I ensure that the if condition is always true, I still cannot grab the image source string. Angular does provide an attribute ng-reflect-ng-src however the string is truncated after 30 characters.
I have tried using waitForAsync, tick, fixture.whenStable, fakeAsync but nothing seems to be working and the source value is always an empty string, but never the actual link that it should be.
Here's some example code for the component HTML:
<div *ngIf="this.hero" class="hero_container">
<img
[ngSrc]="this.hero.hero_image"
alt="hero"
class="hero--broadband"
fill
priority
/>
</div>
Trying to get the element:
const image: HTMLImageElement = fixture.debugElement.query(By.css('.hero--broadband')).nativeElement;
Using Angular 15, what would be the correct way to wait for the image.src value to be set before trying to test against it?
Thanks!
Here's something you can try:
waitForAsyncandfakeAsyncutilities from@angular/core/testingat the top of your test file:waitForAsyncandfakeAsyncutilities. Replace the existing test code with the following: