I'm trying to test a function call inside a forEach loop, but I get the following error:
Expected a spy, but got Function.
What I don't understand, is that the tests are successful.
it("should emit event", async () => {
component.type = "confirm";
fixture.detectChanges();
spyOn(component.dialogClose, "emit");
const buttons = await loader.getAllHarnesses(MatButtonHarness);
buttons.forEach(async (button) => {
await button.click();
expect(component.dialogClose.emit).toHaveBeenCalled();
});
});
if I change the test to test only one button (and not use a foreach), I don't have any errors:
it("should emit event", async () => {
component.type = "confirm";
fixture.detectChanges();
spyOn(component.dialogClose, "emit");
const button = await loader.getHarness(MatButtonHarness);
await button.click();
expect(component.dialogClose.emit).toHaveBeenCalled();
});
Here the part I'm testing:
@Output()
public dialogClose: EventEmitter<boolean> = new EventEmitter<boolean>();
<div mat-dialog-actions [ngSwitch]="type">
...
<ng-container *ngSwitchCase="'confirm'">
<button mat-button color="warn" (click)="dialogClose.emit(false)">Cancel</button>
<button mat-raised-button color="accent" (click)="dialogClose.emit(true)">Yes</button>
</ng-container>
...
</div>
Why and what's the issue?