JohnnyCurran

JohnnyCurran

Allow Floki options to be passed to render_* functions in Phoenix.LiveViewTest

Hello,

The problem: HTML special characters can cause =~ assertions to fail in LiveViewTest - Especially if used in conjunction with libraries that generate random data AKA Faker

Under the hood, all render_* functions in Phoenix.LiveViewTest eventually find their way back to the function Phoenix.LiveViewTest.render/1 which is essentially a wrapper around Floki.raw_html/2

raw_html/2 accepts a list of options (currently only 1 option): a boolean named :encode. If :encode is true (which it is by default), then things such as O'Connell become encoded and rendered as O'Connell in the test. This causes test assertions to fail intermittently.

What’s the current solution?

You can, in config/test.exs, pass config :floki, :encode_raw_html, false which will turn this off for all tests.

In the event we want to enable / disable this behavior for specific assertion(s), we are unable.

I would like to see the ability to do:

assert render(view, encode: false) =~ "O'Doyle"
assert render(view, encode: true) =~ "O&#39Doyle"

Currently, the only way I can think to do this is expand each of the render_* functions with an optional args \\ [] argument but that seems cumbersome - and there are lots of render functions already.

I welcome all thoughts for or against the idea as well as ideas on how this might be better implemented

Thank you

You can read more about the problem described at Fix Your Flaky Faker Tests - John Elm Labs

First Post!

sodapopcan

sodapopcan

I basically just stopped using test data with special chars since I found it too frustrating. A little tangentially I do find it a bit of a footgun since I generally think of LiveView tests of asserting what the user sees, but of course they live in this strange realm between e2es and integration tests.

The simplest option I can think of now that you’ve made me think of it again would be:

assert render(view) =~ Floki.raw_html("O'Doyle")

…which could be wrapped up in an encode or raw helper or something. It might be more flexible if it were:

def encode(string) do
  string
  |> Floki.parse_fragment!()
  |> Floki.raw_html()
end

though I haven’t thought through how that might mess with regexes :thinking:

Happy you brought this up!

Where Next?

Popular in Proposals: Ideas Top

alaadahmed
Hi folks, I tried Phoenix 1.7 and it is awesome, but I have small suggestion, which in my opinion will organize files in a better way. ...
New
hst337
Elixir compiler and language specification Purpose of the proposal Elixir language is in mature state and no breaking or heavy changes ar...
New
Aduril
Hello there, Whenever I setup a new project, there is a small function I always add: reply/1. What does it do? In a LiveView mount, han...
New
lessless
Hi, There are a few writeups describing alternative config arrangements by a topic (or an OTP app): Configuring Phoenix apps: Two sma...
New
pinetops
LiveView is by far my favorite web tech, but a few things have been nagging me. So with all the fancy and ill advised elixir tricks I cou...
New
andypearson
Hey all, I have been working on some performance improvements for the Phoenix application I work on. As part of this, through trial and...
New
cevado
I was reading the EEP-79, and thinking about the poor record support in Elixir(i’ve tried to discuss about that in the forum before). I s...
New

Other popular topics Top

grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 54006 488
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 40042 209
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New

We're in Beta

About us Mission Statement