Skip to content

Commit 10e2e87

Browse files
test: fix mmkv mock
1 parent 1ab22ab commit 10e2e87

File tree

3 files changed

+39
-12
lines changed

3 files changed

+39
-12
lines changed

__mocks__/react-native-mmkv.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
const sharedMockStorage = new Map<string, string>();
2+
3+
const createMockStorage = () => ({
4+
getString: (key: string): string | undefined => sharedMockStorage.get(key),
5+
set: (key: string, value: string): void => {
6+
sharedMockStorage.set(key, value);
7+
},
8+
remove: (key: string): void => {
9+
sharedMockStorage.delete(key);
10+
},
11+
clearAll: (): void => {
12+
sharedMockStorage.clear();
13+
},
14+
getAllKeys: (): Array<string> => Array.from(sharedMockStorage.keys()),
15+
contains: (key: string): boolean => sharedMockStorage.has(key),
16+
getNumber: (key: string): number | undefined => {
17+
const value = sharedMockStorage.get(key);
18+
return value ? Number(value) : undefined;
19+
},
20+
getBoolean: (key: string): boolean | undefined => {
21+
const value = sharedMockStorage.get(key);
22+
if (value === undefined) {
23+
return undefined;
24+
}
25+
return value === 'true';
26+
},
27+
setNumber: (key: string, value: number): void => {
28+
sharedMockStorage.set(key, String(value));
29+
},
30+
setBoolean: (key: string, value: boolean): void => {
31+
sharedMockStorage.set(key, String(value));
32+
},
33+
});
34+
35+
export function createMMKV(_options?: { id?: string }) {
36+
return createMockStorage();
37+
}

jest-setup.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,5 @@ import '@testing-library/react-native/extend-expect';
55
global.window = {};
66
// @ts-ignore
77
global.window = global;
8+
9+
jest.mock('react-native-mmkv');

src/components/providers/auth.test.tsx

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,6 @@ import {
1313
useAuth,
1414
} from './auth';
1515

16-
// Mock MMKV Storage
17-
jest.mock('react-native-mmkv', () => {
18-
const mockStorage = new Map();
19-
return {
20-
MMKV: jest.fn().mockImplementation(() => ({
21-
set: (key: string, value: string) => mockStorage.set(key, value),
22-
getString: (key: string) => mockStorage.get(key) ?? null,
23-
delete: (key: string) => mockStorage.delete(key),
24-
})),
25-
};
26-
});
27-
2816
// Mock API client interceptors
2917
jest.mock('@/api', () => ({
3018
client: {

0 commit comments

Comments
 (0)