|
| 1 | +/* |
| 2 | + * This file contains tests only for custom geolocation providers. |
| 3 | + * e2e-tests for built-in browser navigator.geolocation in apps/web-api-demo/test/geolocation.spec.ts |
| 4 | + */ |
| 5 | + |
| 6 | +import { allSettled, createStore, fork } from 'effector'; |
| 7 | +import { trackGeolocation } from 'geolocation'; |
| 8 | +import { describe, expect, test } from 'vitest'; |
| 9 | + |
| 10 | +describe('trackGeolocation', () => { |
| 11 | + test('request', async () => { |
| 12 | + let lat = 41.890221; |
| 13 | + let lon = 12.492348; |
| 14 | + const myCustomProvider = () => ({ |
| 15 | + async getCurrentPosition() { |
| 16 | + return { |
| 17 | + coords: { latitude: lat, longitude: lon }, |
| 18 | + timestamp: Date.now(), |
| 19 | + }; |
| 20 | + }, |
| 21 | + watchPosition(success: any, error: any) { |
| 22 | + return () => {}; |
| 23 | + }, |
| 24 | + }); |
| 25 | + |
| 26 | + const geo = trackGeolocation({ providers: [myCustomProvider] }); |
| 27 | + |
| 28 | + const scope = fork(); |
| 29 | + |
| 30 | + expect(scope.getState(geo.$location)).toMatchInlineSnapshot(`null`); |
| 31 | + |
| 32 | + await allSettled(geo.request, { scope }); |
| 33 | + expect(scope.getState(geo.$location)).toMatchInlineSnapshot(` |
| 34 | + { |
| 35 | + "latitude": 41.890221, |
| 36 | + "longitude": 12.492348, |
| 37 | + } |
| 38 | + `); |
| 39 | + |
| 40 | + lat = 42.890221; |
| 41 | + lon = 13.492348; |
| 42 | + |
| 43 | + await allSettled(geo.request, { scope }); |
| 44 | + expect(scope.getState(geo.$location)).toMatchInlineSnapshot(` |
| 45 | + { |
| 46 | + "latitude": 42.890221, |
| 47 | + "longitude": 13.492348, |
| 48 | + } |
| 49 | + `); |
| 50 | + }); |
| 51 | + |
| 52 | + test('watching', async () => { |
| 53 | + const $externalLocation: any = createStore({ |
| 54 | + latitude: 41.890221, |
| 55 | + longitude: 12.492348, |
| 56 | + }); |
| 57 | + |
| 58 | + const myCustomProvider = () => ({ |
| 59 | + async getCurrentPosition() { |
| 60 | + return { |
| 61 | + coords: $externalLocation.getState(), |
| 62 | + timestamp: Date.now(), |
| 63 | + }; |
| 64 | + }, |
| 65 | + watchPosition(success: any, error: any) { |
| 66 | + return $externalLocation.watch((location: any) => |
| 67 | + success({ |
| 68 | + coords: location, |
| 69 | + timestamp: Date.now(), |
| 70 | + }) |
| 71 | + ); |
| 72 | + }, |
| 73 | + }); |
| 74 | + |
| 75 | + const geo = trackGeolocation({ providers: [myCustomProvider] }); |
| 76 | + |
| 77 | + const scope = fork(); |
| 78 | + |
| 79 | + expect(scope.getState(geo.$location)).toMatchInlineSnapshot(`null`); |
| 80 | + |
| 81 | + await allSettled(geo.watching.start, { scope }); |
| 82 | + expect(scope.getState(geo.$location)).toMatchInlineSnapshot(` |
| 83 | + { |
| 84 | + "latitude": 41.890221, |
| 85 | + "longitude": 12.492348, |
| 86 | + } |
| 87 | + `); |
| 88 | + |
| 89 | + $externalLocation.setState({ latitude: 42.890221, longitude: 13.492348 }); |
| 90 | + expect(scope.getState(geo.$location)).toMatchInlineSnapshot(` |
| 91 | + { |
| 92 | + "latitude": 42.890221, |
| 93 | + "longitude": 13.492348, |
| 94 | + } |
| 95 | + `); |
| 96 | + |
| 97 | + await allSettled(geo.watching.stop, { scope }); |
| 98 | + $externalLocation.setState({ latitude: 43.890221, longitude: 14.492348 }); |
| 99 | + expect(scope.getState(geo.$location)).toMatchInlineSnapshot(` |
| 100 | + { |
| 101 | + "latitude": 42.890221, |
| 102 | + "longitude": 13.492348, |
| 103 | + } |
| 104 | + `); |
| 105 | + }); |
| 106 | +}); |
0 commit comments