mirror of
https://github.com/inaturalist/iNaturalistReactNative.git
synced 2026-05-06 22:56:12 -04:00
* Refactor to use watchPosition
* Update useWatchPosition with permissions/retry
* Replace useUserLocation with useWatchPosition and fix tests; return userLocation from watch position hook
* Only update observation keys when there's an observation
* Improve TypeScript definitions
* Revert TypeScript commit
* Revert "Only update observation keys when there's an observation"
This reverts commit a4cd17a513.
* Code cleanup: make useWatchPosition more modular
* Code cleanup; location permission in ObsEdit instead of subcomponent
* Use correct accuracy in Camera photos
* Camera fixes
* Fixes to watching position in ObsEdit
* Fix useWatchPosition tests
* Fix tests
* Make sure state updates when renavigating to OsEdit; test fixes
117 lines
3.7 KiB
JavaScript
117 lines
3.7 KiB
JavaScript
import Geolocation from "@react-native-community/geolocation";
|
|
import NetInfo from "@react-native-community/netinfo";
|
|
import { screen, waitFor } from "@testing-library/react-native";
|
|
import ObsEdit from "components/ObsEdit/ObsEdit";
|
|
import fetchMock from "jest-fetch-mock";
|
|
import React from "react";
|
|
import ReactNativePermissions from "react-native-permissions";
|
|
import useStore from "stores/useStore";
|
|
import factory from "tests/factory";
|
|
import faker from "tests/helpers/faker";
|
|
import { renderAppWithComponent } from "tests/helpers/render";
|
|
import setupUniqueRealm from "tests/helpers/uniqueRealm";
|
|
import { signIn, signOut } from "tests/helpers/user";
|
|
|
|
const initialStoreState = useStore.getState( );
|
|
|
|
// UNIQUE REALM SETUP
|
|
const mockRealmIdentifier = __filename;
|
|
const { mockRealmModelsIndex, uniqueRealmBeforeAll, uniqueRealmAfterAll } = setupUniqueRealm(
|
|
mockRealmIdentifier
|
|
);
|
|
jest.mock( "realmModels/index", ( ) => mockRealmModelsIndex );
|
|
jest.mock( "providers/contexts", ( ) => {
|
|
const originalModule = jest.requireActual( "providers/contexts" );
|
|
return {
|
|
__esModule: true,
|
|
...originalModule,
|
|
RealmContext: {
|
|
...originalModule.RealmContext,
|
|
useRealm: ( ) => global.mockRealms[mockRealmIdentifier],
|
|
useQuery: ( ) => []
|
|
}
|
|
};
|
|
} );
|
|
beforeAll( uniqueRealmBeforeAll );
|
|
afterAll( uniqueRealmAfterAll );
|
|
// /UNIQUE REALM SETUP
|
|
|
|
beforeEach( async ( ) => {
|
|
useStore.setState( initialStoreState, true );
|
|
const mockUser = factory( "LocalUser", {
|
|
login: faker.internet.userName( ),
|
|
iconUrl: faker.image.url( ),
|
|
locale: "en"
|
|
} );
|
|
await signIn( mockUser, { realm: global.mockRealms[__filename] } );
|
|
const mockedPermissions = {
|
|
"ios.permission.LOCATION": "granted"
|
|
};
|
|
|
|
jest.spyOn( ReactNativePermissions, "checkMultiple" )
|
|
.mockResolvedValueOnce( mockedPermissions );
|
|
} );
|
|
|
|
afterEach( ( ) => {
|
|
signOut( { realm: global.mockRealms[__filename] } );
|
|
jest.clearAllMocks( );
|
|
} );
|
|
|
|
describe( "ObsEdit offline", ( ) => {
|
|
beforeEach( ( ) => {
|
|
// Turn on fetch mocks and make all fetch requests throw an error
|
|
fetchMock.doMock( );
|
|
fetch.mockAbort( );
|
|
expect( fetch( "/" ) ).rejects.toThrow( );
|
|
|
|
// Mock NetInfo so it says internet is not reachable
|
|
NetInfo.fetch.mockImplementation( async ( ) => ( {
|
|
isInternetReachable: false
|
|
} ) );
|
|
} );
|
|
|
|
afterEach( ( ) => {
|
|
fetchMock.dontMock( );
|
|
} );
|
|
|
|
describe( "creation", ( ) => {
|
|
it( "should fetch coordinates", async ( ) => {
|
|
const mockWatchPosition = jest.fn( ( success, _error, _options ) => success( {
|
|
coords: {
|
|
latitude: 1,
|
|
longitude: 1,
|
|
accuracy: 9,
|
|
timestamp: Date.now( )
|
|
}
|
|
} ) );
|
|
Geolocation.watchPosition.mockImplementation( mockWatchPosition );
|
|
const observation = factory( "LocalObservation", {
|
|
observationPhotos: []
|
|
} );
|
|
useStore.setState( {
|
|
observations: [observation],
|
|
currentObservation: observation
|
|
} );
|
|
renderAppWithComponent(
|
|
<ObsEdit />
|
|
);
|
|
// removing the next lines since location fetch is fast enough that the location indicator
|
|
// won't show up in this scenario
|
|
// expect(
|
|
// screen.getByTestId( "EvidenceSection.fetchingLocationIndicator" )
|
|
// ).toBeTruthy( );
|
|
await waitFor( ( ) => {
|
|
expect( mockWatchPosition ).toHaveBeenCalled( );
|
|
} );
|
|
const coords = await screen.findByText( /Lat:/ );
|
|
expect( coords ).toBeTruthy( );
|
|
expect( screen.queryByText( "Finding location..." ) ).toBeFalsy( );
|
|
await waitFor( ( ) => {
|
|
expect(
|
|
screen.queryByTestId( "EvidenceSection.fetchingLocationIndicator" )
|
|
).toBeFalsy( );
|
|
} );
|
|
} );
|
|
} );
|
|
} );
|