sodapopcan

sodapopcan

Testing portals with LiveViewTest

From the portal docs:

Because portals use elements under the hood, you cannot query for elements inside of a portal when using Phoenix.LiveViewTest.element/3 and other LiveViewTest functions.

Instead, Phoenix.LiveViewTest.render/1 the portal element itself to an HTML string and do assertions on those:

<.portal id="my-portal" target="body">
  <div id="something-inside">...</div>
</.portal>

# in your test, instead of
# assert has_element?(view, "#something-inside")  <-- this won't work
html = view |> element("#my-portal") |> render()
assert html =~ "something-inside"

I’m assuming this means that LiveViewTest cannot interact with elements in portal’s? It would seem to be the case as my coworker ran into this and now I’m running into it. I have an extremely hacky work around to click buttons but I want to make sure I’m not missing something.

Most Liked

garrison

garrison

That’s kinda unfortunate.

Portals are an ugly hack all around. In my JS framework my plan is to avoid implementing them at all if I can help it. I’m pretty sure everything I would use portals for is now covered by the top layer APIs (dialog/popover). In typical web platform fashion they’re essentially unusable for anything serious without JS escape hatches, but I can work with that.

This just reminded me to check how Anchor Positioning is doing, and apparently it’s set to drop in FF in 2 days! Then we will be free :slight_smile:

sodapopcan

sodapopcan

I don’t disagree, though they sure are convenient! We use dialog/popover for modals, but portal is so convenient for context menus. I was using a “custom portal” implementation that was testable but quite hacky more to the point, a recent LiveView commit broke it. The portal implementation is far simpler but quite unfortunate about not being able to test with LiveViewTest. I do have the work-around, I just wish the docs were a lil’ bit clearer. I’ll likely just make a PR to try and clarify it.

Last Post!

garrison

garrison

The dream is to use popover/anchor positioning for menus and tooltips. Soon!

Where Next?

Popular in Questions Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New

Other popular topics Top

Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

We're in Beta

About us Mission Statement