Files
iNaturalistReactNative/tests/unit/sharedHooks/useWatchPosition.test.js
Ryan Stelly 9e91642a95 fix stale subscriptions in useObservationLocation & useWatchPosition (#3790)
* useFocusEffect for more correct state

* useFocusEffect for useWatchPosition (bugs & complexity)

* fix tests

* remove stopWatch return

* remove unnecessary useCallback

* restore maybe unnecessary max age check
2026-07-10 14:46:00 -05:00

109 lines
3.4 KiB
JavaScript

import Geolocation from "@react-native-community/geolocation";
import { renderHook, waitFor } from "@testing-library/react-native";
import { useWatchPosition } from "sharedHooks";
jest.mock( "@react-navigation/native", () => ( {
...jest.requireActual( "@react-navigation/native" ),
// Run the focus callback via useEffect so we don't need a NavigationContainer
useFocusEffect: cb => jest.requireActual( "react" ).useEffect( cb, [] ),
} ) );
const mockPositions = [
{
coords: {
latitude: 1,
longitude: 1,
accuracy: 100,
},
},
{
coords: {
latitude: 2,
longitude: 2,
accuracy: 20,
},
},
{
coords: {
latitude: 3,
longitude: 3,
accuracy: 8,
},
},
];
const mockWatchPositionSuccess = jest.fn( onSuccess => onSuccess( ) );
describe( "useWatchPosition with inaccurate location", ( ) => {
beforeEach( ( ) => {
Geolocation.watchPosition.mockReset( );
Geolocation.watchPosition.mockImplementation( success => {
setTimeout( ( ) => mockWatchPositionSuccess( ( ) => success( mockPositions[0] ) ), 100 );
return 0;
} );
} );
// The success callback should have been called and that roughly
// marks the end of async effects, so hopefull this prevents "outside of
// act" warnings
afterEach( ( ) => waitFor( ( ) => {
expect( mockWatchPositionSuccess ).toHaveBeenCalled( );
mockWatchPositionSuccess.mockClear( );
} ) );
it( "should be loading by default", async ( ) => {
const { result } = renderHook( ( ) => useWatchPosition( { shouldFetchLocation: true } ) );
await waitFor( ( ) => {
expect( result.current.isFetchingLocation ).toBeTruthy( );
} );
} );
it( "should return a user location", async ( ) => {
const { result } = renderHook( ( ) => useWatchPosition( { shouldFetchLocation: true } ) );
await waitFor( ( ) => {
expect( result.current.userLocation.latitude ).toBeDefined( );
} );
expect( result.current.userLocation.latitude ).toEqual( mockPositions[0].coords.latitude );
} );
} );
describe( "useWatchPosition with accurate location", ( ) => {
beforeEach( ( ) => {
Geolocation.clearWatch.mockClear( );
Geolocation.watchPosition.mockImplementation( success => {
setTimeout( ( ) => {
mockWatchPositionSuccess( ( ) => success( mockPositions[0] ) );
setTimeout( ( ) => {
mockWatchPositionSuccess( ( ) => success( mockPositions[1] ) );
setTimeout( ( ) => {
mockWatchPositionSuccess( ( ) => success( mockPositions[2] ) );
}, 100 );
}, 100 );
}, 100 );
return 0;
} );
} );
it( "should stop watching position when target accuracy reached", async ( ) => {
const { result } = renderHook( ( ) => useWatchPosition( { shouldFetchLocation: true } ) );
await waitFor( ( ) => {
expect( result.current.userLocation?.positional_accuracy )
.toEqual( mockPositions[2].coords.accuracy );
} );
expect( Geolocation.clearWatch ).toHaveBeenCalledTimes( 1 );
} );
} );
describe( "useWatchPosition when shouldn't fetch", ( ) => {
beforeEach( ( ) => {
Geolocation.watchPosition.mockReset( );
} );
it( "should not watch position when shouldFetchLocation is false", async ( ) => {
renderHook( ( ) => useWatchPosition( { shouldFetchLocation: false } ) );
await waitFor( ( ) => {
expect( Geolocation.watchPosition ).not.toHaveBeenCalled( );
} );
} );
} );