1
我正在使用angular2和openlayers3在我們的頁面上顯示地圖。 以下singleClick功能是用來顯示一個彈出窗口點擊地圖標記時 -如何從karma測試觸發openLayers 3'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
@SrAxi - 在帖子中添加了測試代碼編輯 –
嘿 - 有誰能幫我解決這個問題嗎? –