99 getPreferredColorScheme ,
1010 getQueryStatusColor ,
1111 getQueryStatusColorByLabel ,
12+ getQueryStatusLabel ,
1213 getSidedProp ,
1314 mutationSortFns ,
1415 setupStyleSheet ,
@@ -20,6 +21,7 @@ import type {
2021 Mutation ,
2122 MutationStatus ,
2223 Query ,
24+ QueryKey ,
2325} from '@tanstack/query-core'
2426
2527describe ( 'Utils tests' , ( ) => {
@@ -1299,6 +1301,121 @@ describe('Utils tests', () => {
12991301 } )
13001302 } )
13011303
1304+ describe ( 'getQueryStatusLabel' , ( ) => {
1305+ let queryClient : QueryClient
1306+
1307+ function buildQuery (
1308+ queryKey : QueryKey ,
1309+ state ?: Partial < Query [ 'state' ] > ,
1310+ ) : Query {
1311+ const query = queryClient . getQueryCache ( ) . build ( queryClient , { queryKey } )
1312+ if ( state ) {
1313+ query . setState ( state )
1314+ }
1315+ return query
1316+ }
1317+
1318+ function addObserver ( query : Query ) {
1319+ const observer = new QueryObserver ( queryClient , {
1320+ queryKey : query . queryKey ,
1321+ enabled : false ,
1322+ } )
1323+ return observer . subscribe ( ( ) => { } )
1324+ }
1325+
1326+ beforeEach ( ( ) => {
1327+ queryClient = new QueryClient ( )
1328+ } )
1329+
1330+ afterEach ( ( ) => {
1331+ queryClient . clear ( )
1332+ } )
1333+
1334+ it ( 'should return "fetching" when fetchStatus is "fetching"' , ( ) => {
1335+ const query = buildQuery ( [ 'q' ] , { fetchStatus : 'fetching' } )
1336+
1337+ expect ( getQueryStatusLabel ( query ) ) . toBe ( 'fetching' )
1338+ } )
1339+
1340+ it ( 'should return "inactive" when there are no observers' , ( ) => {
1341+ const query = buildQuery ( [ 'q' ] , { fetchStatus : 'idle' } )
1342+
1343+ expect ( getQueryStatusLabel ( query ) ) . toBe ( 'inactive' )
1344+ } )
1345+
1346+ it ( 'should return "paused" when fetchStatus is "paused" and there are observers' , ( ) => {
1347+ const query = buildQuery ( [ 'q' ] , { fetchStatus : 'paused' } )
1348+ const unsubscribe = addObserver ( query )
1349+
1350+ try {
1351+ expect ( getQueryStatusLabel ( query ) ) . toBe ( 'paused' )
1352+ } finally {
1353+ unsubscribe ( )
1354+ }
1355+ } )
1356+
1357+ it ( 'should return "paused" even when stale if fetchStatus is "paused"' , ( ) => {
1358+ const observer = new QueryObserver ( queryClient , {
1359+ queryKey : [ 'paused-stale' ] ,
1360+ staleTime : 0 ,
1361+ } )
1362+ const unsubscribe = observer . subscribe ( ( ) => { } )
1363+ const query = queryClient . getQueryCache ( ) . find ( {
1364+ queryKey : [ 'paused-stale' ] ,
1365+ } ) !
1366+ query . setState ( {
1367+ ...query . state ,
1368+ fetchStatus : 'paused' ,
1369+ data : 'data' ,
1370+ dataUpdatedAt : 0 ,
1371+ } )
1372+
1373+ try {
1374+ expect ( query . isStale ( ) ) . toBe ( true )
1375+ expect ( getQueryStatusLabel ( query ) ) . toBe ( 'paused' )
1376+ } finally {
1377+ unsubscribe ( )
1378+ }
1379+ } )
1380+
1381+ it ( 'should return "stale" when query is idle, has observers, and is stale' , ( ) => {
1382+ const observer = new QueryObserver ( queryClient , {
1383+ queryKey : [ 'stale' ] ,
1384+ staleTime : 0 ,
1385+ } )
1386+ const unsubscribe = observer . subscribe ( ( ) => { } )
1387+ const query = queryClient . getQueryCache ( ) . find ( { queryKey : [ 'stale' ] } ) !
1388+ query . setState ( {
1389+ ...query . state ,
1390+ fetchStatus : 'idle' ,
1391+ data : 'data' ,
1392+ dataUpdatedAt : 0 ,
1393+ } )
1394+
1395+ try {
1396+ expect ( query . isStale ( ) ) . toBe ( true )
1397+ expect ( getQueryStatusLabel ( query ) ) . toBe ( 'stale' )
1398+ } finally {
1399+ unsubscribe ( )
1400+ }
1401+ } )
1402+
1403+ it ( 'should return "fresh" when query is idle, has observers, and is not stale' , ( ) => {
1404+ const query = buildQuery ( [ 'q' ] , {
1405+ fetchStatus : 'idle' ,
1406+ data : 'fresh-data' ,
1407+ dataUpdatedAt : Date . now ( ) ,
1408+ } )
1409+ const unsubscribe = addObserver ( query )
1410+
1411+ try {
1412+ expect ( getQueryStatusLabel ( query ) ) . toBe ( 'fresh' )
1413+ } finally {
1414+ unsubscribe ( )
1415+ }
1416+ } )
1417+ } )
1418+
13021419 describe ( 'getQueryStatusColor' , ( ) => {
13031420 let queryClient : QueryClient
13041421
0 commit comments