I am a beginner in working with Angular. I am currently facing an issue while writing a unit test for mat dialog which is resulting in an error.
This is the method in my TypeScript file :
isMobileScreen= Observable<BreakpointState>= this.breakpointObserver.observe('(max-width:600px)');
OpenDialog(){
if(index===0){
this.dialogref=this.dialog.open(MyComonent,{
maxWidth:'600px'
});
Const dialogSub= this.isMobileScreen.subscribe(result=> {
if(result.matches){
this.dialogref.updatesize('100%','100%');
}
else{
this.dialogref.updatesize('50%');
}
});
this.dialogref.afterclosed(). subscribe (results=>{
dialogSub.unsubcribe():
});
}
Spec file
class isMobileScreen {
Value= new subject ();
isMobileScreen= this.value.asObservable();
setValue(val){ // encountering an error here - cannot read property 'setvalue' of undefined
this.value.next(val);
}
}
// Inside describe
describe('component', ()=>{
...
Let isMobileScreen: isMobileScreen;
Const dialogRefMock={
afterClosed(){
return of(true);
},
updatesize(width?:string, height?: string){}
};
Const dialogMock={ open:()=> dialogRefMock};
// Inside before each, I provided dialogMock as a provider
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ MyComponent],
imports: [RouterTestingModule,MatDialogModule],
providers: [ { provide: MatDialog, useValue: dialogStub },{provide : MatDialogRef, useValue : {}, { provide: isMobileScreen, useValue: isMobileScreen }]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(MyComponent);
component = fixture.componentInstance;
fixture.detectChanges();
isMobileScreen = TestBed.get(isMobileScreen);
});
it('',async(()=>{
component.Opendialog();
isMobileScreen.setvalue(true);
fixture.detectChanges();
fixture.whenStable().then(()=>{
let spy= spyOn(component.dialogref, 'updatesize').and.callThough();
expect (spy).toHaveBeenCalled(); // encountering an error here - this.dialogref.updatesize is not a function.
});
}));
});
I am facing two errors:
- cannot read property 'setvalue' of undefined
- this.dialogref.updatesize is not a function.
It seems like there might be an issue with my mocking. Any help to resolve these errors would be greatly appreciated.