I'm trying to spy a call service to know if have been called after matdialog is closed.
But I have this error..
TypeError: Cannot read property 'close' of undefined
But if I not try to close the dialog, the code inside "afterClosed()" subscription, is never called
This is my Component method:
constructor(private service:CustomService,
public dialog: MatDialog,
private dialogRef:MatDialogRef<EditarGrupoDialogComponent, any>) {}
//[....]
modificar:void{
//Open dialog to edit object
this.dialogRef = this.dialog.open(EditarGrupoDialogComponent, {
width: '80%',
height: 'auto',
disableClose: true,
data: {
groupToEdit: group
}
});
//If close with modification, we send the mew version to service
this.dialogRef.afterClosed().subscribe(
resultados => {
//If there are results
if(resul){
//Peticion al servicio de historial para publicar modificacion
this.service.realiza(resul).subscribe(
resul => {
//RequestOk
// [...]
},
(err) => {
//error
this.error=true;
}
);
}// ESLE.No results
}
);
}
This is my spec.ts file:
describe('Test GComponent', () => {
let component: GComponent;
let fixture: ComponentFixture<GComponent>;
const dialogMock = new MatDialogMock();
beforeEach(async () => {
await TestBed.configureTestingModule({
declarations: [ GruposCComponent ],
imports:[
HttpClientTestingModule,
MatDialogModule,
NoopAnimationsModule
],
providers: [
{ provide: CustomService, useClass: ServiceMock },
{ provide: MatDialogRef, useClass: MatDialogRefMock},
{ provide: MatDialog, useValue: dialogMock}
],
schemas: [CUSTOM_ELEMENTS_SCHEMA]
})
.compileComponents();
});
beforeEach(() => {
fixture = TestBed.createComponent(GruposCComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('Clik at button, allow modify group in dialog, and save it afterClosed', ()=>{
//Espiamos el metodo 'modificarGrupoGrupo'
const spy1 = spyOn(component, 'modificar').and.callThrough();
//Espiamos el open Mat dialog
const spy2 = spyOn(component["dialog"], 'open');
//Espiamos metodo que emite suscripcion de cierre de dialog
const spy3= spyOn(component["dialogRef"],"afterClosed").and.returnValue(
of({
group: {
id: "1CESO",
refFrame: "B"
}
})
);
//Espiamos servicio de historial
//Intervenimos injeccion del servicio
const service = fixture.debugElement.injector.get(CustomService);
//Espiamos el servicio para forzar retorno de error
const spy4 =spyOn(service, 'realiza');
//Actualice view
fixture.detectChanges();
//Click the button to edit/open dialog
const botonEditar= fixture.debugElement.query(By.css('#but'));
botonEditar.nativeElement.click();
//Actualice view
fixture.detectChanges();
//Close dialog
component["dialogRef"].close();
//Actualice view
fixture.detectChanges();
expect(spy1).toHaveBeenCalled();
expect(spy2).toHaveBeenCalled();
expect(spy3).toHaveBeenCalled();
expect(spy4.calls.count()).toBe(1);
});
});
My mocks:
export class ServiceMock {
realiza(accion:Accion):Observable<boolean>{
//Simulate ok
return of(true);
}
}
/** Mock of refDialog */
export class MatDialogRefMock {
close():void{}
afterClosed(): Observable<any>{
return of(true);
}
}
/** Mock del Dialog */
export class MatDialogMock {
open() {
return {afterClosed: () => of({
group: {
id: "1CESO",
refFrame: "B"
}
}) };
}
}
What could be wrong? I only need check that dialogs open when the button of the view is cliked, and when de dialog is closed and retuns the object, te service is called with the object information.
THANS YOU FOR THE HELP!!!