This is the HTML block:
<mat-select ...>
<mat-option
*ngFor="let option of options"
>
{{ option.text }} // I am looking to retrieve this data.
</mat-option>
</mat-select>
When writing my typescript unit test, I encountered difficulties in querying the content of option.text
. Despite trying multiple approaches, such as the ones below, I was unable to achieve the desired results:
// Preparation.
const matSelect = fixture.debugElement.query(By.directive(MatSelect)).nativeElement;
matSelect.click();
fixture.detectChanges();
// Attempted queries resulted in null errors:
fixture.debugElement.query(By.directive(MatOption)).nativeElement.textContent
fixture.debugElement.query(By.css('.mat-option')).nativeElement.textContent
fixture.debugElement.query(By.css('mat-option')).nativeElement.textContent