Skip to content

test(Switch): mode switching tests (#1825)#2000

Open
kotAPI wants to merge 1 commit into
mainfrom
test/issue-1825-switch-controlled-switch
Open

test(Switch): mode switching tests (#1825)#2000
kotAPI wants to merge 1 commit into
mainfrom
test/issue-1825-switch-controlled-switch

Conversation

@kotAPI

@kotAPI kotAPI commented Jun 15, 2026

Copy link
Copy Markdown
Collaborator

Summary

  • Adds regression tests for controlled/uncontrolled transitions

Addresses #1825

Test plan

  • New test file passes

Summary by CodeRabbit

  • Tests
    • Added comprehensive test coverage for Switch component mode transitions to ensure reliable behavior across different usage patterns.

@changeset-bot

changeset-bot Bot commented Jun 15, 2026

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: 51424c0

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@coderabbitai

coderabbitai Bot commented Jun 15, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

A new test file is added for the Switch component that covers two mode-transition scenarios: uncontrolled-to-controlled and controlled-to-uncontrolled. Each test verifies data-state attribute updates and onCheckedChange callback invocation across rerenders.

Changes

Switch Controlled/Uncontrolled Transition Tests

Layer / File(s) Summary
Switch mode transition tests
src/components/ui/Switch/tests/Switch.controlledSwitch.test.tsx
Two tests added: the first renders in uncontrolled mode, clicks to confirm data-state="checked", rerenders in controlled mode, and verifies onCheckedChange(true) fires on click; the second starts controlled, rerenders to uncontrolled with defaultChecked={false}, and confirms a click transitions data-state to checked.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Possibly related issues

Possibly related PRs

  • rad-ui/ui#786: Overlaps strongly at the Switch test level, covering controlled vs. uncontrolled checked-state behavior and click-triggered handler verification.

Suggested labels

automerge

🐇 Hop, hop! The Switch flips free,
From controlled to wild it can be!
data-state checked, then unchecked too,
No bug shall slip past this bunny crew.
Tests in place — the UI stays true! 🎉

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and specifically describes the main change: adding mode switching tests for the Switch component, which aligns with the changeset that adds a test file for controlled/uncontrolled mode transitions.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
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/issue-1825-switch-controlled-switch

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

@coderabbitai coderabbitai Bot left a comment

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.

🧹 Nitpick comments (1)
src/components/ui/Switch/tests/Switch.controlledSwitch.test.tsx (1)

17-17: ⚡ Quick win

Use role-based queries instead of querySelector with non-null assertion.

Line 17 and Line 41 use container.querySelector('button')!, which is less resilient and bypasses null safety. Prefer screen.getByRole('switch') (or getByRole from render result) to keep tests aligned with accessible contracts and avoid brittle selector coupling.

Suggested refactor
-        const { container, rerender } = render(
+        const { rerender } = render(
             <Switch.Root defaultChecked={false}>
                 <Switch.Thumb />
             </Switch.Root>
         );

-        const button = container.querySelector('button')!;
+        const button = screen.getByRole('switch');
-        const { container, rerender } = render(
+        const { rerender } = render(
             <Switch.Root checked onCheckedChange={() => {}}>
                 <Switch.Thumb />
             </Switch.Root>
         );

-        const button = container.querySelector('button')!;
+        const button = screen.getByRole('switch');

Also applies to: 41-41

🤖 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 `@src/components/ui/Switch/tests/Switch.controlledSwitch.test.tsx` at line 17,
Replace the direct querySelector calls that select 'button' elements using
non-null assertions with role-based queries. Instead of using
container.querySelector('button')!, use screen.getByRole('switch') to query the
switch element. This makes the tests more resilient to DOM structure changes and
aligns with testing best practices by focusing on accessible roles rather than
implementation details. Apply this change to all instances where querySelector
is used with a non-null assertion for button selection in this test file.
🤖 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 `@src/components/ui/Switch/tests/Switch.controlledSwitch.test.tsx`:
- Line 17: Replace the direct querySelector calls that select 'button' elements
using non-null assertions with role-based queries. Instead of using
container.querySelector('button')!, use screen.getByRole('switch') to query the
switch element. This makes the tests more resilient to DOM structure changes and
aligns with testing best practices by focusing on accessible roles rather than
implementation details. Apply this change to all instances where querySelector
is used with a non-null assertion for button selection in this test file.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 6a241dda-9528-48b9-910a-0d3664f97641

📥 Commits

Reviewing files that changed from the base of the PR and between fe87b36 and 51424c0.

📒 Files selected for processing (1)
  • src/components/ui/Switch/tests/Switch.controlledSwitch.test.tsx

@github-actions

Copy link
Copy Markdown
Contributor

Coverage

This report compares the PR with the base branch. "Δ" shows how the PR affects each metric.

Metric PR Δ
Statements 75.73% +0.00%
Branches 59.17% +0.00%
Functions 61.75% +0.00%
Lines 77.26% +0.00%

Coverage improved or stayed the same. Great job!

Run npm run coverage:ci locally for detailed reports and target untested areas to raise these numbers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant