1
1
// Sample test
2
2
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' ;
4
4
import { promises as fs } from 'fs' ;
5
5
6
6
describe ( 'Flagsmith.init' , ( ) => {
@@ -213,8 +213,8 @@ describe('Flagsmith.init', () => {
213
213
214
214
await waitFor ( ( ) => {
215
215
expect ( onError ) . toHaveBeenCalledTimes ( 1 ) ;
216
- expect ( onError ) . toHaveBeenCalledWith ( new Error ( 'Mocked fetch error' ) ) ;
217
216
} ) ;
217
+ expect ( onError ) . toHaveBeenCalledWith ( new Error ( 'Mocked fetch error' ) ) ;
218
218
} ) ;
219
219
test ( 'should not reject when the API cannot be reached but default flags are set' , async ( ) => {
220
220
const { flagsmith, initConfig } = getFlagsmith ( {
@@ -228,4 +228,24 @@ describe('Flagsmith.init', () => {
228
228
229
229
expect ( getStateToCheck ( flagsmith . getState ( ) ) ) . toEqual ( defaultState ) ;
230
230
} ) ;
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
+ } ) ;
231
251
} ) ;
0 commit comments