Asserting `Phoenix.Component.live_title/1`

Hello!

I write LiveView tests using GitHub - iamvery/skipper style helpers (awesome library btw, thank you @iamvery). I added assert_title/2 to easily ensure I’m on the right page and that the page title is correct.

test "example", %{conn: conn} do
  conn
  |> visit("/")
  |> assert_title("Home")
end

However, mix format enforces the live title to be formatted like so:

<.live_title suffix=" · Phoenix Framework">
  <%= assigns[:page_title] %>
</.live_title>

This adds extra spaces, causing the outputted HTML to look like this:

<title data-suffix=\" · Phoenix Framework\">\nHome\n     · Phoenix Framework</title>

Having the <.live_title> on a single line works as expected:

<title data-suffix=\" · Phoenix Framework\">Home · Phoenix Framework</title>

Asserting the title does work, but it’s a bit funky:

assert html =~ ~s(<title data-suffix=" · Phoenix Framework">\n#{title}\n     · Phoenix Framework</title>)

We have mix format --check-formatted in our CI, so not formatting the file isn’t an option either.

Has anyone dealt with this kind of issue or do you have a better way of dealing with this?

Or is there a way to ignore that part from being formatted?

1 Like

You can use phx-no-format attribute to prevent formatting of a html tag and its children in heex.

1 Like

Ah how did I miss that, thank you! Damn you’re fast to reply.

I think it should be there by default, what’s your opinion?

Browsers will strip the newlines anyways. You could really do so in your test helper as well. I don’t think that really belongs into the codebase by default.

Since you control assert_title why not:

import Phoenix.LiveViewTest

def assert_title(lv, title) do
  assert lv
         |> element("title")
         |> render() =~ title
end
1 Like

I might do just that! Thanks guys.