Skip to content

Commit fc5d632

Browse files
authored
FF-3014 fix: "throwOnFailedInitialization" option should throw init errors (#55)
1 parent 412777f commit fc5d632

File tree

2 files changed

+76
-4
lines changed

2 files changed

+76
-4
lines changed

src/__tests__/index.test.tsx

Lines changed: 75 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ import {
66
Flag,
77
IAssignmentLogger,
88
} from '@eppo/js-client-sdk-common';
9-
import { EppoReactNativeClient } from '..';
9+
import { EppoReactNativeClient, init } from '..';
1010

11-
describe('EppoReactNativeClient E2E test', () => {
11+
describe('EppoReactNativeClient integration test', () => {
1212
const flagKey = 'mock-experiment';
1313

1414
const mockExperimentConfig: Flag = {
@@ -93,6 +93,14 @@ describe('EppoReactNativeClient E2E test', () => {
9393
}) as jest.Mock;
9494
});
9595

96+
beforeEach(() => {
97+
EppoReactNativeClient.initialized = false;
98+
});
99+
100+
afterAll(() => {
101+
EppoReactNativeClient.initialized = false;
102+
});
103+
96104
it('returns default value when experiment config is absent', () => {
97105
const mockConfigStore = td.object<EppoAsyncStorage>();
98106
const mockLogger = td.object<IAssignmentLogger>();
@@ -153,4 +161,69 @@ describe('EppoReactNativeClient E2E test', () => {
153161
);
154162
expect(assignment).toEqual('control');
155163
});
164+
165+
it('handles a successful init', async () => {
166+
global.fetch = jest.fn(() => {
167+
return Promise.resolve({
168+
ok: true,
169+
status: 200,
170+
json: () => Promise.resolve(mockExperimentConfig),
171+
});
172+
}) as jest.Mock;
173+
await init({
174+
apiKey: 'some-key',
175+
assignmentLogger: {
176+
logAssignment(assignment) {
177+
console.log('TODO: log', assignment);
178+
},
179+
},
180+
throwOnFailedInitialization: false,
181+
});
182+
expect(EppoReactNativeClient.initialized).toBe(true);
183+
});
184+
185+
it('handles errors during init with throwOnFailedInitialization set to true', async () => {
186+
global.fetch = jest.fn(() => {
187+
return Promise.reject({
188+
ok: false,
189+
status: 500,
190+
text: 'Network error',
191+
});
192+
}) as jest.Mock;
193+
expect.assertions(2);
194+
try {
195+
await init({
196+
apiKey: 'some-key',
197+
assignmentLogger: {
198+
logAssignment(assignment) {
199+
console.log('TODO: log', assignment);
200+
},
201+
},
202+
throwOnFailedInitialization: true,
203+
});
204+
} catch (err: any) {
205+
expect(err.message).toBe('Network error');
206+
}
207+
expect(EppoReactNativeClient.initialized).toBe(false);
208+
});
209+
210+
it('handles errors during init with throwOnFailedInitialization set to false', async () => {
211+
global.fetch = jest.fn(() => {
212+
return Promise.reject({
213+
ok: false,
214+
status: 500,
215+
text: 'Network error',
216+
});
217+
}) as jest.Mock;
218+
await init({
219+
apiKey: 'some-key',
220+
assignmentLogger: {
221+
logAssignment(assignment) {
222+
console.log('TODO: log', assignment);
223+
},
224+
},
225+
throwOnFailedInitialization: false,
226+
});
227+
expect(EppoReactNativeClient.initialized).toBe(false);
228+
});
156229
});

src/index.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ export async function init(config: IClientConfig): Promise<EppoClient> {
110110

111111
await EppoReactNativeClient.instance.fetchFlagConfigurations();
112112

113+
EppoReactNativeClient.initialized = true;
113114
return EppoReactNativeClient.instance;
114115
} catch (error) {
115116
console.warn(
@@ -118,8 +119,6 @@ export async function init(config: IClientConfig): Promise<EppoClient> {
118119
if (config.throwOnFailedInitialization ?? true) {
119120
throw error;
120121
}
121-
} finally {
122-
EppoReactNativeClient.initialized = true;
123122
return EppoReactNativeClient.instance;
124123
}
125124
}

0 commit comments

Comments
 (0)