如何从业力测试中触发开放层 3 "单击"



我正在使用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。

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

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

相关内容

最新更新