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,75 @@ 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 "fresh" when query is idle, has observers, and is not stale' , ( ) => {
1358+ const query = buildQuery ( [ 'q' ] , {
1359+ fetchStatus : 'idle' ,
1360+ data : 'fresh-data' ,
1361+ dataUpdatedAt : Date . now ( ) ,
1362+ } )
1363+ const unsubscribe = addObserver ( query )
1364+
1365+ try {
1366+ expect ( getQueryStatusLabel ( query ) ) . toBe ( 'fresh' )
1367+ } finally {
1368+ unsubscribe ( )
1369+ }
1370+ } )
1371+ } )
1372+
13021373 describe ( 'getQueryStatusColor' , ( ) => {
13031374 let queryClient : QueryClient
13041375
0 commit comments