|
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" |
2 | 4 |
|
3 | | -export const storage = createMMKV() |
| 5 | +import mockFile from "./mockFile" |
4 | 6 |
|
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 | +}) |
18 | 27 |
|
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 | +})) |
33 | 37 |
|
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 | +})) |
48 | 42 |
|
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 | +})) |
63 | 53 |
|
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 |
74 | 55 |
|
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 |
82 | 58 | } |
0 commit comments