test: fix by mocking geolocation

This commit is contained in:
Jovan Krunić
2022-02-22 11:08:38 +01:00
parent 562f357374
commit 89dc41ad35
4 changed files with 88 additions and 34 deletions

View File

@@ -0,0 +1,74 @@
/*
* Copyright (C) 2022 StApps
* This program 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, version 3.
*
* 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 <https://www.gnu.org/licenses/>.
*/
export interface Position {
timestamp: number;
coords: {
latitude: number;
longitude: number;
accuracy: number;
altitudeAccuracy: number | null | undefined;
altitude: number | null;
speed: number | null;
heading: number | null;
};
}
const samplePosition: Position = {
coords: {
heading: 123,
latitude: 34.12,
longitude: 12.34,
accuracy: 1,
altitude: 123,
altitudeAccuracy: 1,
speed: 1,
},
timestamp: 1_565_275_805_901,
} as Position;
export class GeolocationMock {
// @ts-ignore
checkPermissions(): Promise<PermissionStatus> {
// @ts-ignore
return Promise.resolve({});
}
clearWatch(_options: any): Promise<void> {
return Promise.resolve(undefined);
}
getCurrentPosition(_options?: any): Promise<Position> {
return Promise.resolve(samplePosition);
}
// @ts-ignore
requestPermissions(permissions?: any): Promise<PermissionStatus> {
// @ts-ignore
return Promise.resolve(undefined);
}
watchPosition(_options: PositionOptions, callback: (position: Position, error?: any) => void): Promise<string> {
callback(samplePosition);
return Promise.resolve('');
}
}
export interface PermissionStatus {
location: PermissionState;
coarseLocation: PermissionState;
}
export const Geolocation = new GeolocationMock();

View File

@@ -19,7 +19,7 @@ var isDocker = require('is-docker');
module.exports = function (config) {
config.set({
basePath: 'src',
basePath: '',
frameworks: ['jasmine', '@angular-devkit/build-angular'],
plugins: [
require('karma-jasmine'),

View File

@@ -24,7 +24,6 @@ import {
NGXLogger,
NGXMapperService,
} from 'ngx-logger';
import {Geolocation, Position} from '@capacitor/geolocation';
describe('PositionService', () => {
let positionService: PositionService;
@@ -35,18 +34,8 @@ describe('PositionService', () => {
latitude: 34.12,
longitude: 12.34,
};
const samplePosition: Position = {
coords: {
...sampleMapPosition,
accuracy: 1,
altitude: 123,
altitudeAccuracy: 1,
speed: 1,
},
timestamp: 1_565_275_805_901,
} as Position;
beforeEach(async () => {
beforeEach(() => {
configProviderMock = jasmine.createSpyObj('ConfigProvider', {
getValue: () => {
return;
@@ -65,30 +54,18 @@ describe('PositionService', () => {
],
});
positionService = TestBed.inject(PositionService);
spyOn(Geolocation, 'getCurrentPosition').and.callFake(_options =>
Promise.resolve(samplePosition),
);
spyOn(Geolocation, 'watchPosition').and.callFake((_options, callback) => {
callback(samplePosition);
return Promise.resolve('');
});
});
it('should provide the current location of the device', async () => {
expect(
// jasmine spys are not working as they should, so we use a workaround with a fake position argument
// TODO: find a better way to test this
await positionService.getCurrentLocation(undefined, samplePosition),
).toEqual(sampleMapPosition);
const currentLocation = await positionService.getCurrentLocation();
expect(currentLocation).toEqual(sampleMapPosition);
});
it('should continuously provide (watch) location of the device', async () => {
expect(
positionService
.watchCurrentLocation()
.subscribe(result => expect(result).toEqual(sampleMapPosition)),
);
it('should continuously provide (watch) location of the device', done => {
positionService.watchCurrentLocation().subscribe(location => {
expect(location).toBeDefined();
done();
});
});
});

View File

@@ -6,7 +6,10 @@
"types": [
"jasmine",
"node"
]
],
"paths": {
"@capacitor/*": ["__mocks__/@capacitor/*"]
}
},
"files": [
"src/test.ts"