Skip to content

Commit 4713411

Browse files
committed
Add test case to cover the case when the identities/ API cannot be reached
1 parent 3d99ac1 commit 4713411

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

test/init.test.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Sample test
22
import { waitFor } from '@testing-library/react';
3-
import { defaultState, environmentID, getFlagsmith, getStateToCheck, identityState } from './test-constants';
3+
import { defaultState, getFlagsmith, getStateToCheck, identityState } from './test-constants';
44
import { promises as fs } from 'fs';
55

66
describe('Flagsmith.init', () => {
@@ -213,8 +213,8 @@ describe('Flagsmith.init', () => {
213213

214214
await waitFor(() => {
215215
expect(onError).toHaveBeenCalledTimes(1);
216-
expect(onError).toHaveBeenCalledWith(new Error('Mocked fetch error'));
217216
});
217+
expect(onError).toHaveBeenCalledWith(new Error('Mocked fetch error'));
218218
});
219219
test('should not reject when the API cannot be reached but default flags are set', async () => {
220220
const { flagsmith, initConfig } = getFlagsmith({
@@ -228,4 +228,24 @@ describe('Flagsmith.init', () => {
228228

229229
expect(getStateToCheck(flagsmith.getState())).toEqual(defaultState);
230230
});
231+
test('should not reject but call onError, when the identities/ API cannot be reached with the cache populated', async () => {
232+
const onError = jest.fn();
233+
const { flagsmith, initConfig, AsyncStorage } = getFlagsmith({
234+
evaluationContext: identityState.evaluationContext,
235+
cacheFlags: true,
236+
fetch: async () => {
237+
return Promise.resolve({ text: () => Promise.resolve('Mocked fetch error'), ok: false, status: 401 });
238+
},
239+
onError,
240+
});
241+
await AsyncStorage.setItem('BULLET_TRAIN_DB', JSON.stringify(identityState));
242+
await flagsmith.init(initConfig);
243+
244+
expect(getStateToCheck(flagsmith.getState())).toEqual(identityState);
245+
246+
await waitFor(() => {
247+
expect(onError).toHaveBeenCalledTimes(1);
248+
});
249+
expect(onError).toHaveBeenCalledWith(new Error('Mocked fetch error'));
250+
});
231251
});

0 commit comments

Comments
 (0)