Skip to content

Commit 25cdd97

Browse files
authored
fix(query-devtools/utils): make 'last updated' sort return 0 for queries with equal 'dataUpdatedAt' to follow the standard comparator contract (#10812)
1 parent 9cb8eac commit 25cdd97

3 files changed

Lines changed: 16 additions & 2 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@tanstack/query-devtools': patch
3+
---
4+
5+
fix(query-devtools/utils): make 'last updated' sort return 0 for queries with equal 'dataUpdatedAt' to follow the standard comparator contract

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1076,6 +1076,13 @@ describe('Utils tests', () => {
10761076
expect(dateSort(older, newer)).toBe(1)
10771077
expect(dateSort(newer, older)).toBe(-1)
10781078
})
1079+
1080+
it('should return 0 when both queries share the same "dataUpdatedAt"', () => {
1081+
const a = buildQuery(['a'], { dataUpdatedAt: 100 })
1082+
const b = buildQuery(['b'], { dataUpdatedAt: 100 })
1083+
1084+
expect(dateSort(a, b)).toBe(0)
1085+
})
10791086
})
10801087

10811088
describe("'query hash'", () => {

packages/query-devtools/src/utils.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,10 @@ const getStatusRank = (q: Query) =>
101101

102102
const queryHashSort: SortFn = (a, b) => a.queryHash.localeCompare(b.queryHash)
103103

104-
const dateSort: SortFn = (a, b) =>
105-
a.state.dataUpdatedAt < b.state.dataUpdatedAt ? 1 : -1
104+
const dateSort: SortFn = (a, b) => {
105+
const diff = b.state.dataUpdatedAt - a.state.dataUpdatedAt
106+
return diff < 0 ? -1 : diff > 0 ? 1 : 0
107+
}
106108

107109
const statusAndDateSort: SortFn = (a, b) => {
108110
if (getStatusRank(a) === getStatusRank(b)) {

0 commit comments

Comments
 (0)