Skip to content

Not able to set readonly attribute from React component boolean prop. #1394

Closed
@Shrikanth9

Description

@Shrikanth9
  • @testing-library/react 16.2.0:
  • Testing Framework and version: jest 29.7.0
  • DOM Environment: jest-environment-jsdom 29.7.0

Relevant code or config:

it('renders as read-only when isEditable is false', async () => {
    render(<DataField {...defaultProps} isEditable={false} />);

    await waitFor(() => {
      const inputField = screen.getByRole('textbox');
      expect(inputField).toHaveAttribute('readonly', 'true');
    })
  });

What you did:

Called an above test case under a test file

What happened:

<input
checked={isChecked}
type={type}
readOnly={!isEditable}
defaultValue={defaultValue}
className={p-2 rounded-md w-[${props.width || "100%"}] border-[1px] border-grey bg-white self-start focus:outline-none focus:border-[1.5px] focus:border-primary}
{...props}
/>
I am calling component which has the above input. I am setting readOnly attribute to the !isEditable prop.

Reproduction:

  • Render a react component passing a boolean prop
  • Add a html input under a component and assign readOnly attribute to the boolean prop.
  • Noticed readonly is assigned to an empty string ""

Problem description:

The expected behaviour is readonly attribute should be set to true. but It's been assigned to ""

  Expected the element to have attribute:
      readonly="true"
    Received:
      readonly=""

Suggested solution:

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions