dsnipe

dsnipe

Testing stateful LiveComponent which holds the state of the previous action

Hi all,
I have a LiveView which has LiveComponent, which is the form with 2 steps:

  1. the form itself, with inputs
  2. the confirmation modal where a user needs to fill the same input for one field (like when you delete a github repository, you need to fill in the name of it in the confirmation pop-up)

This is how it works. When all fields are filled and validated, a user submits the form which triggers a modal. Then a user fills in the confirmation field and submits it. After the first submitting the initial form is assigned to the socket, so it can be used for the validation and sending it further.

The problem is - I can’t test the second confirmation form, because it requires the component allready to have some profiled state.
There is some code as an example:

defmodule FormComponent do
  # live component stuff here

  def handle_event("show_modal", params, socket) do
    case Form.changeset(params) do
      %{valid?: true} -> 
        {:noreply, assign(socket, show_modal: true, form: form)}

      changeset -> 
        {:noreply, assign(socket, form: form)}
    end 
  end

  def handle_event("submit", params, socket) do
    Map.merge(params, socket.assigns.form.changes)
    |> Form.confirm_changeset()
    # do some actions if it is valid
    {:noreply, socket}
  end
end

So, to be able to test the modal confirmation form I need to have assigned form changeset from the first step.
But I can only render a result of the first submit and check that the pop-up is rendered. But the second form I can’t test, because there is not possible to assign necessary data to the socket.

Is it any way I can test it? Or do I use live components incorrectly?

First Post!

gabriel137

gabriel137

Where does this “form” you are signing on the socket in handle_event “show_modal” come from?

Where Next?

Popular in Questions Top

chokchit
** (DBConnection.ConnectionError) connection not available and request was dropped from queue after 2733ms. You can configure how long re...
New
New
Kurisu
For example for a current url like http://localhost:4000/cosmetic/products?_utf8=✓&query=perfume&page=2, I would like to get: ...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
JulienCorb
I am trying to implement my new.html.eex file to create new posts on my website. new.html.eex: <h1>Create Post</h1> <%= ...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: The documentation above suggests that while ...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

Other popular topics Top

hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a > b) do {:ok, "a"} end if (a < b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement