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()?