-
Notifications
You must be signed in to change notification settings - Fork 86
Expand file tree
/
Copy pathconfig_manager_factory.react_native.spec.ts
More file actions
166 lines (136 loc) · 5.51 KB
/
config_manager_factory.react_native.spec.ts
File metadata and controls
166 lines (136 loc) · 5.51 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/**
* Copyright 2024-2025, Optimizely
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { describe, it, expect, beforeEach, vi } from 'vitest';
await vi.hoisted(async () => {
await mockRequireAsyncStorage();
});
let isAsyncStorageAvailable = true;
async function mockRequireAsyncStorage() {
const { Module } = await import('module');
const M: any = Module;
M._load_original = M._load;
M._load = (uri: string, parent: string) => {
if (uri === '@react-native-async-storage/async-storage') {
if (isAsyncStorageAvailable) return { default: {} };
throw new Error("Module not found: @react-native-async-storage/async-storage");
}
return M._load_original(uri, parent);
};
}
vi.mock('./config_manager_factory', () => {
return {
getPollingConfigManager: vi.fn().mockReturnValueOnce({ foo: 'bar' }),
getOpaquePollingConfigManager: vi.fn().mockRejectedValueOnce({ foo: 'bar' }),
};
});
vi.mock('../utils/http_request_handler/request_handler.browser', () => {
const BrowserRequestHandler = vi.fn();
return { BrowserRequestHandler };
});
vi.mock('../utils/cache/async_storage_cache.react_native', async (importOriginal) => {
const original: any = await importOriginal();
const OriginalAsyncStorageCache = original.AsyncStorageCache;
const MockAsyncStorageCache = vi.fn().mockImplementation(function (this: any, ...args) {
Object.setPrototypeOf(this, new OriginalAsyncStorageCache(...args));
});
return { AsyncStorageCache: MockAsyncStorageCache };
});
import { getOpaquePollingConfigManager, getPollingConfigManager, PollingConfigManagerConfig } from './config_manager_factory';
import { createPollingProjectConfigManager } from './config_manager_factory.react_native';
import { BrowserRequestHandler } from '../utils/http_request_handler/request_handler.browser';
import { AsyncStorageCache } from '../utils/cache/async_storage_cache.react_native';
import { getMockSyncCache } from '../tests/mock/mock_cache';
describe('createPollingConfigManager', () => {
const mockGetOpaquePollingConfigManager = vi.mocked(getOpaquePollingConfigManager);
const MockBrowserRequestHandler = vi.mocked(BrowserRequestHandler);
const MockAsyncStorageCache = vi.mocked(AsyncStorageCache);
beforeEach(() => {
mockGetOpaquePollingConfigManager.mockClear();
MockBrowserRequestHandler.mockClear();
MockAsyncStorageCache.mockClear();
});
it('creates and returns the instance by calling getPollingConfigManager', () => {
const config = {
sdkKey: 'sdkKey',
};
const projectConfigManager = createPollingProjectConfigManager(config);
expect(Object.is(projectConfigManager, mockGetOpaquePollingConfigManager.mock.results[0].value)).toBe(true);
});
it('uses an instance of BrowserRequestHandler as requestHandler', () => {
const config = {
sdkKey: 'sdkKey',
};
createPollingProjectConfigManager(config);
expect(
Object.is(
mockGetOpaquePollingConfigManager.mock.calls[0][0].requestHandler,
MockBrowserRequestHandler.mock.instances[0]
)
).toBe(true);
});
it('uses uses autoUpdate = true by default', () => {
const config = {
sdkKey: 'sdkKey',
};
createPollingProjectConfigManager(config);
expect(mockGetOpaquePollingConfigManager.mock.calls[0][0].autoUpdate).toBe(true);
});
it('uses an instance of ReactNativeAsyncStorageCache for caching by default', () => {
const config = {
sdkKey: 'sdkKey',
};
createPollingProjectConfigManager(config);
expect(
Object.is(mockGetOpaquePollingConfigManager.mock.calls[0][0].cache, MockAsyncStorageCache.mock.instances[0])
).toBe(true);
});
it('uses the provided options', () => {
const config: PollingConfigManagerConfig = {
datafile: '{}',
jsonSchemaValidator: vi.fn(),
sdkKey: 'sdkKey',
updateInterval: 50000,
autoUpdate: false,
urlTemplate: 'urlTemplate',
datafileAccessToken: 'datafileAccessToken',
customHeaders: { 'X-Test-Header': 'test-value' },
cache: getMockSyncCache(),
};
createPollingProjectConfigManager(config);
expect(mockGetOpaquePollingConfigManager).toHaveBeenNthCalledWith(1, expect.objectContaining(config));
});
it('Should not throw error if a cache is present in the config, and async storage is not available', async () => {
isAsyncStorageAvailable = false;
const config = {
sdkKey: 'sdkKey',
requestHandler: { makeRequest: vi.fn() },
cache: getMockSyncCache<string>(),
};
expect(() => createPollingProjectConfigManager(config)).not.toThrow();
isAsyncStorageAvailable = true;
});
it('should throw an error if cache is not present in the config, and async storage is not available', async () => {
isAsyncStorageAvailable = false;
const config = {
sdkKey: 'sdkKey',
requestHandler: { makeRequest: vi.fn() },
};
expect(() => createPollingProjectConfigManager(config)).toThrowError(
"Module not found: @react-native-async-storage/async-storage"
);
isAsyncStorageAvailable = true;
});
});