简体   繁体   English

如何使用茉莉花测试对 mapbox 弹出窗口进行单元测试?

[英]How to unit test a mapbox popup with jasmine testing?

I have a mapbox popup.我有一个地图框弹出窗口。 I am using jasmine and I want to write a unit test for it.我正在使用茉莉花,我想为它编写一个单元测试。

So I have this function:所以我有这个功能:


  selectCluster(event: MouseEvent, feature: any) {
    event.stopPropagation(); 
    this.selectedCluster = {geometry: feature.geometry, properties: feature.properties};
  }

and this is the template:这是模板:

 <ng-template mglClusterPoint let-feature>
        <div class="marker-cluster" (click)="selectCluster($event, feature)">
          <fa-icon [icon]="faVideo" [styles]="{'stroke': 'black', 'color': 'black'}" size="lg" class="pr-2"></fa-icon>
          <fa-icon [icon]="faWifi" [styles]="{'stroke': 'black', 'color': 'black'}" size="lg" class="pr-2"></fa-icon>
        </div>
      </ng-template>

and this is my unit test:这是我的单元测试:

 fit('Should prevent popup will be closing after popup is triggered', () => {
    const ev = new Event('MouseEvent');
    spyOn(ev, 'stopPropagation');   
    expect(ev.stopPropagation).toHaveBeenCalled();
  });

But I get this error:但我收到此错误:

Expected spy stopPropagation to have been called.

So what I have to change?那我必须改变什么?

Thank you谢谢

I dont think you should be testing like that.我不认为你应该那样测试。

 it('Should set selectedCluster when clicked', () => {
    spyOn(component,'selectCluster').and.callThrough();
    fixture.debugElement.query(By.css('.marker-cluster')).nativeElement.click();
    fixture.detectChanges();
    expect(component.selectCluster).toHaveBeenCalled();
    expect(component.selectedCluster).toBe('whatever you are expecting')
  });

To test stopPropagation , you should rather focus on the event which is being stopped by it.要测试stopPropagation ,您应该专注于被它停止的事件。 If your test checks that the event is not happening, you can be sure that event.stopPropagation();如果您的测试检查事件没有发生,您可以确定event.stopPropagation(); is doing its thing in the code.在代码中做它的事情。

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

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