Skip to content

test(query-devtools/utils): add tests for 'mutationSortFns'#10651

Merged
sukvvon merged 1 commit intomainfrom
test/query-devtools-mutation-sort-fns
May 6, 2026
Merged

test(query-devtools/utils): add tests for 'mutationSortFns'#10651
sukvvon merged 1 commit intomainfrom
test/query-devtools-mutation-sort-fns

Conversation

@sukvvon
Copy link
Copy Markdown
Collaborator

@sukvvon sukvvon commented May 6, 2026

🎯 Changes

Add unit tests for the mutationSortFns record of mutation-sort helpers in query-devtools/utils.tsx.

mutationSortFns exposes two string keys ('last updated', 'status') that map to comparators used to order mutations in the devtools panel.

Cases:

  • 'last updated' (mutationDateSort) — places the more recently submitted mutation first
  • 'status' (mutationStatusSort) — places paused mutations first, places pending and errored mutations before successful ones, and falls back to "last updated" sort within the same status rank

Each test uses a real Mutation instance built via mutationCache.build. queryClient.clear() runs in afterEach to keep tests isolated.

mutationDateSort's behavior when submittedAt values are equal is intentionally left unasserted while its intent is being clarified separately.

✅ Checklist

  • I have followed the steps in the Contributing guide.
  • I have tested this code locally with pnpm run test:pr.

🚀 Release Impact

  • This change affects published code, and I have generated a changeset.
  • This change is docs/CI/dev-only (no release).

Summary by CodeRabbit

  • Tests
    • Expanded test coverage for mutation sorting functionality with comprehensive tests for status-based ordering (paused, pending, error, success) and last-updated timestamp fallback logic.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 6, 2026

📝 Walkthrough

Walkthrough

The PR extends a test file for query-devtools utilities by importing four additional utility functions and introducing a comprehensive test suite for mutationSortFns that validates sorting behavior based on last-updated time and mutation status (paused, pending, error, success).

Changes

Mutation Sorting Function Tests

Layer / File(s) Summary
Test Utilities Import
packages/query-devtools/src/__tests__/utils.test.ts
Import statements extended to include mutationSortFns, setupStyleSheet, sortFns, and updateNestedDataByPath alongside existing imports.
Mutation Sort Function Tests
packages/query-devtools/src/__tests__/utils.test.ts
New test suite added for mutationSortFns covering last-updated ordering, status-based ordering (paused, pending, error, success), and fallback behavior when mutation statuses are equal.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Possibly related PRs

  • TanStack/query#10650: Adds and tests sortFns export in packages/query-devtools/src/utils, closely related to mutation sorting functionality.

Suggested labels

package: query-devtools

Poem

🐰 A test suite hops in with sorting cheer,
Mutations organized, crystal clear,
Paused, pending, error, success so bright,
Devtools now test what's wrong and right! ✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately and specifically describes the main change: adding tests for 'mutationSortFns' in the query-devtools utils module.
Description check ✅ Passed The description includes all required template sections with detailed information about the changes, test cases, and checklist items properly marked.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch test/query-devtools-mutation-sort-fns

Comment @coderabbitai help to get the list of available commands and usage tips.

@nx-cloud
Copy link
Copy Markdown

nx-cloud Bot commented May 6, 2026

View your CI Pipeline Execution ↗ for commit 6066160

Command Status Duration Result
nx affected --targets=test:sherif,test:knip,tes... ✅ Succeeded 3m 20s View ↗
nx run-many --target=build --exclude=examples/*... ✅ Succeeded 1s View ↗

☁️ Nx Cloud last updated this comment at 2026-05-06 07:50:45 UTC

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 6, 2026

🚀 Changeset Version Preview

No changeset entries found. Merging this PR will not cause a version bump for any packages.

@sukvvon sukvvon self-assigned this May 6, 2026
@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new Bot commented May 6, 2026

More templates

@tanstack/angular-query-experimental

npm i https://pkg.pr.new/@tanstack/angular-query-experimental@10651

@tanstack/eslint-plugin-query

npm i https://pkg.pr.new/@tanstack/eslint-plugin-query@10651

@tanstack/preact-query

npm i https://pkg.pr.new/@tanstack/preact-query@10651

@tanstack/preact-query-devtools

npm i https://pkg.pr.new/@tanstack/preact-query-devtools@10651

@tanstack/preact-query-persist-client

npm i https://pkg.pr.new/@tanstack/preact-query-persist-client@10651

@tanstack/query-async-storage-persister

npm i https://pkg.pr.new/@tanstack/query-async-storage-persister@10651

@tanstack/query-broadcast-client-experimental

npm i https://pkg.pr.new/@tanstack/query-broadcast-client-experimental@10651

@tanstack/query-core

npm i https://pkg.pr.new/@tanstack/query-core@10651

@tanstack/query-devtools

npm i https://pkg.pr.new/@tanstack/query-devtools@10651

@tanstack/query-persist-client-core

npm i https://pkg.pr.new/@tanstack/query-persist-client-core@10651

@tanstack/query-sync-storage-persister

npm i https://pkg.pr.new/@tanstack/query-sync-storage-persister@10651

@tanstack/react-query

npm i https://pkg.pr.new/@tanstack/react-query@10651

@tanstack/react-query-devtools

npm i https://pkg.pr.new/@tanstack/react-query-devtools@10651

@tanstack/react-query-next-experimental

npm i https://pkg.pr.new/@tanstack/react-query-next-experimental@10651

@tanstack/react-query-persist-client

npm i https://pkg.pr.new/@tanstack/react-query-persist-client@10651

@tanstack/solid-query

npm i https://pkg.pr.new/@tanstack/solid-query@10651

@tanstack/solid-query-devtools

npm i https://pkg.pr.new/@tanstack/solid-query-devtools@10651

@tanstack/solid-query-persist-client

npm i https://pkg.pr.new/@tanstack/solid-query-persist-client@10651

@tanstack/svelte-query

npm i https://pkg.pr.new/@tanstack/svelte-query@10651

@tanstack/svelte-query-devtools

npm i https://pkg.pr.new/@tanstack/svelte-query-devtools@10651

@tanstack/svelte-query-persist-client

npm i https://pkg.pr.new/@tanstack/svelte-query-persist-client@10651

@tanstack/vue-query

npm i https://pkg.pr.new/@tanstack/vue-query@10651

@tanstack/vue-query-devtools

npm i https://pkg.pr.new/@tanstack/vue-query-devtools@10651

commit: 6066160

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 6, 2026

size-limit report 📦

Path Size
react full 12.1 KB (0%)
react minimal 9.07 KB (0%)

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@packages/query-devtools/src/__tests__/utils.test.ts`:
- Around line 1108-1109: The tests assert exact comparator return values; update
the two assertions that call mutationDateSort(older, newer) and
mutationDateSort(newer, older) to assert sign rather than exact numbers —
replace the toBe(1) check with toBeGreaterThan(0) and the toBe(-1) check with
toBeLessThan(0) so the tests only require positive/negative results from
mutationDateSort when comparing older and newer.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 3ca13393-48de-43b4-8d2e-37ac54c95559

📥 Commits

Reviewing files that changed from the base of the PR and between 0db5b90 and 6066160.

📒 Files selected for processing (1)
  • packages/query-devtools/src/__tests__/utils.test.ts

Comment on lines +1108 to +1109
expect(mutationDateSort(older, newer)).toBe(1)
expect(mutationDateSort(newer, older)).toBe(-1)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor | ⚡ Quick win

🧩 Analysis chain

🌐 Web query:

For JavaScript Array.prototype.sort, must a comparator return exactly -1/0/1, or is any negative/zero/positive number valid?

💡 Result:

Any negative/zero/positive number is valid for the comparator in JavaScript Array.prototype.sort. The specification and documentation define the behavior based on the sign of the return value: less than 0 means a before b, 0 means equal (original order preserved where stable), greater than 0 means a after b [1][2][3]. Examples like numeric sorting with (a - b), which can return values like -60, are standard and recommended [4][1]. MDN explicitly uses < 0, === 0, > 0 in its table, not requiring exactly -1/0/1 [1]. While examples often show -1/0/1 for clarity, any value with the correct sign works, as sorting algorithms (like quicksort, timsort) only check the sign [1][5]. The ECMAScript spec's SortCompare abstract operation treats the result based on whether it's less than, equal to, or greater than zero after coercion [6][7]. Returning exactly -1/0/1 is a best practice for readability and to avoid large number precision issues with floats, but not required [1].

Citations:


🏁 Script executed:

# Check the test file content at the specified lines
sed -n '1108,1109p; 1128,1129p; 1144,1145p; 1160,1161p; 1176,1177p' packages/query-devtools/src/__tests__/utils.test.ts

Repository: TanStack/query

Length of output: 647


🏁 Script executed:

# Find where mutationDateSort and mutationStatusSort are defined
rg -l "mutationDateSort|mutationStatusSort" packages/query-devtools/src --type ts --type tsx

Repository: TanStack/query

Length of output: 86


🏁 Script executed:

# Find where mutationDateSort and mutationStatusSort are defined
rg "mutationDateSort|mutationStatusSort" packages/query-devtools/src -l

Repository: TanStack/query

Length of output: 148


🏁 Script executed:

# Find and examine the implementations
rg "mutationDateSort|mutationStatusSort" packages/query-devtools/src/utils.tsx -A 5

Repository: TanStack/query

Length of output: 599


Assert comparator sign, not exact -1/1.

While these comparators currently return exactly -1 and 1, the test assertions encode this implementation detail unnecessarily. JavaScript sort comparators only need to return negative/zero/positive values. Using toBeGreaterThan(0) and toBeLessThan(0) makes tests more resilient to refactoring—the assertions will pass if implementations return other signed values (e.g., 2 / -2), which is semantically correct for sorting.

Suggested update
-        expect(mutationDateSort(older, newer)).toBe(1)
-        expect(mutationDateSort(newer, older)).toBe(-1)
+        expect(mutationDateSort(older, newer)).toBeGreaterThan(0)
+        expect(mutationDateSort(newer, older)).toBeLessThan(0)

-        expect(mutationStatusSort(paused, pending)).toBe(-1)
-        expect(mutationStatusSort(pending, paused)).toBe(1)
+        expect(mutationStatusSort(paused, pending)).toBeLessThan(0)
+        expect(mutationStatusSort(pending, paused)).toBeGreaterThan(0)

-        expect(mutationStatusSort(pending, success)).toBe(-1)
-        expect(mutationStatusSort(success, pending)).toBe(1)
+        expect(mutationStatusSort(pending, success)).toBeLessThan(0)
+        expect(mutationStatusSort(success, pending)).toBeGreaterThan(0)

-        expect(mutationStatusSort(error, success)).toBe(-1)
-        expect(mutationStatusSort(success, error)).toBe(1)
+        expect(mutationStatusSort(error, success)).toBeLessThan(0)
+        expect(mutationStatusSort(success, error)).toBeGreaterThan(0)

-        expect(mutationStatusSort(older, newer)).toBe(1)
-        expect(mutationStatusSort(newer, older)).toBe(-1)
+        expect(mutationStatusSort(older, newer)).toBeGreaterThan(0)
+        expect(mutationStatusSort(newer, older)).toBeLessThan(0)
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
expect(mutationDateSort(older, newer)).toBe(1)
expect(mutationDateSort(newer, older)).toBe(-1)
expect(mutationDateSort(older, newer)).toBeGreaterThan(0)
expect(mutationDateSort(newer, older)).toBeLessThan(0)
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/query-devtools/src/__tests__/utils.test.ts` around lines 1108 -
1109, The tests assert exact comparator return values; update the two assertions
that call mutationDateSort(older, newer) and mutationDateSort(newer, older) to
assert sign rather than exact numbers — replace the toBe(1) check with
toBeGreaterThan(0) and the toBe(-1) check with toBeLessThan(0) so the tests only
require positive/negative results from mutationDateSort when comparing older and
newer.

@sukvvon sukvvon merged commit 9d1ce70 into main May 6, 2026
10 checks passed
@sukvvon sukvvon deleted the test/query-devtools-mutation-sort-fns branch May 6, 2026 12:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant