-
Notifications
You must be signed in to change notification settings - Fork 18
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 using hidden inputs #37
Comments
Hi @revoltaxz, thanks for opening this issue! I think I see why your form is failing, and I think it's something we should fix. The problemPhoenixTest uses LiveViewTest's From the docs we can see:
But right now, we're passing all the form data to We need to update the code to pass hidden inputs through I'll try to get to that when I can, but if someone wants to take a stab at it first, feel free! I don't think it'll be too straightforward since we have to parse the form and pull the hidden fields. But we already do some parsing, so it might not be too complicated either. |
@revoltaxz could you share the HTML form you're trying to test that's giving you that error? I'm trying to reproduce this issue, but not hitting it for some reason. I'm now thinking that the current implementation works (regardless of what the docs say). And I'm guessing that you're getting that error because your hidden field's HTML doesn't have a |
@germsvel Sorry my late! Yes, of course, I could: def render(assigns) do
~H"""
<div>
<.simple_form
for={@form}
id={@id}
phx-submit="submit"
phx-target={@myself}
phx-change="validate"
>
<div class="grid xl:grid-cols-2 lg:grid-cols-2 md:grid-cols-2 sm:grid-cols-1 gap-3">
<.live_select
field={@form[:symbol]}
label="Buscar por"
placeholder="Insira ao menos 4 letras"
phx-target={@myself}
phx-focus="clear"
debounce="500"
options={[]}
update_min_len={4}
dropdown_extra_class="max-h-60 overflow-y-scroll"
/>
<.field
field={@form[:category]}
label="Tipo de posição"
type="select"
options={Helpers.asset_types()}
phx-change="change_category"
/>
<.field
field={@form[:quantity]}
label="Quantidade"
type="number"
step="100"
min="100"
value="100"
/>
<.field field={@form[:price]} label="Valor unitário (R$)" type="text" phx-hook="MoneyMask" />
<.field field={@form[:open_date]} label="Data de compra" type="date" />
<%= if @category == "option" do %>
<.field
field={@form[:contract_type]}
label="Tipo de opção"
type="select"
options={Helpers.options_types()}
/>
<% end %>
</div>
<.field
field={@form[:direction]}
label="Tipo de operação"
type="radio-group"
group_layout="col"
options={Helpers.operation_types()}
/>
<.button type="submit" phx-disable-with="Salvando..." label="Adicionar" />
</.simple_form>
</div>
"""
end OBS: I'm using the LiveSelect. |
@revoltaxz thanks for posting the form. I've never used LiveSelect. I don't see a hidden input there. I imagine the Do you know how you would test that with LiveView? Or how were you testing that previously? |
@germsvel No, I was trying to write tests using PhoenixTest, but I found a PR on the Live Select repo with some instructions, so I think that it is possible to pass the hidden input as you mentioned here. Do you think this helps you? |
@revoltaxz I don't think it's a matter of whether or not we can test the hidden inputs. If you look at this branch, I added a test that passes when testing hidden inputs. I think the issue is that whatever hidden input ** (ArgumentError) value for hidden "position[symbol]" must be one of [""], got: "PETR" LiveViewTest is saying that So, to give you another example, if you had this HTML <input type="hidden" name="admin" value="true"/> And you tried testing it like this: |> submit_form("#position_form", admin: "Roger") You would get the error: ** (ArgumentError) value for hidden "admin" must be one of ["true"], got: "Roger" So, I don't think it's an issue with PhoenixTest. It's an issue of LiveSelect doing something with hidden inputs that is not matching what you're passing to your form. Does that make sense? |
Oh yes, it makes sense! So, I will close this issue and try a workaround with LiveSelect! Thank you for helping me! |
Late to the party 😃 I just stumbled upon this thread yesterday by chance... @revoltaxz @germsvel I have no experience with form_attrs = %{
symbol: "PETR",
category: "stock",
symbol_text_input: "PETR4",
price: "12.50",
quantity: 100,
open_date: "2024-04-01",
direction: "short"
}
Phoenix.LiveView.send_update(
live.pid, # live = your test's live view
LiveSelect.Component,
%{id: live_select_id, value: form_attrs.symbol}
# `live_select_id` is the id of your live select input component
# (you can pass it explicitly, otherwise, one is generated automatically from form + field name)
)
conn
|> form("#position_form", form_attrs)
|> render_submit() I tested it and it worked for me. Yes, So the I think I will add a section to the docs documenting this, since quite a few folks asked similar questions already Thanks! |
Hello! I'm trying to test a Live page with a form with a LiveSelect component. The LiveSelect provides a component with a hidden input with value, but when I try using the
fill_form
orsubmit_form
to pass the arguments, I get the following error:Is there a way to test with hidden inputs in the form?
The text was updated successfully, but these errors were encountered: