Skip to content

Commit 3d2cc8d

Browse files
committed
Remove deprecation, document alias
Remove all deprecations of `assert_select`-prefixed methods, but document the `assert_dom` variations.
1 parent 267487e commit 3d2cc8d

File tree

4 files changed

+55
-63
lines changed

4 files changed

+55
-63
lines changed

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22

33
This gem is responsible for comparing HTML doms and asserting that DOM elements are present in Rails applications.
44
Doms are compared via `assert_dom_equal` and `assert_dom_not_equal`.
5-
Elements are asserted via `assert_select`, `assert_select_encoded`, `assert_select_email` and a subset of the dom can be selected with `css_select`.
5+
Elements are asserted via `assert_dom`, `assert_dom_encoded`, `assert_dom_email` and a subset of the dom can be selected with `css_select`.
66
The gem is developed for Rails 4.2 and above, and will not work on previous versions.
77

88
## Nokogiri::CSS::SyntaxError exceptions when upgrading to Rails 4.2:
99

1010
Nokogiri is slightly stricter about the format of CSS selectors than the previous implementation.
1111

12-
Check the 4.2 release notes [section on `assert_select`](http://edgeguides.rubyonrails.org/4_2_release_notes.html#assert-select) for help.
12+
Check the 4.2 release notes [section on `assert_dom`](http://edgeguides.rubyonrails.org/4_2_release_notes.html#assert-select) for help.
1313

1414
## Installation
1515

@@ -41,14 +41,14 @@ assert_dom_not_equal '<h1>Portuguese</h1>', '<h1>Danish</h1>'
4141
# implicitly selects from the document_root_element
4242
css_select '.hello' # => Nokogiri::XML::NodeSet of elements with hello class
4343

44-
# select from a supplied node. assert_select asserts elements exist.
45-
assert_select document_root_element.at('.hello'), '.goodbye'
44+
# select from a supplied node. assert_dom asserts elements exist.
45+
assert_dom document_root_element.at('.hello'), '.goodbye'
4646

4747
# elements in CDATA encoded sections can also be selected
48-
assert_select_encoded '#out-of-your-element'
48+
assert_dom_encoded '#out-of-your-element'
4949

5050
# assert elements within an html email exists
51-
assert_select_email '#you-got-mail'
51+
assert_dom_email '#you-got-mail'
5252
```
5353

5454
The documentation in [selector_assertions.rb](https://github.com/rails/rails-dom-testing/blob/master/lib/rails/dom/testing/assertions/selector_assertions.rb) goes into a lot more detail of how selector assertions can be used.

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

Lines changed: 48 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,22 @@
1-
require 'active_support/deprecation'
21
require_relative 'selector_assertions/count_describable'
32
require_relative 'selector_assertions/html_selector'
43

54
module Rails
65
module Dom
76
module Testing
87
module Assertions
9-
# Adds the +assert_select+ method for use in Rails functional
8+
# Adds the +assert_dom+ method for use in Rails functional
109
# test cases, which can be used to make assertions on the response HTML of a controller
11-
# action. You can also call +assert_select+ within another +assert_select+ to
10+
# action. You can also call +assert_dom+ within another +assert_dom+ to
1211
# make assertions on elements selected by the enclosing assertion.
1312
#
1413
# Use +css_select+ to select elements without making an assertions, either
1514
# from the response HTML or elements selected by the enclosing assertion.
1615
#
1716
# In addition to HTML responses, you can make the following assertions:
1817
#
19-
# * +assert_select_encoded+ - Assertions on HTML encoded inside XML, for example for dealing with feed item descriptions.
20-
# * +assert_select_email+ - Assertions on the HTML body of an e-mail.
18+
# * +assert_dom_encoded+ - Assertions on HTML encoded inside XML, for example for dealing with feed item descriptions.
19+
# * +assert_dom_email+ - Assertions on the HTML body of an e-mail.
2120
module SelectorAssertions
2221

2322
# Select and return all matching elements.
@@ -70,41 +69,41 @@ def css_select(*args)
7069
# starting from (and including) that element and all its children in
7170
# depth-first order.
7271
#
73-
# If no element is specified +assert_select+ selects from
72+
# If no element is specified +assert_dom+ selects from
7473
# the element returned in +document_root_element+
75-
# unless +assert_select+ is called from within an +assert_select+ block.
76-
# Override +document_root_element+ to tell +assert_select+ what to select from.
74+
# unless +assert_dom+ is called from within an +assert_dom+ block.
75+
# Override +document_root_element+ to tell +assert_dom+ what to select from.
7776
# The default implementation raises an exception explaining this.
7877
#
79-
# When called with a block +assert_select+ passes an array of selected elements
80-
# to the block. Calling +assert_select+ from the block, with no element specified,
78+
# When called with a block +assert_dom+ passes an array of selected elements
79+
# to the block. Calling +assert_dom+ from the block, with no element specified,
8180
# runs the assertion on the complete set of elements selected by the enclosing assertion.
82-
# Alternatively the array may be iterated through so that +assert_select+ can be called
81+
# Alternatively the array may be iterated through so that +assert_dom+ can be called
8382
# separately for each element.
8483
#
8584
#
8685
# ==== Example
8786
# If the response contains two ordered lists, each with four list elements then:
88-
# assert_select "ol" do |elements|
87+
# assert_dom "ol" do |elements|
8988
# elements.each do |element|
90-
# assert_select element, "li", 4
89+
# assert_dom element, "li", 4
9190
# end
9291
# end
9392
#
9493
# will pass, as will:
95-
# assert_select "ol" do
96-
# assert_select "li", 8
94+
# assert_dom "ol" do
95+
# assert_dom "li", 8
9796
# end
9897
#
9998
# The selector may be a CSS selector expression (String, Symbol, or Numeric) or an expression
10099
# with substitution values (Array).
101100
# Substitution uses a custom pseudo class match. Pass in whatever attribute you want to match (enclosed in quotes) and a ? for the substitution.
102-
# assert_select returns nil if called with an invalid css selector.
101+
# assert_dom returns nil if called with an invalid css selector.
103102
#
104-
# assert_select "div:match('id', ?)", "id_string"
105-
# assert_select "div:match('id', ?)", :id_string
106-
# assert_select "div:match('id', ?)", 1
107-
# assert_select "div:match('id', ?)", /\d+/
103+
# assert_dom "div:match('id', ?)", "id_string"
104+
# assert_dom "div:match('id', ?)", :id_string
105+
# assert_dom "div:match('id', ?)", 1
106+
# assert_dom "div:match('id', ?)", /\d+/
108107
#
109108
# === Equality Tests
110109
#
@@ -136,32 +135,32 @@ def css_select(*args)
136135
# evaluated the block is called with an array of all matched elements.
137136
#
138137
# # At least one form element
139-
# assert_select "form"
138+
# assert_dom "form"
140139
#
141140
# # Form element includes four input fields
142-
# assert_select "form input", 4
141+
# assert_dom "form input", 4
143142
#
144143
# # Page title is "Welcome"
145-
# assert_select "title", "Welcome"
144+
# assert_dom "title", "Welcome"
146145
#
147146
# # Page title is "Welcome" and there is only one title element
148-
# assert_select "title", {count: 1, text: "Welcome"},
147+
# assert_dom "title", {count: 1, text: "Welcome"},
149148
# "Wrong title or more than one title element"
150149
#
151150
# # Page contains no forms
152-
# assert_select "form", false, "This page must contain no forms"
151+
# assert_dom "form", false, "This page must contain no forms"
153152
#
154153
# # Test the content and style
155-
# assert_select "body div.header ul.menu"
154+
# assert_dom "body div.header ul.menu"
156155
#
157156
# # Use substitution values
158-
# assert_select "ol>li:match('id', ?)", /item-\d+/
157+
# assert_dom "ol>li:match('id', ?)", /item-\d+/
159158
#
160159
# # All input fields in the form have a name
161-
# assert_select "form input" do
162-
# assert_select ":match('name', ?)", /.+/ # Not empty
160+
# assert_dom "form input" do
161+
# assert_dom ":match('name', ?)", /.+/ # Not empty
163162
# end
164-
def assert_select(*args, &block)
163+
def assert_dom(*args, &block)
165164
@selected ||= nil
166165

167166
selector = HTMLSelector.new(args, @selected) { nodeset document_root_element }
@@ -179,7 +178,6 @@ def assert_select(*args, &block)
179178
end
180179
end
181180
alias_method :assert_dom, :assert_select
182-
deprecate assert_select: "assert_select will be renamed to assert_dom in an upcoming release"
183181

184182
# Extracts the content of an element, treats it as encoded HTML and runs
185183
# nested assertion on it.
@@ -192,30 +190,30 @@ def assert_select(*args, &block)
192190
# element +encoded+. It then calls the block with all un-encoded elements.
193191
#
194192
# # Selects all bold tags from within the title of an Atom feed's entries (perhaps to nab a section name prefix)
195-
# assert_select "feed[xmlns='http://www.w3.org/2005/Atom']" do
193+
# assert_dom "feed[xmlns='http://www.w3.org/2005/Atom']" do
196194
# # Select each entry item and then the title item
197-
# assert_select "entry>title" do
195+
# assert_dom "entry>title" do
198196
# # Run assertions on the encoded title elements
199-
# assert_select_encoded do
200-
# assert_select "b"
197+
# assert_dom_encoded do
198+
# assert_dom "b"
201199
# end
202200
# end
203201
# end
204202
#
205203
#
206204
# # Selects all paragraph tags from within the description of an RSS feed
207-
# assert_select "rss[version=2.0]" do
205+
# assert_dom "rss[version=2.0]" do
208206
# # Select description element of each feed item.
209-
# assert_select "channel>item>description" do
207+
# assert_dom "channel>item>description" do
210208
# # Run assertions on the encoded elements.
211-
# assert_select_encoded do
212-
# assert_select "p"
209+
# assert_dom_encoded do
210+
# assert_dom "p"
213211
# end
214212
# end
215213
# end
216-
def assert_select_encoded(element = nil, &block)
214+
def assert_dom_encoded(element = nil, &block)
217215
if !element && !@selected
218-
raise ArgumentError, "Element is required when called from a nonnested assert_select"
216+
raise ArgumentError, "Element is required when called from a nonnested assert_dom"
219217
end
220218

221219
content = nodeset(element || @selected).map do |elem|
@@ -229,50 +227,48 @@ def assert_select_encoded(element = nil, &block)
229227
if content.empty?
230228
yield selected
231229
else
232-
assert_select ":root", &block
230+
assert_dom ":root", &block
233231
end
234232
end
235233
end
236234
alias_method :assert_dom_encoded, :assert_select_encoded
237-
deprecate assert_select_encoded: "assert_select_encoded will be renamed to assert_dom_encoded in an upcoming release"
238235

239236
# Extracts the body of an email and runs nested assertions on it.
240237
#
241238
# You must enable deliveries for this assertion to work, use:
242239
# ActionMailer::Base.perform_deliveries = true
243240
#
244-
# assert_select_email do
245-
# assert_select "h1", "Email alert"
241+
# assert_dom_email do
242+
# assert_dom "h1", "Email alert"
246243
# end
247244
#
248-
# assert_select_email do
249-
# items = assert_select "ol>li"
245+
# assert_dom_email do
246+
# items = assert_dom "ol>li"
250247
# items.each do
251248
# # Work with items here...
252249
# end
253250
# end
254-
def assert_select_email(&block)
251+
def assert_dom_email(&block)
255252
deliveries = ActionMailer::Base.deliveries
256253
assert !deliveries.empty?, "No e-mail in delivery list"
257254

258255
deliveries.each do |delivery|
259256
(delivery.parts.empty? ? [delivery] : delivery.parts).each do |part|
260257
if part["Content-Type"].to_s =~ /^text\/html\W/
261258
root = Nokogiri::HTML::DocumentFragment.parse(part.body.to_s)
262-
assert_select root, ":root", &block
259+
assert_dom root, ":root", &block
263260
end
264261
end
265262
end
266263
end
267264
alias_method :assert_dom_email, :assert_select_email
268-
deprecate assert_select_email: "assert_select_email will be renamed to assert_dom_email in an upcoming release"
269265

270266
private
271267
include CountDescribable
272268

273269
def document_root_element
274270
raise NotImplementedError, 'Implementing document_root_element makes ' \
275-
'assert_select work without needing to specify an element to select from.'
271+
'assert_dom work without needing to specify an element to select from.'
276272
end
277273

278274
# +equals+ must contain :minimum, :maximum and :count keys
@@ -289,7 +285,7 @@ def assert_size_match!(size, equals, css_selector, message = nil)
289285
end
290286

291287
def nest_selection(selection)
292-
# Set @selected to allow nested assert_select.
288+
# Set @selected to allow nested assert_dom.
293289
# Can be nested several levels deep.
294290
old_selected, @selected = @selected, selection
295291
yield @selected

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def extract_root(previous_selection, root_fallback)
6161

6262
if possible_root == nil
6363
raise ArgumentError, 'First argument is either selector or element ' \
64-
'to select, but nil found. Perhaps you called assert_select with ' \
64+
'to select, but nil found. Perhaps you called assert_dom with ' \
6565
'an element that does not exist?'
6666
elsif possible_root.respond_to?(:css)
6767
@values.shift # remove the root, so selector is the first argument

test/selector_assertions_test.rb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ def assert_failure(message, &block)
2121
def test_assert_select
2222
render_html %Q{<div id="1"></div><div id="2"></div>}
2323
assert_select "div", 2
24-
assert_dom "div", 2
2524
assert_failure(/Expected at least 1 element matching \"p\", found 0/) { assert_select "p" }
2625
end
2726

@@ -278,9 +277,6 @@ def test_feed_item_encoded
278277
assert_select_encoded do
279278
assert_select "p", :count=>2, :text=>/Test/
280279
end
281-
assert_dom_encoded do
282-
assert_select "p", :count=>2, :text=>/Test/
283-
end
284280

285281
# Test individually.
286282
assert_select "description" do |elements|

0 commit comments

Comments
 (0)