ulogger-server/js/test/mapviewmodel.test.js

331 lines
10 KiB
JavaScript
Raw Normal View History

2019-12-17 22:19:26 +01:00
/*
* μ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 <http://www.gnu.org/licenses/>.
*/
import { config, lang } from '../src/initializer.js';
2019-12-24 13:33:54 +01:00
import Fixture from './helpers/fixture.js';
2019-12-17 22:19:26 +01:00
import MapViewModel from '../src/mapviewmodel.js';
import TrackFactory from './helpers/trackfactory.js';
import ViewModel from '../src/viewmodel.js';
import uObserve from '../src/observe.js';
import uState from '../src/state.js';
import uUtils from '../src/utils.js';
describe('MapViewModel tests', () => {
let vm;
let state;
let mapEl;
2019-12-22 15:52:16 +01:00
let menuButtonEl;
2019-12-17 22:19:26 +01:00
let mockApi;
let bounds;
let track;
const defaultApi = 'mockApi';
2019-12-24 13:33:54 +01:00
beforeEach((done) => {
Fixture.load('main.html')
.then(() => done())
.catch((e) => done.fail(e));
});
2019-12-17 22:19:26 +01:00
beforeEach(() => {
mapEl = document.querySelector('#map-canvas');
2019-12-22 19:34:56 +01:00
menuButtonEl = document.querySelector('#menu-button a');
2019-12-17 22:19:26 +01:00
config.reinitialize();
config.mapApi = defaultApi;
lang.init(config);
mockApi = jasmine.createSpyObj('mockApi', {
'init': Promise.resolve(),
'getBounds': { /* ignored */ },
'cleanup': { /* ignored */ },
'zoomToBounds': { /* ignored */ },
'zoomToExtent': { /* ignored */ },
'displayTrack': { /* ignored */ },
2019-12-22 15:52:16 +01:00
'clearMap': { /* ignored */ },
'updateSize': { /* ignored */ }
2019-12-17 22:19:26 +01:00
});
state = new uState();
vm = new MapViewModel(state);
spyOn(vm, 'getApi').and.returnValue(mockApi);
2019-12-19 22:20:55 +01:00
spyOn(lang, 'getLocaleSpeed');
spyOn(lang, 'getLocaleDistance');
spyOn(lang, 'getLocaleDistanceMajor');
spyOn(lang, '_').and.returnValue('{placeholder}');
2019-12-17 22:19:26 +01:00
bounds = [ 1, 2, 3, 4 ];
track = TrackFactory.getTrack(0);
});
afterEach(() => {
2019-12-24 13:33:54 +01:00
Fixture.clear();
2019-12-19 22:20:55 +01:00
uObserve.unobserveAll(lang);
2019-12-17 22:19:26 +01:00
});
it('should create instance', () => {
// then
expect(vm).toBeInstanceOf(ViewModel);
expect(vm.state).toBe(state);
expect(vm.mapElement).toBe(mapEl);
expect(vm.api).toBe(null);
});
2019-12-22 15:52:16 +01:00
it('should initialize instance', () => {
// given
spyOn(vm, 'bindAll');
spyOn(vm, 'setObservers');
// when
vm.init();
// then
expect(vm.bindAll).toHaveBeenCalledTimes(1);
expect(vm.setObservers).toHaveBeenCalledTimes(1);
});
2019-12-17 22:19:26 +01:00
it('should load openlayers api and call onReady', (done) => {
// given
spyOn(vm, 'onReady');
// when
vm.loadMapAPI('openlayers');
// then
setTimeout(() => {
expect(vm.getApi).toHaveBeenCalledWith('openlayers');
expect(vm.onReady).toHaveBeenCalledTimes(1);
done();
}, 100);
});
it('should load gmaps api and fail with error, config map api should be set to another api', (done) => {
// given
spyOn(vm, 'onReady');
spyOn(uUtils, 'error');
mockApi.init.and.returnValue(Promise.reject(new Error('init failed')));
// when
vm.loadMapAPI('gmaps');
// then
setTimeout(() => {
expect(vm.getApi).toHaveBeenCalledWith('gmaps');
expect(vm.onReady).not.toHaveBeenCalled();
expect(config.mapApi).toBe('openlayers');
expect(uUtils.error).toHaveBeenCalledWith(jasmine.any(Error), jasmine.stringMatching('init failed'));
done();
}, 100);
});
it('should replace map api, get bounds from map and clean up previous api', (done) => {
// given
spyOn(vm, 'onReady');
vm.api = mockApi;
// when
vm.loadMapAPI('gmaps');
// then
setTimeout(() => {
expect(mockApi.getBounds).toHaveBeenCalledTimes(1);
expect(mockApi.cleanup).toHaveBeenCalledTimes(1);
done();
}, 100);
});
it('should zoom to bounds if has saved bounds', () => {
// given
vm.api = mockApi;
vm.savedBounds = bounds;
// when
vm.onReady();
// then
expect(mockApi.zoomToBounds).toHaveBeenCalledTimes(1);
expect(mockApi.zoomToBounds).toHaveBeenCalledWith(bounds);
});
it('should not zoom to bounds if there are no saved bounds', () => {
// given
vm.api = mockApi;
vm.savedBounds = null;
// when
vm.onReady();
// then
expect(mockApi.zoomToBounds).not.toHaveBeenCalled();
});
it('should display track with update if current track is set in state and bounds are not set', () => {
// given
vm.api = mockApi;
state.currentTrack = track;
vm.savedBounds = null;
// when
vm.onReady();
// then
expect(mockApi.displayTrack).toHaveBeenCalledTimes(1);
expect(mockApi.displayTrack).toHaveBeenCalledWith(track, true);
});
it('should display track without update if current track is set in state and bounds are set', () => {
// given
vm.api = mockApi;
state.currentTrack = track;
vm.savedBounds = bounds;
// when
vm.onReady();
// then
expect(mockApi.displayTrack).toHaveBeenCalledTimes(1);
expect(mockApi.displayTrack).toHaveBeenCalledWith(track, false);
});
it('should load map api on api changed in config', (done) => {
// given
spyOn(vm, 'loadMapAPI');
vm.api = mockApi;
2019-12-21 14:51:40 +01:00
vm.setObservers();
2019-12-17 22:19:26 +01:00
const newApi = 'newapi';
// when
config.mapApi = newApi;
// then
setTimeout(() => {
expect(vm.loadMapAPI).toHaveBeenCalledTimes(1);
expect(vm.loadMapAPI).toHaveBeenCalledWith(newApi);
done();
}, 100);
2019-12-22 15:52:16 +01:00
});
it('should resize map on menu toggle', (done) => {
// given
vm.api = mockApi;
vm.bindAll();
// when
menuButtonEl.click();
// then
setTimeout(() => {
expect(mockApi.updateSize).toHaveBeenCalledTimes(1);
done();
}, 100);
2019-12-17 22:19:26 +01:00
});
it('should clear map when state current track is cleared', (done) => {
// given
vm.api = mockApi;
state.currentTrack = null;
2019-12-21 14:51:40 +01:00
vm.setObservers();
2019-12-17 22:19:26 +01:00
uObserve.setSilently(state, 'currentTrack', track);
// when
state.currentTrack = null;
// then
setTimeout(() => {
expect(mockApi.clearMap).toHaveBeenCalledTimes(1);
expect(mockApi.displayTrack).not.toHaveBeenCalled();
done();
}, 100);
});
it('should display track when state current track is set and update track when new positions are added', (done) => {
// given
vm.api = mockApi;
state.currentTrack = null;
2019-12-21 14:51:40 +01:00
vm.setObservers();
2019-12-17 22:19:26 +01:00
// when
state.currentTrack = track;
// then
setTimeout(() => {
expect(mockApi.clearMap).toHaveBeenCalledTimes(1);
expect(mockApi.displayTrack).toHaveBeenCalledTimes(1);
expect(mockApi.displayTrack).toHaveBeenCalledWith(track, true);
// when
mockApi.displayTrack.calls.reset();
state.currentTrack.positions.push(TrackFactory.getPosition(100));
// then
setTimeout(() => {
expect(mockApi.zoomToExtent).toHaveBeenCalledTimes(1);
expect(mockApi.displayTrack).toHaveBeenCalledTimes(1);
expect(mockApi.displayTrack).toHaveBeenCalledWith(track, false);
done();
}, 100);
}, 100);
});
it('should get popup html content', () => {
// given
const id = 0;
state.currentTrack = TrackFactory.getTrack(2);
// when
2019-12-30 23:33:07 +01:00
const element = vm.getPopupElement(id);
2019-12-17 22:19:26 +01:00
// then
expect(element).toBeInstanceOf(HTMLDivElement);
expect(element.id).toBe('popup');
expect(lang._.calls.mostRecent().args[1]).toBe(id + 1);
expect(lang._.calls.mostRecent().args[2]).toBe(state.currentTrack.length);
2019-12-17 22:19:26 +01:00
});
it('should get popup with stats when track does not contain only latest positions', () => {
// given
const id = 0;
spyOn(uUtils, 'sprintf');
state.currentTrack = TrackFactory.getTrack(2);
state.showLatest = false;
// when
2019-12-30 23:33:07 +01:00
const popupEl = vm.getPopupElement(id);
2019-12-17 22:19:26 +01:00
// then
2019-12-30 23:33:07 +01:00
expect(popupEl.querySelector('#pright')).toBeInstanceOf(HTMLDivElement);
2019-12-17 22:19:26 +01:00
});
it('should get popup without stats when track contains only latest positions', () => {
// given
const id = 0;
spyOn(uUtils, 'sprintf');
state.currentTrack = TrackFactory.getTrack(2);
state.showLatest = true;
// when
2019-12-30 23:33:07 +01:00
const popupEl = vm.getPopupElement(id);
2019-12-17 22:19:26 +01:00
// then
2020-01-06 21:26:31 +01:00
expect(popupEl.querySelector('#pright')).toBe(null);
2019-12-17 22:19:26 +01:00
});
it('should get marker svg source with given size and without extra border', () => {
// given
spyOn(MapViewModel, 'getMarkerPath').and.callThrough();
spyOn(MapViewModel, 'getMarkerExtra').and.callThrough();
const fill = 'black';
const isLarge = false;
const isExtra = false;
// when
const dataUri = MapViewModel.getSvgSrc(fill, isLarge, isExtra);
const svgSrc = decodeURIComponent(dataUri.replace(/data:image\/svg\+xml,/, ''));
const element = uUtils.nodeFromHtml(svgSrc);
// then
expect(element).toBeInstanceOf(SVGElement);
expect(svgSrc).toContain(`fill="${fill}"`);
expect(MapViewModel.getMarkerPath).toHaveBeenCalledWith(isLarge);
expect(MapViewModel.getMarkerExtra).not.toHaveBeenCalled();
});
it('should get marker svg source with given size and with extra border', () => {
// given
spyOn(MapViewModel, 'getMarkerPath').and.callThrough();
spyOn(MapViewModel, 'getMarkerExtra').and.callThrough();
const fill = 'black';
const isLarge = true;
const isExtra = true;
// when
const dataUri = MapViewModel.getSvgSrc(fill, isLarge, isExtra);
const svgSrc = decodeURIComponent(dataUri.replace(/data:image\/svg\+xml,/, ''));
const element = uUtils.nodeFromHtml(svgSrc);
// then
expect(element).toBeInstanceOf(SVGElement);
expect(svgSrc).toContain(`fill="${fill}"`);
expect(MapViewModel.getMarkerPath).toHaveBeenCalledWith(isLarge);
expect(MapViewModel.getMarkerExtra).toHaveBeenCalledWith(isLarge);
});
});