import { GeoService } from './geo.service';
import { take, skip } from 'rxjs/operators';

describe('GeoService', () => {
    let service: GeoService;
    let geolocation: any;
    const coords = {
        latitude: 10,
        longitude: 20,
    };
    const position = { coords };
    beforeEach(() => {
        geolocation = {};
        service = new GeoService({ navigator: { geolocation } } as any);
    });
    it('should resolve geolocation', async () => {
        geolocation.getCurrentPosition = (success) => success(position);

        const coord = await service.getPosition();
        expect(coord).toEqual(coords as any);
    });

    it('should not resolve geolocation', (done) => {
        geolocation.getCurrentPosition = (_, fail) => fail(position);

        service.getPosition().catch(done);
    });

    it('should resolve geolocation as boolean', async () => {
        geolocation.getCurrentPosition = (success) => success(position);

        expect(await service.hasPosition.pipe(take(1)).toPromise()).toEqual(false);

        await service.getPosition();

        expect(await service.hasPosition.pipe(take(1)).toPromise()).toEqual(true);
    });

    it('should not resolve geolocation as boolean', async () => {
        geolocation.getCurrentPosition = (_, fail) => fail(position);

        expect(await service.hasPosition.pipe(take(1)).toPromise()).toEqual(false);

        try {
            await service.getPosition();
        } catch (e) {
            //
        }

        expect(await service.hasPosition.pipe(take(1)).toPromise()).toEqual(false);
    });
});
