Files
iNaturalistReactNative/tests/integration/sharedHooks/useObservationUpdatesWhenFocused.test.js
Johannes Klein e1a7a4383d 549 filled activity icons (#574)
* Refactor ObsStatus, add filled property

* Remove viewed prop from observation

* Update properties for ObsDetail mark viewed

* Use separate fields for comments and ids for ObsStatus

* Add hook for fetching updates

* Unit tests for the updates hook

* Unit tests for different paths for updating the realm observation when using the hook

* Update MyObservations.test.js

* Add integration test if updates has been called in my observations

* Update Podfile.lock

* Update observation to not viewed if the keys have not been initialized

* Add test case if observation fields are not initialized

* Add test case

* Refetch stale data on coming back from the background

* Subscribe to observation updates also in ObsDetail screen

This also invalidates and refetches the data after a successful mutation of the id or comment being viewed.

* Refetch observation updates data on sync button press

* Throw error in updates query

* Return refetch function from hook

* Add mock to ObsDetails

* Remove unused var

* Update useObservationsUpdates.test.js

* Reset realm viewed state to true ...

for all observations on app mount and coming to the foreground.

* Reorder function calls

* Change hook to use boolean as enabled param

* Create ObsDetails.test.js

* Update date handle tests to make sure Remote data classes have parseable date

* Add test case for comment and identification

* Update ObsDetails.test.js

* Also check for user in ObsDetails

* Refactored into separate hook for App.js

* Unit and integration test for new hook

* Rename const

* Rename field

* Rename field

* Export query key from hook

* Refactor true check

* Additional methods for obs being viewed or not

* Changed test description

* Add per_page to observations updated
2023-05-11 16:33:37 +02:00

38 lines
1.3 KiB
JavaScript

import { renderHook } from "@testing-library/react-native";
import useObservationUpdatesWhenFocused from "sharedHooks/useObservationUpdatesWhenFocused";
import factory from "../../factory";
jest.mock( "react-native/Libraries/AppState/AppState", () => ( {
addEventListener: jest.fn( ( event, callback ) => {
callback( "active" );
} )
} ) );
const mockObservations = [
factory( "LocalObservation", { comments_viewed: false, identifications_viewed: false } ),
factory( "LocalObservation", { comments_viewed: true, identifications_viewed: false } ),
factory( "LocalObservation", { comments_viewed: false, identifications_viewed: true } ),
factory( "LocalObservation", { comments_viewed: true, identifications_viewed: true } )
];
describe( "useObservationUpdatesWhenFocused", () => {
beforeAll( async () => {
// Write mock observations to realm
await global.realm.write( () => {
mockObservations.forEach( o => {
global.realm.create( "Observation", o );
} );
} );
} );
it( "should reset state of all observations in realm", () => {
renderHook( () => useObservationUpdatesWhenFocused() );
const observations = global.realm.objects( "Observation" );
observations.forEach( o => {
expect( o.comments_viewed ).toBe( true );
expect( o.identifications_viewed ).toBe( true );
} );
} );
} );