From 94934fbefd492af072e92d3af3f3bca90e195a2f Mon Sep 17 00:00:00 2001 From: yonash24 Date: Wed, 11 Nov 2026 15:38:49 +0200 Subject: [PATCH] fix(useGeolocation): restore updates after effect replay --- src/useGeolocation.ts | 41 ++++++++-------- tests/useGeolocation.test.ts | 90 ++++++++++++++++++++++++++++++++++++ 2 files changed, 110 insertions(+), 21 deletions(-) create mode 100644 tests/useGeolocation.test.ts diff --git a/src/useGeolocation.ts b/src/useGeolocation.ts index 5c3f64c770..042bed81e0 100644 --- a/src/useGeolocation.ts +++ b/src/useGeolocation.ts @@ -38,30 +38,29 @@ const useGeolocation = (options?: PositionOptions): GeoLocationSensorState => { speed: null, timestamp: Date.now(), }); - let mounted = true; - let watchId: any; + useEffect(() => { + let mounted = true; - const onEvent = (event: any) => { - if (mounted) { - setState({ - loading: false, - accuracy: event.coords.accuracy, - altitude: event.coords.altitude, - altitudeAccuracy: event.coords.altitudeAccuracy, - heading: event.coords.heading, - latitude: event.coords.latitude, - longitude: event.coords.longitude, - speed: event.coords.speed, - timestamp: event.timestamp, - }); - } - }; - const onEventError = (error: IGeolocationPositionError) => - mounted && setState((oldState) => ({ ...oldState, loading: false, error })); + const onEvent = (event: any) => { + if (mounted) { + setState({ + loading: false, + accuracy: event.coords.accuracy, + altitude: event.coords.altitude, + altitudeAccuracy: event.coords.altitudeAccuracy, + heading: event.coords.heading, + latitude: event.coords.latitude, + longitude: event.coords.longitude, + speed: event.coords.speed, + timestamp: event.timestamp, + }); + } + }; + const onEventError = (error: IGeolocationPositionError) => + mounted && setState((oldState) => ({ ...oldState, loading: false, error })); - useEffect(() => { navigator.geolocation.getCurrentPosition(onEvent, onEventError, options); - watchId = navigator.geolocation.watchPosition(onEvent, onEventError, options); + const watchId = navigator.geolocation.watchPosition(onEvent, onEventError, options); return () => { mounted = false; diff --git a/tests/useGeolocation.test.ts b/tests/useGeolocation.test.ts new file mode 100644 index 0000000000..042325a2e2 --- /dev/null +++ b/tests/useGeolocation.test.ts @@ -0,0 +1,90 @@ +import React from 'react'; +import { act, cleanup, renderHook } from '@testing-library/react-hooks'; +import useGeolocation from '../src/useGeolocation'; + +describe('useGeolocation effect replay', () => { + const originalUseEffect = React.useEffect; + const originalGeolocation = Object.getOwnPropertyDescriptor(navigator, 'geolocation'); + let successes: PositionCallback[]; + let errors: PositionErrorCallback[]; + const clearWatch = jest.fn(); + + beforeEach(() => { + successes = []; + errors = []; + Object.defineProperty(navigator, 'geolocation', { + configurable: true, + value: { + getCurrentPosition: jest.fn(), + watchPosition: jest.fn((success, error) => { + successes.push(success); + errors.push(error); + return successes.length; + }), + clearWatch, + }, + }); + // Exercise setup/cleanup/setup with the repository's React 17 test renderer. + jest.spyOn(React, 'useEffect').mockImplementation((effect, deps) => { + originalUseEffect(() => { + const cleanup = effect(); + if (cleanup) cleanup(); + return effect(); + }, deps); + }); + }); + + afterEach(async () => { + await cleanup(); + jest.restoreAllMocks(); + if (originalGeolocation) { + Object.defineProperty(navigator, 'geolocation', originalGeolocation); + } else { + delete (navigator as any).geolocation; + } + }); + + it('receives positions from the active watcher after replay', () => { + const { result, unmount } = renderHook(() => useGeolocation()); + const position = { + coords: { + accuracy: 1, + altitude: null, + altitudeAccuracy: null, + heading: null, + latitude: 32, + longitude: 34, + speed: null, + }, + timestamp: 123, + } as GeolocationPosition; + + act(() => successes[1](position)); + expect(result.current.loading).toBe(false); + expect(result.current.latitude).toBe(32); + expect(clearWatch).toHaveBeenCalledWith(1); + + const previous = result.current; + act(() => successes[0]({ ...position, timestamp: 456 })); + expect(result.current).toBe(previous); + unmount(); + expect(clearWatch).toHaveBeenCalledWith(2); + }); + + it('receives errors from the active watcher after replay', () => { + const { result } = renderHook(() => useGeolocation()); + const error = { + code: 1, + message: 'Permission denied', + PERMISSION_DENIED: 1, + POSITION_UNAVAILABLE: 2, + TIMEOUT: 3, + } as GeolocationPositionError; + + act(() => { + errors[1](error); + }); + expect(result.current.loading).toBe(false); + expect(result.current.error).toBe(error); + }); +});