Skip to content

Commit 794c9c7

Browse files
[FSSDK-10936] test addition
1 parent 07052b6 commit 794c9c7

File tree

2 files changed

+84
-7
lines changed

2 files changed

+84
-7
lines changed

lib/project_config/config_manager_factory.react_native.spec.ts

Lines changed: 83 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,26 @@
1616

1717
import { describe, it, expect, beforeEach, vi } from 'vitest';
1818

19+
await vi.hoisted(async () => {
20+
await mockRequireNetInfo();
21+
});
22+
23+
let isAsyncStorageAvailable = true;
24+
25+
async function mockRequireNetInfo() {
26+
const { Module } = await import('module');
27+
const M: any = Module;
28+
29+
M._load_original = M._load;
30+
M._load = (uri: string, parent: string) => {
31+
if (uri === '@react-native-async-storage/async-storage') {
32+
if (isAsyncStorageAvailable) return {};
33+
throw new Error('Module not found: @react-native-async-storage/async-storage');
34+
}
35+
return M._load_original(uri, parent);
36+
};
37+
}
38+
1939
vi.mock('./config_manager_factory', () => {
2040
return {
2141
getPollingConfigManager: vi.fn().mockReturnValueOnce({ foo: 'bar' }),
@@ -29,10 +49,10 @@ vi.mock('../utils/http_request_handler/browser_request_handler', () => {
2949

3050
vi.mock('../plugins/key_value_cache/reactNativeAsyncStorageCache', () => {
3151
const ReactNativeAsyncStorageCache = vi.fn();
32-
return { 'default': ReactNativeAsyncStorageCache };
52+
return { default: ReactNativeAsyncStorageCache };
3353
});
3454

35-
import { getPollingConfigManager, PollingConfigManagerConfig, PollingConfigManagerFactoryOptions } from './config_manager_factory';
55+
import { getPollingConfigManager, PollingConfigManagerConfig } from './config_manager_factory';
3656
import { createPollingProjectConfigManager } from './config_manager_factory.react_native';
3757
import { BrowserRequestHandler } from '../utils/http_request_handler/browser_request_handler';
3858
import ReactNativeAsyncStorageCache from '../plugins/key_value_cache/reactNativeAsyncStorageCache';
@@ -63,7 +83,12 @@ describe('createPollingConfigManager', () => {
6383
};
6484

6585
const projectConfigManager = createPollingProjectConfigManager(config);
66-
expect(Object.is(mockGetPollingConfigManager.mock.calls[0][0].requestHandler, MockBrowserRequestHandler.mock.instances[0])).toBe(true);
86+
expect(
87+
Object.is(
88+
mockGetPollingConfigManager.mock.calls[0][0].requestHandler,
89+
MockBrowserRequestHandler.mock.instances[0]
90+
)
91+
).toBe(true);
6792
});
6893

6994
it('uses uses autoUpdate = true by default', () => {
@@ -81,7 +106,9 @@ describe('createPollingConfigManager', () => {
81106
};
82107

83108
const projectConfigManager = createPollingProjectConfigManager(config);
84-
expect(Object.is(mockGetPollingConfigManager.mock.calls[0][0].cache, MockReactNativeAsyncStorageCache.mock.instances[0])).toBe(true);
109+
expect(
110+
Object.is(mockGetPollingConfigManager.mock.calls[0][0].cache, MockReactNativeAsyncStorageCache.mock.instances[0])
111+
).toBe(true);
85112
});
86113

87114
it('uses the provided options', () => {
@@ -98,5 +125,56 @@ describe('createPollingConfigManager', () => {
98125

99126
const projectConfigManager = createPollingProjectConfigManager(config);
100127
expect(mockGetPollingConfigManager).toHaveBeenNthCalledWith(1, expect.objectContaining(config));
101-
});
128+
});
129+
130+
it('Should not throw error if a cache is present in the config, and async storage is not available', async () => {
131+
isAsyncStorageAvailable = false;
132+
const { getPollingConfigManager } = await vi.importActual<typeof import('./config_manager_factory')>(
133+
'./config_manager_factory'
134+
);
135+
const { default: ReactNativeAsyncStorageCache } = await vi.importActual<
136+
typeof import('../plugins/key_value_cache/reactNativeAsyncStorageCache')
137+
>('../plugins/key_value_cache/reactNativeAsyncStorageCache');
138+
const config = {
139+
sdkKey: 'sdkKey',
140+
requestHandler: { makeRequest: vi.fn() },
141+
cache: { get: vi.fn(), set: vi.fn(), contains: vi.fn(), remove: vi.fn() },
142+
};
143+
144+
mockGetPollingConfigManager.mockImplementationOnce(() => {
145+
return getPollingConfigManager(config);
146+
});
147+
148+
MockReactNativeAsyncStorageCache.mockImplementationOnce(() => {
149+
return new ReactNativeAsyncStorageCache();
150+
});
151+
152+
expect(() => createPollingProjectConfigManager(config)).not.toThrow();
153+
});
154+
155+
it('should throw an error if cache is not present in the config, and async storage is not available', async () => {
156+
isAsyncStorageAvailable = false;
157+
const { getPollingConfigManager } = await vi.importActual<typeof import('./config_manager_factory')>(
158+
'./config_manager_factory'
159+
);
160+
const { default: ReactNativeAsyncStorageCache } = await vi.importActual<
161+
typeof import('../plugins/key_value_cache/reactNativeAsyncStorageCache')
162+
>('../plugins/key_value_cache/reactNativeAsyncStorageCache');
163+
const config = {
164+
sdkKey: 'sdkKey',
165+
requestHandler: { makeRequest: vi.fn() },
166+
};
167+
168+
mockGetPollingConfigManager.mockImplementationOnce(() => {
169+
return getPollingConfigManager(config);
170+
});
171+
172+
MockReactNativeAsyncStorageCache.mockImplementationOnce(() => {
173+
return new ReactNativeAsyncStorageCache();
174+
});
175+
176+
expect(() => createPollingProjectConfigManager(config)).toThrowError(
177+
'Module not found: @react-native-async-storage/async-storage'
178+
);
179+
});
102180
});

lib/utils/import.react_native/@react-native-async-storage/async-storage.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ export const getDefaultAsyncStorage = (): AsyncStorageStatic => {
2121
// eslint-disable-next-line @typescript-eslint/no-var-requires
2222
return require('@react-native-async-storage/async-storage').default;
2323
} catch (e) {
24-
// Better error message than unknown module not found
25-
throw new Error('@react-native-async-storage/async-storage is not available');
24+
throw new Error('Module not found: @react-native-async-storage/async-storage');
2625
}
2726
};

0 commit comments

Comments
 (0)