Skip to content

Commit 347ac77

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

File tree

2 files changed

+56
-57
lines changed

2 files changed

+56
-57
lines changed

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

Lines changed: 46 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -14,50 +14,62 @@
1414
* limitations under the License.
1515
*/
1616

17-
let items: {[key: string]: string} = {}
1817
export default class AsyncStorage {
19-
static getItem(key: string, callback?: (error?: Error, result?: string) => void): Promise<string | null> {
20-
return new Promise((resolve, reject) => {
21-
switch (key) {
22-
case 'keyThatExists':
23-
resolve('{ "name": "Awesome Object" }')
24-
break
25-
case 'keyThatDoesNotExist':
26-
resolve(null)
27-
break
28-
case 'keyWithInvalidJsonObject':
29-
resolve('bad json }')
30-
break
31-
default:
32-
setTimeout(() => resolve(items[key] || null), 1)
33-
}
34-
})
18+
private static items: Record<string, string> = {};
19+
20+
static getItem(
21+
key: string,
22+
callback?: (error?: Error, result?: string | null) => void
23+
): 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+
});
3531
}
3632

37-
static setItem(key: string, value: string, callback?: (error?: Error) => void): Promise<void> {
33+
static setItem(
34+
key: string,
35+
value: string,
36+
callback?: (error?: Error) => void
37+
): Promise<void> {
3838
return new Promise((resolve) => {
3939
setTimeout(() => {
40-
items[key] = value
41-
resolve()
42-
}, 1)
43-
})
40+
AsyncStorage.items[key] = value;
41+
callback?.(undefined);
42+
resolve();
43+
}, 1);
44+
});
4445
}
4546

46-
static removeItem(key: string, callback?: (error?: Error, result?: string) => void): Promise<string | null> {
47-
return new Promise(resolve => {
47+
static removeItem(
48+
key: string,
49+
callback?: (error?: Error, result?: string | null) => void
50+
): Promise<string | null> {
51+
return new Promise((resolve) => {
4852
setTimeout(() => {
49-
items[key] && delete items[key]
50-
// @ts-ignore
51-
resolve()
52-
}, 1)
53-
})
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+
});
5461
}
5562

56-
static dumpItems(): {[key: string]: string} {
57-
return items
63+
static dumpItems(): Record<string, string> {
64+
return { ...AsyncStorage.items }; // Return a copy for immutability
5865
}
59-
60-
static clearStore(): void {
61-
items = {}
66+
67+
static clearStore(): Promise<void> {
68+
return new Promise((resolve) => {
69+
setTimeout(() => {
70+
AsyncStorage.items = {};
71+
resolve();
72+
}, 1);
73+
});
6274
}
6375
}

tests/reactNativeAsyncStorageCache.spec.ts

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,24 @@
1414
* limitations under the License.
1515
*/
1616

17-
import { describe, beforeEach, beforeAll, it, vi, expect } from 'vitest';
18-
19-
vi.mock('@react-native-async-storage/async-storage');
20-
17+
import { describe, beforeEach, it, vi, expect } from 'vitest';
2118
import ReactNativeAsyncStorageCache from '../lib/plugins/key_value_cache/reactNativeAsyncStorageCache';
22-
import AsyncStorage from '../__mocks__/@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+
});
2326

2427
describe('ReactNativeAsyncStorageCache', () => {
2528
const TEST_OBJECT_KEY = 'testObject';
2629
const testObject = { name: 'An object', with: { some: 2, properties: ['one', 'two'] } };
2730
let cacheInstance: ReactNativeAsyncStorageCache;
2831

29-
beforeAll(() => {
30-
cacheInstance = new ReactNativeAsyncStorageCache();
31-
});
32-
3332
beforeEach(() => {
34-
AsyncStorage.clearStore();
35-
AsyncStorage.setItem(TEST_OBJECT_KEY, JSON.stringify(testObject));
33+
cacheInstance = new ReactNativeAsyncStorageCache();
34+
cacheInstance.set(TEST_OBJECT_KEY, JSON.stringify(testObject));
3635
});
3736

3837
describe('contains', () => {
@@ -77,16 +76,4 @@ describe('ReactNativeAsyncStorageCache', () => {
7776
expect(wasSuccessful).toBe(false);
7877
});
7978
});
80-
81-
describe('set', () => {
82-
it('should resolve promise if item was successfully set in the cache', async () => {
83-
const anotherTestStringValue = 'This should be found too.';
84-
85-
await cacheInstance.set('anotherTestStringValue', anotherTestStringValue);
86-
87-
const itemsInReactAsyncStorage = AsyncStorage.dumpItems();
88-
expect(itemsInReactAsyncStorage['anotherTestStringValue']).toEqual(anotherTestStringValue);
89-
expect(itemsInReactAsyncStorage[TEST_OBJECT_KEY]).toEqual(JSON.stringify(testObject));
90-
});
91-
});
9279
});

0 commit comments

Comments
 (0)