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

Improve failure wording #634

Merged
merged 2 commits into from
Oct 16, 2021
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
12 changes: 9 additions & 3 deletions lib/wallaby/query/error_message.ex
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,9 @@ defmodule Wallaby.Query.ErrorMessage do

defp found_error_message(query) do
"""
#{expected_count(query)}, #{visibility_and_selection(query)} #{method(query)} #{
#{expected_count(query)} #{visibility_and_selection(query)} #{method(query)} #{
selector(query)
} but #{result_count(query.result)}, #{visibility_and_selection(query)} #{
} but #{result_adverb(query)}#{result_count(query.result)} #{visibility_and_selection(query)} #{
short_method(query.method, Enum.count(query.result))
} #{result_expectation(query.result)}.
"""
Expand Down Expand Up @@ -214,7 +214,13 @@ defmodule Wallaby.Query.ErrorMessage do
end
end

defp result_count([_]), do: "only 1"
defp result_adverb(query) do
conditions = query.conditions
min = conditions[:count] || conditions[:minimum]

if min && min > Enum.count(query.result) && query.result != [], do: "only "
end

defp result_count(result), do: "#{Enum.count(result)}"

defp times(1), do: "1 time"
Expand Down
34 changes: 17 additions & 17 deletions test/wallaby/query/error_message_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ defmodule Wallaby.Query.ErrorMessageTest do

assert message ==
format("""
Expected to find 1, visible element that matched the css '.test' but 3, visible
Expected to find 1 visible element that matched the css '.test' but 3 visible
elements were found.
""")
end
Expand All @@ -28,7 +28,7 @@ defmodule Wallaby.Query.ErrorMessageTest do

assert message ==
format("""
Expected to find 1, visible element that matched the css '.test' but 0, visible
Expected to find 1 visible element that matched the css '.test' but 0 visible
elements were found.
""")
end
Expand All @@ -42,22 +42,22 @@ defmodule Wallaby.Query.ErrorMessageTest do

assert message ==
format("""
Expected to find 3, visible elements that matched the css '.test' but
only 1, visible element was found.
Expected to find 3 visible elements that matched the css '.test' but
only 1 visible element was found.
""")
end

test "when the result is less then the minimum result" do
message =
Query.css(".test", minimum: 3, maximum: 5)
|> Map.put(:result, [1])
|> Map.put(:result, [1, 2])
|> ErrorMessage.message(:not_found)
|> format

assert message ==
format("""
Expected to find at least 3, visible elements that matched the css
'.test' but only 1, visible element was found.
Expected to find at least 3 visible elements that matched the css
'.test' but only 2 visible elements were found.
""")
end

Expand All @@ -70,8 +70,8 @@ defmodule Wallaby.Query.ErrorMessageTest do

assert message ==
format("""
Expected to find no more then 5, visible elements that matched the css
'.test' but 6, visible elements were found.
Expected to find no more then 5 visible elements that matched the css
'.test' but 6 visible elements were found.
""")
end

Expand All @@ -84,7 +84,7 @@ defmodule Wallaby.Query.ErrorMessageTest do

assert message ==
format("""
Expected to find 1, invisible element that matched the css '.test' but 3,
Expected to find 1 invisible element that matched the css '.test' but 3
invisible elements were found.
""")
end
Expand All @@ -106,7 +106,7 @@ defmodule Wallaby.Query.ErrorMessageTest do

assert message ==
format("""
Expected to find 1, visible, selected element that matched the css '.test' but 0,
Expected to find 1 visible, selected element that matched the css '.test' but 0
visible, selected elements were found.
""")
end
Expand All @@ -119,7 +119,7 @@ defmodule Wallaby.Query.ErrorMessageTest do

assert message ==
format("""
Expected to find 1, visible, unselected element that matched the css '.test' but 0,
Expected to find 1 visible, unselected element that matched the css '.test' but 0
visible, unselected elements were found.
""")
end
Expand All @@ -132,7 +132,7 @@ defmodule Wallaby.Query.ErrorMessageTest do

assert message ==
format("""
Expected to find 1, visible element with the text 'test' but 0, visible elements with the text were found.
Expected to find 1 visible element with the text 'test' but 0 visible elements with the text were found.
""")

message =
Expand All @@ -142,7 +142,7 @@ defmodule Wallaby.Query.ErrorMessageTest do

assert message ==
format("""
Expected to find 2, visible elements with the text 'test' but 0, visible elements with the text were found.
Expected to find 2 visible elements with the text 'test' but 0 visible elements with the text were found.
""")
end

Expand All @@ -154,7 +154,7 @@ defmodule Wallaby.Query.ErrorMessageTest do

assert message ==
format("""
Expected to find 1, visible element with the attribute 'value' with value 'test' but 0, visible elements with the attribute were found.
Expected to find 1 visible element with the attribute 'value' with value 'test' but 0 visible elements with the attribute were found.
""")
end

Expand All @@ -166,7 +166,7 @@ defmodule Wallaby.Query.ErrorMessageTest do

assert message ==
format("""
Expected to find 1, visible element with the attribute 'an-attribute' with value 'an-attribute-value' but 0, visible elements with the attribute were found.
Expected to find 1 visible element with the attribute 'an-attribute' with value 'an-attribute-value' but 0 visible elements with the attribute were found.
""")
end

Expand All @@ -178,7 +178,7 @@ defmodule Wallaby.Query.ErrorMessageTest do

assert message ==
format("""
Expected to find 1, visible element with the attribute 'data-role' with value 'data-attribute-value' but 0, visible elements with the attribute were found.
Expected to find 1 visible element with the attribute 'data-role' with value 'data-attribute-value' but 0 visible elements with the attribute were found.
""")
end
end
Expand Down