【问题标题】:How to Trigger openLayers 3 'singleClick' from karma test如何从业力测试触发 openLayers 3 'singleClick'
【发布时间】:2017-11-02 18:36:04
【问题描述】:

我正在使用 angular2 和 openlayers3 在我们的页面上显示地图。 以下 singleClick 功能用于在单击地图上的标记时显示弹出窗口 -

        this.map.on('singleclick', (e) => {
            this.showWellDetails(e.pixel);
        });

这工作得很好,但我无法从我的业力测试中触发。 因此,这块代码没有被覆盖。

编辑 - 在这里也添加了测试用例的代码 -

@Component({
    selector: 'test-component-wrapper',
    template: '<map [markers]="markers"  [isDocked]="isDocked" ></map>',
})
class TestComponentWrapper {
    public isDocked = true;
    public markers = [
        {
            "latitude": 101.106074,
            "longitude": 1.307283,
            "name": "211/27-A17",
        },
        {
            "latitude": 61.034344,
            "longitude": 1.703716,
            "name": "211/29-A17",
        },
    ];
}
describe('MapComponent events', () => {
    let component: MapComponent;
    let fixture: ComponentFixture<TestComponentWrapper>;

    beforeEach(async(() => {
        TestBed.configureTestingModule({
            declarations: [
                TestComponentWrapper,
                MapComponent,
            ],
            schemas: [CUSTOM_ELEMENTS_SCHEMA]
        })
            .compileComponents();

        fixture = TestBed.createComponent(TestComponentWrapper);
        component = fixture.debugElement.children[0].componentInstance;
        fixture.detectChanges();
    }));

    it('should create', () => {
        expect(component).toBeTruthy();
    });

    it('should test showDetails functionality', () => {
        component.isDocked = false;
        component.initMap();
        expect(fixture.debugElement.query(By.css('#mapId'))).not.toBe(null);
        let pixel = [
            233.10227584838867,
            191.25,
        ];

        // I want to trigger 'singleClick' on map from here...
        // component.map.singleClick();
        // Since this is not working, I have to make showWellDetails function public and call it directly.
        //at least it adds coverage for this function, but the original block of code mentioned above remains uncovered.


        component.showWellDetails(pixel);
        expect(fixture.debugElement.query(By.css('#featureDetails'))).toBe(null);
        expect(component.locationCoordinate).toBeUndefined();
    });
    //other tests
});

感谢任何帮助! TIA。

【问题讨论】:

  • 拜托,你能发布你的单元测试代码吗?
  • @SrAxi - 在帖子编辑中添加了测试代码
  • 嘿 - 谁能帮我解决这个问题?

标签: angular karma-jasmine openlayers-3 karma-coverage


【解决方案1】:

我如何最终实现它可能会帮助其他正在寻找此信息的人 -

public singleClickCallback = (e) => {
        this.showWellDetails(e.pixel);
 }

 this.map.on('singleclick', this.singleClickCallback);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-10-13
    • 2019-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多