mirror of
https://gitlab.com/openstapps/openstapps.git
synced 2026-01-19 16:13:06 +00:00
test: fix by mocking geolocation
This commit is contained in:
@@ -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();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user