Hello!
I’m still a newcomer to Elixir and Phoenix, I’ve only started using them early this year. So asking kindly for understanding if the question I ask turns out to be trivial
The app I’m now working on in my project has several buttons which serve as links to patch the current LiveView (which results in UI changes, e.g. tab change in a menu). In its simplest form, the pattern is as follows:
<.link patch={~p(/users/#{@id}/edit)}>
<.button>
Edit
</.button>
</.link>
The issue I’m currently facing is how to properly test such components - specifically, how to test that pressing this link will result in a patch as expected. Basically, I’d like to test the following series of events: (1) open view → (2) assert some html → (3) click link → (4) assert patch → (5) assert some different html. So step #3 is where I’m currently stuck at.
render_click
won’t do in this case, since the Phoenix.Component.link/1
component does not have a phx-click
attribute. Looking through the Phoenix.LiveViewTest documentation, I didn’t find anything that seems to work specifically for links.
Of course, the two less-than-satisfying options that come to mind now are:
- Not test interacting with the link directly and just simulate the resulting patch with
render_patch/2
(seems like this reduces test reliability since the patch link is dynamic, dependent onassigns
) - Replace all instances of the above pattern with regular buttons with
phx-click
, and trigger the patch in the event handler (much better option from test reliability perspective, but modifying app code solely for testing purposes is probably not ideal, unless necessary)
But before I go with either option I wanted to ask here - is there any way to test patch navigations triggered by Phoenix link components in LiveViewTest that I’m missing?