11/**
2- * Copyright 2022, Optimizely
2+ * Copyright 2022, 2024, Optimizely
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
@@ -20,6 +20,7 @@ import { DatafileManagerConfig } from '../lib/modules/datafile-manager/datafileM
2020import { advanceTimersByTime , getTimerCount } from './testUtils' ;
2121import PersistentKeyValueCache from '../lib/modules/datafile-manager/persistentKeyValueCache' ;
2222
23+
2324jest . mock ( '../lib/modules/datafile-manager/backoffController' , ( ) => {
2425 return jest . fn ( ) . mockImplementation ( ( ) => {
2526 const getDelayMock = jest . fn ( ) . mockImplementation ( ( ) => 0 ) ;
@@ -32,6 +33,8 @@ jest.mock('../lib/modules/datafile-manager/backoffController', () => {
3233} ) ;
3334
3435import BackoffController from '../lib/modules/datafile-manager/backoffController' ;
36+ import { LoggerFacade , getLogger } from '../lib/modules/logging' ;
37+ import { resetCalls , spy , verify } from 'ts-mockito' ;
3538
3639// Test implementation:
3740// - Does not make any real requests: just resolves with queued responses (tests push onto queuedResponses)
@@ -93,8 +96,19 @@ const testCache: PersistentKeyValueCache = {
9396} ;
9497
9598describe ( 'httpPollingDatafileManager' , ( ) => {
99+
100+ let spiedLogger : LoggerFacade ;
101+
102+ const loggerName = 'DatafileManager' ;
103+
104+ beforeAll ( ( ) => {
105+ const actualLogger = getLogger ( loggerName ) ;
106+ spiedLogger = spy ( actualLogger ) ;
107+ } ) ;
108+
96109 beforeEach ( ( ) => {
97110 jest . useFakeTimers ( ) ;
111+ resetCalls ( spiedLogger ) ;
98112 } ) ;
99113
100114 let manager : TestDatafileManager ;
@@ -179,6 +193,22 @@ describe('httpPollingDatafileManager', () => {
179193 manager = new TestDatafileManager ( { sdkKey : '123' , updateInterval : 1000 , autoUpdate : true } ) ;
180194 } ) ;
181195
196+ it ( 'logs an error if fetching datafile fails' , async ( ) => {
197+ manager . queuedResponses . push (
198+ {
199+ statusCode : 500 ,
200+ body : '' ,
201+ headers : { } ,
202+ }
203+ ) ;
204+
205+ manager . start ( ) ;
206+ await advanceTimersByTime ( 1000 ) ;
207+ await manager . responsePromises [ 0 ] ;
208+
209+ verify ( spiedLogger . error ( 'Datafile fetch request failed with status: 500' ) ) . once ( ) ;
210+ } ) ;
211+
182212 describe ( 'initial state' , ( ) => {
183213 it ( 'returns null from get before becoming ready' , ( ) => {
184214 expect ( manager . get ( ) ) . toEqual ( '' ) ;
0 commit comments