Skip to content

Commit e415a00

Browse files
committed
test(query-devtools/utils): add test for stale vs fresh rank in 'status' sort of 'sortFns'
1 parent a709b04 commit e415a00

1 file changed

Lines changed: 33 additions & 0 deletions

File tree

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1146,6 +1146,39 @@ describe('Utils tests', () => {
11461146
}
11471147
})
11481148

1149+
it('should place a stale query after a fresh one when both are idle and have observers', () => {
1150+
const staleObserver = new QueryObserver(queryClient, {
1151+
queryKey: ['stale'],
1152+
staleTime: 0,
1153+
})
1154+
const staleUnsubscribe = staleObserver.subscribe(() => {})
1155+
const stale = queryClient
1156+
.getQueryCache()
1157+
.find({ queryKey: ['stale'] })!
1158+
stale.setState({
1159+
...stale.state,
1160+
fetchStatus: 'idle',
1161+
data: 'data',
1162+
dataUpdatedAt: 200,
1163+
})
1164+
1165+
const fresh = buildQuery(['fresh'], {
1166+
fetchStatus: 'idle',
1167+
data: 'fresh-data',
1168+
dataUpdatedAt: 100,
1169+
})
1170+
const freshUnsubscribe = addObserver(fresh)
1171+
1172+
try {
1173+
expect(stale.isStale()).toBe(true)
1174+
expect(statusSort(fresh, stale)).toBe(-1)
1175+
expect(statusSort(stale, fresh)).toBe(1)
1176+
} finally {
1177+
staleUnsubscribe()
1178+
freshUnsubscribe()
1179+
}
1180+
})
1181+
11491182
it('should fall back to "last updated" sort within the same status rank', () => {
11501183
const older = buildQuery(['older'], { dataUpdatedAt: 100 })
11511184
const newer = buildQuery(['newer'], { dataUpdatedAt: 200 })

0 commit comments

Comments
 (0)