Today I played with testing LiveViews, super easy and fast testing as a result, big thanks for this feature!
I found one inconsistency but I think it is just because newcomers like me don’t understand how LiveView works under the hood. That is related to events which happen inside the stateful LiveComponent. I’ve created a small example here. As you can see there are two LiveViews: one with component and the other one without it just as an example. There is just a
phx-submit event handling.
I used the way where the View is a source of truth of the current state so in LiveComponent’s
hande_event/3 callback I just send the message to the view existing process which leads to update of the socket assign in the View’s
What I expected in the tests for the view with the form component is that when I do
html = page_live |> element("form") |> render(submit)
it will call
LIveView.render/1 function again as said in the documentation here. I assume that it don’t because the html doesn’t change.
You can see these assertions in this test file. To get the updated html I had to call
Phoenix.LiveViewTest.render/1 function explicitly.
I think that is expected behavior, I just wanted to share with experience because it took some time to realize that LiveView won’t be re-rendered by itself. I noticed it when I started splitting my LiveView into components and had the failed test. I suppose that the docs should be updated accordingly.