Skip to content

test(query-devtools/utils): add tests for 'getPreferredColorScheme'#10654

Merged
sukvvon merged 1 commit intomainfrom
test/query-devtools-get-preferred-color-scheme
May 8, 2026
Merged

test(query-devtools/utils): add tests for 'getPreferredColorScheme'#10654
sukvvon merged 1 commit intomainfrom
test/query-devtools-get-preferred-color-scheme

Conversation

@sukvvon
Copy link
Copy Markdown
Collaborator

@sukvvon sukvvon commented May 8, 2026

🎯 Changes

Add unit tests for the getPreferredColorScheme helper in query-devtools/utils.tsx.

The helper returns a Solid signal that defaults to 'dark', then on mount reads window.matchMedia('(prefers-color-scheme: dark)') and reflects its value, listening to change events for live updates.

Cases:

  • returns 'dark' before onMount runs (default signal value)
  • after onMount, calls matchMedia with '(prefers-color-scheme: dark)', registers a listener, and reflects matches: true as 'dark'
  • reflects matches: false as 'light'
  • updates the signal when the change event fires
  • removes the change listener on cleanup

window.matchMedia is stubbed via vi.stubGlobal (auto-cleaned by vi.unstubAllGlobals in afterEach). Solid's onMount runs as a microtask, so each test uses fake timers and await vi.advanceTimersByTimeAsync(0) to flush — matching the pattern in solid-query-persist-client.

✅ 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
    • Added comprehensive test suite for color scheme detection and theme switching behavior. Tests validate proper initialization of theme state, accurate system preference tracking, reactive signal updates when user theme preferences change, and complete cleanup of associated event listeners during component lifecycle management and teardown.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 8, 2026

Review Change Stack

📝 Walkthrough

Walkthrough

New test suite validates that the getPreferredColorScheme Solid signal correctly detects and responds to system color scheme preferences via window.matchMedia, with proper initialization, reactive updates on theme changes, and event listener cleanup.

Changes

getPreferredColorScheme Tests

Layer / File(s) Summary
Test Imports
packages/query-devtools/src/__tests__/utils.test.ts
Import statement updated to include getPreferredColorScheme from ../utils.
Test Suite Implementation
packages/query-devtools/src/__tests__/utils.test.ts
New describe('getPreferredColorScheme') block with five test cases covering: default pre-onMount value, post-onMount value based on matches, reactive signal updates on "change" events, listener removal on disposal, and fake-timer lifecycle management with setup and teardown.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Poem

🐰 A rabbit hops through test cases bright,
Mocking the matchMedia with all its might,
Signals dance when themes change sight,
Listeners cleanup—everything's tight! ✨

🚥 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 clearly and specifically describes the main change: adding tests for the 'getPreferredColorScheme' function in query-devtools/utils.
Description check ✅ Passed The description comprehensively covers all required sections: Changes, Checklist (both items completed), and Release Impact. It provides detailed context about the helper function and test cases.
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-get-preferred-color-scheme

Tip

💬 Introducing Slack Agent: The best way for teams to turn conversations into code.

Slack Agent is built on CodeRabbit's deep understanding of your code, so your team can collaborate across the entire SDLC without losing context.

  • Generate code and open pull requests
  • Plan features and break down work
  • Investigate incidents and troubleshoot customer tickets together
  • Automate recurring tasks and respond to alerts with triggers
  • Summarize progress and report instantly

Built for teams:

  • Shared memory across your entire org—no repeating context
  • Per-thread sandboxes to safely plan and execute work
  • Governance built-in—scoped access, auditability, and budget controls

One agent for your entire SDLC. Right inside Slack.

👉 Get started


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

@nx-cloud
Copy link
Copy Markdown

nx-cloud Bot commented May 8, 2026

View your CI Pipeline Execution ↗ for commit acdaf2c

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

☁️ Nx Cloud last updated this comment at 2026-05-08 00:33:38 UTC

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 8, 2026

🚀 Changeset Version Preview

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

@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new Bot commented May 8, 2026

More templates

@tanstack/angular-query-experimental

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

@tanstack/eslint-plugin-query

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

@tanstack/preact-query

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

@tanstack/preact-query-devtools

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

@tanstack/preact-query-persist-client

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

@tanstack/query-async-storage-persister

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

@tanstack/query-broadcast-client-experimental

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

@tanstack/query-core

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

@tanstack/query-devtools

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

@tanstack/query-persist-client-core

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

@tanstack/query-sync-storage-persister

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

@tanstack/react-query

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

@tanstack/react-query-devtools

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

@tanstack/react-query-next-experimental

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

@tanstack/react-query-persist-client

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

@tanstack/solid-query

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

@tanstack/solid-query-devtools

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

@tanstack/solid-query-persist-client

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

@tanstack/svelte-query

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

@tanstack/svelte-query-devtools

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

@tanstack/svelte-query-persist-client

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

@tanstack/vue-query

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

@tanstack/vue-query-devtools

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

commit: acdaf2c

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 8, 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.

🧹 Nitpick comments (1)
packages/query-devtools/src/__tests__/utils.test.ts (1)

1187-1219: 💤 Low value

LGTM — shared-listener design is intentional and correct.

Each window.matchMedia() call returns a new MediaQueryList object, but since addEventListener and removeEventListener on every returned object close over the same listeners Set, both registration and cleanup work correctly even if the implementation stores the MediaQueryList reference and calls both methods on the same object.

One minor precision gap worth noting: the addEventListener mock ignores its first argument (_event: string), so listeners counts callbacks registered for any event type, not just 'change'. The listenerCount() === 1 assertions would still pass if the implementation incorrectly registered two listeners for two different event types whose counts happened to cancel out after disposal. In practice this is a non-issue given the implementation only registers for 'change', but filtering on _event === 'change' would make listenerCount semantically precise without extra complexity.

🔧 Optional: filter by event type in the mock
  addEventListener: vi.fn(
-   (_event: string, listener: MatchMediaListener) => {
-     listeners.add(listener)
+   (event: string, listener: MatchMediaListener) => {
+     if (event === 'change') listeners.add(listener)
    },
  ) as MediaQueryList['addEventListener'],
  removeEventListener: vi.fn(
-   (_event: string, listener: MatchMediaListener) => {
-     listeners.delete(listener)
+   (event: string, listener: MatchMediaListener) => {
+     if (event === 'change') listeners.delete(listener)
    },
  ) as MediaQueryList['removeEventListener'],
🤖 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 1187 -
1219, The mock in setupMatchMediaMock currently ignores the event name so
listeners Set counts callbacks for any event; change the
addEventListener/removeEventListener handlers in setupMatchMediaMock to only
add/remove the listener when the first argument equals 'change' (i.e., check the
_event parameter) so the Set only tracks 'change' listeners, and ensure
listenerCount() continues to return listeners.size (now representing only
'change' listeners).
🤖 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.

Nitpick comments:
In `@packages/query-devtools/src/__tests__/utils.test.ts`:
- Around line 1187-1219: The mock in setupMatchMediaMock currently ignores the
event name so listeners Set counts callbacks for any event; change the
addEventListener/removeEventListener handlers in setupMatchMediaMock to only
add/remove the listener when the first argument equals 'change' (i.e., check the
_event parameter) so the Set only tracks 'change' listeners, and ensure
listenerCount() continues to return listeners.size (now representing only
'change' listeners).

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: e3e6b97d-af28-4bdd-8f39-c96dc897b750

📥 Commits

Reviewing files that changed from the base of the PR and between 9d1ce70 and acdaf2c.

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

@sukvvon sukvvon self-assigned this May 8, 2026
@sukvvon sukvvon merged commit 3d1ee9d into main May 8, 2026
10 checks passed
@sukvvon sukvvon deleted the test/query-devtools-get-preferred-color-scheme branch May 8, 2026 00:52
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