@@ -1242,4 +1242,134 @@ describe('query', () => {
1242
1242
data : 'data1' ,
1243
1243
} )
1244
1244
} )
1245
+
1246
+ test ( 'should continue retry in background when refetchIntervalInBackground is true' , async ( ) => {
1247
+ const key = queryKey ( )
1248
+
1249
+ // make page unfocused
1250
+ const visibilityMock = mockVisibilityState ( 'hidden' )
1251
+
1252
+ let count = 0
1253
+ let result
1254
+
1255
+ const promise = queryClient . fetchQuery ( {
1256
+ queryKey : key ,
1257
+ queryFn : ( ) => {
1258
+ count ++
1259
+
1260
+ if ( count === 3 ) {
1261
+ return `data${ count } `
1262
+ }
1263
+
1264
+ throw new Error ( `error${ count } ` )
1265
+ } ,
1266
+ retry : 3 ,
1267
+ retryDelay : 1 ,
1268
+ refetchIntervalInBackground : true ,
1269
+ } )
1270
+
1271
+ promise . then ( ( data ) => {
1272
+ result = data
1273
+ } )
1274
+
1275
+ // Check if we do not have a result yet
1276
+ expect ( result ) . toBeUndefined ( )
1277
+
1278
+ // Query should continue retrying in background
1279
+ await vi . advanceTimersByTimeAsync ( 50 )
1280
+ expect ( result ) . toBe ( 'data3' )
1281
+
1282
+ // Reset visibilityState to original value
1283
+ visibilityMock . mockRestore ( )
1284
+ } )
1285
+
1286
+ test ( 'should pause retry when unfocused if refetchIntervalInBackground is false' , async ( ) => {
1287
+ const key = queryKey ( )
1288
+
1289
+ // make page unfocused
1290
+ const visibilityMock = mockVisibilityState ( 'hidden' )
1291
+
1292
+ let count = 0
1293
+ let result
1294
+
1295
+ const promise = queryClient . fetchQuery ( {
1296
+ queryKey : key ,
1297
+ queryFn : ( ) => {
1298
+ count ++
1299
+
1300
+ if ( count === 3 ) {
1301
+ return `data${ count } `
1302
+ }
1303
+
1304
+ throw new Error ( `error${ count } ` )
1305
+ } ,
1306
+ retry : 3 ,
1307
+ retryDelay : 1 ,
1308
+ refetchIntervalInBackground : false ,
1309
+ } )
1310
+
1311
+ promise . then ( ( data ) => {
1312
+ result = data
1313
+ } )
1314
+
1315
+ // Check if we do not have a result
1316
+ expect ( result ) . toBeUndefined ( )
1317
+
1318
+ // Check if the query is really paused
1319
+ await vi . advanceTimersByTimeAsync ( 50 )
1320
+ expect ( result ) . toBeUndefined ( )
1321
+
1322
+ // Reset visibilityState to original value
1323
+ visibilityMock . mockRestore ( )
1324
+ window . dispatchEvent ( new Event ( 'visibilitychange' ) )
1325
+
1326
+ // Query should now continue and resolve
1327
+ await vi . advanceTimersByTimeAsync ( 50 )
1328
+ expect ( result ) . toBe ( 'data3' )
1329
+ } )
1330
+
1331
+ test ( 'should pause retry when unfocused if refetchIntervalInBackground is undefined (default behavior)' , async ( ) => {
1332
+ const key = queryKey ( )
1333
+
1334
+ // make page unfocused
1335
+ const visibilityMock = mockVisibilityState ( 'hidden' )
1336
+
1337
+ let count = 0
1338
+ let result
1339
+
1340
+ const promise = queryClient . fetchQuery ( {
1341
+ queryKey : key ,
1342
+ queryFn : ( ) => {
1343
+ count ++
1344
+
1345
+ if ( count === 3 ) {
1346
+ return `data${ count } `
1347
+ }
1348
+
1349
+ throw new Error ( `error${ count } ` )
1350
+ } ,
1351
+ retry : 3 ,
1352
+ retryDelay : 1 ,
1353
+ // refetchIntervalInBackground is not set (undefined by default)
1354
+ } )
1355
+
1356
+ promise . then ( ( data ) => {
1357
+ result = data
1358
+ } )
1359
+
1360
+ // Check if we do not have a result
1361
+ expect ( result ) . toBeUndefined ( )
1362
+
1363
+ // Check if the query is really paused
1364
+ await vi . advanceTimersByTimeAsync ( 50 )
1365
+ expect ( result ) . toBeUndefined ( )
1366
+
1367
+ // Reset visibilityState to original value
1368
+ visibilityMock . mockRestore ( )
1369
+ window . dispatchEvent ( new Event ( 'visibilitychange' ) )
1370
+
1371
+ // Query should now continue and resolve
1372
+ await vi . advanceTimersByTimeAsync ( 50 )
1373
+ expect ( result ) . toBe ( 'data3' )
1374
+ } )
1245
1375
} )
0 commit comments