stepchud
I have a problem testing my LiveView that is similar to this post, except that I’m not broadcasting messages via PubSub. In my case, I have a :live_component that renders a button and handles the phx-click event. The handle_event looks like this:
def handle_event("next-step", _params, socket) do
IO.puts("clicked next")
send(self(), {__MODULE__, :go_to_step, 1})
{:noreply, socket}
end
the handle_info(:go_to_step, 1) is defined in the parent :live_view of the live component:
def handle_info({_component, :go_to_step, step_index}, socket) do
IO.puts("handle go_to_step")
socket = socket
|> assign(:current_step, Enum.at(socket.assigns.wizard.steps, step_index))
{:noreply, socket}
end
I want to test that clicking the button renders the next step. the IO.puts show my test is properly clicking the button and the handle_info function is reached, so the send() is being received (also the feature works in the browser). However my test assertion fails because the view still shows the original :live_component content and not the one that should be rendered after handle_info finishes. The LiveViewTest:
describe "starts first step" do
test "clicking Begin button goes to first step", %{conn: conn} do
{:ok, view, _html} = live(conn, "/wizard")
assert view
|> element("button", "Begin")
|> render_click() =~ "<h1>The Aim</h1>"
end
end
Any idea how I can get the test process to assert the rendered state at the end of that handle_info()?
Trending in Questions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #ai
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 2- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
JonRowe
Given you send a message to yourself during the event handler, the result of “that” render doesn’t change, as you’ve observed, you can always request a render from the view in tests with
render(view), which in this case is probably the go.There is a chance of a race condition in such a rest (click → render then requiring a sleep) but I don’t think in this case it will cause an issue, as the view should process the
handle_eventmessage, then thehandle_infomessage, before being free to handle any others (and I believerender(view)requires a message on the view).stepchud
That fixed it, and the test is pretty simple still. Thanks again for the help!