Skip to content
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

Test for hidden value workarounds for checkbox inputs so they can sen… #609

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

winstonwolff
Copy link

@winstonwolff winstonwolff commented Jul 8, 2024

What:

This PR adds a test and special case logic to handle the somewhat common case of checkboxes with hidden inputs. See the why section below for an example. We suspect this situation is not too common in React code since a lot of react code is reading checkboxes and submitting the data through XHR or some other method. But some react code will render a

tag with inside it and submit it through the normal browser mechanism. When one is doing that, checkboxes often need this special hidden field. So this PR allows this case to not raise an error when calling toHaveFormValues()

Why:

The bug is when I have a form with a checkbox input such as below, it is a common workaround to have a matching hidden input so that if the checkbox is NOT checked, a false false is still sent through the form. However toHaveFormValues flags this as an error "Multiple form elements with the same name must be of the same type"

      <form>
         <input type="hidden" name="sample-checkbox" value=0>
         <input type="checkbox" name="sample-checkbox" value=1>
       </form>

How:

We added a test case, and an if statement in the code for identifying this situation.

Checklist:

  • Documentation
  • Tests
  • Updated Type Definitions N/A
  • Ready to be merged

@winstonwolff winstonwolff force-pushed the bug-checkbox-and-hidden-inputs-with-same-name branch from e395ac4 to b833800 Compare July 8, 2024 18:49
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