|
1 |
| - |
2 | 1 | /**
|
3 | 2 | * Copyright 2022-2024, Optimizely
|
4 | 3 | *
|
|
15 | 14 | * limitations under the License.
|
16 | 15 | */
|
17 | 16 |
|
18 |
| -vi.mock('@react-native-async-storage/async-storage', () => { |
19 |
| - const MockAsyncStorage = { |
20 |
| - data: new Map<string, any>(), |
21 |
| - async setItem(key: string, value: string) { |
22 |
| - this.data.set(key, value); |
23 |
| - }, |
24 |
| - async getItem(key: string) { |
25 |
| - return this.data.get(key) || null; |
26 |
| - }, |
27 |
| - async removeItem(key: string) { |
28 |
| - this.data.delete(key); |
29 |
| - }, |
30 |
| - async getAllKeys() { |
31 |
| - return Array.from(this.data.keys()); |
32 |
| - }, |
33 |
| - async clear() { |
34 |
| - this.data.clear(); |
35 |
| - }, |
36 |
| - async multiGet(keys: string[]) { |
37 |
| - return keys.map(key => [key, this.data.get(key)]); |
38 |
| - }, |
39 |
| - } |
40 |
| - return { default: MockAsyncStorage }; |
41 |
| -}); |
42 |
| - |
43 |
| -import { vi, describe, it, expect, beforeEach } from 'vitest'; |
| 17 | +import { vi, describe, it, expect } from 'vitest'; |
44 | 18 | import { AsyncStorageCache } from './async_storage_cache.react_native';
|
45 |
| -import AsyncStorage from '@react-native-async-storage/async-storage'; |
| 19 | +import AsyncStorage from '../../../__mocks__/@react-native-async-storage/async-storage'; |
| 20 | + |
| 21 | +vi.mock('../lib/utils/import.react_native/@react-native-async-storage/async-storage', () => { |
| 22 | + return { |
| 23 | + getDefaultAsyncStorage: () => AsyncStorage, |
| 24 | + }; |
| 25 | +}); |
46 | 26 |
|
47 | 27 | type TestData = {
|
48 | 28 | a: number;
|
49 | 29 | b: string;
|
50 | 30 | d: { e: boolean };
|
51 |
| -} |
52 |
| - |
| 31 | +}; |
53 | 32 |
|
54 | 33 | describe('AsyncStorageCache', () => {
|
55 |
| - beforeEach(async () => { |
56 |
| - await AsyncStorage.clear(); |
57 |
| - }); |
58 |
| - |
59 |
| - it('should store a stringified value in asyncstorage', async () => { |
| 34 | + it('should store a stringified value in asyncstorag', async () => { |
60 | 35 | const cache = new AsyncStorageCache<TestData>();
|
| 36 | + |
61 | 37 | const data = { a: 1, b: '2', d: { e: true } };
|
62 | 38 | await cache.set('key', data);
|
63 |
| - expect(await AsyncStorage.getItem('key')).toBe(JSON.stringify(data)); |
| 39 | + |
| 40 | + // eslint-disable-next-line @typescript-eslint/ban-ts-comment |
| 41 | + // @ts-ignore |
| 42 | + expect(await cache.asyncStorage.getItem('key')).toBe(JSON.stringify(data)); |
| 43 | + expect(await cache.get('key')).toEqual(data); |
64 | 44 | });
|
65 | 45 |
|
66 | 46 | it('should return undefined if get is called for a nonexistent key', async () => {
|
67 | 47 | const cache = new AsyncStorageCache<string>();
|
| 48 | + |
68 | 49 | expect(await cache.get('nonexistent')).toBeUndefined();
|
69 | 50 | });
|
70 | 51 |
|
71 | 52 | it('should return the value if get is called for an existing key', async () => {
|
72 | 53 | const cache = new AsyncStorageCache<string>();
|
73 | 54 | await cache.set('key', 'value');
|
| 55 | + |
74 | 56 | expect(await cache.get('key')).toBe('value');
|
75 | 57 | });
|
76 | 58 |
|
77 | 59 | it('should return the value after json parsing if get is called for an existing key', async () => {
|
78 | 60 | const cache = new AsyncStorageCache<TestData>();
|
79 | 61 | const data = { a: 1, b: '2', d: { e: true } };
|
80 | 62 | await cache.set('key', data);
|
| 63 | + |
81 | 64 | expect(await cache.get('key')).toEqual(data);
|
82 | 65 | });
|
83 | 66 |
|
84 | 67 | it('should remove the key from async storage when remove is called', async () => {
|
85 | 68 | const cache = new AsyncStorageCache<string>();
|
86 | 69 | await cache.set('key', 'value');
|
87 | 70 | await cache.remove('key');
|
88 |
| - expect(await AsyncStorage.getItem('key')).toBeNull(); |
| 71 | + |
| 72 | + // eslint-disable-next-line @typescript-eslint/ban-ts-comment |
| 73 | + // @ts-ignore |
| 74 | + expect(await cache.asyncStorage.getItem('key')).toBeNull(); |
89 | 75 | });
|
90 | 76 |
|
91 | 77 | it('should remove all keys from async storage when clear is called', async () => {
|
92 | 78 | const cache = new AsyncStorageCache<string>();
|
93 | 79 | await cache.set('key1', 'value1');
|
94 | 80 | await cache.set('key2', 'value2');
|
95 |
| - expect((await AsyncStorage.getAllKeys()).length).toBe(2); |
| 81 | + |
| 82 | + // eslint-disable-next-line @typescript-eslint/ban-ts-comment |
| 83 | + // @ts-ignore |
| 84 | + expect((await cache.asyncStorage.getAllKeys()).length).toBe(2); |
96 | 85 | cache.clear();
|
97 |
| - expect((await AsyncStorage.getAllKeys()).length).toBe(0); |
| 86 | + // eslint-disable-next-line @typescript-eslint/ban-ts-comment |
| 87 | + // @ts-ignore |
| 88 | + expect((await cache.asyncStorage.getAllKeys()).length).toBe(0); |
98 | 89 | });
|
99 | 90 |
|
100 | 91 | it('should return all keys when getKeys is called', async () => {
|
101 | 92 | const cache = new AsyncStorageCache<string>();
|
102 | 93 | await cache.set('key1', 'value1');
|
103 | 94 | await cache.set('key2', 'value2');
|
| 95 | + |
104 | 96 | expect(await cache.getKeys()).toEqual(['key1', 'key2']);
|
105 | 97 | });
|
106 | 98 |
|
107 | 99 | it('should return an array of values for an array of keys when getBatched is called', async () => {
|
108 | 100 | const cache = new AsyncStorageCache<string>();
|
109 | 101 | await cache.set('key1', 'value1');
|
110 | 102 | await cache.set('key2', 'value2');
|
| 103 | + |
111 | 104 | expect(await cache.getBatched(['key1', 'key2'])).toEqual(['value1', 'value2']);
|
112 | 105 | });
|
113 | 106 | });
|
0 commit comments