Problems regarding LiveView test

Hi all!

I am trying to write a test for a liveview. Within this liveview, the user is supposed to be able to select shifts from a calendar and be redirected to a normal view where he/she can print the shifts, after having clicked a print button.

I started to write the following test:

test "Redirects to a view where all selected shifts can be printed, after user has clicked the print button",
  %{conn: conn, token: token, shift: shift} do

  {:ok, view, _html} = conn |> live(Routes.live_path(conn, ShiftApplicationLive, token))

  params = [shift_ids: []]

  redirect_path = Routes.shift_path(conn, :print_shifts, token, params)

  render_click(view, :select_shift, %{"shift-id" =>})

  assert_redirect(view, ^redirect_path, fn ->
    assert render_click(view, :print_shifts)

However, I always got the error:

No message matching {^ref, {:redirect, ^topic, %{to: ^redirect_path}}} after 100ms.
[... truncated error ...]
code: assert_redirect(view, ^redirect_path, fn ->
       lib/shift_application_web/live/shift_application_live_test.exs:289: (test)

The problems seems to be with this part of my test:

  assert_redirect(view, ^redirect_path, fn ->
    assert render_click(view, :print_shifts)

When I execute the render_click(view, :print_shifts), I got the following data back:

    to: "/shift-application/contract/tlOSOpL2E_5a7yrxr4NeWgdOJbG5mz2WcuD7ClCBvdeBw1iKHcfv0Gb20b51rKBYAAAAAF4h6rd7ImF1ZnRyYWdzbnVtbWVyIjoiNTAwMDAifQ%253D%253D/print_shifts?schift_ids[]=8eec-4547-a3c8-7a6174051021"
  • Is it normal that the renders_click method returns an :error?

  • Why is there different level of encodings? The URL encoded by redirect_path = Routes.shift_path(conn, :print_shifts, token, params) is encoded one time, while the URL returned by the renders_click is encoded two times; so if I try to compare them, they’ll always be different, unless I decode them with URI.decode().

Without looking at the code being tested is hard to know the problem, but I still could say a few things to check:

test "redirected mount", %{conn: conn} do
  assert {:error, %{redirect: %{to: "/somewhere"}}} = live(conn, "my-path")

You may notice it matches the error you’re getting in another point of the test.

  • Based on the last part of your description (redirected to a normal view where he/she can print the shifts, after having clicked a print button), it is expected that the render_click helper, which is for live view components, is no longer useful when testing a button from another part of the application.

It would require to do some extra work to keep testing the whole flow. Maybe this discussion has some insight


Thanks jmbejar.

Yes, after looking at the LiveView source code, it seems that it is the default behavior.

I’ll take a look at the other post you have linked.