Skip to content

assert_dom :text collapses whitespace #123

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ GEM

PLATFORMS
aarch64-linux
x86_64-darwin-23
x86_64-linux

DEPENDENCIES
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,16 @@ def filter(matches)

content_mismatch = nil
text_matches = tests.has_key?(:text)
html_matches = tests.has_key?(:html)
regex_matching = match_with.is_a?(Regexp)

remaining = matches.reject do |match|
# Preserve markup with to_s for html elements
content = text_matches ? match.text : match.children.to_s
content = text_matches ? match.text : match.inner_html

content.strip! unless NO_STRIP.include?(match.name)
content.delete_prefix!("\n") if text_matches && match.name == "textarea"
collapse_html_whitespace!(content) unless NO_STRIP.include?(match.name) || html_matches

next if regex_matching ? (content =~ match_with) : (content == match_with)
content_mismatch ||= diff(match_with, content)
Expand Down Expand Up @@ -138,6 +140,10 @@ def extract_equality_tests(refute)

comparisons
end

def collapse_html_whitespace!(text)
text.gsub!(/\s+/, " ")
end
end
end
end
Expand Down
36 changes: 36 additions & 0 deletions test/selector_assertions_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,42 @@ def test_assert_select_with_invalid_minimum_and_maximum
assert_equal "Range begin or :minimum cannot be greater than Range end or :maximum", error.message
end

def test_assert_select_text_equality_collapses_whitespace
render_html "<p>Some\n line-broken\n text</p>"

assert_nothing_raised do
assert_select "p", {
text: "Some line-broken text",
}, "Whitespace was not collapsed from text"
end

render_html "<p>Some<br><br>line-broken<br><br>text</p>"

assert_nothing_raised do
assert_select "p", {
text: "Someline-brokentext",
}, "<br> was not removed from text"
end
end

def test_assert_select_html_equality_respects_whitespace
render_html "<p>Some\n line-broken\n text</p>"

assert_nothing_raised do
assert_select "p", {
html: "Some\n line-broken\n text",
}, "Whitespace was collapsed from html"
end

render_html "<p>Some<br><br>line-broken<br><br>text</p>"

assert_nothing_raised do
assert_select "p", {
html: "Some<br><br>line-broken<br><br>text",
}, "<br> was removed from html"
end
end

#
# Test assert_not_select.
#
Expand Down