From 4a2618e2f23b8501ba12c0860f127e8896f5abaf Mon Sep 17 00:00:00 2001 From: 3v0k4 Date: Sat, 31 May 2025 12:14:10 +0200 Subject: [PATCH] Fix assert_not_select on body for text or html --- .../dom/testing/assertions/selector_assertions.rb | 6 +----- .../selector_assertions/html_selector.rb | 8 ++++---- test/selector_assertions_test.rb | 14 +++++++++++++- 3 files changed, 18 insertions(+), 10 deletions(-) diff --git a/lib/rails/dom/testing/assertions/selector_assertions.rb b/lib/rails/dom/testing/assertions/selector_assertions.rb index 18602d0..41fcb75 100644 --- a/lib/rails/dom/testing/assertions/selector_assertions.rb +++ b/lib/rails/dom/testing/assertions/selector_assertions.rb @@ -191,11 +191,7 @@ def assert_not_dom(*args, &block) alias_method :refute_select, :assert_not_dom private def dom_assertions(selector, &block) - if selector.selecting_no_body? - assert true - return - end - + selector.ensure_no_assertion_on_nokogiri_body! count, max = selector.tests.slice(:count, :maximum).values selector.select.tap do |matches| 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 b3bcce2..c478222 100644 --- a/lib/rails/dom/testing/assertions/selector_assertions/html_selector.rb +++ b/lib/rails/dom/testing/assertions/selector_assertions/html_selector.rb @@ -30,10 +30,10 @@ def initialize(values, previous_selection = nil, refute: false, &root_fallback) end end - def selecting_no_body? # :nodoc: - # Nokogiri gives the document a body element. Which means we can't - # run an assertion expecting there to not be a body. - @selector == "body" && @tests[:count] == 0 + def ensure_no_assertion_on_nokogiri_body! # :nodoc: + # Nokogiri gives the document a body element when missing + return unless @selector == "body" && (@tests.keys & [:text, :html]).none? + raise ArgumentError, "Assertions on body can only be for text or html" end def select diff --git a/test/selector_assertions_test.rb b/test/selector_assertions_test.rb index 6797495..acf3d99 100644 --- a/test/selector_assertions_test.rb +++ b/test/selector_assertions_test.rb @@ -477,7 +477,8 @@ def test_feed_item_encoded_with_html_version def test_body_not_present_in_empty_document render_html "
" - assert_select "body", 0 + error = assert_raises(ArgumentError) { assert_select "body", 0 } + assert_equal "Assertions on body can only be for text or html", error.message end def test_body_class_can_be_tested @@ -490,6 +491,17 @@ def test_body_class_can_be_tested_with_html assert_select ".foo" end + def test_assert_not_select_on_body_with_text + render_html "

foo

bar

" + assert_failure(/Expected exactly 0 elements matching "body", found 1/) { assert_not_select "body", "foobar" } + assert_failure(/Expected exactly 0 elements matching "body", found 1/) { assert_not_select "body", /foo/ } + end + + def test_assert_not_select_on_body_with_html + render_html "
foo
" + assert_failure(/Expected exactly 0 elements matching "body", found 1/) { assert_not_select "body", { html: "
foo
" } } + end + def test_assert_select_with_extra_argument render_html "Welcome
"