Files
iNaturalistReactNative/tests/realm.setup.js
Ryan Stelly 277816b0e0 MOB-1458 use local observation ids (#3763)
* only return observations from useLocalObs

* remove unnecessary var

* export unsync'd filter

* remove obsoleted numUnuploadedObservations zustand state

* tests, typo

* fix tests

* add sync fields to keypaths & depp array

* consistent naming

* handle only ids downstream

* switch over to useIds

* restore ids return

* memoize realm mapping result

* extract realm hooks mocks into factory-available helper

* reapply to myobsresults

* reapply to myobsresults

* go back to wrapped ids for list parity

* fix test

* restrict mocked prop for more accurate MyObsSimple test

* comment cleanup

* fix fake timer flake w/ corrected setup

Claude:

1. withAnimatedTimeTravelEnabled({ skipFakeTimers: true }) does NOT call jest.useFakeTimers(), but still calls jest.setSystemTime(new Date(0)) in beforeEach — which requires fake timers.
2. Both SoundRecorder.test.js and PhotoDeletionExisting.test.js (and ObsEdit.test.js) call global.timeTravel(300) inside waitFor, which also requires fake timers.
3. Tests pass flakily when fake timers accidentally leak from another test file running in the same Jest worker. When they don't leak, timeTravel throws.
4. FadeInView is fully mocked to a plain View (instant) and Reanimated uses setUpTests(), so the 300ms timeTravel isn't actually advancing any real animation — it's just broken.

The fix: guard setSystemTime with the same check, and remove the timeTravel(300) calls from waitFor in tests that use skipFakeTimers: true.

* fix remaining time travel uses

* reset / restore timers to fix leaking fake timers
2026-06-30 11:48:47 -05:00

56 lines
1.8 KiB
JavaScript

import mockOs from "os";
import mockPath from "path";
import Realm from "realm";
// eslint-disable-next-line import/extensions
import realmConfig from "realmModels/index";
// Mock the realm config so it uses an in-memory database. This means data is
// only persisted until realm.close() gets called, so if the code under test
// needs data to persist in Realm across opening/closing events, we will need
// to take a different approach, e.g. writing to Realm to disk and erasing
// those files after each test run
jest.mock( "realmModels/index", ( ) => {
const originalModule = jest.requireActual( "realmModels/index" );
// Mock the default export and named export 'foo'
return {
__esModule: true,
...originalModule,
default: {
schema: originalModule.default.schema,
schemaVersion: originalModule.default.schemaVersion,
inMemory: true,
path: mockPath.join( mockOs.tmpdir( ), "testArtifacts.realm" ),
},
};
} );
// Mock the contexts so the useRealm hook will provide a single, custom copy
// of the Realm connection
jest.mock( "providers/contexts", ( ) => {
const originalModule = jest.requireActual( "providers/contexts" );
return {
__esModule: true,
...originalModule,
RealmContext: {
...originalModule.RealmContext,
useObject: ( type, uuid ) => global.realm?.objectForPrimaryKey( type, uuid ) ?? null,
useRealm: ( ) => global.realm,
useQuery: ( ) => [],
},
};
} );
// Open a realm connection and stuff it in global. If there's a better way to
// do this without having jest.mock complain about referring to variables out
// of scope, please propose it.
beforeAll( async ( ) => {
global.realm = await Realm.open( realmConfig );
} );
// Ensure the realm connection gets closed
afterAll( ( ) => {
global.realm?.close( );
Realm.shutdown();
} );