Skip to content

Commit

Permalink
Final touches
Browse files Browse the repository at this point in the history
  • Loading branch information
igorkamyshev committed Apr 29, 2024
1 parent 82993b4 commit 17bd97b
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 18 deletions.
51 changes: 45 additions & 6 deletions packages/web-api/src/geolocation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
* e2e-tests for built-in browser navigator.geolocation in apps/web-api-demo/test/geolocation.spec.ts
*/

import { allSettled, createStore, fork } from 'effector';
import { allSettled, createStore, createWatch, fork } from 'effector';
import { trackGeolocation } from 'geolocation';
import { describe, expect, test } from 'vitest';
import { describe, expect, test, vi } from 'vitest';

describe('trackGeolocation', () => {
test('request', async () => {
let lat = 41.890221;
let lon = 12.492348;
const myCustomProvider = () => ({
const myCustomProvider = vi.fn(() => ({
async getCurrentPosition() {
return {
coords: { latitude: lat, longitude: lon },
Expand All @@ -21,7 +21,7 @@ describe('trackGeolocation', () => {
watchPosition(success: any, error: any) {
return () => {};
},
});
}));

const geo = trackGeolocation({ providers: [myCustomProvider] });

Expand All @@ -47,6 +47,8 @@ describe('trackGeolocation', () => {
"longitude": 13.492348,
}
`);

expect(myCustomProvider).toBeCalledTimes(1);
});

test('watching', async () => {
Expand All @@ -55,7 +57,7 @@ describe('trackGeolocation', () => {
longitude: 12.492348,
});

const myCustomProvider = () => ({
const myCustomProvider = vi.fn(() => ({
async getCurrentPosition() {
return {
coords: $externalLocation.getState(),
Expand All @@ -70,7 +72,7 @@ describe('trackGeolocation', () => {
})
);
},
});
}));

const geo = trackGeolocation({ providers: [myCustomProvider] });

Expand Down Expand Up @@ -102,5 +104,42 @@ describe('trackGeolocation', () => {
"longitude": 13.492348,
}
`);

expect(myCustomProvider).toBeCalledTimes(1);
});

test('reporting', async () => {
const myCustomProvider = () => ({
async getCurrentPosition() {
throw {
code: 'PERMISSION_DENIED',
message: 'User denied the request for Geolocation.',
raw: '用户拒绝了地理位置请求。',
};
},
watchPosition(success: any, error: any) {
return () => {};
},
});

const failedListener = vi.fn();

const geo = trackGeolocation({ providers: [myCustomProvider] });

const scope = fork();

createWatch({ unit: geo.reporting.failed, fn: failedListener, scope });

await allSettled(geo.request, { scope });

expect(failedListener).toBeCalledWith({
code: 'PERMISSION_DENIED',
message: 'User denied the request for Geolocation.',
raw: '用户拒绝了地理位置请求。',
});
});

test('do not throw if default provider is not available', async () => {
expect(() => trackGeolocation()).not.toThrow();
});
});
32 changes: 20 additions & 12 deletions packages/web-api/src/geolocation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,24 @@ export function trackGeolocation(
params?.providers ?? /* In case of no providers, we will use the default one only */ [
BrowserProvider,
]
).map(
/* BrowserProvider symbol means usage of navigator.geolocation */
(provider) =>
(provider === BrowserProvider ? navigator.geolocation : provider) as
| CustomProvider
| globalThis.Geolocation
);
)
.map((provider) => {
/* BrowserProvider symbol means usage of navigator.geolocation */
if (provider === BrowserProvider) {
const browserGeolocationAvailable =
globalThis.navigator && 'geolocation' in globalThis.navigator;
if (!browserGeolocationAvailable) {
return null;
}

return globalThis.navigator.geolocation;
}

return (provider as CustomProvider)(params ?? {});
})
.filter(Boolean) as Array<
ReturnType<CustomProvider> | globalThis.Geolocation
>;

// -- units

Expand Down Expand Up @@ -150,7 +161,7 @@ export function trackGeolocation(
provider.getCurrentPosition(resolve, rejest, params)
);
} else {
geolocation = await provider(params ?? {}).getCurrentPosition();
geolocation = await provider.getCurrentPosition();
}
}

Expand Down Expand Up @@ -192,10 +203,7 @@ export function trackGeolocation(

defaultUnwatchMap.set((id: number) => provider.clearWatch(id), watchId);
} else {
const unwatch = provider(params ?? {}).watchPosition(
boundNewPosition,
boundFailed
);
const unwatch = provider.watchPosition(boundNewPosition, boundFailed);

customUnwatchSet.add(unwatch);
}
Expand Down

0 comments on commit 17bd97b

Please sign in to comment.