Hi,
I cannot figure out how to test some LiveComponents simulating the actual events that would be sent by the user.
In my case, the LiveComponent is not visible when the parent LiveView is rendered. I guess this is where the problem lies but it is required. To test my Liveview, I do:
{:ok, view, html} = live(conn, "/myview")
Doing the action to show the LiveComponent works:
render_click(view, "show", %{"param1" => "value1", "param2" => "value2"})
but I can only test the initial state of the LiveComponent because render_click
only renders the html.
For that mater, I can’t use find_child
to get the LiveComponent view because it is always nil:
find_child(view, "component_id") # returns nil
So I cannot send other render_*
events to the LiveComponent.
It other LiveComponents I created, I used send
to change the state in the parent view. This did work fine but in this case the rendering of LiveComponent is not handled in the same way and it would feel wrong to add an handle_info
only for testing.
The last thing I looked into is render_component
. Again, render_component
seems to only render a definite state of the LiveComponent and that is not what I am looking for. Sure, testing that all different component states render what is expected is fine but there is no guarantee that the user actions on the UI set the component in the right state. Moreover, such tests would be hard to maintain: I want to test what is rendered when the user makes some actions, no mater what the implementation is (ie. the assigns
state).
There are also other solutions like rendering the LiveComponent and hidding it with css but I am not confortable doing such design decisions only to solve testing problems.
I may not be familiar with all the tools available for testing and maybe I am missing something, that’s why I am asking for help here… Thanks!