1

I have an angular app where I need to test a mat-dilaog by passing data. I am able to pass it through as below.

{ provide: MAT_DIALOG_DATA, useValue: staffData },

But I am trying to find a way to pass different values as part of staffData for different tests. I am not finding a way to do it

  let staffData: any = {
    gridData: [],
    type: "add",
    adConfigured: false,
  };
  beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [StaffAddComponent, HelpDialogComponent],
      imports: [
        FormsModule,
        ReactiveFormsModule,
        HttpClientTestingModule,
        OverlayModule,
        MatDialogModule,
      ],
      providers: [
        StaffaddService,
        AuthService,
        { provide: MatDialogRef, useValue: {} },
        { provide: MAT_DIALOG_DATA, useValue: staffData },
        { provide: MatDialog, useValue: {} },
      ],
      schemas: [CUSTOM_ELEMENTS_SCHEMA, NO_ERRORS_SCHEMA],
    }).compileComponents();

Here I am able to pass staffData to the StaffAddComponent. But I want to test different scenarios by passing different values for staffData

AlexElin
  • 1,044
  • 14
  • 23
indra257
  • 66
  • 3
  • 24
  • 50
  • 1
    Show your test code. – Adam Jenkins Oct 02 '20 at 18:30
  • @Adam Added to the question – indra257 Oct 02 '20 at 18:34
  • 1
    Looks like this could (possibly?) be closed as a dupe of [this one](https://stackoverflow.com/questions/48811091/how-override-provider-in-angular-5-for-only-one-test). Just override the MAT_DIALOG_DATA provider in a given unit test. – Adam Jenkins Oct 02 '20 at 18:37
  • Thanks Adam. I will try it. Also I am struggling with formGroup. Any insight will be helpful. https://stackoverflow.com/questions/64175331/angular-unit-test-mat-form-field-in-a-formgroup – indra257 Oct 02 '20 at 18:40

0 Answers0