Skip to content

Commit

Permalink
Fix assert_has/refute_has title matching exactly (#65)
Browse files Browse the repository at this point in the history
What changed?
============

Our `assert_has/3` and `refute_has/3` helpers with `"title"` behave
differently from the rest of our assertion helpers.

By default our other assertion helpers perform a match (`=~`) operation.
Our "title" assertion performs an equality (`==`) operation.

In addition to being different from the rest, it's annoying because we
typically render the title with a `<.live_title>` tag which has newlines
and spaces.

We fix that in this commit and allow people to pass `exact: true` to
match exactly.
  • Loading branch information
germsvel authored Apr 15, 2024
1 parent fff82d1 commit 7b2f243
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
14 changes: 10 additions & 4 deletions lib/phoenix_test/assertions.ex
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,13 @@ defmodule PhoenixTest.Assertions do
assert_has(session, "title", text: text)
end

def assert_has(session, "title", text: text) do
def assert_has(session, "title", opts) do
text = Keyword.fetch!(opts, :text)
exact = Keyword.get(opts, :exact, false)
title = PhoenixTest.Driver.render_page_title(session)
matches? = if exact, do: title == text, else: title =~ text

if title == text do
if matches? do
assert true
else
raise AssertionError,
Expand Down Expand Up @@ -148,10 +151,13 @@ defmodule PhoenixTest.Assertions do
refute_has(session, "title", text: text)
end

def refute_has(session, "title", text: text) do
def refute_has(session, "title", opts) do
text = Keyword.fetch!(opts, :text)
exact = Keyword.get(opts, :exact, false)
title = PhoenixTest.Driver.render_page_title(session)
matches? = if exact, do: title == text, else: title =~ text

if title == text do
if matches? do
raise AssertionError,
message: """
Expected title not to be #{inspect(text)}
Expand Down
17 changes: 15 additions & 2 deletions test/phoenix_test/assertions_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,12 @@ defmodule PhoenixTest.AssertionsTest do
|> assert_has("title", text: "PhoenixTest is the best!")
end

test "can assert title's exactness", %{conn: conn} do
conn
|> visit("/live/index")
|> assert_has("title", text: "PhoenixTest is the best!", exact: true)
end

test "raises if title does not match expected value (Static)", %{conn: conn} do
msg =
"""
Expand Down Expand Up @@ -170,7 +176,8 @@ defmodule PhoenixTest.AssertionsTest do
end
end

test "raises if title is contained but is not exactly the same as expected", %{conn: conn} do
test "raises if title is contained but is not exactly the same as expected (with exact=true)",
%{conn: conn} do
msg =
"""
Expected title to be "PhoenixTest" but got "PhoenixTest is the best!"
Expand All @@ -180,7 +187,7 @@ defmodule PhoenixTest.AssertionsTest do
assert_raise AssertionError, msg, fn ->
conn
|> visit("/page/index")
|> assert_has("title", text: "PhoenixTest")
|> assert_has("title", text: "PhoenixTest", exact: true)
end
end

Expand Down Expand Up @@ -430,6 +437,12 @@ defmodule PhoenixTest.AssertionsTest do
|> refute_has("title", text: "Not this title either")
end

test "can be used to refute page title's exactness", %{conn: conn} do
conn
|> visit("/live/index")
|> refute_has("title", text: "PhoenixTest is the", exact: true)
end

test "raises if title matches value (Static)", %{conn: conn} do
msg =
"""
Expand Down

0 comments on commit 7b2f243

Please sign in to comment.