Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix assert_has/refute_has title matching exactly #65

Merged
merged 1 commit into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading