From 9812061008d10f2f7b5622da031c4712b3970eee Mon Sep 17 00:00:00 2001 From: Chris Kapinga Date: Mon, 11 Mar 2024 23:50:07 +0100 Subject: [PATCH 1/2] refactor: use .mock as intended; remove platformChecker .mock files are supposed to replace their regular counterparts when running in mock mode. It doesn't make much sense to import from both and have a check to determine which to use. Instead, metro should pick the one that is currently relevant. This is achieved by giving the functions the same name and only importing from the regular one. the .mock one is used when running in mock mode. --- src/MMKV.ts | 10 +++------- src/PlatformChecker.ts | 7 ------- src/createMMKV.mock.ts | 3 ++- 3 files changed, 5 insertions(+), 15 deletions(-) delete mode 100644 src/PlatformChecker.ts diff --git a/src/MMKV.ts b/src/MMKV.ts index 973a5e74..e8b81fa2 100644 --- a/src/MMKV.ts +++ b/src/MMKV.ts @@ -1,6 +1,4 @@ import { createMMKV } from './createMMKV'; -import { createMockMMKV } from './createMMKV.mock'; -import { isJest } from './PlatformChecker'; interface Listener { remove: () => void; @@ -29,8 +27,8 @@ export interface MMKVConfiguration { * ```ts * const temporaryStorage = new MMKV({ path: '/tmp/' }) * ``` - * - * _Notice_: On iOS you can set the AppGroup bundle property to share the same storage between your app and its extensions. + * + * _Notice_: On iOS you can set the AppGroup bundle property to share the same storage between your app and its extensions. * In this case `path` property will be ignored. * See more on MMKV configuration [here](https://github.com/Tencent/MMKV/wiki/iOS_tutorial#configuration). */ @@ -147,9 +145,7 @@ export class MMKV implements MMKVInterface { */ constructor(configuration: MMKVConfiguration = { id: 'mmkv.default' }) { this.id = configuration.id; - this.nativeInstance = isJest() - ? createMockMMKV() - : createMMKV(configuration); + this.nativeInstance = createMMKV(configuration); this.functionCache = {}; } diff --git a/src/PlatformChecker.ts b/src/PlatformChecker.ts deleted file mode 100644 index 94d4d686..00000000 --- a/src/PlatformChecker.ts +++ /dev/null @@ -1,7 +0,0 @@ -export function isJest(): boolean { - if (global.process == null) { - // In a WebBrowser/Electron the `process` variable does not exist - return false; - } - return process.env.JEST_WORKER_ID != null; -} diff --git a/src/createMMKV.mock.ts b/src/createMMKV.mock.ts index c81bb571..223598b8 100644 --- a/src/createMMKV.mock.ts +++ b/src/createMMKV.mock.ts @@ -1,7 +1,8 @@ import type { NativeMMKV } from 'react-native-mmkv'; +import { MMKVConfiguration } from 'react-native-mmkv'; /* Mock MMKV instance for use in tests */ -export const createMockMMKV = (): NativeMMKV => { +export const createMMKV = (_: MMKVConfiguration): NativeMMKV => { const storage = new Map(); return { From 640ec5b3d520e208acfbe09063d4429cc071fa22 Mon Sep 17 00:00:00 2001 From: Chris Kapinga Date: Tue, 12 Mar 2024 00:29:59 +0100 Subject: [PATCH 2/2] fix: add mock for createMMKV in unit test jest doesn't pick the .mock file by default. adding jest.mock in the test file solves this. If there are more testFiles, the mock can be made globally in a jest.setup --- test/hooks.test.tsx | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/hooks.test.tsx b/test/hooks.test.tsx index e24abd16..25bb7be5 100644 --- a/test/hooks.test.tsx +++ b/test/hooks.test.tsx @@ -9,6 +9,8 @@ import { } from '@testing-library/react-native'; import { MMKV, useMMKVNumber, useMMKVString } from '../src'; +jest.mock('../src/createMMKV', () => require('../src/createMMKV.mock.ts')); + const mmkv = new MMKV(); beforeEach(() => {