forked from obytes/react-native-template-obytes
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathreact-native-mmkv.ts
More file actions
49 lines (44 loc) · 1.43 KB
/
react-native-mmkv.ts
File metadata and controls
49 lines (44 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
const sharedMockStorage = new Map<string, string>();
const createMockStorage = () => {
const mockGetString = jest.fn((key: string): string | undefined =>
sharedMockStorage.get(key),
);
const mockSet = jest.fn((key: string, value: string): void => {
sharedMockStorage.set(key, value);
});
const mockRemove = jest.fn((key: string): void => {
sharedMockStorage.delete(key);
});
return {
getString: mockGetString,
set: mockSet,
remove: mockRemove,
clearAll: jest.fn((): void => {
sharedMockStorage.clear();
}),
getAllKeys: jest.fn(
(): Array<string> => Array.from(sharedMockStorage.keys()),
),
contains: jest.fn((key: string): boolean => sharedMockStorage.has(key)),
getNumber: jest.fn((key: string): number | undefined => {
const value = sharedMockStorage.get(key);
return value ? Number(value) : undefined;
}),
getBoolean: jest.fn((key: string): boolean | undefined => {
const value = sharedMockStorage.get(key);
if (value === undefined) {
return undefined;
}
return value === 'true';
}),
setNumber: jest.fn((key: string, value: number): void => {
sharedMockStorage.set(key, String(value));
}),
setBoolean: jest.fn((key: string, value: boolean): void => {
sharedMockStorage.set(key, String(value));
}),
};
};
export function createMMKV(_options?: { id?: string }) {
return createMockStorage();
}