11import { afterEach , beforeEach , describe , expect , it } from 'vitest'
2+ import { QueryClient , QueryObserver } from '@tanstack/query-core'
23import {
34 convertRemToPixels ,
45 deleteNestedDataByPath ,
@@ -7,9 +8,10 @@ import {
78 getQueryStatusColorByLabel ,
89 getSidedProp ,
910 setupStyleSheet ,
11+ sortFns ,
1012 updateNestedDataByPath ,
1113} from '../utils'
12- import type { MutationStatus } from '@tanstack/query-core'
14+ import type { MutationStatus , Query } from '@tanstack/query-core'
1315
1416describe ( 'Utils tests' , ( ) => {
1517 describe ( 'updatedNestedDataByPath' , ( ) => {
@@ -953,4 +955,116 @@ describe('Utils tests', () => {
953955 expect ( styleTags [ 0 ] ?. getAttribute ( 'nonce' ) ) . toBe ( 'first-nonce' )
954956 } )
955957 } )
958+
959+ describe ( 'sortFns' , ( ) => {
960+ let queryClient : QueryClient
961+
962+ function buildQuery (
963+ queryKey : ReadonlyArray < unknown > ,
964+ state ?: Partial < Query [ 'state' ] > ,
965+ ) : Query {
966+ const query = queryClient . getQueryCache ( ) . build ( queryClient , { queryKey } )
967+ if ( state ) {
968+ query . setState ( state )
969+ }
970+ return query
971+ }
972+
973+ beforeEach ( ( ) => {
974+ queryClient = new QueryClient ( )
975+ } )
976+
977+ afterEach ( ( ) => {
978+ queryClient . clear ( )
979+ } )
980+
981+ describe ( "'last updated'" , ( ) => {
982+ const dateSort = sortFns [ 'last updated' ] !
983+
984+ it ( 'should place the more recently updated query first' , ( ) => {
985+ const older = buildQuery ( [ 'a' ] , { dataUpdatedAt : 100 } )
986+ const newer = buildQuery ( [ 'b' ] , { dataUpdatedAt : 200 } )
987+
988+ expect ( dateSort ( older , newer ) ) . toBe ( 1 )
989+ expect ( dateSort ( newer , older ) ) . toBe ( - 1 )
990+ } )
991+ } )
992+
993+ describe ( "'query hash'" , ( ) => {
994+ const queryHashSort = sortFns [ 'query hash' ] !
995+
996+ it ( 'should sort queries by query hash alphabetically' , ( ) => {
997+ const a = buildQuery ( [ 'a' ] )
998+ const b = buildQuery ( [ 'b' ] )
999+
1000+ expect ( queryHashSort ( a , b ) ) . toBeLessThan ( 0 )
1001+ expect ( queryHashSort ( b , a ) ) . toBeGreaterThan ( 0 )
1002+ } )
1003+
1004+ it ( 'should return 0 when query hashes are identical' , ( ) => {
1005+ const a = buildQuery ( [ 'same' ] )
1006+ const b = buildQuery ( [ 'same' ] )
1007+
1008+ expect ( queryHashSort ( a , b ) ) . toBe ( 0 )
1009+ } )
1010+ } )
1011+
1012+ describe ( "'status'" , ( ) => {
1013+ const statusSort = sortFns [ 'status' ] !
1014+
1015+ function addObserver ( query : Query ) {
1016+ const observer = new QueryObserver ( queryClient , {
1017+ queryKey : query . queryKey ,
1018+ enabled : false ,
1019+ } )
1020+ return observer . subscribe ( ( ) => { } )
1021+ }
1022+
1023+ it ( 'should place a fetching query before an idle one' , ( ) => {
1024+ const fetching = buildQuery ( [ 'fetching' ] , {
1025+ fetchStatus : 'fetching' ,
1026+ dataUpdatedAt : 100 ,
1027+ } )
1028+ const idle = buildQuery ( [ 'idle' ] , {
1029+ fetchStatus : 'idle' ,
1030+ dataUpdatedAt : 100 ,
1031+ } )
1032+ const unsubscribe = addObserver ( idle )
1033+
1034+ try {
1035+ expect ( statusSort ( fetching , idle ) ) . toBe ( - 1 )
1036+ expect ( statusSort ( idle , fetching ) ) . toBe ( 1 )
1037+ } finally {
1038+ unsubscribe ( )
1039+ }
1040+ } )
1041+
1042+ it ( 'should place an inactive (no observers) query last' , ( ) => {
1043+ const active = buildQuery ( [ 'active' ] , {
1044+ fetchStatus : 'idle' ,
1045+ dataUpdatedAt : 100 ,
1046+ } )
1047+ const inactive = buildQuery ( [ 'inactive' ] , {
1048+ fetchStatus : 'idle' ,
1049+ dataUpdatedAt : 100 ,
1050+ } )
1051+ const unsubscribe = addObserver ( active )
1052+
1053+ try {
1054+ expect ( statusSort ( active , inactive ) ) . toBe ( - 1 )
1055+ expect ( statusSort ( inactive , active ) ) . toBe ( 1 )
1056+ } finally {
1057+ unsubscribe ( )
1058+ }
1059+ } )
1060+
1061+ it ( 'should fall back to "last updated" sort within the same status rank' , ( ) => {
1062+ const older = buildQuery ( [ 'older' ] , { dataUpdatedAt : 100 } )
1063+ const newer = buildQuery ( [ 'newer' ] , { dataUpdatedAt : 200 } )
1064+
1065+ expect ( statusSort ( older , newer ) ) . toBe ( 1 )
1066+ expect ( statusSort ( newer , older ) ) . toBe ( - 1 )
1067+ } )
1068+ } )
1069+ } )
9561070} )
0 commit comments