@@ -1192,4 +1192,135 @@ describe('query', () => {
1192
1192
expect ( initialDataFn ) . toHaveBeenCalledTimes ( 1 )
1193
1193
expect ( query . state . data ) . toBe ( 'initial data' )
1194
1194
} )
1195
+
1196
+
1197
+ it ( 'should continue retry in background when refetchIntervalInBackground is true' , async ( ) => {
1198
+ const key = queryKey ( )
1199
+
1200
+ // make page unfocused
1201
+ const visibilityMock = mockVisibilityState ( 'hidden' )
1202
+
1203
+ let count = 0
1204
+ let result
1205
+
1206
+ const promise = queryClient . fetchQuery ( {
1207
+ queryKey : key ,
1208
+ queryFn : ( ) => {
1209
+ count ++
1210
+
1211
+ if ( count === 3 ) {
1212
+ return `data${ count } `
1213
+ }
1214
+
1215
+ throw new Error ( `error${ count } ` )
1216
+ } ,
1217
+ retry : 3 ,
1218
+ retryDelay : 1 ,
1219
+ refetchIntervalInBackground : true ,
1220
+ } )
1221
+
1222
+ promise . then ( ( data ) => {
1223
+ result = data
1224
+ } )
1225
+
1226
+ // Check if we do not have a result yet
1227
+ expect ( result ) . toBeUndefined ( )
1228
+
1229
+ // Query should continue retrying in background
1230
+ await vi . advanceTimersByTimeAsync ( 50 )
1231
+ expect ( result ) . toBe ( 'data3' )
1232
+
1233
+ // Reset visibilityState to original value
1234
+ visibilityMock . mockRestore ( )
1235
+ } )
1236
+
1237
+ it ( 'should pause retry when unfocused if refetchIntervalInBackground is false' , async ( ) => {
1238
+ const key = queryKey ( )
1239
+
1240
+ // make page unfocused
1241
+ const visibilityMock = mockVisibilityState ( 'hidden' )
1242
+
1243
+ let count = 0
1244
+ let result
1245
+
1246
+ const promise = queryClient . fetchQuery ( {
1247
+ queryKey : key ,
1248
+ queryFn : ( ) => {
1249
+ count ++
1250
+
1251
+ if ( count === 3 ) {
1252
+ return `data${ count } `
1253
+ }
1254
+
1255
+ throw new Error ( `error${ count } ` )
1256
+ } ,
1257
+ retry : 3 ,
1258
+ retryDelay : 1 ,
1259
+ refetchIntervalInBackground : false ,
1260
+ } )
1261
+
1262
+ promise . then ( ( data ) => {
1263
+ result = data
1264
+ } )
1265
+
1266
+ // Check if we do not have a result
1267
+ expect ( result ) . toBeUndefined ( )
1268
+
1269
+ // Check if the query is really paused
1270
+ await vi . advanceTimersByTimeAsync ( 50 )
1271
+ expect ( result ) . toBeUndefined ( )
1272
+
1273
+ // Reset visibilityState to original value
1274
+ visibilityMock . mockRestore ( )
1275
+ window . dispatchEvent ( new Event ( 'visibilitychange' ) )
1276
+
1277
+ // Query should now continue and resolve
1278
+ await vi . advanceTimersByTimeAsync ( 50 )
1279
+ expect ( result ) . toBe ( 'data3' )
1280
+ } )
1281
+
1282
+ it ( 'should pause retry when unfocused if refetchIntervalInBackground is undefined (default behavior)' , async ( ) => {
1283
+ const key = queryKey ( )
1284
+
1285
+ // make page unfocused
1286
+ const visibilityMock = mockVisibilityState ( 'hidden' )
1287
+
1288
+ let count = 0
1289
+ let result
1290
+
1291
+ const promise = queryClient . fetchQuery ( {
1292
+ queryKey : key ,
1293
+ queryFn : ( ) => {
1294
+ count ++
1295
+
1296
+ if ( count === 3 ) {
1297
+ return `data${ count } `
1298
+ }
1299
+
1300
+ throw new Error ( `error${ count } ` )
1301
+ } ,
1302
+ retry : 3 ,
1303
+ retryDelay : 1 ,
1304
+ // refetchIntervalInBackground is not set (undefined by default)
1305
+ } )
1306
+
1307
+ promise . then ( ( data ) => {
1308
+ result = data
1309
+ } )
1310
+
1311
+ // Check if we do not have a result
1312
+ expect ( result ) . toBeUndefined ( )
1313
+
1314
+ // Check if the query is really paused
1315
+ await vi . advanceTimersByTimeAsync ( 50 )
1316
+ expect ( result ) . toBeUndefined ( )
1317
+
1318
+ // Reset visibilityState to original value
1319
+ visibilityMock . mockRestore ( )
1320
+ window . dispatchEvent ( new Event ( 'visibilitychange' ) )
1321
+
1322
+ // Query should now continue and resolve
1323
+ await vi . advanceTimersByTimeAsync ( 50 )
1324
+ expect ( result ) . toBe ( 'data3' )
1325
+ } )
1195
1326
} )
0 commit comments