Files
iNaturalistReactNative/tests/integration/navigation/ObsEdit.test.js
Amanda Bullington b12057cbc3 Clear synced photoUploads paths (#1527)
* Clear synced photoUploads paths

* Add mock for useQuery
2024-05-10 16:30:49 -07:00

114 lines
3.6 KiB
JavaScript

import {
screen,
userEvent
} from "@testing-library/react-native";
// import os from "os";
// import path from "path";
// import Realm from "realm";
// import realmConfig from "realmModels/index";
import factory from "tests/factory";
import faker from "tests/helpers/faker";
import {
renderAppWithObservations
} from "tests/helpers/render";
import setupUniqueRealm from "tests/helpers/uniqueRealm";
import { signIn, signOut } from "tests/helpers/user";
// We're explicitly testing navigation here so we want react-navigation
// working normally
jest.unmock( "@react-navigation/native" );
// 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
describe( "ObsEdit", ( ) => {
const actor = userEvent.setup( );
async function findAndPressById( labelText ) {
const pressable = await screen.findByTestId( labelText );
await actor.press( pressable );
return pressable;
}
const mockUser = factory( "LocalUser", {
login: faker.internet.userName( ),
iconUrl: faker.image.url( ),
locale: "en"
} );
beforeAll( async () => {
jest.useFakeTimers( );
} );
describe( "from MyObservations", ( ) => {
const observation = factory( "LocalObservation", {
_created_at: faker.date.past( ),
taxon: factory( "LocalTaxon", {
name: faker.person.firstName( )
} )
} );
async function navigateToObsEditOrObsDetails( observations ) {
await renderAppWithObservations( observations, __filename );
const observationRow = await screen.findByTestId(
`MyObservations.obsListItem.${observations[0].uuid}`
);
await actor.press( observationRow );
}
it( "should show correct observation when navigating from MyObservations", async ( ) => {
const observations = [observation];
await navigateToObsEditOrObsDetails( observations );
expect( await screen.findByText( /Edit Observation/ ) ).toBeTruthy( );
expect( await screen.findByText( observations[0].taxon.name ) ).toBeTruthy( );
} );
describe( "while signed in", ( ) => {
beforeEach( async ( ) => {
await signIn( mockUser, { realm: global.mockRealms[__filename] } );
} );
afterEach( ( ) => {
signOut( { realm: global.mockRealms[__filename] } );
} );
it( "should show correct observation when navigating from ObsDetails", async ( ) => {
const syncedObservation = factory( "LocalObservation", {
_created_at: faker.date.past( ),
_synced_at: faker.date.past( ),
wasSynced: jest.fn( ( ) => true ),
needsSync: jest.fn( ( ) => false ),
taxon: factory( "LocalTaxon", {
name: faker.person.firstName( )
} )
} );
await navigateToObsEditOrObsDetails( [syncedObservation] );
await findAndPressById( "ObsDetail.editButton" );
expect( await screen.findByText( /Edit Observation/ ) ).toBeTruthy( );
expect( await screen.findByText( syncedObservation.taxon.name ) ).toBeTruthy( );
} );
it.todo( "should show photos when reached from ObsDetails" );
} );
} );
} );