germsvel / phoenix_test

PhoenixTest provides a unified way of writing feature tests -- regardless of whether you're testing LiveView pages or static (non-LiveView) pages.
https://hex.pm/packages/phoenix_test
MIT License
178 stars 23 forks source link

Update `phx_click?` to check for valid JS commands #139

Closed germsvel closed 1 month ago

germsvel commented 1 month ago

Closes #122

What changed?

Unless phx-click has a valid JS command, we don't really want to handle it as part of the Field.phx_click?, Button.phx_click?, or Select.phx_click? actions.

So, in this commit, we parse the phx-click attribute. When it's a JS command, we check to see if the value is push or navigate.

Why do we do this? Why not let LiveViewTest handle it?

LiveViewTest already throws an error when a phx-click has a JS command that doesn't include a push or navigate. So, why do this?

Because PhoenixTest automatically handles phx-clicks.

When an element is inside a form AND it has a phx-click that doesn't have a push or navigate (e.g. JS.dispatch/1), we need to ignore the element. We don't want to raise because it's technically valid code.

Something to note: PhoenixTest doesn't currently handle a form element AND a valid phx-click event at the same time. That is, if a form has a phx-change attribute and a checkbox has a phx-click attribute with an event or a JS.push, only one of those events (the phx-change OR the phx-click) will make it to the server. In production LiveView, both events get sent. So, that's something we want to handle, but it is not covered in this commit.