mirror of
https://gitlab.com/openstapps/openstapps.git
synced 2026-01-21 09:03:02 +00:00
test: fix by mocking geolocation
This commit is contained in:
74
__mocks__/@capacitor/geolocation.ts
Normal file
74
__mocks__/@capacitor/geolocation.ts
Normal 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();
|
||||||
@@ -19,7 +19,7 @@ var isDocker = require('is-docker');
|
|||||||
|
|
||||||
module.exports = function (config) {
|
module.exports = function (config) {
|
||||||
config.set({
|
config.set({
|
||||||
basePath: 'src',
|
basePath: '',
|
||||||
frameworks: ['jasmine', '@angular-devkit/build-angular'],
|
frameworks: ['jasmine', '@angular-devkit/build-angular'],
|
||||||
plugins: [
|
plugins: [
|
||||||
require('karma-jasmine'),
|
require('karma-jasmine'),
|
||||||
|
|||||||
@@ -24,7 +24,6 @@ import {
|
|||||||
NGXLogger,
|
NGXLogger,
|
||||||
NGXMapperService,
|
NGXMapperService,
|
||||||
} from 'ngx-logger';
|
} from 'ngx-logger';
|
||||||
import {Geolocation, Position} from '@capacitor/geolocation';
|
|
||||||
|
|
||||||
describe('PositionService', () => {
|
describe('PositionService', () => {
|
||||||
let positionService: PositionService;
|
let positionService: PositionService;
|
||||||
@@ -35,18 +34,8 @@ describe('PositionService', () => {
|
|||||||
latitude: 34.12,
|
latitude: 34.12,
|
||||||
longitude: 12.34,
|
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', {
|
configProviderMock = jasmine.createSpyObj('ConfigProvider', {
|
||||||
getValue: () => {
|
getValue: () => {
|
||||||
return;
|
return;
|
||||||
@@ -65,30 +54,18 @@ describe('PositionService', () => {
|
|||||||
],
|
],
|
||||||
});
|
});
|
||||||
positionService = TestBed.inject(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 () => {
|
it('should provide the current location of the device', async () => {
|
||||||
expect(
|
const currentLocation = await positionService.getCurrentLocation();
|
||||||
// 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
|
expect(currentLocation).toEqual(sampleMapPosition);
|
||||||
await positionService.getCurrentLocation(undefined, samplePosition),
|
|
||||||
).toEqual(sampleMapPosition);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should continuously provide (watch) location of the device', async () => {
|
it('should continuously provide (watch) location of the device', done => {
|
||||||
expect(
|
positionService.watchCurrentLocation().subscribe(location => {
|
||||||
positionService
|
expect(location).toBeDefined();
|
||||||
.watchCurrentLocation()
|
done();
|
||||||
.subscribe(result => expect(result).toEqual(sampleMapPosition)),
|
});
|
||||||
);
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -6,7 +6,10 @@
|
|||||||
"types": [
|
"types": [
|
||||||
"jasmine",
|
"jasmine",
|
||||||
"node"
|
"node"
|
||||||
]
|
],
|
||||||
|
"paths": {
|
||||||
|
"@capacitor/*": ["__mocks__/@capacitor/*"]
|
||||||
|
}
|
||||||
},
|
},
|
||||||
"files": [
|
"files": [
|
||||||
"src/test.ts"
|
"src/test.ts"
|
||||||
|
|||||||
Reference in New Issue
Block a user