Files
iNaturalistReactNative/tests/realm.setup.js
Amanda Bullington 1a27ef68cb Upgrade to Realm 12 (#925)
* Update to realm 12

* Remove unused mocks to fix some tests

* Fix test by unmocking ObsEditProvider

* Fix more tests by removing mocks/changing when tests write to realm

* Remove mocks on ObsEdit component test and fix accessibility labels

* Fix hooks tests with realm

* Set test path for realm; finish fixing tests

* Small tweak

* Upgrade realm/react library

* Migrate schema
2023-11-22 13:01:59 -08:00

51 lines
1.6 KiB
JavaScript

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: "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,
useRealm: ( ) => global.realm
}
};
} );
// 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( );
} );