Skip to content

Commit 396ac4b

Browse files
committed
Add assert_not_dom, refute_dom, assert_not_select, refute_select & refute_dom_equal
1 parent a14001a commit 396ac4b

File tree

5 files changed

+85
-6
lines changed

5 files changed

+85
-6
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ css_select '.hello' # => Nokogiri::XML::NodeSet of elements with hello class
2424
# select from a supplied node. assert_dom asserts elements exist.
2525
assert_dom document_root_element.at('.hello'), '.goodbye'
2626

27+
# select from a supplied node. assert_not_dom asserts elements do not exist.
28+
assert_not_dom document_root_element.at('.hello'), '.goodbye'
29+
2730
# elements in CDATA encoded sections can also be selected
2831
assert_dom_encoded '#out-of-your-element'
2932

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ def assert_dom_not_equal(expected, actual, message = nil, strict: false, html_ve
7070
message ||= "Expected: #{expected}\nActual: #{actual}"
7171
assert_not compare_doms(expected_dom, actual_dom, strict), message
7272
end
73+
alias_method :refute_dom_equal, :assert_dom_not_equal
7374

7475
protected
7576
def compare_doms(expected, actual, strict)

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

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,33 @@ def assert_dom(*args, &block)
164164
@selected ||= nil
165165

166166
selector = HTMLSelector.new(args, @selected) { nodeset document_root_element }
167+
dom_assertions(selector, &block)
168+
end
169+
alias_method :assert_select, :assert_dom
170+
171+
# The negated form of +assert_dom+.
172+
#
173+
# === Equality Tests
174+
#
175+
# Supports the same equality tests as +assert_dom+ except for:
176+
# * <tt>true</tt>
177+
# * <tt>false</tt>
178+
# * <tt>Integer</tt>
179+
# * <tt>Range</tt>
180+
# * <tt>:count</tt>
181+
# * <tt>:minimum</tt>
182+
# * <tt>:maximum</tt>
183+
def assert_not_dom(*args, &block)
184+
@selected ||= nil
167185

186+
selector = HTMLSelector.new(args, @selected, refute: true) { nodeset document_root_element }
187+
dom_assertions(selector, &block)
188+
end
189+
alias_method :refute_dom, :assert_not_dom
190+
alias_method :assert_not_select, :assert_not_dom
191+
alias_method :refute_select, :assert_not_select
192+
193+
private def dom_assertions(selector, &block)
168194
if selector.selecting_no_body?
169195
assert true
170196
return
@@ -177,7 +203,6 @@ def assert_dom(*args, &block)
177203
nest_selection(matches, &block) if block_given? && !matches.empty?
178204
end
179205
end
180-
alias_method :assert_select, :assert_dom
181206

182207
# Extracts the content of an element, treats it as encoded HTML and runs
183208
# nested assertion on it.

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

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ class HTMLSelector # :nodoc:
1414

1515
include Minitest::Assertions
1616

17-
def initialize(values, previous_selection = nil, &root_fallback)
17+
def initialize(values, previous_selection = nil, refute: false, &root_fallback)
1818
@values = values
1919
@root = extract_root(previous_selection, root_fallback)
2020
extract_selectors
21-
@tests = extract_equality_tests
21+
@tests = extract_equality_tests(refute)
2222
@message = @values.shift
2323

2424
if @message.is_a?(Hash)
@@ -97,7 +97,7 @@ def extract_selectors
9797
@selector = context.substitute!(selector, @values)
9898
end
9999

100-
def extract_equality_tests
100+
def extract_equality_tests(refute)
101101
comparisons = {}
102102
case comparator = @values.shift
103103
when Hash
@@ -113,7 +113,16 @@ def extract_equality_tests
113113
comparisons[:count] = 0
114114
when NilClass, TrueClass
115115
comparisons[:minimum] = 1
116-
else raise ArgumentError, "I don't understand what you're trying to match"
116+
else
117+
raise ArgumentError, "I don't understand what you're trying to match"
118+
end
119+
120+
if refute
121+
if comparisons[:count] || (comparisons[:minimum] && !comparator.nil?) || comparisons[:maximum]
122+
raise ArgumentError, "Cannot use true, false, Integer, Range, :count, :minimum and :maximum when asserting that a selector does not match"
123+
end
124+
125+
comparisons[:count] = 0
117126
end
118127

119128
# By default we're looking for at least one match.

test/selector_assertions_test.rb

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def assert_failure(message, &block)
1616
end
1717

1818
#
19-
# Test assert select.
19+
# Test assert_select.
2020
#
2121

2222
def test_assert_select
@@ -210,6 +210,47 @@ def test_assert_select_text_match
210210
end
211211
end
212212

213+
#
214+
# Test assert_not_select.
215+
#
216+
217+
def test_not_select
218+
render_html '<div id="1"></div>'
219+
assert_not_select "p"
220+
assert_failure(/Expected exactly 0 elements matching "div", found 1/) { assert_not_select "div" }
221+
assert_failure(/Expected exactly 0 elements matching "div#1", found 1/) { assert_not_select "div#1" }
222+
end
223+
224+
def test_not_select_with_true
225+
render_html '<div id="1"></div>'
226+
assert_raises(ArgumentError) { assert_not_select "div", true }
227+
end
228+
229+
def test_not_select_with_false
230+
render_html '<div id="1"></div>'
231+
assert_raises(ArgumentError) { assert_not_select "div", false }
232+
end
233+
234+
def test_not_select_with_integer
235+
render_html '<div id="1"></div>'
236+
assert_raises(ArgumentError) { assert_not_select "div", 1 }
237+
end
238+
239+
def test_not_select_with_count
240+
render_html '<div id="1"></div>'
241+
assert_raises(ArgumentError) { assert_not_select "div", count: 1 }
242+
end
243+
244+
def test_not_select_with_minimum
245+
render_html '<div id="1"></div>'
246+
assert_raises(ArgumentError) { assert_not_select "div", minimum: 1 }
247+
end
248+
249+
def test_not_select_with_maximum
250+
render_html '<div id="1"></div>'
251+
assert_raises(ArgumentError) { assert_not_select "div", maximum: 1 }
252+
end
253+
213254
#
214255
# Test css_select.
215256
#

0 commit comments

Comments
 (0)