diff --git a/Gemfile.lock b/Gemfile.lock index d977de2..63df29a 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -89,6 +89,7 @@ GEM PLATFORMS aarch64-linux + x86_64-darwin-23 x86_64-linux DEPENDENCIES diff --git a/lib/rails/dom/testing/assertions/selector_assertions/html_selector.rb b/lib/rails/dom/testing/assertions/selector_assertions/html_selector.rb index b44d655..51f0b90 100644 --- a/lib/rails/dom/testing/assertions/selector_assertions/html_selector.rb +++ b/lib/rails/dom/testing/assertions/selector_assertions/html_selector.rb @@ -18,6 +18,7 @@ def initialize(values, previous_selection = nil, refute: false, &root_fallback) @values = values @root = extract_root(previous_selection, root_fallback) extract_selectors + @strict = false @tests = extract_equality_tests(refute) @message = @values.shift @@ -59,6 +60,7 @@ def filter(matches) 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) || @strict next if regex_matching ? (content =~ match_with) : (content == match_with) content_mismatch ||= diff(match_with, content) @@ -136,8 +138,14 @@ def extract_equality_tests(refute) raise ArgumentError, "Range begin or :minimum cannot be greater than Range end or :maximum" end + @strict = comparisons[:strict] + comparisons end + + def collapse_html_whitespace!(text) + text.gsub!(/\s+/, " ") + end end end end diff --git a/test/selector_assertions_test.rb b/test/selector_assertions_test.rb index 6aff0fc..0d98251 100644 --- a/test/selector_assertions_test.rb +++ b/test/selector_assertions_test.rb @@ -237,6 +237,66 @@ 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_not_strict_collapses_whitespace + render_html "
Some\n line-broken\n text
" + + assert_nothing_raised do + assert_select "p", { + text: "Some line-broken text", + strict: false + }, "Whitespace was not collapsed from text when not strict" + + assert_select "p", { + html: "Some line-broken text", + strict: false + }, "Whitespace was not collapsed from html when not strict" + end + + render_html "Some
line-broken
text
Some\n line-broken\n text
" + + assert_nothing_raised do + assert_select "p", { + text: "Some\n line-broken\n text", + strict: true + }, "Whitespace was collapsed from text when strict" + + assert_select "p", { + html: "Some\n line-broken\n text", + strict: true + }, "Whitespace was collapsed from html when strict" + end + + render_html "Some
line-broken
text