Testing Liveview - for loop is not visible

Hi , I’ve got liveview page generated by render function like this :

def render(assigns) do
    ~L"""
    <h1 class="page-title">Strategies : <%= @name %> </h1> 
    <div>
       <%= for strategy <- @strategies do %>
         <div class="strategy">
           <div class="strategy-name"><%= strategy.name %></div>
          <div class="strategy-slug"><%= strategy.slug %></div>
       </div>
      <% end %>
  </div>
  """
  end

When I try to test it:

{:ok, view, html} = live(conn)
IO.inspect html

I see only :
<h1 class="page-title">Strategies : Some Name </h1>
Html body inside “for” loop is not visible (doesn’t exist) .

So I can’t test it not via Hound, Wallaby nor native LiveViewTest . I see only html inside h1 tag.
How to make visible html body inside “for” loop during test ?

Thanks in advance .

:wave:

When testing, are there any @strategies to be rendered? Maybe the @strategies assign is an empty list.

1 Like

Strategies are rendered. I have one liveview page depends on routing :
live “/strategies”, Engine.StrategiesLive , :index
live “/strategies/:category”, Engine.StrategiesLive , :type

In web browser I see whole page , but during testing not.

So how do you test?

For example :

defmodule SimpleTest do 
  use MyAppWeb.ConnCase, async: false 
  import Phoenix.LiveViewTest

  test "simple test ", %{conn: conn} do
    conn = get(conn, "/strategies")
    {:ok, view, html} = live(conn)
    IO.inspect html #no html in "for" loop place

  end
end

And how do you assign @categories?

def handle_params(params, _uri, socket) do
    socket = 
      case params["category"] do      
        nil ->                 
          socket               
        category ->            
          assign(socket, :strategies, MyApp.Engine.get_type_strategies(category) ) |> assign(:name, category ) 
      end
      {:noreply, socket}       
  end

Have you logged the value of params during tests to see if it contains "category" key? So far I haven’t seen where you would pass it in.

To see page with strategies of given category

test "simple test ", %{conn: conn} do
    conn = get(conn, "/strategies/somecategory")
    {:ok, view, html} = live(conn)

is It not enough , should I pass params somehow to liveview in test ?

The test you have shown before did not contain the second path segment.

And what does MyApp.Engine.get_type_strategies("somecategory") return?

1 Like

Solved , my stupid mistake , strategies are related with entity category and Repo.insert had wrong behavior during uploading testing database .

Thanks to NobbZ and Idiot for hints . I tried to find solution in wrong place.