I have a custom dialog component class which I am trying to test. Specifically my close function which is this
close(): void {
this.dialogRef.close();
}
dialogRef
is injected in that classes constructor as shown below
constructor(public dialogRef: MatDialogRef<CustomDialogComponent>,
@Inject(MAT_DIALOG_DATA) public data: any) {
}
So my question would be, how could I mock dialogRef
in my spec test file and test its close function?
My project is using angular 5, and angular material 5.
MatDialog creates modal dialogs that implements the ARIA role="dialog" pattern by default. You can change the dialog's role to alertdialog via MatDialogConfig . You should provide an accessible label to this root dialog element by setting the ariaLabel or ariaLabelledBy properties of MatDialogConfig .
TestBed. configureTestingModule() helps you configure the providers. Configuring the providers means you are letting the Angular dependency injection system know about this dependency which later it can inject in to components when requested through a dependency injection token.
You shouldn't need to test this because the library you are using should already test its own component (which is not always the case).
But if you need a specific test which uses the close dialog reference, have a look at their test on this component which should help you write yours :
https://github.com/angular/components/blob/master/src/material/dialog/dialog.spec.ts#L186
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With