Skip to content

Commit 8ac0e5e

Browse files
fix: fall back to mmkv 3.1.1
1 parent 32f6b4b commit 8ac0e5e

File tree

3 files changed

+51
-84
lines changed

3 files changed

+51
-84
lines changed
Lines changed: 50 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,58 @@
1-
import { createMMKV } from "react-native-mmkv"
1+
// we always make sure 'react-native' gets included first
2+
// eslint-disable-next-line no-restricted-imports
3+
import * as ReactNative from "react-native"
24

3-
export const storage = createMMKV()
5+
import mockFile from "./mockFile"
46

5-
/**
6-
* Loads a string from storage.
7-
*
8-
* @param key The key to fetch.
9-
*/
10-
export function loadString(key: string): string | null {
11-
try {
12-
return storage.getString(key) ?? null
13-
} catch {
14-
// not sure why this would fail... even reading the RN docs I'm unclear
15-
return null
16-
}
17-
}
7+
// libraries to mock
8+
jest.doMock("react-native", () => {
9+
// Extend ReactNative
10+
return Object.setPrototypeOf(
11+
{
12+
Image: {
13+
...ReactNative.Image,
14+
resolveAssetSource: jest.fn((_source) => mockFile), // eslint-disable-line @typescript-eslint/no-unused-vars
15+
getSize: jest.fn(
16+
(
17+
uri: string, // eslint-disable-line @typescript-eslint/no-unused-vars
18+
success: (width: number, height: number) => void,
19+
failure?: (_error: any) => void, // eslint-disable-line @typescript-eslint/no-unused-vars
20+
) => success(100, 100),
21+
),
22+
},
23+
},
24+
ReactNative,
25+
)
26+
})
1827

19-
/**
20-
* Saves a string to storage.
21-
*
22-
* @param key The key to fetch.
23-
* @param value The value to store.
24-
*/
25-
export function saveString(key: string, value: string): boolean {
26-
try {
27-
storage.set(key, value)
28-
return true
29-
} catch {
30-
return false
31-
}
32-
}
28+
jest.mock("i18next", () => ({
29+
currentLocale: "en",
30+
t: (key: string, params: Record<string, string>) => {
31+
return `${key} ${JSON.stringify(params)}`
32+
},
33+
translate: (key: string, params: Record<string, string>) => {
34+
return `${key} ${JSON.stringify(params)}`
35+
},
36+
}))
3337

34-
/**
35-
* Loads something from storage and runs it thru JSON.parse.
36-
*
37-
* @param key The key to fetch.
38-
*/
39-
export function load<T>(key: string): T | null {
40-
let almostThere: string | null = null
41-
try {
42-
almostThere = loadString(key)
43-
return JSON.parse(almostThere ?? "") as T
44-
} catch {
45-
return (almostThere as T) ?? null
46-
}
47-
}
38+
jest.mock("expo-localization", () => ({
39+
...jest.requireActual("expo-localization"),
40+
getLocales: () => [{ languageTag: "en-US", textDirection: "ltr" }],
41+
}))
4842

49-
/**
50-
* Saves an object to storage.
51-
*
52-
* @param key The key to fetch.
53-
* @param value The value to store.
54-
*/
55-
export function save(key: string, value: unknown): boolean {
56-
try {
57-
saveString(key, JSON.stringify(value))
58-
return true
59-
} catch {
60-
return false
61-
}
62-
}
43+
jest.mock("../app/i18n/index.ts", () => ({
44+
i18n: {
45+
isInitialized: true,
46+
language: "en",
47+
t: (key: string, params: Record<string, string>) => {
48+
return `${key} ${JSON.stringify(params)}`
49+
},
50+
numberToCurrency: jest.fn(),
51+
},
52+
}))
6353

64-
/**
65-
* Removes something from storage.
66-
*
67-
* @param key The key to kill.
68-
*/
69-
export function remove(key: string): void {
70-
try {
71-
storage.remove(key)
72-
} catch {}
73-
}
54+
declare const tron // eslint-disable-line @typescript-eslint/no-unused-vars
7455

75-
/**
76-
* Burn it all to the ground.
77-
*/
78-
export function clear(): void {
79-
try {
80-
storage.clearAll()
81-
} catch {}
56+
declare global {
57+
let __TEST__: boolean
8258
}

boilerplate/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
"react-native-edge-to-edge": "~1.6.1",
5656
"react-native-gesture-handler": "~2.28.0",
5757
"react-native-keyboard-controller": "1.18.5",
58-
"react-native-mmkv": "4.0.0-beta.12",
58+
"react-native-mmkv": "3.1.1",
5959
"react-native-nitro-modules": "^0.29.0",
6060
"react-native-reanimated": "~4.1.1",
6161
"react-native-safe-area-context": "5.6.0",

boilerplate/test/setup.ts

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,6 @@ jest.mock("../app/i18n/index.ts", () => ({
5151
},
5252
}))
5353

54-
jest.mock("react-native-nitro-modules", () => ({
55-
createHybridObject: () => ({
56-
init: () => {},
57-
add: () => {},
58-
createHybridNavigationBar: () => {},
59-
createHybridStatusBar: () => ({}),
60-
}),
61-
}))
62-
6354
declare const tron // eslint-disable-line @typescript-eslint/no-unused-vars
6455

6556
declare global {

0 commit comments

Comments
 (0)