diff --git a/js/test/api_gmaps.test.js b/js/test/api_gmaps.test.js index 09fa8d8..f2b3c37 100644 --- a/js/test/api_gmaps.test.js +++ b/js/test/api_gmaps.test.js @@ -20,10 +20,7 @@ import * as gmStub from './googlemaps.stub.js'; import { config, lang } from '../src/initializer.js' import GoogleMapsApi from '../src/mapapi/api_gmaps.js'; -import uPosition from '../src/position.js'; -import uPositionSet from '../src/positionset.js'; -import uTrack from '../src/track.js'; -import uUser from '../src/user.js'; +import TrackFactory from './helpers/trackfactory.js'; import uUtils from '../src/utils.js'; describe('Google Maps map API tests', () => { @@ -203,7 +200,7 @@ describe('Google Maps map API tests', () => { it('should construct track polyline and markers', () => { // given - const track = getTrack(); + const track = TrackFactory.getTrack(); spyOn(api, 'setMarker'); spyOn(google.maps, 'LatLng').and.callThrough(); spyOn(google.maps.LatLngBounds.prototype, 'extend').and.callThrough(); @@ -231,7 +228,7 @@ describe('Google Maps map API tests', () => { it('should construct non-continuous track markers without polyline', () => { // given - const track = getPositionSet(); + const track = TrackFactory.getPositionSet(); spyOn(api, 'setMarker'); // when api.displayTrack(track, false); @@ -243,7 +240,7 @@ describe('Google Maps map API tests', () => { it('should fit bounds if update without zoom (should not add listener for "bounds_changed")', () => { // given - const track = getTrack(); + const track = TrackFactory.getTrack(); spyOn(google.maps.event, 'addListenerOnce'); spyOn(google.maps.Map.prototype, 'fitBounds'); spyOn(api, 'setMarker'); @@ -262,7 +259,7 @@ describe('Google Maps map API tests', () => { it('should fit bounds and zoom (add listener for "bounds_changed") if update with single position', () => { // given - const track = getTrack(1); + const track = TrackFactory.getTrack(1); spyOn(google.maps.event, 'addListenerOnce'); spyOn(google.maps.Map.prototype, 'fitBounds'); spyOn(api, 'setMarker'); @@ -281,7 +278,7 @@ describe('Google Maps map API tests', () => { it('should create marker from track position and add it to markers array', () => { // given - const track = getTrack(1); + const track = TrackFactory.getTrack(1); track.positions[0].timestamp = 1; spyOn(google.maps.Marker.prototype, 'addListener'); spyOn(google.maps.Marker.prototype, 'setIcon'); @@ -307,7 +304,7 @@ describe('Google Maps map API tests', () => { it('should create marker different marker icon for start, end and normal position', () => { // given - const track = getTrack(3); + const track = TrackFactory.getTrack(3); spyOn(google.maps.Marker.prototype, 'setIcon'); spyOn(GoogleMapsApi, 'getMarkerIcon'); api.map = new google.maps.Map(container); @@ -330,7 +327,7 @@ describe('Google Maps map API tests', () => { it('should create different marker for position with comment or image', () => { // given - const track = getTrack(4); + const track = TrackFactory.getTrack(4); const positionWithComment = 0; const positionWithImage = 1; const positionWithImageAndComment = 2; @@ -473,36 +470,4 @@ describe('Google Maps map API tests', () => { expect(GoogleMapsApi.loadTimeoutMs).toEqual(jasmine.any(Number)); }); - function getSet(length = 2, type) { - let track; - if (type === uTrack) { - track = new uTrack(1, 'test track', new uUser(1, 'testUser')); - } else { - track = new uPositionSet(); - } - track.positions = []; - let lat = 21.01; - let lon = 52.23; - for (let i = 0; i < length; i++) { - track.positions.push(getPosition(lat, lon)); - lat += 0.5; - lon += 0.5; - } - return track; - } - - function getTrack(length = 2) { - return getSet(length, uTrack); - } - - function getPositionSet(length = 2) { - return getSet(length, uPositionSet); - } - - function getPosition(latitude = 52.23, longitude = 21.01) { - const position = new uPosition(); - position.latitude = latitude; - position.longitude = longitude; - return position; - } }); diff --git a/js/test/api_openlayers.test.js b/js/test/api_openlayers.test.js index 2830526..3f56cea 100644 --- a/js/test/api_openlayers.test.js +++ b/js/test/api_openlayers.test.js @@ -20,11 +20,8 @@ /* global ol */ import OpenlayersApi from '../src/mapapi/api_openlayers.js'; +import TrackFactory from './helpers/trackfactory.js'; import { config } from '../src/initializer.js' -import uPosition from '../src/position.js'; -import uPositionSet from '../src/positionset.js'; -import uTrack from '../src/track.js'; -import uUser from '../src/user.js'; import uUtils from '../src/utils.js'; describe('Openlayers map API tests', () => { @@ -249,7 +246,7 @@ describe('Openlayers map API tests', () => { api.map = mockMap; api.layerTrack = new ol.layer.VectorLayer({ source: new ol.source.Vector() }); api.layerMarkers = new ol.layer.VectorLayer({ source: new ol.source.Vector() }); - const track = getTrack(); + const track = TrackFactory.getTrack(); spyOn(api, 'setMarker'); spyOn(api, 'fitToExtent'); // when @@ -277,7 +274,7 @@ describe('Openlayers map API tests', () => { api.map.addControl(new ol.control.ZoomToExtent()); api.layerTrack = new ol.layer.VectorLayer({ source: new ol.source.Vector() }); api.layerMarkers = new ol.layer.VectorLayer({ source: new ol.source.Vector() }); - const track = getPositionSet(); + const track = TrackFactory.getPositionSet(); spyOn(api, 'setMarker'); spyOn(api, 'fitToExtent'); // when @@ -303,7 +300,7 @@ describe('Openlayers map API tests', () => { api.map = mockMap; api.layerTrack = new ol.layer.VectorLayer({ source: new ol.source.Vector() }); api.layerMarkers = new ol.layer.VectorLayer({ source: new ol.source.Vector() }); - const track = getTrack(); + const track = TrackFactory.getTrack(); spyOn(api, 'setMarker'); const markersExtent = [ 3, 2, 1, 0 ]; spyOn(api, 'fitToExtent').and.callFake((_extent) => _extent); @@ -358,7 +355,7 @@ describe('Openlayers map API tests', () => { it('should create marker from track position and add it to markers layer', () => { // given - const track = getTrack(1); + const track = TrackFactory.getTrack(1); track.positions[0].timestamp = 1; const id = 0; api.map = mockMap; @@ -374,7 +371,7 @@ describe('Openlayers map API tests', () => { it('should get different marker style for start, end and normal position', () => { // given - const track = getTrack(3); + const track = TrackFactory.getTrack(3); api.markerStyles = { normal: 'normal', stop: 'stop', @@ -396,7 +393,7 @@ describe('Openlayers map API tests', () => { it('should create different marker for position with comment', () => { // given - const track = getTrack(3); + const track = TrackFactory.getTrack(3); track.positions[0].comment = 'comment'; track.positions[1].comment = 'comment'; track.positions[2].comment = 'comment'; @@ -421,7 +418,7 @@ describe('Openlayers map API tests', () => { it('should create different marker for position with image', () => { // given - const track = getTrack(3); + const track = TrackFactory.getTrack(3); track.positions[0].image = 'image'; track.positions[1].image = 'image'; track.positions[2].image = 'image'; @@ -548,37 +545,4 @@ describe('Openlayers map API tests', () => { expect(api.popup.getElement().firstElementChild.innerHTML).toBe('content 1'); expect(mockViewModel.model.markerSelect).toBe(null); }); - - function getSet(length = 2, type) { - let track; - if (type === uTrack) { - track = new uTrack(1, 'test track', new uUser(1, 'testUser')); - } else { - track = new uPositionSet(); - } - track.positions = []; - let lat = 21.01; - let lon = 52.23; - for (let i = 0; i < length; i++) { - track.positions.push(getPosition(lat, lon)); - lat += 0.5; - lon += 0.5; - } - return track; - } - - function getTrack(length = 2) { - return getSet(length, uTrack); - } - - function getPositionSet(length = 2) { - return getSet(length, uPositionSet); - } - - function getPosition(latitude = 52.23, longitude = 21.01) { - const position = new uPosition(); - position.latitude = latitude; - position.longitude = longitude; - return position; - } }); diff --git a/js/test/helpers/trackfactory.js b/js/test/helpers/trackfactory.js new file mode 100644 index 0000000..dd42cc8 --- /dev/null +++ b/js/test/helpers/trackfactory.js @@ -0,0 +1,102 @@ +/* + * μlogger + * + * Copyright(C) 2019 Bartek Fabiszewski (www.fabiszewski.net) + * + * This is free software; you can redistribute it and/or modify it under + * the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, see . + */ + + +import uPosition from '../../src/position.js'; +import uPositionSet from '../../src/positionset.js'; +import uTrack from '../../src/track.js'; +import uUser from '../../src/user.js'; + +export default class TrackFactory { + + /** + * @template T + * @param {number} length + * @param {T} type + * @param {Object} params + * @return {T} + */ + static getSet(length = 2, type, params) { + let track; + if (type === uTrack) { + track = new uTrack(params.id, params.name, params.user); + } else { + track = new uPositionSet(); + } + if (length) { + track.positions = []; + let lat = 21.01; + let lon = 52.23; + for (let i = 0; i < length; i++) { + track.positions.push(this.getPosition(i + 1, lat, lon)); + lat += 0.5; + lon += 0.5; + } + } + return track; + } + + /** + * @param {number=} length + * @param {{ id: number, name: string, user: uUser }=} params + * @return {uTrack} + */ + static getTrack(length = 2, params) { + params = params || {}; + params.id = params.id || 1; + params.name = params.name || 'test track'; + params.user = params.user || new uUser(1, 'testUser'); + return this.getSet(length, uTrack, params); + } + + /** + * @param {number} length + * @return {uPositionSet} + */ + static getPositionSet(length = 2) { + return this.getSet(length, uPositionSet); + } + + /** + * @param {number=} id + * @param {number=} latitude + * @param {number=} longitude + * @return {uPosition} + */ + static getPosition(id = 1, latitude = 52.23, longitude = 21.01) { + const position = new uPosition(); + position.id = id; + position.latitude = latitude; + position.longitude = longitude; + position.altitude = null; + position.speed = null; + position.bearing = null; + position.timestamp = 1; + position.accuracy = null; + position.provider = null; + position.comment = null; + position.image = null; + position.username = 'testUser'; + position.trackid = 1; + position.trackname = 'test track'; + position.meters = 0; + position.seconds = 0; + return position; + } +}