Skip to content

[checkbox] improve unit testing value #4048

@cgatian

Description

@cgatian

Bug report

Current behavior

Using React Testing Library to access a checkbox doesn't accurately return the checkbox's value.

Note

This issue is focused on improving unit testing. I think the current behavior is not intuitive to developers writing unit tests for Base UI wrapped component libraries. I understand the underlying complexity due to Base UI creating a faux checkbox.

render(<Checkbox value="test">Test checkbox</Checkbox>);
const checkbox = screen.getByRole('checkbox', { name: 'Test checkbox' });
expect(checkbox).toHaveValue('test');

Expected behavior

Using a testing library, when querying for the checkbox element, you ideally can access the value.

Base UI version

1.1.0

Which browser are you using?

NA

Additional context

Because Checkbox is using a span element to simulate the checkbox component, properties like value are not available. These are only available on the sibling hidden input, e.g. <input type="checkbox" aria-hidden="true" />.

I believe you could update the span (or element) to accept the value prop. Although, that's not a part of the HTMLElement DOM spec. Will update this issue with other ideas and at least the workaround once I have it.

const element = useRenderElement('span', componentProps, {
state,
ref: [buttonRef, controlRef, forwardedRef, groupContext?.registerControlRef],
props: [
{
id: nativeButton ? (inputId ?? undefined) : id,
role: 'checkbox',
'aria-checked': groupIndeterminate ? 'mixed' : checked,
'aria-readonly': readOnly || undefined,
'aria-required': required || undefined,
'aria-labelledby': labelId,
[PARENT_CHECKBOX as string]: parent ? '' : undefined,
onFocus() {
setFocused(true);
},

Metadata

Metadata

Assignees

No one assigned

    Labels

    component: checkboxChanges related to the checkbox component.status: waiting for maintainerThese issues haven't been looked at yet by a maintainer.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions