Angular: How to mock MatDialogRef while testing

前端 未结 4 2255
隐瞒了意图╮
隐瞒了意图╮ 2021-02-18 13:56

I have a DialogComponent that has the following constructor where Dialog is a custom object:

constructor(
    public dialogRef: MatDialogRef

        
相关标签:
4条回答
  • 2021-02-18 14:28

    Import MatDialogModule and MatDialogRef from angular/material/dialog instead of angular/material. Import the ModalDialogModule and provide providers for MatDialogRef in your TestBed.

    Import {MatdialogModule,MatDialogRef} from '@angular/material/dialog';
    
    TestBed.configureTestingModule({
    declarations: [componentName],
    imports: [MatdialogModule],
    providers: [{provide : MatDialogRef, useValue : {}}]
    });
    
    0 讨论(0)
  • 2021-02-18 14:32

    To solve this problem, you can do the following steps:

    1- You used the data as input to the dialog and used it in the constructor to set the variables. (in .ts)

      constructor(public dialogRef: MatDialogRef<any>,
              @Inject(MAT_DIALOG_DATA) public data: any) {
         this.ip= data.ip;
         this.name= data.name;
      }
    

    2- We should create fake data, (in .spec.ts)

    const data = {
        ip: '1.1.1.1',
        name: 'test'
     }
    

    3- We define provider as follows And we introduce data, (in .spec.ts)

     providers: [
       {provide: MatDialogRef, useValue: {}},
       {provide: MAT_DIALOG_DATA, useValue: data},
      ]
    

    4- full code in .spec.ts

    import {async, ComponentFixture, TestBed} from '@angular/core/testing';
    
    import {CustomDialogComponent} from './backup-list.component';
    import {TranslateModule} from '@ngx-translate/core';
    import {MAT_DIALOG_DATA, MatDialogModule, MatDialogRef} from '@angular/material';
    import {BrowserDynamicTestingModule} from '@angular/platform-browser-dynamic/testing';
    import {NO_ERRORS_SCHEMA} from '@angular/core';
    
    const data = {
        ip: '1.1.1.1',
        name: 'test'
     };
    
    describe('BackupListComponent', () => {
       let component: BackupListComponent;
       let fixture: ComponentFixture<BackupListComponent>;
    
        beforeEach(async(() => {
        TestBed.configureTestingModule({
            declarations: [CustomDialogComponent],
            imports: [
                TranslateModule.forRoot(),
                MatDialogModule,
                BrowserDynamicTestingModule
            ],
            providers: [
                {provide: MatDialogRef, useValue: {}},
                {provide: MAT_DIALOG_DATA, useValue: data},
            ],
            schemas: [NO_ERRORS_SCHEMA]
        })
            .compileComponents();
      }));
     });
    
    0 讨论(0)
  • 2021-02-18 14:34

    I solved it by changing the component constructor to:

    constructor(
      public dialogRef: MatDialogRef<CustomDialogComponent>,
      @Inject(MAT_DIALOG_DATA) public data: Dialog | any
    )
    

    The providers in the TestBed were:

    providers: [{ provide: MatDialogRef, useValue: {} }, { provide: MAT_DIALOG_DATA, useValue: data }]
    
    0 讨论(0)
  • 2021-02-18 14:44

    If you use at least one MatDialogRef method, you should create a mock. For example I use the close() method. Without it errors would be generated so I made the below class with an empty method.

    export class MatDialogRefMock {
        close(value = '') {
    
        }
    }
    

    and use that instead of an empty value, with useClass

    { provide: MatDialogRef, useClass: MatDialogRefMock },
    
    0 讨论(0)
提交回复
热议问题