Skip to content

Commit 07052b6

Browse files
[FSSDK-10936] test adjustment
1 parent 347ac77 commit 07052b6

File tree

2 files changed

+50
-76
lines changed

2 files changed

+50
-76
lines changed

__mocks__/@react-native-async-storage/async-storage.ts

Lines changed: 18 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -21,55 +21,36 @@ export default class AsyncStorage {
2121
key: string,
2222
callback?: (error?: Error, result?: string | null) => void
2323
): Promise<string | null> {
24-
return new Promise((resolve) => {
25-
setTimeout(() => {
26-
const value = AsyncStorage.items[key] || null;
27-
callback?.(undefined, value);
28-
resolve(value);
29-
}, 1);
30-
});
24+
const value = AsyncStorage.items[key] || null;
25+
callback?.(undefined, value);
26+
return Promise.resolve(value);
3127
}
32-
28+
3329
static setItem(
3430
key: string,
3531
value: string,
3632
callback?: (error?: Error) => void
3733
): Promise<void> {
38-
return new Promise((resolve) => {
39-
setTimeout(() => {
40-
AsyncStorage.items[key] = value;
41-
callback?.(undefined);
42-
resolve();
43-
}, 1);
44-
});
34+
AsyncStorage.items[key] = value;
35+
callback?.(undefined);
36+
return Promise.resolve();
4537
}
46-
38+
4739
static removeItem(
4840
key: string,
4941
callback?: (error?: Error, result?: string | null) => void
5042
): Promise<string | null> {
51-
return new Promise((resolve) => {
52-
setTimeout(() => {
53-
const value = AsyncStorage.items[key] || null;
54-
if (key in AsyncStorage.items) {
55-
delete AsyncStorage.items[key];
56-
}
57-
callback?.(undefined, value);
58-
resolve(value);
59-
}, 1);
60-
});
43+
const value = AsyncStorage.items[key] || null;
44+
if (key in AsyncStorage.items) {
45+
delete AsyncStorage.items[key];
46+
}
47+
callback?.(undefined, value);
48+
return Promise.resolve(value);
6149
}
62-
63-
static dumpItems(): Record<string, string> {
64-
return { ...AsyncStorage.items }; // Return a copy for immutability
65-
}
66-
50+
6751
static clearStore(): Promise<void> {
68-
return new Promise((resolve) => {
69-
setTimeout(() => {
70-
AsyncStorage.items = {};
71-
resolve();
72-
}, 1);
73-
});
52+
AsyncStorage.items = {};
53+
return Promise.resolve();
7454
}
55+
7556
}
Lines changed: 32 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
/**
32
* Copyright 2022-2024, Optimizely
43
*
@@ -15,99 +14,93 @@
1514
* limitations under the License.
1615
*/
1716

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';
4418
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+
});
4626

4727
type TestData = {
4828
a: number;
4929
b: string;
5030
d: { e: boolean };
51-
}
52-
31+
};
5332

5433
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 () => {
6035
const cache = new AsyncStorageCache<TestData>();
36+
6137
const data = { a: 1, b: '2', d: { e: true } };
6238
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);
6444
});
6545

6646
it('should return undefined if get is called for a nonexistent key', async () => {
6747
const cache = new AsyncStorageCache<string>();
48+
6849
expect(await cache.get('nonexistent')).toBeUndefined();
6950
});
7051

7152
it('should return the value if get is called for an existing key', async () => {
7253
const cache = new AsyncStorageCache<string>();
7354
await cache.set('key', 'value');
55+
7456
expect(await cache.get('key')).toBe('value');
7557
});
7658

7759
it('should return the value after json parsing if get is called for an existing key', async () => {
7860
const cache = new AsyncStorageCache<TestData>();
7961
const data = { a: 1, b: '2', d: { e: true } };
8062
await cache.set('key', data);
63+
8164
expect(await cache.get('key')).toEqual(data);
8265
});
8366

8467
it('should remove the key from async storage when remove is called', async () => {
8568
const cache = new AsyncStorageCache<string>();
8669
await cache.set('key', 'value');
8770
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();
8975
});
9076

9177
it('should remove all keys from async storage when clear is called', async () => {
9278
const cache = new AsyncStorageCache<string>();
9379
await cache.set('key1', 'value1');
9480
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);
9685
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);
9889
});
9990

10091
it('should return all keys when getKeys is called', async () => {
10192
const cache = new AsyncStorageCache<string>();
10293
await cache.set('key1', 'value1');
10394
await cache.set('key2', 'value2');
95+
10496
expect(await cache.getKeys()).toEqual(['key1', 'key2']);
10597
});
10698

10799
it('should return an array of values for an array of keys when getBatched is called', async () => {
108100
const cache = new AsyncStorageCache<string>();
109101
await cache.set('key1', 'value1');
110102
await cache.set('key2', 'value2');
103+
111104
expect(await cache.getBatched(['key1', 'key2'])).toEqual(['value1', 'value2']);
112105
});
113106
});

0 commit comments

Comments
 (0)