mirror of
https://github.com/inaturalist/iNaturalistReactNative.git
synced 2026-04-19 06:23:12 -04:00
* Add a safeRealmWrite transaction for better logging around writes; code cleanup and realm update * Add safeRealmWrite to tests and make sure action is called synchronously * Fix final test * Only write to realm when useObservationsUpdates data changes; code cleanup * Code cleanup
38 lines
1.5 KiB
JavaScript
38 lines
1.5 KiB
JavaScript
import { renderHook } from "@testing-library/react-native";
|
|
import safeRealmWrite from "sharedHelpers/safeRealmWrite";
|
|
import useObservationUpdatesWhenFocused from "sharedHooks/useObservationUpdatesWhenFocused";
|
|
import factory from "tests/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( ( ) => {
|
|
// Write mock observations to realm
|
|
safeRealmWrite( global.realm, ( ) => {
|
|
mockObservations.forEach( o => {
|
|
global.realm.create( "Observation", o );
|
|
} );
|
|
}, "write observations to realm, useObservationUpdatesWhenFocused test" );
|
|
} );
|
|
|
|
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 );
|
|
} );
|
|
} );
|
|
} );
|