Files
iNaturalistReactNative/tests/integration/ObsDetails.test.js
Johannes Klein 43443f5cfc Consolidate ObsDetails screens (#3649)
* Create new hook shell

* Revert "Create new hook shell"

This reverts commit b6918db347.

* Copy ObsDetailsDefaultModeScreensWrapper as is to new file

* Copy ObsDetailsContainer as is into new file

* We don't need this param at this level

* Have new ObsDetailsContainer be the next child of screen

* Update props of new child and remove thereby hoisted code

* Move route param extraction into component that needs it

* Extract presentational logic into new component

* Also copy over flow types

* Also copy over flow types

* Return null if no observation

* Remove mode switch logic from navigator

* Remove no longer needed advanced mode container

* Consolidate default mode container into new one as well, and switch over default mode to show default mode UI

* Same as in advanced mode, read param where it is needed

* Remove prop

* Run the integration test on the only container

* Git mv test file and update with new import

* Move test and only keep the UI logic part being tested

* Move tests relevant for ObsDetailsScreen

* Update ObsDetailsScreen.test.js

* Update IdentificationSheets.test.js

* Remove unused screen wrapper
2026-05-27 16:27:27 +02:00

105 lines
3.6 KiB
JavaScript

import { screen, waitFor } from "@testing-library/react-native";
import ObsDetailsScreen from "components/ObsDetailsSharedComponents/ObsDetailsScreen";
import inatjs from "inaturalistjs";
import React from "react";
import Observation from "realmModels/Observation";
import factory, { makeResponse } from "tests/factory";
import { renderAppWithComponent } from "tests/helpers/render";
import setupUniqueRealm from "tests/helpers/uniqueRealm";
import { signIn, signOut } from "tests/helpers/user";
// 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
const mockComment = factory( "RemoteComment" );
const mockObservation = factory( "RemoteObservation", {
comments: [mockComment],
} );
const mockUpdate = factory( "RemoteUpdate", {
resource_uuid: mockObservation.uuid,
comment_id: mockComment.id,
viewed: false,
} );
const mockUser = factory( "LocalUser" );
// Mock api call to fetch observation so it looks like a remote copy exists
inatjs.observations.fetch.mockResolvedValue( makeResponse( [mockObservation] ) );
// Mock api call to observations
jest.mock( "inaturalistjs" );
inatjs.observations.update.mockResolvedValue( makeResponse( [mockUpdate] ) );
jest.mock( "@react-navigation/native", () => {
const actualNav = jest.requireActual( "@react-navigation/native" );
return {
...actualNav,
addEventListener: () => undefined,
useNavigation: () => ( {
navigate: jest.fn(),
setOptions: jest.fn(),
canGoBack: jest.fn( ( ) => true ),
} ),
useRoute: () => ( {
params: {
uuid: mockObservation.uuid,
},
} ),
};
} );
describe( "ObsDetails", ( ) => {
beforeAll( async () => {
jest.useFakeTimers( );
signIn( mockUser, { realm: global.mockRealms[__filename] } );
Observation.upsertRemoteObservations( [mockObservation], global.mockRealms[__filename] );
} );
afterEach( () => {
jest.clearAllMocks();
signOut( { realm: global.mockRealms[__filename] } );
} );
describe( "ObsDetailsScreen", ( ) => {
describe( "with an observation where we don't know if the user has viewed comments", ( ) => {
it( "should make a request to observation/viewedUpdates", async ( ) => {
// Let's make sure the mock hasn't already been used
expect( inatjs.observations.viewedUpdates ).not.toHaveBeenCalled();
const observation = global.mockRealms[__filename].objectForPrimaryKey(
"Observation",
mockObservation.uuid,
);
// Expect the observation in realm to have comments_viewed param not initialized
expect( observation.comments_viewed ).not.toBeTruthy();
renderAppWithComponent( <ObsDetailsScreen /> );
expect(
await screen.findByText( observation.user.login ),
).toBeTruthy();
await waitFor( ( ) => {
expect( inatjs.observations.viewedUpdates ).toHaveBeenCalledTimes( 1 );
} );
// Expect the observation in realm to have been updated with comments_viewed = true
expect( observation.comments_viewed ).toBe( true );
} );
} );
} );
} );