Skip to content

Commit

Permalink
Add assert_not_dom, refute_dom, assert_not_select, `refute_sele…
Browse files Browse the repository at this point in the history
…ct` & `refute_dom_equal`
  • Loading branch information
joshuay03 committed Dec 13, 2023
1 parent a14001a commit 396ac4b
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 6 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ css_select '.hello' # => Nokogiri::XML::NodeSet of elements with hello class
# select from a supplied node. assert_dom asserts elements exist.
assert_dom document_root_element.at('.hello'), '.goodbye'

# select from a supplied node. assert_not_dom asserts elements do not exist.
assert_not_dom document_root_element.at('.hello'), '.goodbye'

# elements in CDATA encoded sections can also be selected
assert_dom_encoded '#out-of-your-element'

Expand Down
1 change: 1 addition & 0 deletions lib/rails/dom/testing/assertions/dom_assertions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ def assert_dom_not_equal(expected, actual, message = nil, strict: false, html_ve
message ||= "Expected: #{expected}\nActual: #{actual}"
assert_not compare_doms(expected_dom, actual_dom, strict), message
end
alias_method :refute_dom_equal, :assert_dom_not_equal

protected
def compare_doms(expected, actual, strict)
Expand Down
27 changes: 26 additions & 1 deletion lib/rails/dom/testing/assertions/selector_assertions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,33 @@ def assert_dom(*args, &block)
@selected ||= nil

selector = HTMLSelector.new(args, @selected) { nodeset document_root_element }
dom_assertions(selector, &block)
end
alias_method :assert_select, :assert_dom

# The negated form of +assert_dom+.
#
# === Equality Tests
#
# Supports the same equality tests as +assert_dom+ except for:
# * <tt>true</tt>
# * <tt>false</tt>
# * <tt>Integer</tt>
# * <tt>Range</tt>
# * <tt>:count</tt>
# * <tt>:minimum</tt>
# * <tt>:maximum</tt>
def assert_not_dom(*args, &block)
@selected ||= nil

selector = HTMLSelector.new(args, @selected, refute: true) { nodeset document_root_element }
dom_assertions(selector, &block)
end
alias_method :refute_dom, :assert_not_dom
alias_method :assert_not_select, :assert_not_dom
alias_method :refute_select, :assert_not_select

private def dom_assertions(selector, &block)
if selector.selecting_no_body?
assert true
return
Expand All @@ -177,7 +203,6 @@ def assert_dom(*args, &block)
nest_selection(matches, &block) if block_given? && !matches.empty?
end
end
alias_method :assert_select, :assert_dom

# Extracts the content of an element, treats it as encoded HTML and runs
# nested assertion on it.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ class HTMLSelector # :nodoc:

include Minitest::Assertions

def initialize(values, previous_selection = nil, &root_fallback)
def initialize(values, previous_selection = nil, refute: false, &root_fallback)
@values = values
@root = extract_root(previous_selection, root_fallback)
extract_selectors
@tests = extract_equality_tests
@tests = extract_equality_tests(refute)
@message = @values.shift

if @message.is_a?(Hash)
Expand Down Expand Up @@ -97,7 +97,7 @@ def extract_selectors
@selector = context.substitute!(selector, @values)
end

def extract_equality_tests
def extract_equality_tests(refute)
comparisons = {}
case comparator = @values.shift
when Hash
Expand All @@ -113,7 +113,16 @@ def extract_equality_tests
comparisons[:count] = 0
when NilClass, TrueClass
comparisons[:minimum] = 1
else raise ArgumentError, "I don't understand what you're trying to match"
else
raise ArgumentError, "I don't understand what you're trying to match"
end

if refute
if comparisons[:count] || (comparisons[:minimum] && !comparator.nil?) || comparisons[:maximum]
raise ArgumentError, "Cannot use true, false, Integer, Range, :count, :minimum and :maximum when asserting that a selector does not match"
end

comparisons[:count] = 0
end

# By default we're looking for at least one match.
Expand Down
43 changes: 42 additions & 1 deletion test/selector_assertions_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def assert_failure(message, &block)
end

#
# Test assert select.
# Test assert_select.
#

def test_assert_select
Expand Down Expand Up @@ -210,6 +210,47 @@ def test_assert_select_text_match
end
end

#
# Test assert_not_select.
#

def test_not_select
render_html '<div id="1"></div>'
assert_not_select "p"
assert_failure(/Expected exactly 0 elements matching "div", found 1/) { assert_not_select "div" }
assert_failure(/Expected exactly 0 elements matching "div#1", found 1/) { assert_not_select "div#1" }
end

def test_not_select_with_true
render_html '<div id="1"></div>'
assert_raises(ArgumentError) { assert_not_select "div", true }
end

def test_not_select_with_false
render_html '<div id="1"></div>'
assert_raises(ArgumentError) { assert_not_select "div", false }
end

def test_not_select_with_integer
render_html '<div id="1"></div>'
assert_raises(ArgumentError) { assert_not_select "div", 1 }
end

def test_not_select_with_count
render_html '<div id="1"></div>'
assert_raises(ArgumentError) { assert_not_select "div", count: 1 }
end

def test_not_select_with_minimum
render_html '<div id="1"></div>'
assert_raises(ArgumentError) { assert_not_select "div", minimum: 1 }
end

def test_not_select_with_maximum
render_html '<div id="1"></div>'
assert_raises(ArgumentError) { assert_not_select "div", maximum: 1 }
end

#
# Test css_select.
#
Expand Down

0 comments on commit 396ac4b

Please sign in to comment.