@@ -1304,4 +1304,134 @@ describe('query', () => {
1304
1304
data : 'data1' ,
1305
1305
} )
1306
1306
} )
1307
+
1308
+ test ( 'should continue retry in background when refetchIntervalInBackground is true' , async ( ) => {
1309
+ const key = queryKey ( )
1310
+
1311
+ // make page unfocused
1312
+ const visibilityMock = mockVisibilityState ( 'hidden' )
1313
+
1314
+ let count = 0
1315
+ let result
1316
+
1317
+ const promise = queryClient . fetchQuery ( {
1318
+ queryKey : key ,
1319
+ queryFn : ( ) => {
1320
+ count ++
1321
+
1322
+ if ( count === 3 ) {
1323
+ return `data${ count } `
1324
+ }
1325
+
1326
+ throw new Error ( `error${ count } ` )
1327
+ } ,
1328
+ retry : 3 ,
1329
+ retryDelay : 1 ,
1330
+ refetchIntervalInBackground : true ,
1331
+ } )
1332
+
1333
+ promise . then ( ( data ) => {
1334
+ result = data
1335
+ } )
1336
+
1337
+ // Check if we do not have a result yet
1338
+ expect ( result ) . toBeUndefined ( )
1339
+
1340
+ // Query should continue retrying in background
1341
+ await vi . advanceTimersByTimeAsync ( 50 )
1342
+ expect ( result ) . toBe ( 'data3' )
1343
+
1344
+ // Reset visibilityState to original value
1345
+ visibilityMock . mockRestore ( )
1346
+ } )
1347
+
1348
+ test ( 'should pause retry when unfocused if refetchIntervalInBackground is false' , async ( ) => {
1349
+ const key = queryKey ( )
1350
+
1351
+ // make page unfocused
1352
+ const visibilityMock = mockVisibilityState ( 'hidden' )
1353
+
1354
+ let count = 0
1355
+ let result
1356
+
1357
+ const promise = queryClient . fetchQuery ( {
1358
+ queryKey : key ,
1359
+ queryFn : ( ) => {
1360
+ count ++
1361
+
1362
+ if ( count === 3 ) {
1363
+ return `data${ count } `
1364
+ }
1365
+
1366
+ throw new Error ( `error${ count } ` )
1367
+ } ,
1368
+ retry : 3 ,
1369
+ retryDelay : 1 ,
1370
+ refetchIntervalInBackground : false ,
1371
+ } )
1372
+
1373
+ promise . then ( ( data ) => {
1374
+ result = data
1375
+ } )
1376
+
1377
+ // Check if we do not have a result
1378
+ expect ( result ) . toBeUndefined ( )
1379
+
1380
+ // Check if the query is really paused
1381
+ await vi . advanceTimersByTimeAsync ( 50 )
1382
+ expect ( result ) . toBeUndefined ( )
1383
+
1384
+ // Reset visibilityState to original value
1385
+ visibilityMock . mockRestore ( )
1386
+ window . dispatchEvent ( new Event ( 'visibilitychange' ) )
1387
+
1388
+ // Query should now continue and resolve
1389
+ await vi . advanceTimersByTimeAsync ( 50 )
1390
+ expect ( result ) . toBe ( 'data3' )
1391
+ } )
1392
+
1393
+ test ( 'should pause retry when unfocused if refetchIntervalInBackground is undefined (default behavior)' , async ( ) => {
1394
+ const key = queryKey ( )
1395
+
1396
+ // make page unfocused
1397
+ const visibilityMock = mockVisibilityState ( 'hidden' )
1398
+
1399
+ let count = 0
1400
+ let result
1401
+
1402
+ const promise = queryClient . fetchQuery ( {
1403
+ queryKey : key ,
1404
+ queryFn : ( ) => {
1405
+ count ++
1406
+
1407
+ if ( count === 3 ) {
1408
+ return `data${ count } `
1409
+ }
1410
+
1411
+ throw new Error ( `error${ count } ` )
1412
+ } ,
1413
+ retry : 3 ,
1414
+ retryDelay : 1 ,
1415
+ // refetchIntervalInBackground is not set (undefined by default)
1416
+ } )
1417
+
1418
+ promise . then ( ( data ) => {
1419
+ result = data
1420
+ } )
1421
+
1422
+ // Check if we do not have a result
1423
+ expect ( result ) . toBeUndefined ( )
1424
+
1425
+ // Check if the query is really paused
1426
+ await vi . advanceTimersByTimeAsync ( 50 )
1427
+ expect ( result ) . toBeUndefined ( )
1428
+
1429
+ // Reset visibilityState to original value
1430
+ visibilityMock . mockRestore ( )
1431
+ window . dispatchEvent ( new Event ( 'visibilitychange' ) )
1432
+
1433
+ // Query should now continue and resolve
1434
+ await vi . advanceTimersByTimeAsync ( 50 )
1435
+ expect ( result ) . toBe ( 'data3' )
1436
+ } )
1307
1437
} )
0 commit comments