Skip to content

Commit 9d1ce70

Browse files
authored
test(query-devtools/utils): add tests for 'mutationSortFns' (#10651)
1 parent 0db5b90 commit 9d1ce70

1 file changed

Lines changed: 112 additions & 1 deletion

File tree

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

Lines changed: 112 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@ import {
77
getMutationStatusColor,
88
getQueryStatusColorByLabel,
99
getSidedProp,
10+
mutationSortFns,
1011
setupStyleSheet,
1112
sortFns,
1213
updateNestedDataByPath,
1314
} from '../utils'
14-
import type { MutationStatus, Query } from '@tanstack/query-core'
15+
import type { Mutation, MutationStatus, Query } from '@tanstack/query-core'
1516

1617
describe('Utils tests', () => {
1718
describe('updatedNestedDataByPath', () => {
@@ -1067,4 +1068,114 @@ describe('Utils tests', () => {
10671068
})
10681069
})
10691070
})
1071+
1072+
describe('mutationSortFns', () => {
1073+
let queryClient: QueryClient
1074+
1075+
const defaultMutationState: Mutation['state'] = {
1076+
context: undefined,
1077+
data: undefined,
1078+
error: null,
1079+
failureCount: 0,
1080+
failureReason: null,
1081+
isPaused: false,
1082+
status: 'idle',
1083+
variables: undefined,
1084+
submittedAt: 0,
1085+
}
1086+
1087+
function buildMutation(overrides: Partial<Mutation['state']>): Mutation {
1088+
return queryClient
1089+
.getMutationCache()
1090+
.build(queryClient, {}, { ...defaultMutationState, ...overrides })
1091+
}
1092+
1093+
beforeEach(() => {
1094+
queryClient = new QueryClient()
1095+
})
1096+
1097+
afterEach(() => {
1098+
queryClient.clear()
1099+
})
1100+
1101+
describe("'last updated'", () => {
1102+
const mutationDateSort = mutationSortFns['last updated']!
1103+
1104+
it('should place the more recently submitted mutation first', () => {
1105+
const older = buildMutation({ submittedAt: 100 })
1106+
const newer = buildMutation({ submittedAt: 200 })
1107+
1108+
expect(mutationDateSort(older, newer)).toBe(1)
1109+
expect(mutationDateSort(newer, older)).toBe(-1)
1110+
})
1111+
})
1112+
1113+
describe("'status'", () => {
1114+
const mutationStatusSort = mutationSortFns['status']!
1115+
1116+
it('should place a paused mutation first', () => {
1117+
const paused = buildMutation({
1118+
isPaused: true,
1119+
status: 'pending',
1120+
submittedAt: 100,
1121+
})
1122+
const pending = buildMutation({
1123+
isPaused: false,
1124+
status: 'pending',
1125+
submittedAt: 100,
1126+
})
1127+
1128+
expect(mutationStatusSort(paused, pending)).toBe(-1)
1129+
expect(mutationStatusSort(pending, paused)).toBe(1)
1130+
})
1131+
1132+
it('should place a pending mutation before a successful one', () => {
1133+
const pending = buildMutation({
1134+
isPaused: false,
1135+
status: 'pending',
1136+
submittedAt: 100,
1137+
})
1138+
const success = buildMutation({
1139+
isPaused: false,
1140+
status: 'success',
1141+
submittedAt: 100,
1142+
})
1143+
1144+
expect(mutationStatusSort(pending, success)).toBe(-1)
1145+
expect(mutationStatusSort(success, pending)).toBe(1)
1146+
})
1147+
1148+
it('should place an errored mutation before a successful one', () => {
1149+
const error = buildMutation({
1150+
isPaused: false,
1151+
status: 'error',
1152+
submittedAt: 100,
1153+
})
1154+
const success = buildMutation({
1155+
isPaused: false,
1156+
status: 'success',
1157+
submittedAt: 100,
1158+
})
1159+
1160+
expect(mutationStatusSort(error, success)).toBe(-1)
1161+
expect(mutationStatusSort(success, error)).toBe(1)
1162+
})
1163+
1164+
it('should fall back to "last updated" sort within the same status rank', () => {
1165+
const older = buildMutation({
1166+
isPaused: false,
1167+
status: 'success',
1168+
submittedAt: 100,
1169+
})
1170+
const newer = buildMutation({
1171+
isPaused: false,
1172+
status: 'success',
1173+
submittedAt: 200,
1174+
})
1175+
1176+
expect(mutationStatusSort(older, newer)).toBe(1)
1177+
expect(mutationStatusSort(newer, older)).toBe(-1)
1178+
})
1179+
})
1180+
})
10701181
})

0 commit comments

Comments
 (0)