简体   繁体   English

angular:7如何在父组件上测试子发射器?

[英]angular : 7 How test child emitter on parent component?

Write test case for event emitter. 为事件发射器编写测试用例。 Write test case for modelopen() method modelopen()方法编写测试用例

parent ts file author-article-carousel.component.ts 父ts文件author-article-carousel.component.ts

public edit(row) {
    console.log(row);
    this.activeArticleService.getArticleDetail(row.id).subscribe((res: any) => {
      this.modalOpen(res);
    });
  }


  public modalOpen(value) {
    let config = {};
    config = {
      disableClose: true,
      maxWidth: '1050px',
      data: { value: value, user: 'author' }
    };
    if (this.dialogRef == null) {
      this.dialogRef = this.dialog.open(ArticleModalComponent, config);
    }
    this.dialogRef.afterClosed().subscribe(result => {
      console.log(`Dialog closed: ${result}`);
      this.dialogRef = null;
    });

    this.dialogRef.componentInstance.downloadreport.subscribe(response => {
      if (response) {
        this.activeArticleService.downloadReport(response).subscribe((response: HttpResponse<Blob>) => {
          this.activeArticleService.download(response, 'report.pdf');
        });
      }
    });
}

parent HTML file author-article-carousel.component.html 父HTML文件author-article-carousel.component.html

<prism-article-carousel
  (clickOnTitle)="edit($event)"
></prism-article-carousel>

child HTML file article-carousel.component.html 子HTML文件article-carousel.component.html

 <div class="row">
            <div class="col-md-12">
              <a (click)="edit(item)" class="mat-card-title" style="cursor: pointer">{{ item.title }}</a>
            </div>

child ts file article-modal.component.ts 子ts文件article-modal.component.ts

  public downloadReport(url) {
    this.downloadreport.emit(url);
  }

Assuming that you have a working test setup and created a working spy for your activeArticleService 假设您具有有效的测试设置并为activeArticleService创建了有效的间谍程序

There are two approaches depending on whether you are using a shallow test or actually have setup a test where all the components are declared. 有两种方法取决于您使用的是浅层测试还是实际上已设置了声明所有组件的测试。 (I would suggest using the shallow test approach since it is only a unit test) (我建议使用浅层测试方法,因为它只是一个单元测试)

Using shallow testing (setting NO_ERRORS_SCHEMA ) you can trigger a custom EventHandler by accessing the desired element using: 使用浅层测试(设置NO_ERRORS_SCHEMA ),您可以使用以下方法访问所需的元素来触发自定义EventHandler:

const debugElem = fixture.debugElement.query(By.css('prism-article-carousel'));
debugElem.triggerEventHandler('clickOnTitle', YOUR_EXPECTED_EVENT_OBJECT)
tick();

For this to work your tests needs to use fakeAsync . 为此,您的测试需要使用fakeAsync

If you don't use this schema you would get the component instance with 如果不使用此架构,则将获得具有

fixture.debugElement.query(By.css('prism-article-carousel'))

and you would need something like this (be careful this is only pseudocode) 并且您将需要这样的内容(请注意,这只是伪代码)

childComponent = fixture.debugElement.query(By.css('prism-article-carousel')).componentInstance;
childComponent.emit(YOUR_VALUE_HERE);
tick();

Please have a look here in the official documentation for more information on those different approaches to test a nested component. 请在此处查看官方文档 ,以获取有关测试嵌套组件的这些不同方法的更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM