coen.bakker
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?
Trending in Questions
Hey guys,
I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly
Do you guys have any suggestions what is the best prac...
New
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
Hello!
Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app.
I creat...
New
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
Other Trending Topics
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve.
They are GUI (Emerge) and State management (S...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixirconf-us
- #elixir-ls
- #ai
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming











Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
LostKobrakai
I don’t think you can test those callbacks in isolation due to missing simple constructors for that state involved.
Instead I’d suggest creating a full LV module within the test and interact with that one to assert the hooks functionality.
coen.bakker
Let me see if I understand correctly.
So I create a minimal LiveView and attach to it the hook I want to test?
Would I be able to make assertions about the lv’s assigns directly? Since I see that the
lvreturned bylive_isolated/3does not carry the assigns.sodapopcan
With LiveView tests you can only assert on behaviour, ie, on the outputted HTML. Well, ok, you can use
:sys.get_stateto look at a process’ state, but I realllllly wouldn’t recommend it.Harking back to your recent post regarding testing, these are the types of unit tests I don’t bother with. Dialogs are very simple so there is really nothing to be gained from unit testing them in that way. So long as they open, display the correct contents, and all the buttons do what they are supposed to, then the desin of the internal state really doesn’t matter. It also means you could rejig the internals later on without changing the test. That is to say, a LiveViewTest asserting on HTML in a LiveView test is perfectly adequate here. Just my opinion, of course.
coen.bakker
Let’s say someone is stubborn and wants to see how they can test it anyway.
Would this be ridiculous?
Asking for a friend, of course.
coen.bakker
And I started reading the “Testing Elixir” book. Have good hope I’ll learn some valuable lessons from it. Want to demystify testing…
sodapopcan
For argument’s sake you could do this, yes, but you’re making a little more work for yourself, cluttering up your markup with test-related data, and coupling the test to the implementation.
LiveView tests are UI tests so we generally try and just test how the user interacts with it. A better option would be to set up a LiveView with a button that opens the dialog then you can write tests like this:
It’s up to your friend, of course!
coen.bakker
Maybe it’s my implementation then
.
The dialog root component is placed in a layout. All the live views that use that layout automatically can use that dialog. The live views will, however, pass the inner block that needs to be rendering inside the dialog dynamically.
So they never do this.
But instead they do.
Dailog.set_dialog(socket, &functional_component/1, payload). TheDialogmodule exposes all the dialog actions that are possible. There is alsoDialog.update_payload/1, for example.And the live views get events back from the dialog so they can react to an event appropriately according to their needs.
So far it works well, actually. So far.
Definitely learning from the process, so that’s a plus.
coen.bakker
Appreciate all the input!
sodapopcan
Sorry, that was me just not understanding how the dialog is set up so I just made it up, lol. The open button’s click event should be
<button phx-click="set_dialog/3">Open</button>and thenassert render(lv) =~ "test value". Of course maybe you aren’t using an open button? If not, just set up the LV however it’s going to work in the wild and then you can use all the functions inPhoenix.LiveViewTestto test it just like someone would actually interact with it.I do wonder why you aren’t using a LiveComponent for this, they are generally very well suited to dialogs! If you use a slot for the open trigger (again, if you even have one) you can keep all the state completely encapsulated in the component. And of course this style of testing makes it really easy to refactor to one.
No prob! I love talking testing. Though as I said in the other thread, I wouldn’t call myself an expert, so maybe I’ll say something stupid enough that it’ll cause someone else to jump in
I do know a thing or two, though 
coen.bakker
Well… That’s a great question.
Back when the dialog was just a baby it seemed unnecessary, but I think it’s time to reconsider
.
I was originally motivated by this “confirm” component in the LiveBook codebase.
https://github.com/livebook-dev/livebook/blob/main/lib/livebook_web/components/confirm.ex
I thought it was such a nice solution. But that was before I started stuffing whole inner_blocks and payloads down the dialog.