coladarci
Hello, all!
I’m curious if anyone has ideas/experience around the best ways to go about testing live view components. Obviously you can write a test for the live route that loads up the full page, start clicking things, etc, but for a complicated setup with many stateful components, it’s extremely difficult to reach good coverage from testing that high up.
In ember (and I’m sure other front-end frameworks) there are testing practices where you can render a component on its own (the equivalent of an ephemeral live route?) and interact with it asserting along the way that the right things happen. This helps assert strong contracts; i.e what happens if you embed a component with a specific field set to nil; does the component handle it correctly.
Is there are a way to do this? Does this sound like a good path for the framework to go down? A (terrible?) hack would be a /testing scope in the router that is used to just embed a single component on the page and we’d manually add one for each component, but that might give an idea of roughly what we’d want to automate in a testing routine..
Trending in Questions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ai
- #ecto-query
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #metaprogramming
- #hex










Marked As Solved- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
mcrumm
With a little bit of setup you can definitely drive a LiveComponent under test without a library.
I will share an example and an explanation of how I got there, but it should be noted that I don’t use LiveComponent very often so this may not meet your every need
Explanation
A common pattern used in the LiveView tests themselves is to create a LiveView that you can drive under test, for example:
We can use the
run/2function to execute code on the LiveView process, so it’s capable of doing a lot of heavy lifting.If we pair this with LiveView lifecycle events, we can intercept messages to the LiveView under test as well:
So if we can construct a custom LiveView that renders a dynamic LiveComponent, then we can drive the isolated LiveComponent under test
Example
This gist has a complete example: Testing Phoenix.LiveComponent in Isolation · GitHub
The
LiveComponentTestsupport module defined in the gist exposes three functions:live_component_isolated/3- Starts a LiveView process dedicated to driving the LiveComponent under test. You pass in the conn, the LiveComponent module, and any attributes.live_component_intercept/2- Attaches a lifecycle hook to the:handle_infostage of the driver process. Use this to intercept messages from the LiveComponent. You can send the results back to the test pid to assert on them. Returns an opaque ref that can be used to remove the intercept if needed.live_component_remove_intercept/1- Removes a previously added intercept.Overall, I think this approach provides a lot of flexibility. With some additional work under-the-hood I think instead of intercept it could expose macros like
assert_lv_receive, but I will leave that for someone else to explore for nowI hope that helps!
Also Liked
mcrumm
You can use
live_isolated/3to test a LiveView in isolation, but it’s recommended to test through the router (i.e. using thelive/2test helper) as that is required to test live navigation.Note there is also
render_component/3for testing function and stateful components in isolation.nicocirio
Hi! Thanks for this great solution! In my case, I also needed to intercept events that were targeted to the parent LiveView (in cases where the LiveComponent does not define
phx-target={@myself}for a specific event). Adding the following function tolive_component_tests.exhelped me test that behavior:Hope this function is helpful for others!
APB9785
Maybe this library could be helpful also?
Last Post!
kamaroly
For those who are still looking for a solution,
I found the following package helpful: Overview — live_isolated_component v0.10.0