Usually in tests of my views, I have used safe_to_string() to convert a returned :safe tuple to HTML that I can assert on. For example:
test "greets the user" do
user = %{name: "Kurt"}
html = MyView.render("some_template.html", user: user) |> Phoenix.HTML.safe_to_string()
assert html =~ "Hello, Kurt"
end
However, if the template is a .heex file, this no longer works because the return struct is a %Phoenix.LiveView.Rendered{} struct, e.g.:
Okay, here’s what I found after digging around for a while:
test "greets the user" do
user = %{name: "Kurt"}
html = MyView.render("some_template.html", user: user)
|> Phoenix.HTML.Safe.to_iodata()
|> IO.iodata_to_binary()
assert html =~ "Hello, Kurt"
end
Will give the returned HTML as string, ready for assertions.
but that what I was asking, because we have no views now, what’s the appropriate code, I am new to phx so without seeing any concrete code I couldn’t imagine what it should look like
Thank you for the suggestion, I know about liveview and I will look at it when I am ready, for now I just have a specific use case to send the html that way
from your output I see that tags are replaced by < and > how I can only stripes the tags coming from the assigns, as the static tags in the template are already safe
I could not figure out how to skip the annotations when they are globally enabled in dev. Would you have an example of how to skip them?
Having the annotations in dev is not problem since they won’t be there in prod, but your comment got me curious
In my testing everything rendered off a ~H"""...""" HEEx template in an environment with annotations enabled gets the annotations. IIUC that’s expected because sigil_H/2 is a macro that injects the annotations at compilation time.
It’s the first time I’m looking at this code, so excuse me if I make unsound conclusions.
My reading is that sigil_H/1 calls EEx.compile_string/2 passing Phoenix.LiveView.HTMLEngine. The former will do the annotations based on Application.get_env(:phoenix_live_view, :debug_heex_annotations, false).