-
Notifications
You must be signed in to change notification settings - Fork 2.8k
feat(checkbox): adds checkbox component in ui with story and unit tests #8861
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
sachin-thakur-bruno
wants to merge
22
commits into
usebruno:main
Choose a base branch
from
sachin-thakur-bruno:chore/checkbox-ui
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
10503ae
WIP feat(checkbox)/adds checkbox component in ui with story and unit …
sachin-thakur-bruno b57a055
updated nord border color
sachin-thakur-bruno 1a70b70
updated check icon path
sachin-thakur-bruno c4e3c81
added indeterminate prop and story for it
sachin-thakur-bruno 00e3412
removed default test id
sachin-thakur-bruno 76be3fe
updated radius
sachin-thakur-bruno ef81c5d
memoize the refs
sachin-thakur-bruno 74fd445
Merge remote-tracking branch 'upstream/main' into chore/checkbox-ui
sachin-thakur-bruno 0aa7043
updated tests
sachin-thakur-bruno 5cce5dd
Merge branch 'main' into chore/checkbox-ui
sachin-thakur-bruno d290395
Merge remote-tracking branch 'upstream/main' into chore/checkbox-ui
sachin-thakur-bruno aa97b28
Merge branch 'chore/checkbox-ui' of github.com:sachin-thakur-bruno/br…
sachin-thakur-bruno 4efb610
used use layout effect
sachin-thakur-bruno 994e47e
removed bprder 3
sachin-thakur-bruno d35525e
Merge remote-tracking branch 'upstream/main' into chore/checkbox-ui
sachin-thakur-bruno 9659b61
added border 3 for dark and light themes
sachin-thakur-bruno cbef029
Merge remote-tracking branch 'upstream/main' into chore/checkbox-ui
sachin-thakur-bruno 9d8779d
Merge remote-tracking branch 'upstream/main' into chore/checkbox-ui
sachin-thakur-bruno 2de1bc2
updated intermediate
sachin-thakur-bruno 36378a0
Merge remote-tracking branch 'upstream/main' into chore/checkbox-ui
sachin-thakur-bruno 6e83fd6
label size based on checkbox size
sachin-thakur-bruno 169641c
remove spreading rest props
sachin-thakur-bruno File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,187 @@ | ||
| import '@testing-library/jest-dom'; | ||
| import React, { createRef, useState } from 'react'; | ||
| import { render, screen } from '@testing-library/react'; | ||
| import userEvent from '@testing-library/user-event'; | ||
| import { ThemeProvider } from 'styled-components'; | ||
| import Checkbox from './index'; | ||
|
|
||
| const theme = { | ||
| mode: 'light', | ||
| text: '#343434', | ||
| border: { border3: '#B1B1B1' }, | ||
| primary: { solid: '#D37F17' }, | ||
| button2: { color: { primary: { text: '#ffffff' } } }, | ||
| font: { size: { sm: '0.75rem' } } | ||
| }; | ||
|
|
||
| const renderWithTheme = (ui) => render(<ThemeProvider theme={theme}>{ui}</ThemeProvider>); | ||
|
|
||
| const renderCheckbox = (props = {}) => { | ||
| const hasAccessibleName = 'label' in props || 'ariaLabel' in props || 'ariaLabelledBy' in props; | ||
| return renderWithTheme( | ||
| <Checkbox onChange={() => {}} {...(hasAccessibleName ? {} : { ariaLabel: 'Checkbox' })} {...props} /> | ||
| ); | ||
| }; | ||
|
|
||
| describe('Checkbox', () => { | ||
| let user; | ||
|
|
||
| beforeEach(() => { | ||
| user = userEvent.setup(); | ||
| }); | ||
|
|
||
| it('renders unchecked by default', () => { | ||
| renderCheckbox(); | ||
| expect(screen.getByRole('checkbox')).not.toBeChecked(); | ||
| }); | ||
|
|
||
| it('reflects the checked prop', () => { | ||
| renderCheckbox({ checked: true }); | ||
| expect(screen.getByRole('checkbox')).toBeChecked(); | ||
| }); | ||
|
|
||
| it('calls onChange when clicked', async () => { | ||
| const onChange = jest.fn(); | ||
| renderCheckbox({ onChange }); | ||
|
|
||
| await user.click(screen.getByRole('checkbox')); | ||
|
|
||
| expect(onChange).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it('does not call onChange when disabled', async () => { | ||
| const onChange = jest.fn(); | ||
| renderCheckbox({ onChange, disabled: true }); | ||
|
|
||
| const checkbox = screen.getByRole('checkbox'); | ||
| expect(checkbox).toBeDisabled(); | ||
| await user.click(checkbox); | ||
|
|
||
| expect(onChange).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('updates when driven as a controlled component', async () => { | ||
| const Controlled = () => { | ||
| const [checked, setChecked] = useState(false); | ||
| return <Checkbox checked={checked} onChange={(e) => setChecked(e.target.checked)} />; | ||
| }; | ||
| renderWithTheme(<Controlled />); | ||
|
|
||
| const checkbox = screen.getByRole('checkbox'); | ||
| expect(checkbox).not.toBeChecked(); | ||
|
|
||
| await user.click(checkbox); | ||
| expect(checkbox).toBeChecked(); | ||
|
|
||
| await user.click(checkbox); | ||
| expect(checkbox).not.toBeChecked(); | ||
| }); | ||
|
|
||
| it('renders the optional label and associates it with the input', () => { | ||
| renderCheckbox({ label: 'Accept terms' }); | ||
|
|
||
| expect(screen.getByText('Accept terms')).toBeInTheDocument(); | ||
| expect(screen.getByRole('checkbox', { name: 'Accept terms' })).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('toggles via keyboard (space)', async () => { | ||
| const onChange = jest.fn(); | ||
| renderCheckbox({ onChange }); | ||
|
|
||
| const checkbox = screen.getByRole('checkbox'); | ||
| checkbox.focus(); | ||
| await user.keyboard(' '); | ||
|
|
||
| expect(onChange).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it('applies the data-testid to the input', () => { | ||
| renderCheckbox({ 'data-testid': 'my-checkbox' }); | ||
| expect(screen.getByTestId('my-checkbox')).toBe(screen.getByRole('checkbox')); | ||
| }); | ||
|
|
||
| it('forwards the ref to the input element', () => { | ||
| const ref = createRef(); | ||
| renderCheckbox({ ref }); | ||
|
|
||
| expect(ref.current).toBe(screen.getByRole('checkbox')); | ||
| }); | ||
|
|
||
| it('forwards additional props to the input', () => { | ||
| renderCheckbox({ required: true, name: 'terms' }); | ||
|
|
||
| const checkbox = screen.getByRole('checkbox'); | ||
| expect(checkbox).toBeRequired(); | ||
| expect(checkbox).toHaveAttribute('name', 'terms'); | ||
| }); | ||
|
|
||
| describe('indeterminate', () => { | ||
| it('sets the DOM indeterminate property when the prop is true', () => { | ||
| renderCheckbox({ indeterminate: true }); | ||
| expect(screen.getByRole('checkbox').indeterminate).toBe(true); | ||
| }); | ||
|
|
||
| it('does not set the DOM indeterminate property by default', () => { | ||
| renderCheckbox(); | ||
| expect(screen.getByRole('checkbox').indeterminate).toBe(false); | ||
| }); | ||
|
|
||
| it('clears the DOM indeterminate property when the prop turns false', () => { | ||
| const { rerender } = renderCheckbox({ indeterminate: true }); | ||
| expect(screen.getByRole('checkbox').indeterminate).toBe(true); | ||
|
|
||
| rerender( | ||
| <ThemeProvider theme={theme}> | ||
| <Checkbox onChange={() => {}} ariaLabel="Checkbox" indeterminate={false} /> | ||
| </ThemeProvider> | ||
| ); | ||
|
|
||
| expect(screen.getByRole('checkbox').indeterminate).toBe(false); | ||
| }); | ||
|
|
||
| it('re-applies the DOM property on re-render even though the browser clears it on click', async () => { | ||
| const onChange = jest.fn(); | ||
| const { rerender } = renderCheckbox({ indeterminate: true, onChange }); | ||
|
|
||
| const checkbox = screen.getByRole('checkbox'); | ||
| await user.click(checkbox); | ||
| // Clicking a native checkbox always resets `indeterminate` to false in the browser, | ||
| // regardless of what the app does with the click. this is the behavior our effect | ||
| // (re-applied on every render, no dependency array) must correct for. | ||
| checkbox.indeterminate = false; | ||
|
|
||
| rerender( | ||
| <ThemeProvider theme={theme}> | ||
| <Checkbox onChange={onChange} ariaLabel="Checkbox" indeterminate={true} /> | ||
| </ThemeProvider> | ||
| ); | ||
|
|
||
| expect(checkbox.indeterminate).toBe(true); | ||
| }); | ||
|
|
||
| it('still forwards the ref to the input element when indeterminate', () => { | ||
| const ref = createRef(); | ||
| renderCheckbox({ indeterminate: true, ref }); | ||
|
|
||
| expect(ref.current).toBe(screen.getByRole('checkbox')); | ||
| expect(ref.current.indeterminate).toBe(true); | ||
| }); | ||
| }); | ||
|
|
||
| describe('accessible name', () => { | ||
| it('is derived from ariaLabel when there is no visible label', () => { | ||
| renderCheckbox({ ariaLabel: 'Select all' }); | ||
| expect(screen.getByRole('checkbox', { name: 'Select all' })).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('is derived from an external element via ariaLabelledBy', () => { | ||
| renderWithTheme( | ||
| <> | ||
| <span id="ext-label">Select all</span> | ||
| <Checkbox onChange={() => {}} ariaLabelledBy="ext-label" /> | ||
| </> | ||
| ); | ||
| expect(screen.getByRole('checkbox', { name: 'Select all' })).toBeInTheDocument(); | ||
| }); | ||
| }); | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win
Add
border3to the Border Hierarchy preview.The overview now states that four border levels exist and lists
border3, but the Border Hierarchy preview still renders onlyborder0,border1, andborder2. Add aborder3row so both sections show the same hierarchy.Also applies to: 1309-1310
🤖 Prompt for AI Agents