Skip to content

Commit 8ae1ccd

Browse files
Hugo PerezHugo Perez
authored andcommitted
refactor(react-native): simplify config provider logic by removing AsyncStorage caching
Prioritize environment variables over AsyncStorage and remove redundant tests. This makes the configuration behavior more predictable and reduces complexity.
1 parent 9ca97f9 commit 8ae1ccd

File tree

2 files changed

+26
-67
lines changed

2 files changed

+26
-67
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@sovereign-net/api-client",
3-
"version": "1.1.4",
3+
"version": "1.1.5",
44
"type": "module",
55
"description": "Platform-agnostic API client for Sovereign Network ZHTP nodes",
66
"main": "./dist/vanilla-js/index.js",

src/react-native/config-provider.test.ts

Lines changed: 25 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,6 @@ describe('ReactNativeConfigProvider', () => {
2020
});
2121

2222
describe('getConfig()', () => {
23-
it('should load config from AsyncStorage if available', async () => {
24-
const mockAsyncStorage = {
25-
getItem: vi.fn().mockResolvedValue(JSON.stringify(mockConfig)),
26-
setItem: vi.fn().mockResolvedValue(undefined),
27-
};
28-
29-
provider = new ReactNativeConfigProvider(undefined, mockAsyncStorage);
30-
const result = await provider.getConfig();
31-
32-
expect(result).toEqual(mockConfig);
33-
expect(mockAsyncStorage.getItem).toHaveBeenCalledWith('zhtp_config');
34-
});
35-
3623
it('should use environment variables if provided', async () => {
3724
const envVars = {
3825
ZHTP_NODE_URL: 'http://custom-api:9000',
@@ -50,23 +37,6 @@ describe('ReactNativeConfigProvider', () => {
5037
expect(result.enableBiometrics).toBe(false);
5138
});
5239

53-
it('should prioritize AsyncStorage over environment variables', async () => {
54-
const mockAsyncStorage = {
55-
getItem: vi.fn().mockResolvedValue(JSON.stringify(mockConfig)),
56-
setItem: vi.fn().mockResolvedValue(undefined),
57-
};
58-
59-
const envVars = {
60-
ZHTP_NODE_URL: 'http://different-api:8000',
61-
};
62-
63-
provider = new ReactNativeConfigProvider(envVars, mockAsyncStorage);
64-
const result = await provider.getConfig();
65-
66-
// Should return cached config from AsyncStorage, not env var
67-
expect(result.zhtpNodeUrl).toBe('http://192.168.1.31:8000');
68-
});
69-
7040
it('should use environment variables if AsyncStorage is not available', async () => {
7141
const envVars = {
7242
ZHTP_NODE_URL: 'http://env-api:8000',
@@ -130,23 +100,25 @@ describe('ReactNativeConfigProvider', () => {
130100
expect(result.enableBiometrics).toBe(true);
131101
});
132102

133-
it('should cache config in AsyncStorage after loading from env', async () => {
103+
it('should prioritize envVars over AsyncStorage', async () => {
134104
const mockAsyncStorage = {
135-
getItem: vi.fn().mockResolvedValue(null),
105+
getItem: vi.fn().mockResolvedValue(JSON.stringify({
106+
zhtpNodeUrl: 'http://cached:8000',
107+
networkType: 'testnet',
108+
debugMode: false,
109+
enableBiometrics: true,
110+
})),
136111
setItem: vi.fn().mockResolvedValue(undefined),
137112
};
138113

139114
const envVars = {
140-
ZHTP_NODE_URL: 'http://test-api:8000',
115+
ZHTP_NODE_URL: 'http://env-api:8000',
141116
};
142117

143118
provider = new ReactNativeConfigProvider(envVars, mockAsyncStorage);
144-
await provider.getConfig();
119+
const result = await provider.getConfig();
145120

146-
expect(mockAsyncStorage.setItem).toHaveBeenCalled();
147-
const callArg = (mockAsyncStorage.setItem as any).mock.calls[0][1];
148-
const cached = JSON.parse(callArg);
149-
expect(cached.zhtpNodeUrl).toBe('http://test-api:8000');
121+
expect(result.zhtpNodeUrl).toBe('http://env-api:8000');
150122
});
151123

152124
it('should handle AsyncStorage.getItem returning null', async () => {
@@ -165,48 +137,31 @@ describe('ReactNativeConfigProvider', () => {
165137
expect(result.zhtpNodeUrl).toBe('http://fallback-api:8000');
166138
});
167139

168-
it('should handle invalid JSON in AsyncStorage', async () => {
169-
const mockAsyncStorage = {
170-
getItem: vi.fn().mockResolvedValue('invalid json'),
171-
setItem: vi.fn().mockResolvedValue(undefined),
172-
};
173-
140+
it('should use envVars when AsyncStorage is not available', async () => {
174141
const envVars = {
175-
ZHTP_NODE_URL: 'http://fallback-api:8000',
142+
ZHTP_NODE_URL: 'http://env-api:8000',
176143
};
177144

178-
provider = new ReactNativeConfigProvider(envVars, mockAsyncStorage);
179-
const consoleSpy = vi.spyOn(console, 'warn').mockImplementation(() => {});
180-
145+
provider = new ReactNativeConfigProvider(envVars);
181146
const result = await provider.getConfig();
182147

183-
expect(result.zhtpNodeUrl).toBe('http://fallback-api:8000');
184-
expect(consoleSpy).toHaveBeenCalled();
185-
186-
consoleSpy.mockRestore();
148+
expect(result.zhtpNodeUrl).toBe('http://env-api:8000');
187149
});
188150

189-
it('should handle AsyncStorage.getItem error', async () => {
151+
it('should use envVars even with AsyncStorage errors', async () => {
190152
const mockAsyncStorage = {
191-
getItem: vi
192-
.fn()
193-
.mockRejectedValue(new Error('AsyncStorage error')),
153+
getItem: vi.fn().mockRejectedValue(new Error('AsyncStorage error')),
194154
setItem: vi.fn().mockResolvedValue(undefined),
195155
};
196156

197157
const envVars = {
198-
ZHTP_NODE_URL: 'http://fallback-api:8000',
158+
ZHTP_NODE_URL: 'http://env-api:8000',
199159
};
200160

201161
provider = new ReactNativeConfigProvider(envVars, mockAsyncStorage);
202-
const consoleSpy = vi.spyOn(console, 'warn').mockImplementation(() => {});
203-
204162
const result = await provider.getConfig();
205163

206-
expect(result.zhtpNodeUrl).toBe('http://fallback-api:8000');
207-
expect(consoleSpy).toHaveBeenCalled();
208-
209-
consoleSpy.mockRestore();
164+
expect(result.zhtpNodeUrl).toBe('http://env-api:8000');
210165
});
211166

212167
it('should handle AsyncStorage.setItem error silently', async () => {
@@ -253,11 +208,15 @@ describe('ReactNativeConfigProvider', () => {
253208

254209
it('should merge partial updates with existing config', async () => {
255210
const mockAsyncStorage = {
256-
getItem: vi.fn().mockResolvedValue(JSON.stringify(mockConfig)),
211+
getItem: vi.fn().mockResolvedValue(null),
257212
setItem: vi.fn().mockResolvedValue(undefined),
258213
};
259214

260-
provider = new ReactNativeConfigProvider(undefined, mockAsyncStorage);
215+
const envVars = {
216+
ZHTP_NODE_URL: 'http://test-api:8000',
217+
};
218+
219+
provider = new ReactNativeConfigProvider(envVars, mockAsyncStorage);
261220

262221
await provider.updateConfig({ networkType: 'mainnet' });
263222

@@ -266,7 +225,7 @@ describe('ReactNativeConfigProvider', () => {
266225
][1];
267226
const updated = JSON.parse(callArg);
268227
expect(updated.networkType).toBe('mainnet');
269-
expect(updated.zhtpNodeUrl).toBe(mockConfig.zhtpNodeUrl);
228+
expect(updated.zhtpNodeUrl).toBe('http://test-api:8000');
270229
});
271230

272231
it('should throw error if AsyncStorage not available', async () => {

0 commit comments

Comments
 (0)