I am currently facing an issue with testing the MatDialog open and close functions. No matter what I try, I cannot seem to successfully test either the open or close functions. I am wondering how I can mock these functions in order to properly test them. When attempting to implement this in my code, I encounter the following error related to spyOn on the 'close' function: "Argument of type '"close"' is not assignable to parameter of type 'keyof MatDialog'" TS:
//... imports etc.
dialogRef:MatDialogRef<DialogComponent>
constructor(public dialogService:MatDialog){}
public test(){
this.openProgressDialog();
//http request
this.closeProgressDialog();
}
public openProgressDialog(){
this.dialogRef=this.dialogService.open(DialogComponent,{
disableClose:true,
height:'150px',
width:'400px'
});
}
public closeProgressDialog(){
this.dialogRef.close();
}
spec.ts:
//... imports
describe("TestComponent",()=>{
beforeEach(async () => {
await TestBed.configureTestingModule({
imports:[appModule,MatDialogModule],
providers:[DialogComponent],
}).compileComponents();
fixture=TestBed.createComponent(TestComponent);
component=fixture.componentInstance;
fixture.detectChanges();
})
test("should test", ()=>{
spyOn(component.dialogService,'open').and.call.Through(); // the problem is at this two lines
spyOn(component.dialogService,'close').and.call.Through();
component.test();
//expect ... (just for example)
})