Enhancing LiveViewTest API for better async testing

Current Challenge

When testing LiveViews that use assign_async, we can’t really operate on the async data.

  • render_async(view) waits for async operations but only returns the html

  • All the other render_* test functions (render_click, render_patch, etc.) can not be used in combination with async content

Problem

  • We can’t use render_patch and then assert on async data that loads after navigation
  • We can’t use render_click on elements that appear in async-loaded content

Solution

I’m not sure yet what the most elegant way to solve this is …

What’s the problem with just returning HTML? The LV test api is not a functional API. view includes a pid, which is what most apis talk to, so you don’t need to get a new view back everywhere.

1 Like

You are right. I missed something here.

This works

      # Navigate to appointments list
      {:ok, view, _html} = live(conn, ~q"/:locale/user/appointments")

      # Navigate to first appointment
      render_patch(view, ~q"/:locale/user/appointments/123")

      # Wait for async data to load and render
      html1 = render_async(view)

      # Verify first appointment data
      assert html1 =~ "Location One"
      assert html1 =~ "123-456"
      assert html1 =~ "John Doe"

      # Navigate directly to second appointment
      render_patch(view, ~q"/:locale/user/appointments/456")

      # Wait for async data to load and render
      html2 = render_async(view)

      # Verify second appointment data
      assert html2 =~ "Location Two"
      assert html2 =~ "789-012"
      assert html2 =~ "Jane Smith"

      # Ensure first appointment data is no longer present
      refute html2 =~ "Location One"