ContentChild 模态未定义单元测试

时间:2021-02-03 10:16:21

标签: angular typescript unit-testing templates modal-dialog

我有一个声明为 ContentChild 的模态模板

@ContentChild('confirmDeleteModalTemplate')
  confirmDeleteModalTemplate: TemplateRef<any>;

点击一个按钮,我打开如下模式

onDeleteClick(e: any): void {
    e.stopPropagation();
    this.confirmDeleteModalReference = this.modalService.open(this.confirmDeleteModalTemplate, { size: 'small' });

    this.confirmDeleteModalReference.onClose$.pipe(take(1)).subscribe((isDeleteConfirmed: boolean) => {
      if (isDeleteConfirmed) {
        this.handleDelete.emit(isDeleteConfirmed);
      }
    });
  }

我已将单元测试编写为:

it('should emit handleDelete', () => {
    const event = new MouseEvent('click');
    const spy = spyOn(component.handleDelete, 'emit');
    component.onDeleteClick(event);
    component.confirmDeleteModalReference.onClose$.next(true);
    expect(spy).toHaveBeenCalled();
  });

,但我收到错误 Modal undefined

我想我需要在测试文件中声明 TemplateRef,但我不知道如何。

提前致谢。

0 个答案:

没有答案