From b40fada48489bdad0c8f9f6188f846d69bad7cac Mon Sep 17 00:00:00 2001 From: Bartek Fabiszewski Date: Wed, 8 Jan 2020 21:01:16 +0100 Subject: [PATCH] Add lint to travis tests --- js/src/mapapi/api_gmaps.js | 21 ++++++++++++++++++++- js/src/mapapi/api_openlayers.js | 24 +++++++++++++++++++++++- js/test/api_gmaps.test.js | 21 +++++++++++++++++++++ js/test/api_openlayers.test.js | 22 ++++++++++++++++++++++ 4 files changed, 86 insertions(+), 2 deletions(-) diff --git a/js/src/mapapi/api_gmaps.js b/js/src/mapapi/api_gmaps.js index 9dd794c..414d0ec 100644 --- a/js/src/mapapi/api_gmaps.js +++ b/js/src/mapapi/api_gmaps.js @@ -153,7 +153,10 @@ export default class GoogleMapsApi { this.polies.push(poly); } const path = poly.getPath(); - const start = this.markers.length; + let start = this.markers.length; + if (start > 0) { + this.removePoint(--start); + } for (let i = start; i < track.length; i++) { // set marker this.setMarker(i, track); @@ -256,6 +259,22 @@ export default class GoogleMapsApi { this.markers.push(marker); } + /** + * @param {number} id + */ + removePoint(id) { + if (this.markers.length > id) { + this.markers[id].setMap(null); + this.markers.splice(id, 1); + if (this.polies.length) { + this.polies[0].getPath().removeAt(id); + } + if (this.viewModel.model.markerSelect === id) { + this.popupClose(); + } + } + } + /** * Open popup on marker with given id * @param {number} id diff --git a/js/src/mapapi/api_openlayers.js b/js/src/mapapi/api_openlayers.js index 976c21f..543cfff 100644 --- a/js/src/mapapi/api_openlayers.js +++ b/js/src/mapapi/api_openlayers.js @@ -427,7 +427,10 @@ export default class OpenLayersApi { if (!track || !track.hasPositions) { return; } - const start = this.layerMarkers ? this.layerMarkers.getSource().getFeatures().length : 0; + let start = this.layerMarkers ? this.layerMarkers.getSource().getFeatures().length : 0; + if (start > 0) { + this.removePoint(--start); + } for (let i = start; i < track.length; i++) { this.setMarker(i, track); } @@ -540,6 +543,25 @@ export default class OpenLayersApi { this.layerMarkers.getSource().addFeature(marker); } + /** + * @param {number} id + */ + removePoint(id) { + const marker = this.layerMarkers.getSource().getFeatureById(id); + if (marker) { + this.layerMarkers.getSource().removeFeature(marker); + if (this.layerTrack) { + const lineString = this.layerTrack.getSource().getFeatures()[0].getGeometry(); + const coordinates = lineString.getCoordinates(); + coordinates.splice(id, 1); + lineString.setCoordinates(coordinates); + } + if (this.viewModel.model.markerSelect === id) { + this.popupClose(); + } + } + } + /** * Animate marker * @param id Marker sequential id diff --git a/js/test/api_gmaps.test.js b/js/test/api_gmaps.test.js index 09f4c43..1506982 100644 --- a/js/test/api_gmaps.test.js +++ b/js/test/api_gmaps.test.js @@ -177,6 +177,27 @@ describe('Google Maps map API tests', () => { expect(api.map).toBe(null); }); + it('should remove features by id', () => { + // given + const poly = new google.maps.Polyline(); + const marker = new google.maps.Marker(); + api.polies.push(poly); + api.markers.push(marker); + const path = {}; + path.removeAt = () => {/* ignore */}; + poly.getPath = () => path; + marker.setMap = () => {/* ignore */}; + spyOn(marker, 'setMap'); + spyOn(path, 'removeAt'); + const id = 0; + // when + api.removePoint(id); + // then + expect(marker.setMap).toHaveBeenCalledWith(null); + expect(api.markers.length).toBe(0); + expect(path.removeAt).toHaveBeenCalledWith(id); + }); + it('should clear map features', () => { // given const poly = new google.maps.Polyline(); diff --git a/js/test/api_openlayers.test.js b/js/test/api_openlayers.test.js index 5d71929..a479689 100644 --- a/js/test/api_openlayers.test.js +++ b/js/test/api_openlayers.test.js @@ -224,6 +224,28 @@ describe('Openlayers map API tests', () => { expect(api.map).toBe(null); }); + it('should remove features with given index', () => { + // given + const id = 0; + const marker = new ol.Feature(); + const lineString = new ol.geom.LineString([]); + lineString.appendCoordinate(ol.proj.fromLonLat([ 0, 0 ])); + const lineFeature = new ol.Feature({ geometry: lineString }); + marker.setId(id); + api.layerTrack = new ol.layer.VectorLayer({ source: new ol.source.Vector() }); + api.layerTrack.getSource().addFeature(lineFeature); + api.layerMarkers = new ol.layer.VectorLayer({ source: new ol.source.Vector() }); + api.layerMarkers.getSource().addFeature(marker); + + expect(lineString.getCoordinates().length).toBe(1); + expect(api.layerMarkers.getSource().getFeatures().length).toBe(1); + // when + api.removePoint(id); + // then + expect(lineString.getCoordinates().length).toBe(0); + expect(api.layerMarkers.getSource().getFeatures().length).toBe(0); + }); + it('should clear marker and track layers features', () => { // given api.layerTrack = new ol.layer.VectorLayer({ source: new ol.source.Vector() });