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

@@ -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();
});
});
});