Skip to content

Commit 0500ae5

Browse files
committed
Merge pull request #27 from rails/special-body
Fix #26 by running assertion when we aren't testing for a body.
2 parents b320028 + 59a91c7 commit 0500ae5

File tree

3 files changed

+38
-19
lines changed

3 files changed

+38
-19
lines changed

lib/rails/dom/testing/assertions/selector_assertions.rb

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,8 @@ def css_select(*args)
6060
raise ArgumentError, "you at least need a selector argument" if args.empty?
6161

6262
root = args.size == 1 ? document_root_element : args.shift
63-
selector = args.first
6463

65-
root.css(selector).tap do |matches|
66-
return nodeset(root).css(selector) if matches.empty?
67-
end
64+
nodeset(root).css(args.first)
6865
rescue Nokogiri::CSS::SyntaxError => e
6966
ActiveSupport::Deprecation.warn("The assertion was not run because of an invalid css selector.\n#{e}", caller(2))
7067
return
@@ -167,7 +164,13 @@ def css_select(*args)
167164
def assert_select(*args, &block)
168165
@selected ||= nil
169166

170-
selector = HTMLSelector.new(args, self, @selected)
167+
selector = HTMLSelector.new(args, @selected) { nodeset document_root_element }
168+
169+
if selecting_no_body?(selector)
170+
assert true
171+
return
172+
end
173+
171174
selector.select.tap do |matches|
172175
assert_size_match!(matches.size, selector.tests, selector.selector, selector.message)
173176

@@ -258,14 +261,14 @@ def assert_select_email(&block)
258261
end
259262
end
260263

261-
def document_root_element
262-
raise NotImplementedError, 'Implementing document_root_element makes ' \
263-
'assert_select work without needing to specify an element to select from.'
264-
end
265-
266264
private
267265
include CountDescripable
268266

267+
def document_root_element
268+
raise NotImplementedError, 'Implementing document_root_element makes ' \
269+
'assert_select work without needing to specify an element to select from.'
270+
end
271+
269272
# +equals+ must contain :minimum, :maximum and :count keys
270273
def assert_size_match!(size, equals, css_selector, message = nil)
271274
min, max, count = equals[:minimum], equals[:maximum], equals[:count]
@@ -279,6 +282,12 @@ def assert_size_match!(size, equals, css_selector, message = nil)
279282
end
280283
end
281284

285+
def selecting_no_body?(html_selector)
286+
# Nokogiri gives the document a body element. Which means we can't
287+
# run an assertion expecting there to not be a body.
288+
html_selector.selector == 'body' && html_selector.tests[:count] == 0
289+
end
290+
282291
def nest_selection(selection)
283292
# Set @selected to allow nested assert_select.
284293
# Can be nested several levels deep.

lib/rails/dom/testing/assertions/selector_assertions/html_selector.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
class HTMLSelector #:nodoc:
55
attr_reader :selector, :tests, :message
66

7-
def initialize(values, rootable, previous_selection = nil)
7+
def initialize(values, previous_selection = nil, &root_fallback)
88
@values = values
9-
@root = extract_root(rootable, previous_selection)
9+
@root = extract_root(previous_selection, root_fallback)
1010
@selector = extract_selector
1111
@tests = extract_equality_tests
1212
@message = @values.shift
@@ -50,7 +50,7 @@ def filter(matches)
5050
Nokogiri::XML::NodeSet.new(matches.document, remaining)
5151
end
5252

53-
def extract_root(rootable, previous_selection)
53+
def extract_root(previous_selection, root_fallback)
5454
possible_root = @values.first
5555

5656
if possible_root == nil
@@ -63,7 +63,7 @@ def extract_root(rootable, previous_selection)
6363
elsif previous_selection
6464
previous_selection
6565
else
66-
rootable.document_root_element
66+
root_fallback.call
6767
end
6868
end
6969

test/selector_assertions_test.rb

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -280,12 +280,18 @@ def test_feed_item_encoded
280280
end
281281

282282
def test_body_not_present_in_empty_document
283-
render_html ''
283+
render_html '<div></div>'
284284
assert_select 'body', 0
285285
end
286286

287-
def document_root_element
288-
@html_document
287+
def test_body_class_can_be_tested
288+
render_html '<body class="foo"></body>'
289+
assert_select '.foo'
290+
end
291+
292+
def test_body_class_can_be_tested_with_html
293+
render_html '<html><body class="foo"><div></div></body></html>'
294+
assert_select '.foo'
289295
end
290296

291297
protected
@@ -299,9 +305,13 @@ def render_xml(xml)
299305

300306
def fake_render(content_type, content)
301307
@html_document = if content_type == :xml
302-
Nokogiri::XML::DocumentFragment.parse(content)
308+
Nokogiri::XML::Document.parse(content)
303309
else
304-
Nokogiri::HTML::DocumentFragment.parse(content)
310+
Nokogiri::HTML::Document.parse(content)
305311
end
306312
end
313+
314+
def document_root_element
315+
@html_document.root
316+
end
307317
end

0 commit comments

Comments
 (0)