germsvel / phoenix_test

PhoenixTest provides a unified way of writing feature tests -- regardless of whether you're testing LiveView pages or static (non-LiveView) pages.
https://hex.pm/packages/phoenix_test
MIT License
181 stars 23 forks source link

Uncheck checkbox without hidden input error #104

Closed totaltrash closed 3 months ago

totaltrash commented 3 months ago

If you have a checkbox without a hidden:

<input type="checkbox" name="roles[]" id="role_reader" value="reader" checked />
<label for="role_reader">Reader</label>
uncheck(session, "Reader")

Results in:

** (ArgumentError) Could not find element with selector "input[type='hidden'][name='roles[]']"
code: |> uncheck("Reader")
germsvel commented 3 months ago

@totaltrash I think that's expected. You can't really "uncheck" a checkbox. In a form, it needs to have a corresponding hidden input.

Here's a note from MDN docs:

Note: If a checkbox is unchecked when its form is submitted, neither the name nor the value is submitted to the server. There is no HTML-only method of representing a checkbox's unchecked state (e.g. value=unchecked). If you wanted to submit a default value for the checkbox when it is unchecked, you could include JavaScript to create a within the form with a value indicating an unchecked state.

totaltrash commented 3 months ago

Hi @germsvel thanks for your reply. Yes totally understand that, in my case i have a liveview that expects nothing to be posted when all items are unchecked, which works great "in real life" but errors in PhoenixTest. I have a workaround so I'll close this issue and the PR