Testing phx-viewport bindings in LiveView

Hi everyone!

I’m trying to figure out how to test LiveView phx-viewport bindings using Phoenix.LiveViewTest.

Would it make sense to add a render_viewport_event function to the module, or am I missing something here?

Has anyone faced a similar challenge and come up with a working solution they’d be willing to share?

(possibly related to this issue: Add more E2E tests · Issue #3034 · phoenixframework/phoenix_live_view · GitHub)

Thanks in advance! :blush:

1 Like

Hey @moissela,

sorry for the late reply. I’m not sure if it makes sense to have such very specific functions in LiveViewTest. Maybe a generic render_event function would be better?

In the meantime, you can use render_hook(view, "event", %{foo: "bar"}) which will trigger the corresponding handle_event clause. While it’s called render_hook, it can be used for any event.

Hi @steffend,
unfortunately render_hook doesnt work in our case because the handle_event is not on the main liveview but in one of the livecomponents

Have you tried selecting an element inside the LiveComponent first?
https://hexdocs.pm/phoenix_live_view/Phoenix.LiveViewTest.html#element/3

Hey @steffend
I just tried selecting an element first and it works. The only thing worth mentioning is that i needed to put an empty “phx-hook” tag because i was facing the following error

image