Skip to content

test(Checkbox): mode switching tests (#1825)#2001

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

test(Checkbox): mode switching tests (#1825)#2001
kotAPI wants to merge 1 commit into
mainfrom
test/issue-1825-checkbox-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 test coverage for Checkbox component's controlled and uncontrolled mode switching behavior, including verification of attribute updates and callback execution during mode transitions.

@changeset-bot

changeset-bot Bot commented Jun 15, 2026

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: 2f3a566

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

Adds a new test file for the Checkbox component that covers controlled/uncontrolled mode switching. Two tests verify aria-checked updates and onCheckedChange behavior when rerendering from defaultChecked to checked and vice versa.

Changes

Checkbox Controlled/Uncontrolled Switch Tests

Layer / File(s) Summary
Controlled/uncontrolled mode switch tests
src/components/ui/Checkbox/tests/Checkbox.controlledSwitch.test.tsx
Two tests cover rerender transitions: uncontrolled (defaultChecked) → controlled (checked/onCheckedChange) asserting aria-checked and onCheckedChange(true) on click; controlled → uncontrolled asserting aria-checked update and click behavior in uncontrolled mode.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~5 minutes

Possibly related issues

Possibly related PRs

  • rad-ui/ui#1239: Both PRs add React Testing Library coverage for the Checkbox component's controlled vs. uncontrolled behavior, including rerender scenarios and aria-checked/onCheckedChange assertions.
  • rad-ui/ui#1210: The new switch tests exercise the controlled-state behavior and CheckboxPrimitiveRoot context introduced in that PR.

Suggested labels

automerge

Poem

🐰 Hop, hop — the checkbox flips its state,
From defaultChecked to checked — isn't that great?
aria-checked updates, the tests all pass,
onCheckedChange fires as quick as a flash.
Controlled or uncontrolled, I've covered them all,
No sneaky regressions shall slip through my hall! ✅

🚥 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 summarizes the main change: adding mode switching tests for the Checkbox component.
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-checkbox-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 (2)
src/components/ui/Checkbox/tests/Checkbox.controlledSwitch.test.tsx (2)

24-27: ⚡ Quick win

Assert controlled-mode immutability after click.

After switching to controlled with checked={false}, add an assertion that aria-checked remains false after click (until parent rerender). This closes a regression gap where internal state might incorrectly mutate in controlled mode.

Suggested assertion
         expect(button).toHaveAttribute('aria-checked', 'false');
         fireEvent.click(button);
         expect(onCheckedChange).toHaveBeenCalledWith(true);
+        expect(button).toHaveAttribute('aria-checked', 'false');
🤖 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/Checkbox/tests/Checkbox.controlledSwitch.test.tsx` around
lines 24 - 27, The test verifies that onCheckedChange is called with the correct
value but does not assert that the aria-checked attribute remains 'false' after
the click event in controlled mode. Add an assertion after the
fireEvent.click(button) call to verify that aria-checked stays 'false' (before
parent rerender), ensuring the Checkbox component does not mutate its internal
state inappropriately when operating in controlled mode. This assertion should
go after the click but before or alongside the onCheckedChange expectation to
confirm immutability behavior.

14-15: ⚡ Quick win

Use role-based queries instead of container.querySelector('button').

These selectors are brittle and produce weaker failure messages. Prefer screen.getByRole('checkbox') (or getByRole from render result) so the tests bind to the accessibility contract rather than DOM shape.

Suggested update
-import { render, fireEvent } from '`@testing-library/react`';
+import { render, fireEvent, screen } from '`@testing-library/react`';
...
-        const button = container.querySelector('button')!;
+        const button = screen.getByRole('checkbox');
...
-        const button = container.querySelector('button')!;
+        const button = screen.getByRole('checkbox');

Also applies to: 36-37

🤖 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/Checkbox/tests/Checkbox.controlledSwitch.test.tsx` around
lines 14 - 15, The test code uses brittle DOM selectors like
container.querySelector('button') which produce weak failure messages and don't
bind to the accessibility contract. Replace container.querySelector('button')
with screen.getByRole('checkbox') in the fireEvent.click calls to use role-based
queries instead. This makes the tests more resilient to DOM structure changes
and provides better failure messages by ensuring tests interact with elements
based on their accessibility roles.
🤖 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/Checkbox/tests/Checkbox.controlledSwitch.test.tsx`:
- Around line 24-27: The test verifies that onCheckedChange is called with the
correct value but does not assert that the aria-checked attribute remains
'false' after the click event in controlled mode. Add an assertion after the
fireEvent.click(button) call to verify that aria-checked stays 'false' (before
parent rerender), ensuring the Checkbox component does not mutate its internal
state inappropriately when operating in controlled mode. This assertion should
go after the click but before or alongside the onCheckedChange expectation to
confirm immutability behavior.
- Around line 14-15: The test code uses brittle DOM selectors like
container.querySelector('button') which produce weak failure messages and don't
bind to the accessibility contract. Replace container.querySelector('button')
with screen.getByRole('checkbox') in the fireEvent.click calls to use role-based
queries instead. This makes the tests more resilient to DOM structure changes
and provides better failure messages by ensuring tests interact with elements
based on their accessibility roles.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: a7b6fec6-830d-4aca-86fa-f7ba14ed8419

📥 Commits

Reviewing files that changed from the base of the PR and between fe87b36 and 2f3a566.

📒 Files selected for processing (1)
  • src/components/ui/Checkbox/tests/Checkbox.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