refactor: migrate all cordova plugins to capacitor

This commit is contained in:
Thea Schöbl
2022-02-17 08:37:32 +00:00
committed by Rainer Killinger
parent cdb6ac4084
commit 75f4644940
36 changed files with 677 additions and 1118 deletions

View File

@@ -14,12 +14,9 @@
*/
import {TestBed} from '@angular/core/testing';
import {MapModule} from './map.module';
import {Geolocation, Geoposition} from '@ionic-native/geolocation/ngx';
import {defer} from 'rxjs';
import {HttpClientModule} from '@angular/common/http';
import {StorageModule} from '../storage/storage.module';
import {MapPosition, PositionService} from './position.service';
import {Diagnostic} from '@ionic-native/diagnostic/ngx';
import {ConfigProvider} from '../config/config.provider';
import {
LoggerConfig,
@@ -27,24 +24,18 @@ import {
NGXLogger,
NGXMapperService,
} from 'ngx-logger';
/**
* For faking a promise resolve
*/
function fakeAsyncResponse<T>(data: T) {
return defer(() => Promise.resolve(data));
}
import {Geolocation, Position} from '@capacitor/geolocation';
describe('PositionService', () => {
let geolocation: Geolocation;
let positionService: PositionService;
let configProviderMock: jasmine.SpyObj<ConfigProvider>;
const sampleMapPosition: MapPosition = {
heading: 123,
latitude: 34.12,
longitude: 12.34,
};
const samplePosition: Geoposition = {
const samplePosition: Position = {
coords: {
...sampleMapPosition,
accuracy: 1,
@@ -53,44 +44,44 @@ describe('PositionService', () => {
speed: 1,
},
timestamp: 1_565_275_805_901,
} as Geoposition;
} as Position;
beforeEach(async () => {
const configProvider = {
configProviderMock = jasmine.createSpyObj('ConfigProvider', {
getValue: () => {
Promise.resolve();
return;
},
};
});
TestBed.configureTestingModule({
imports: [MapModule, HttpClientModule, StorageModule, LoggerModule],
providers: [
Geolocation,
Diagnostic,
LoggerConfig,
NGXLogger,
NGXMapperService,
{
provider: ConfigProvider,
useValue: configProvider,
useValue: configProviderMock,
},
],
});
positionService = TestBed.inject(PositionService);
geolocation = TestBed.inject(Geolocation);
spyOn(geolocation, 'getCurrentPosition').and.returnValue(
spyOn(Geolocation, 'getCurrentPosition').and.callFake(_options =>
Promise.resolve(samplePosition),
);
spyOn(geolocation, 'watchPosition').and.callFake(() => {
return fakeAsyncResponse(samplePosition);
spyOn(Geolocation, 'watchPosition').and.callFake((_options, callback) => {
callback(samplePosition);
return Promise.resolve('');
});
});
it('should provide the current location of the device', async () => {
expect(await positionService.getCurrentLocation()).toEqual(
sampleMapPosition,
);
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);
});
it('should continuously provide (watch) location of the device', async () => {