Skip to content

Commit dae692c

Browse files
committed
test(query-devtools/utils): add tests for 'sortFns'
1 parent 150300f commit dae692c

1 file changed

Lines changed: 118 additions & 1 deletion

File tree

packages/query-devtools/src/__tests__/utils.test.ts

Lines changed: 118 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { afterEach, beforeEach, describe, expect, it } from 'vitest'
2+
import { QueryClient, QueryObserver } from '@tanstack/query-core'
23
import {
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

1416
describe('Utils tests', () => {
1517
describe('updatedNestedDataByPath', () => {
@@ -953,4 +955,119 @@ 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+
it('should fall back to placing the second query first when timestamps are equal', () => {
993+
const a = buildQuery(['a'], { dataUpdatedAt: 100 })
994+
const b = buildQuery(['b'], { dataUpdatedAt: 100 })
995+
996+
expect(dateSort(a, b)).toBe(-1)
997+
})
998+
})
999+
1000+
describe("'query hash'", () => {
1001+
const queryHashSort = sortFns['query hash']!
1002+
1003+
it('should sort queries by query hash alphabetically', () => {
1004+
const a = buildQuery(['a'])
1005+
const b = buildQuery(['b'])
1006+
1007+
expect(queryHashSort(a, b)).toBeLessThan(0)
1008+
expect(queryHashSort(b, a)).toBeGreaterThan(0)
1009+
})
1010+
1011+
it('should return 0 when query hashes are identical', () => {
1012+
const a = buildQuery(['same'])
1013+
const b = buildQuery(['same'])
1014+
1015+
expect(queryHashSort(a, b)).toBe(0)
1016+
})
1017+
})
1018+
1019+
describe("'status'", () => {
1020+
const statusSort = sortFns['status']!
1021+
1022+
function addObserver(query: Query) {
1023+
const observer = new QueryObserver(queryClient, {
1024+
queryKey: query.queryKey,
1025+
enabled: false,
1026+
})
1027+
return observer.subscribe(() => {})
1028+
}
1029+
1030+
it('should place a fetching query before an idle one', () => {
1031+
const fetching = buildQuery(['fetching'], {
1032+
fetchStatus: 'fetching',
1033+
dataUpdatedAt: 100,
1034+
})
1035+
const idle = buildQuery(['idle'], {
1036+
fetchStatus: 'idle',
1037+
dataUpdatedAt: 100,
1038+
})
1039+
const unsubscribe = addObserver(idle)
1040+
1041+
expect(statusSort(fetching, idle)).toBe(-1)
1042+
expect(statusSort(idle, fetching)).toBe(1)
1043+
1044+
unsubscribe()
1045+
})
1046+
1047+
it('should place an inactive (no observers) query last', () => {
1048+
const active = buildQuery(['active'], {
1049+
fetchStatus: 'idle',
1050+
dataUpdatedAt: 100,
1051+
})
1052+
const inactive = buildQuery(['inactive'], {
1053+
fetchStatus: 'idle',
1054+
dataUpdatedAt: 100,
1055+
})
1056+
const unsubscribe = addObserver(active)
1057+
1058+
expect(statusSort(active, inactive)).toBe(-1)
1059+
expect(statusSort(inactive, active)).toBe(1)
1060+
1061+
unsubscribe()
1062+
})
1063+
1064+
it('should fall back to "last updated" sort within the same status rank', () => {
1065+
const older = buildQuery(['older'], { dataUpdatedAt: 100 })
1066+
const newer = buildQuery(['newer'], { dataUpdatedAt: 200 })
1067+
1068+
expect(statusSort(older, newer)).toBe(1)
1069+
expect(statusSort(newer, older)).toBe(-1)
1070+
})
1071+
})
1072+
})
9561073
})

0 commit comments

Comments
 (0)