Files
iNaturalistReactNative/tests/helpers/uniqueRealm.js
T
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

69 lines
2.3 KiB
JavaScript

import os from "os";
import path from "path";
import Realm from "realm";
import realmConfig from "realmModels/index";
// Named export usable inside jest.mock factories via jest.requireActual.
// Mock factories run at module-load time (before setupUniqueRealm is called),
// so the hooks must be built lazily from __filename rather than from a
// variable captured at factory registration time.
export function makeRealmHooks( realmIdentifier ) {
return {
useRealm: ( ) => global.mockRealms[realmIdentifier],
useQuery: typeOrConfig => {
const realm = global.mockRealms[realmIdentifier];
if ( !realm || realm.isClosed ) return [];
if ( typeOrConfig && typeof typeOrConfig === "object" && typeOrConfig.type ) {
const { type, query: configQuery } = typeOrConfig;
const results = realm.objects( type );
return configQuery
? configQuery( results )
: results;
}
return [];
},
useObject: ( type, primaryKey ) => (
global.mockRealms[realmIdentifier]?.objectForPrimaryKey( type, primaryKey ) ?? null
),
};
}
export default function setupUniqueRealm( realmIdentifier ) {
const mockRealmConfig = {
schema: realmConfig.schema,
schemaVersion: realmConfig.schemaVersion,
// No need to actually write to disk
inMemory: true,
// For an in memory db path is basically a unique identifier, *but* Realm
// may still write some metadata to disk, so this needs to be a real, but
// temporary, path. In theory this should prevent this test from
// interacting with other tests
path: path.join( os.tmpdir( ), `${realmIdentifier}.realm` ),
};
// Mock config so that all code that runs during this test talks to the same
// database
const mockRealmModelsIndex = {
__esModule: true,
default: mockRealmConfig,
};
const uniqueRealmBeforeAll = async ( ) => {
global.mockRealms = global.mockRealms || {};
global.mockRealms[realmIdentifier] = await Realm.open( mockRealmConfig );
};
// Ensure the realm connection gets closed
const uniqueRealmAfterAll = ( ) => {
global.mockRealms[realmIdentifier]?.close( );
jest.clearAllMocks( );
};
return {
mockRealmConfig,
mockRealmModelsIndex,
uniqueRealmBeforeAll,
uniqueRealmAfterAll,
};
}