Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular Unit Test MatDialog Open method

I am trying to unit test this popup method but I cannot, can you please help

 openPram(): void {
    const dialogConfig = new MatDialogConfig();
    this.dialog.open(ParamGameComponent, dialogConfig);
}
like image 859
Mohamed Sameh Sellami Avatar asked Sep 15 '25 08:09

Mohamed Sameh Sellami


1 Answers

Since there isn't a lot of code here and clarification on what you want to test. I am going to assume you want to test that dialog.open() is called with the params of ParamGameComponent and the dialogConfig const. If that is true then your test could look something like this:

  it('open dialog test', () => {
   const openDialogSpy = spyOn(component.dialog, 'open')
   const fakeDialogConfig = new MatDialogConfig;

   component.openPram();

   expect(openDialogSpy).toHaveBeenCalledWith(ParamGameComponent, 
    fakeDialogConfig);
  });

The component var in this test is assuming as well that you have the default Angular CLI generated spec.ts files that creates a local instance of the component you are writing tests for.

like image 151
Brian Stanley Avatar answered Sep 16 '25 23:09

Brian Stanley