I wanted to test this server-side hook.
def on_mount(:default, _params, _session, socket) do
socket =
socket
|> assign(dialog_open: false)
|> assign(dialog_inner_block: nil)
|> assign(dialog_history: [])
|> attach_hook(:dialog_hook, :handle_event, &handle_event/3) # Causes error
{:cont, socket}
end
def handle_event("reset_dialog", _, socket) do
...
end
With this test.
setup %{conn: conn} do
%{conn: init_test_session(conn, %{})}
end
describe "on_mount: default" do
test "assigns dialog_open: false", %{conn: conn} do
session = get_session(conn)
{:cont, updated_socket} = Dialog.DialogHook.on_mount(:default, %{}, session, %LiveView.Socket{})
assert updated_socket.assigns.dialog_open == false
end
end
But I ran into this error.
1) test on_mount: default assigns dialog_open: false (MyAppWeb.DialogTest)
test/my_app_web/dialog_test.exs:26
** (KeyError) key :lifecycle not found in: %{__temp__: %{}}
code: {:cont, updated_socket} = Dialog.DialogHook.on_mount(:default, %{}, session, %LiveView.Socket{})
stacktrace:
:erlang.map_get(:lifecycle, %{__temp__: %{}})
(phoenix_live_view 0.19.5) lib/phoenix_live_view/lifecycle.ex:92: Phoenix.LiveView.Lifecycle.lifecycle/2
(phoenix_live_view 0.19.5) lib/phoenix_live_view/lifecycle.ex:45: Phoenix.LiveView.Lifecycle.attach_hook/4
(my_app 0.1.0) lib/my_app_web/dialog/dialog_hook.ex:11: MyAppWeb.Dialog.DialogHook.on_mount/4
test/my_app_web/dialog_test.exs:29: (test)
The socket.private
doesn’t contain the key :lifecycle
.
%LiveView.Socket{}
|> Map.get(:private)
|> IO.inspect()
=> %{__temp__: %{}}
I have been reading docs and searching around online, but cannot seem to find how to test this scenario.
How would I go about this? Any ideas?