diff --git a/__mocks__/@capacitor/geolocation.ts b/__mocks__/@capacitor/geolocation.ts new file mode 100644 index 00000000..45983bc2 --- /dev/null +++ b/__mocks__/@capacitor/geolocation.ts @@ -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 . + */ + +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 { + // @ts-ignore + return Promise.resolve({}); + } + + clearWatch(_options: any): Promise { + return Promise.resolve(undefined); + } + + getCurrentPosition(_options?: any): Promise { + return Promise.resolve(samplePosition); + } + + // @ts-ignore + requestPermissions(permissions?: any): Promise { + // @ts-ignore + return Promise.resolve(undefined); + } + + watchPosition(_options: PositionOptions, callback: (position: Position, error?: any) => void): Promise { + callback(samplePosition); + return Promise.resolve(''); + } +} + +export interface PermissionStatus { + location: PermissionState; + coarseLocation: PermissionState; +} + +export const Geolocation = new GeolocationMock(); diff --git a/karma.conf.js b/karma.conf.js index 6238abe1..0577af4e 100644 --- a/karma.conf.js +++ b/karma.conf.js @@ -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'), diff --git a/src/app/modules/map/position.service.spec.ts b/src/app/modules/map/position.service.spec.ts index b9035ea8..89467d56 100644 --- a/src/app/modules/map/position.service.spec.ts +++ b/src/app/modules/map/position.service.spec.ts @@ -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(); + }); }); }); diff --git a/tsconfig.spec.json b/tsconfig.spec.json index 89132033..297f6878 100644 --- a/tsconfig.spec.json +++ b/tsconfig.spec.json @@ -6,7 +6,10 @@ "types": [ "jasmine", "node" - ] + ], + "paths": { + "@capacitor/*": ["__mocks__/@capacitor/*"] + } }, "files": [ "src/test.ts"