扩展MatDialog

时间:2019-11-06 14:39:41

标签: angular unit-testing jestjs

我在我的应用程序中的多个位置使用matDialogs,其基本配置在所有对话框中均相等。为了防止重复代码,我做了以下扩展MatDialog的类:

import { MatDialog, MatDialogConfig, MatDialogRef } from '@angular/material';
import { ComponentType } from '@angular/cdk/portal';
import { TemplateRef, Injectable } from '@angular/core';

@Injectable({
  providedIn: 'root',
})
export class CustomMatDialog extends MatDialog {
  open<T, D = any, R = any>(
    componentOrTemplateRef: ComponentType<T> | TemplateRef<T>,
    config?: MatDialogConfig<D>,
  ): MatDialogRef<T, R> {
    let customConfig = { backdropClass: 'md-backdrop' };

    if (config) {
      customConfig = { ...config, ...customConfig };
    }

    return super.open(componentOrTemplateRef, customConfig);
  }
}

除了单元测试(我使用的是玩笑)之外,其他所有东西都按预期工作。到目前为止,它们看起来像这样:

import { CustomMatDialog } from './custom-mat-dialog';
import {
  MatDialogModule,
} from '@angular/material/dialog';
import { TestBed } from '@angular/core/testing';

describe('CustomMatDialog', () => {
  let customMatDialog: CustomMatDialog;

  beforeEach(() => {
    TestBed.configureTestingModule({
      imports: [MatDialogModule],
      providers: [CustomMatDialog],
    });
    customMatDialog = TestBed.get(CustomMatDialog);
  });

  it('should create an instance', () => {
    expect(customMatDialog).toBeTruthy();
  });
});

运行它后,我收到以下错误消息:

  

NullInjectorError:StaticInjectorError(DynamicTestModule)[CustomMatDialog-> InjectionToken mat-dialog-scroll-strategy]:         StaticInjectorError(平台:核心)[CustomMatDialog-> InjectionToken mat-dialog-scroll-strategy]   :           NullInjectorError:没有InjectionToken mat-dialog-scroll-strategy的提供程序!

我尝试提供ScrollStrategyOptionsInjectionToken,但仍然收到错误消息。为了成功运行单元测试,我需要导入/提供/模拟什么?

0 个答案:

没有答案