Hi everyone.
I have
{:phoenix, "~> 1.5.5"},
{:phoenix_live_view, "~> 0.14.6"}
(latest versions when writing this message)
and I'm writing an ExUnit test for a liveview that pushes an event during a connected mount.
In example:
@impl true
def mount(_params, _session, socket) do
socket =
if connected?(socket),
do: push_event(socket, "test", %{"foo" => "bar"}),
else: socket
{:ok, socket}
end
@impl true
def handle_event("ping", params,socket) do
{:noreply, push_event(socket, "pong", params)}
end
It looks like push_event/3 is working fine (both, initial "test" event and the second "ping" event) because I can see the log in the frontend
const hooks = {
test: {
mounted: function () {
this.handleEvent("test", (payload) => console.log("Test:", payload))
this.handleEvent("pong", (payload) => console.log("Pong:", payload))
this.pushEvent("ping", {some: "data"})
}
}
}
let csrfToken = document.querySelector("meta[name='csrf-token']").getAttribute("content")
let liveSocket = new LiveSocket("/live", Socket, {params: {_csrf_token: csrfToken}, hooks: hooks})
<div id="test-element" phx-hook="test"></div>
Then my ExUnit test is:
test "event on mount", %{conn: conn} do
{:ok, page_live, _disconnected_html} = live(conn, "/")
assert_push_event(page_live, "test", %{"foo" => "bar"}, 2_000)
end
test "ping", %{conn: conn} do
{:ok, page_live, _disconnected_html} = live(conn, "/")
render_hook(page_live, "ping", %{"some" => "data"})
assert_push_event(page_live, "pong", %{"some" => "data"}, 2_000)
end
Although "ping" test is passing, it looks like events pushed during mount are not being sent to the test process because it never gets the message no matter how long the timeout is.
Is this a development issue as stated in
CONTRIBUTING.md? Am I using the push event functionality in a wrong way? Is it not meant to be used during a connected mount?
If this is actually an issue, should I create a new issue into the phoenix_live_view github repo? I may try to submit a pull request to improve this.