Mock Component used in MatDialog

119 Views Asked by At

I have two libraries/modules with one component Each. One Component (MainComponent) uses the other (DialogComponent) in a MatDialog open Statement.

The DialogComponent uses a Service (PermissionsServie) that uses MsalModule.

When i mock and provide the PermissionsServie in the MainComponent unit test thou, the Test fails, stating that it can not create the MsalModule because it is missing the crypto object.

My Test Setup for MainComponent:

import { ComponentFixture, TestBed } from '@angular/core/testing';
import { MainComponent } from './main.component';
import { MatDialog } from '@angular/material/dialog';
import { instance, mock } from 'ts-mockito';
import { PermissionsService } from '@my-project/permissions';

describe('TestComponent', () => {
  let component: MainComponent;
  let fixture: ComponentFixture<MainComponent>;
  let mockMatDialog: MatDialog;
  let mockPermissionsService: PermissionsService;

  beforeEach(async () => {
    mockMatDialog = mock(MatDialog);
    mockPermissionsService = mock(PermissionsService);
    await TestBed.configureTestingModule({
      declarations: [MainComponent],
      providers: [
        {
          provide: MatDialog,
          useValue: instance(mockMatDialog),
        },
        {
          provide: PermissionsService,
          useValue: instance(mockPermissionsService),
        },
      ],
    }).compileComponents();

    fixture = TestBed.createComponent(MainComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();
  });

I tried to use MockComponent(DialogComponent) as well, but this error stays and i can not run the test.

    await TestBed.configureTestingModule({
      declarations: [TestComponent, MockComponent(TestDialogComponent)],

I also tried using ng-mocks and MockBuilder, mocking the whole module and in a second attempt, mocking the Dialog Module as well, but it doesn't change anything either.

0

There are 0 best solutions below