rails-dom-testing 2.0.3 → 2.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +5 -5
- data/README.md +47 -26
- data/lib/rails/dom/testing/assertions/dom_assertions.rb +83 -18
- data/lib/rails/dom/testing/assertions/selector_assertions/html_selector.rb +151 -111
- data/lib/rails/dom/testing/assertions/selector_assertions/substitution_context.rb +36 -25
- data/lib/rails/dom/testing/assertions/selector_assertions.rb +318 -231
- data/lib/rails/dom/testing/assertions.rb +5 -8
- data/lib/rails/dom/testing/railtie.rb +14 -0
- data/lib/rails/dom/testing/version.rb +3 -1
- data/lib/rails/dom/testing.rb +52 -0
- data/lib/rails-dom-testing.rb +4 -1
- data/test/dom_assertions_test.rb +169 -6
- data/test/parser_selection_test.rb +75 -0
- data/test/selector_assertions_test.rb +275 -69
- data/test/test_helper.rb +21 -4
- metadata +20 -35
- data/lib/rails/dom/testing/assertions/selector_assertions/count_describable.rb +0 -32
@@ -1,14 +1,14 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
require_relative
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "selector_assertions/html_selector"
|
4
4
|
|
5
5
|
module Rails
|
6
6
|
module Dom
|
7
7
|
module Testing
|
8
8
|
module Assertions
|
9
|
-
# Adds the +
|
9
|
+
# Adds the +assert_dom+ method for use in Rails functional
|
10
10
|
# test cases, which can be used to make assertions on the response HTML of a controller
|
11
|
-
# action. You can also call +
|
11
|
+
# action. You can also call +assert_dom+ within another +assert_dom+ to
|
12
12
|
# make assertions on elements selected by the enclosing assertion.
|
13
13
|
#
|
14
14
|
# Use +css_select+ to select elements without making an assertions, either
|
@@ -16,261 +16,331 @@ module Rails
|
|
16
16
|
#
|
17
17
|
# In addition to HTML responses, you can make the following assertions:
|
18
18
|
#
|
19
|
-
# * +
|
20
|
-
# * +
|
19
|
+
# * +assert_dom_encoded+ - Assertions on HTML encoded inside XML, for example for dealing with feed item descriptions.
|
20
|
+
# * +assert_dom_email+ - Assertions on the HTML body of an e-mail.
|
21
21
|
module SelectorAssertions
|
22
|
+
# Select and return all matching elements.
|
23
|
+
#
|
24
|
+
# If called with a single argument, uses that argument as a selector.
|
25
|
+
# Called without an element +css_select+ selects from
|
26
|
+
# the element returned in +document_root_element+
|
27
|
+
#
|
28
|
+
# The default implementation of +document_root_element+ raises an exception explaining this.
|
29
|
+
#
|
30
|
+
# Returns an empty Nokogiri::XML::NodeSet if no match is found.
|
31
|
+
#
|
32
|
+
# If called with two arguments, uses the first argument as the root
|
33
|
+
# element and the second argument as the selector. Attempts to match the
|
34
|
+
# root element and any of its children.
|
35
|
+
# Returns an empty Nokogiri::XML::NodeSet if no match is found.
|
36
|
+
#
|
37
|
+
# The selector may be a CSS selector expression (String).
|
38
|
+
# css_select returns nil if called with an invalid css selector.
|
39
|
+
#
|
40
|
+
# # Selects all div tags
|
41
|
+
# divs = css_select("div")
|
42
|
+
#
|
43
|
+
# # Selects all paragraph tags and does something interesting
|
44
|
+
# pars = css_select("p")
|
45
|
+
# pars.each do |par|
|
46
|
+
# # Do something fun with paragraphs here...
|
47
|
+
# end
|
48
|
+
#
|
49
|
+
# # Selects all list items in unordered lists
|
50
|
+
# items = css_select("ul>li")
|
51
|
+
#
|
52
|
+
# # Selects all form tags and then all inputs inside the form
|
53
|
+
# forms = css_select("form")
|
54
|
+
# forms.each do |form|
|
55
|
+
# inputs = css_select(form, "input")
|
56
|
+
# ...
|
57
|
+
# end
|
58
|
+
def css_select(*args)
|
59
|
+
raise ArgumentError, "you at least need a selector argument" if args.empty?
|
22
60
|
|
23
|
-
|
24
|
-
#
|
25
|
-
# If called with a single argument, uses that argument as a selector.
|
26
|
-
# Called without an element +css_select+ selects from
|
27
|
-
# the element returned in +document_root_element+
|
28
|
-
#
|
29
|
-
# The default implementation of +document_root_element+ raises an exception explaining this.
|
30
|
-
#
|
31
|
-
# Returns an empty Nokogiri::XML::NodeSet if no match is found.
|
32
|
-
#
|
33
|
-
# If called with two arguments, uses the first argument as the root
|
34
|
-
# element and the second argument as the selector. Attempts to match the
|
35
|
-
# root element and any of its children.
|
36
|
-
# Returns an empty Nokogiri::XML::NodeSet if no match is found.
|
37
|
-
#
|
38
|
-
# The selector may be a CSS selector expression (String).
|
39
|
-
# css_select returns nil if called with an invalid css selector.
|
40
|
-
#
|
41
|
-
# # Selects all div tags
|
42
|
-
# divs = css_select("div")
|
43
|
-
#
|
44
|
-
# # Selects all paragraph tags and does something interesting
|
45
|
-
# pars = css_select("p")
|
46
|
-
# pars.each do |par|
|
47
|
-
# # Do something fun with paragraphs here...
|
48
|
-
# end
|
49
|
-
#
|
50
|
-
# # Selects all list items in unordered lists
|
51
|
-
# items = css_select("ul>li")
|
52
|
-
#
|
53
|
-
# # Selects all form tags and then all inputs inside the form
|
54
|
-
# forms = css_select("form")
|
55
|
-
# forms.each do |form|
|
56
|
-
# inputs = css_select(form, "input")
|
57
|
-
# ...
|
58
|
-
# end
|
59
|
-
def css_select(*args)
|
60
|
-
raise ArgumentError, "you at least need a selector argument" if args.empty?
|
61
|
+
root = args.size == 1 ? document_root_element : args.shift
|
61
62
|
|
62
|
-
|
63
|
+
nodeset(root).css(args.first)
|
64
|
+
end
|
63
65
|
|
64
|
-
|
65
|
-
|
66
|
+
# An assertion that selects elements and makes one or more equality tests.
|
67
|
+
#
|
68
|
+
# If the first argument is an element, selects all matching elements
|
69
|
+
# starting from (and including) that element and all its children in
|
70
|
+
# depth-first order.
|
71
|
+
#
|
72
|
+
# If no element is specified +assert_dom+ selects from
|
73
|
+
# the element returned in +document_root_element+
|
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.
|
76
|
+
# The default implementation raises an exception explaining this.
|
77
|
+
#
|
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,
|
80
|
+
# runs the assertion on the complete set of elements selected by the enclosing assertion.
|
81
|
+
# Alternatively the array may be iterated through so that +assert_dom+ can be called
|
82
|
+
# separately for each element.
|
83
|
+
#
|
84
|
+
#
|
85
|
+
# ==== Example
|
86
|
+
# If the response contains two ordered lists, each with four list elements then:
|
87
|
+
# assert_dom "ol" do |elements|
|
88
|
+
# elements.each do |element|
|
89
|
+
# assert_dom element, "li", 4
|
90
|
+
# end
|
91
|
+
# end
|
92
|
+
#
|
93
|
+
# will pass, as will:
|
94
|
+
# assert_dom "ol" do
|
95
|
+
# assert_dom "li", 8
|
96
|
+
# end
|
97
|
+
#
|
98
|
+
# The selector may be a CSS selector expression (String, Symbol, or Numeric) or an expression
|
99
|
+
# with substitution values (Array).
|
100
|
+
# Substitution uses a custom pseudo class match. Pass in whatever attribute you want to match (enclosed in quotes) and a ? for the substitution.
|
101
|
+
# assert_dom returns nil if called with an invalid css selector.
|
102
|
+
#
|
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+/
|
107
|
+
#
|
108
|
+
# === Equality Tests
|
109
|
+
#
|
110
|
+
# The equality test may be one of the following:
|
111
|
+
# * <tt>true</tt> - Assertion is true if at least one element selected.
|
112
|
+
# * <tt>false</tt> - Assertion is true if no element selected.
|
113
|
+
# * <tt>String/Regexp</tt> - Assertion is true if the text value of at least
|
114
|
+
# one element matches the string or regular expression.
|
115
|
+
# * <tt>Integer</tt> - Assertion is true if exactly that number of
|
116
|
+
# elements are selected.
|
117
|
+
# * <tt>Range</tt> - Assertion is true if the number of selected
|
118
|
+
# elements fit the range.
|
119
|
+
# If no equality test specified, the assertion is true if at least one
|
120
|
+
# element selected.
|
121
|
+
#
|
122
|
+
# To perform more than one equality tests, use a hash with the following keys:
|
123
|
+
# * <tt>:text</tt> - Narrow the selection to elements that have this text
|
124
|
+
# value (string or regexp).
|
125
|
+
# * <tt>:html</tt> - Narrow the selection to elements that have this HTML
|
126
|
+
# content (string or regexp).
|
127
|
+
# * <tt>:count</tt> - Assertion is true if the number of selected elements
|
128
|
+
# is equal to this value.
|
129
|
+
# * <tt>:minimum</tt> - Assertion is true if the number of selected
|
130
|
+
# elements is at least this value.
|
131
|
+
# * <tt>:maximum</tt> - Assertion is true if the number of selected
|
132
|
+
# elements is at most this value.
|
133
|
+
#
|
134
|
+
# If the method is called with a block, once all equality tests are
|
135
|
+
# evaluated the block is called with an array of all matched elements.
|
136
|
+
#
|
137
|
+
# # At least one form element
|
138
|
+
# assert_dom "form"
|
139
|
+
#
|
140
|
+
# # Form element includes four input fields
|
141
|
+
# assert_dom "form input", 4
|
142
|
+
#
|
143
|
+
# # Page title is "Welcome"
|
144
|
+
# assert_dom "title", "Welcome"
|
145
|
+
#
|
146
|
+
# # Page title is "Welcome" and there is only one title element
|
147
|
+
# assert_dom "title", {count: 1, text: "Welcome"},
|
148
|
+
# "Wrong title or more than one title element"
|
149
|
+
#
|
150
|
+
# # Page contains no forms
|
151
|
+
# assert_dom "form", false, "This page must contain no forms"
|
152
|
+
#
|
153
|
+
# # Test the content and style
|
154
|
+
# assert_dom "body div.header ul.menu"
|
155
|
+
#
|
156
|
+
# # Use substitution values
|
157
|
+
# assert_dom "ol>li:match('id', ?)", /item-\d+/
|
158
|
+
#
|
159
|
+
# # All input fields in the form have a name
|
160
|
+
# assert_dom "form input" do
|
161
|
+
# assert_dom ":match('name', ?)", /.+/ # Not empty
|
162
|
+
# end
|
163
|
+
def assert_dom(*args, &block)
|
164
|
+
@selected ||= nil
|
66
165
|
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
# depth-first order.
|
72
|
-
#
|
73
|
-
# If no element is specified +assert_select+ selects from
|
74
|
-
# 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.
|
77
|
-
# The default implementation raises an exception explaining this.
|
78
|
-
#
|
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,
|
81
|
-
# 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
|
83
|
-
# separately for each element.
|
84
|
-
#
|
85
|
-
#
|
86
|
-
# ==== Example
|
87
|
-
# If the response contains two ordered lists, each with four list elements then:
|
88
|
-
# assert_select "ol" do |elements|
|
89
|
-
# elements.each do |element|
|
90
|
-
# assert_select element, "li", 4
|
91
|
-
# end
|
92
|
-
# end
|
93
|
-
#
|
94
|
-
# will pass, as will:
|
95
|
-
# assert_select "ol" do
|
96
|
-
# assert_select "li", 8
|
97
|
-
# end
|
98
|
-
#
|
99
|
-
# The selector may be a CSS selector expression (String) or an expression
|
100
|
-
# with substitution values (Array).
|
101
|
-
# 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.
|
103
|
-
#
|
104
|
-
# assert_select "div:match('id', ?)", /\d+/
|
105
|
-
#
|
106
|
-
# === Equality Tests
|
107
|
-
#
|
108
|
-
# The equality test may be one of the following:
|
109
|
-
# * <tt>true</tt> - Assertion is true if at least one element selected.
|
110
|
-
# * <tt>false</tt> - Assertion is true if no element selected.
|
111
|
-
# * <tt>String/Regexp</tt> - Assertion is true if the text value of at least
|
112
|
-
# one element matches the string or regular expression.
|
113
|
-
# * <tt>Integer</tt> - Assertion is true if exactly that number of
|
114
|
-
# elements are selected.
|
115
|
-
# * <tt>Range</tt> - Assertion is true if the number of selected
|
116
|
-
# elements fit the range.
|
117
|
-
# If no equality test specified, the assertion is true if at least one
|
118
|
-
# element selected.
|
119
|
-
#
|
120
|
-
# To perform more than one equality tests, use a hash with the following keys:
|
121
|
-
# * <tt>:text</tt> - Narrow the selection to elements that have this text
|
122
|
-
# value (string or regexp).
|
123
|
-
# * <tt>:html</tt> - Narrow the selection to elements that have this HTML
|
124
|
-
# content (string or regexp).
|
125
|
-
# * <tt>:count</tt> - Assertion is true if the number of selected elements
|
126
|
-
# is equal to this value.
|
127
|
-
# * <tt>:minimum</tt> - Assertion is true if the number of selected
|
128
|
-
# elements is at least this value.
|
129
|
-
# * <tt>:maximum</tt> - Assertion is true if the number of selected
|
130
|
-
# elements is at most this value.
|
131
|
-
#
|
132
|
-
# If the method is called with a block, once all equality tests are
|
133
|
-
# evaluated the block is called with an array of all matched elements.
|
134
|
-
#
|
135
|
-
# # At least one form element
|
136
|
-
# assert_select "form"
|
137
|
-
#
|
138
|
-
# # Form element includes four input fields
|
139
|
-
# assert_select "form input", 4
|
140
|
-
#
|
141
|
-
# # Page title is "Welcome"
|
142
|
-
# assert_select "title", "Welcome"
|
143
|
-
#
|
144
|
-
# # Page title is "Welcome" and there is only one title element
|
145
|
-
# assert_select "title", {count: 1, text: "Welcome"},
|
146
|
-
# "Wrong title or more than one title element"
|
147
|
-
#
|
148
|
-
# # Page contains no forms
|
149
|
-
# assert_select "form", false, "This page must contain no forms"
|
150
|
-
#
|
151
|
-
# # Test the content and style
|
152
|
-
# assert_select "body div.header ul.menu"
|
153
|
-
#
|
154
|
-
# # Use substitution values
|
155
|
-
# assert_select "ol>li:match('id', ?)", /item-\d+/
|
156
|
-
#
|
157
|
-
# # All input fields in the form have a name
|
158
|
-
# assert_select "form input" do
|
159
|
-
# assert_select ":match('name', ?)", /.+/ # Not empty
|
160
|
-
# end
|
161
|
-
def assert_select(*args, &block)
|
162
|
-
@selected ||= nil
|
166
|
+
selector = HTMLSelector.new(args, @selected) { nodeset document_root_element }
|
167
|
+
dom_assertions(selector, &block)
|
168
|
+
end
|
169
|
+
alias_method :assert_select, :assert_dom
|
163
170
|
|
164
|
-
|
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
|
165
185
|
|
166
|
-
|
167
|
-
|
168
|
-
return
|
186
|
+
selector = HTMLSelector.new(args, @selected, refute: true) { nodeset document_root_element }
|
187
|
+
dom_assertions(selector, &block)
|
169
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_dom
|
170
192
|
|
171
|
-
selector
|
172
|
-
|
173
|
-
|
193
|
+
private def dom_assertions(selector, &block)
|
194
|
+
if selector.selecting_no_body?
|
195
|
+
assert true
|
196
|
+
return
|
197
|
+
end
|
174
198
|
|
175
|
-
|
176
|
-
end
|
177
|
-
end
|
199
|
+
count, max = selector.tests.slice(:count, :maximum).values
|
178
200
|
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
# # Select each entry item and then the title item
|
192
|
-
# assert_select "entry>title" do
|
193
|
-
# # Run assertions on the encoded title elements
|
194
|
-
# assert_select_encoded do
|
195
|
-
# assert_select "b"
|
196
|
-
# end
|
197
|
-
# end
|
198
|
-
# end
|
199
|
-
#
|
200
|
-
#
|
201
|
-
# # Selects all paragraph tags from within the description of an RSS feed
|
202
|
-
# assert_select "rss[version=2.0]" do
|
203
|
-
# # Select description element of each feed item.
|
204
|
-
# assert_select "channel>item>description" do
|
205
|
-
# # Run assertions on the encoded elements.
|
206
|
-
# assert_select_encoded do
|
207
|
-
# assert_select "p"
|
208
|
-
# end
|
209
|
-
# end
|
210
|
-
# end
|
211
|
-
def assert_select_encoded(element = nil, &block)
|
212
|
-
if !element && !@selected
|
213
|
-
raise ArgumentError, "Element is required when called from a nonnested assert_select"
|
201
|
+
selector.select.tap do |matches|
|
202
|
+
assert_size_match!(matches.size, selector.tests,
|
203
|
+
selector.css_selector, selector.message)
|
204
|
+
|
205
|
+
if block_given?
|
206
|
+
if count&.zero? || max&.zero?
|
207
|
+
raise ArgumentError, "Cannot be called with a block when asserting that a selector does not match"
|
208
|
+
end
|
209
|
+
|
210
|
+
nest_selection(matches, &block) unless matches.empty?
|
211
|
+
end
|
212
|
+
end
|
214
213
|
end
|
215
214
|
|
216
|
-
content
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
215
|
+
# Extracts the content of an element, treats it as encoded HTML and runs
|
216
|
+
# nested assertion on it.
|
217
|
+
#
|
218
|
+
# You typically call this method within another assertion to operate on
|
219
|
+
# all currently selected elements. You can also pass an element or array
|
220
|
+
# of elements.
|
221
|
+
#
|
222
|
+
# The content of each element is un-encoded, and wrapped in the root
|
223
|
+
# element +encoded+. It then calls the block with all un-encoded elements.
|
224
|
+
#
|
225
|
+
# # Selects all bold tags from within the title of an Atom feed's entries (perhaps to nab a section name prefix)
|
226
|
+
# assert_dom "feed[xmlns='http://www.w3.org/2005/Atom']" do
|
227
|
+
# # Select each entry item and then the title item
|
228
|
+
# assert_dom "entry>title" do
|
229
|
+
# # Run assertions on the encoded title elements
|
230
|
+
# assert_dom_encoded do
|
231
|
+
# assert_dom "b"
|
232
|
+
# end
|
233
|
+
# end
|
234
|
+
# end
|
235
|
+
#
|
236
|
+
#
|
237
|
+
# # Selects all paragraph tags from within the description of an RSS feed
|
238
|
+
# assert_dom "rss[version=2.0]" do
|
239
|
+
# # Select description element of each feed item.
|
240
|
+
# assert_dom "channel>item>description" do
|
241
|
+
# # Run assertions on the encoded elements.
|
242
|
+
# assert_dom_encoded do
|
243
|
+
# assert_dom "p"
|
244
|
+
# end
|
245
|
+
# end
|
246
|
+
# end
|
247
|
+
#
|
248
|
+
# The DOM is created using an HTML parser specified by
|
249
|
+
# Rails::Dom::Testing.default_html_version (either :html4 or :html5).
|
250
|
+
#
|
251
|
+
# When testing in a Rails application, the parser default can also be set by setting
|
252
|
+
# +Rails.application.config.dom_testing_default_html_version+.
|
253
|
+
#
|
254
|
+
# If you want to specify the HTML parser just for a particular assertion, pass
|
255
|
+
# <tt>html_version: :html4</tt> or <tt>html_version: :html5</tt> keyword arguments:
|
256
|
+
#
|
257
|
+
# assert_dom "feed[xmlns='http://www.w3.org/2005/Atom']" do
|
258
|
+
# assert_dom "entry>title" do
|
259
|
+
# assert_dom_encoded(html_version: :html5) do
|
260
|
+
# assert_dom "b"
|
261
|
+
# end
|
262
|
+
# end
|
263
|
+
# end
|
264
|
+
#
|
265
|
+
def assert_dom_encoded(element = nil, html_version: nil, &block)
|
266
|
+
if !element && !@selected
|
267
|
+
raise ArgumentError, "Element is required when called from a nonnested assert_dom"
|
268
|
+
end
|
221
269
|
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
270
|
+
content = nodeset(element || @selected).map do |elem|
|
271
|
+
elem.children.select do |child|
|
272
|
+
child.cdata? || (child.text? && !child.blank?)
|
273
|
+
end.map(&:content)
|
274
|
+
end.join
|
275
|
+
|
276
|
+
selected = Rails::Dom::Testing.html_document_fragment(html_version: html_version).parse(content)
|
277
|
+
nest_selection(selected) do
|
278
|
+
if content.empty?
|
279
|
+
yield selected
|
280
|
+
else
|
281
|
+
assert_dom ":root", &block
|
282
|
+
end
|
228
283
|
end
|
229
284
|
end
|
230
|
-
|
285
|
+
alias_method :assert_select_encoded, :assert_dom_encoded
|
231
286
|
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
#
|
247
|
-
|
248
|
-
|
249
|
-
|
287
|
+
# Extracts the body of an email and runs nested assertions on it.
|
288
|
+
#
|
289
|
+
# You must enable deliveries for this assertion to work, use:
|
290
|
+
# ActionMailer::Base.perform_deliveries = true
|
291
|
+
#
|
292
|
+
# Example usage:
|
293
|
+
#
|
294
|
+
# assert_dom_email do
|
295
|
+
# assert_dom "h1", "Email alert"
|
296
|
+
# end
|
297
|
+
#
|
298
|
+
# assert_dom_email do
|
299
|
+
# items = assert_dom "ol>li"
|
300
|
+
# items.each do
|
301
|
+
# # Work with items here...
|
302
|
+
# end
|
303
|
+
# end
|
304
|
+
#
|
305
|
+
# The DOM is created using an HTML parser specified by
|
306
|
+
# Rails::Dom::Testing.default_html_version (either :html4 or :html5).
|
307
|
+
#
|
308
|
+
# When testing in a Rails application, the parser default can also be set by setting
|
309
|
+
# +Rails.application.config.dom_testing_default_html_version+.
|
310
|
+
#
|
311
|
+
# If you want to specify the HTML parser just for a particular assertion, pass
|
312
|
+
# <tt>html_version: :html4</tt> or <tt>html_version: :html5</tt> keyword arguments:
|
313
|
+
#
|
314
|
+
# assert_dom_email(html_version: :html5) do
|
315
|
+
# assert_dom "h1", "Email alert"
|
316
|
+
# end
|
317
|
+
#
|
318
|
+
def assert_dom_email(html_version: nil, &block)
|
319
|
+
deliveries = ActionMailer::Base.deliveries
|
320
|
+
assert !deliveries.empty?, "No e-mail in delivery list"
|
250
321
|
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
322
|
+
deliveries.each do |delivery|
|
323
|
+
(delivery.parts.empty? ? [delivery] : delivery.parts).each do |part|
|
324
|
+
if /^text\/html\W/.match?(part["Content-Type"].to_s)
|
325
|
+
root = Rails::Dom::Testing.html_document_fragment(html_version: html_version).parse(part.body.to_s)
|
326
|
+
assert_dom root, ":root", &block
|
327
|
+
end
|
256
328
|
end
|
257
329
|
end
|
258
330
|
end
|
259
|
-
|
331
|
+
alias_method :assert_select_email, :assert_dom_email
|
260
332
|
|
261
333
|
private
|
262
|
-
include CountDescribable
|
263
|
-
|
264
334
|
def document_root_element
|
265
|
-
raise NotImplementedError,
|
266
|
-
|
335
|
+
raise NotImplementedError, "Implementing document_root_element makes " \
|
336
|
+
"assert_dom work without needing to specify an element to select from."
|
267
337
|
end
|
268
338
|
|
269
339
|
# +equals+ must contain :minimum, :maximum and :count keys
|
270
340
|
def assert_size_match!(size, equals, css_selector, message = nil)
|
271
341
|
min, max, count = equals[:minimum], equals[:maximum], equals[:count]
|
272
342
|
|
273
|
-
message ||= %(Expected #{count_description(min, max, count)} matching
|
343
|
+
message ||= %(Expected #{count_description(min, max, count)} matching #{css_selector.inspect}, found #{size})
|
274
344
|
if count
|
275
345
|
assert_equal count, size, message
|
276
346
|
else
|
@@ -279,8 +349,24 @@ module Rails
|
|
279
349
|
end
|
280
350
|
end
|
281
351
|
|
352
|
+
def count_description(min, max, count)
|
353
|
+
if min && max && (max != min)
|
354
|
+
"between #{min} and #{max} elements"
|
355
|
+
elsif min && max && max == min && count
|
356
|
+
"exactly #{count} #{pluralize_element(min)}"
|
357
|
+
elsif min && !(min == 1 && max == 1)
|
358
|
+
"at least #{min} #{pluralize_element(min)}"
|
359
|
+
elsif max
|
360
|
+
"at most #{max} #{pluralize_element(max)}"
|
361
|
+
end
|
362
|
+
end
|
363
|
+
|
364
|
+
def pluralize_element(quantity)
|
365
|
+
quantity == 1 ? "element" : "elements"
|
366
|
+
end
|
367
|
+
|
282
368
|
def nest_selection(selection)
|
283
|
-
# Set @selected to allow nested
|
369
|
+
# Set @selected to allow nested assert_dom.
|
284
370
|
# Can be nested several levels deep.
|
285
371
|
old_selected, @selected = @selected, selection
|
286
372
|
yield @selected
|
@@ -292,6 +378,7 @@ module Rails
|
|
292
378
|
if node.is_a?(Nokogiri::XML::NodeSet)
|
293
379
|
node
|
294
380
|
else
|
381
|
+
node ||= Nokogiri::HTML::Document.new
|
295
382
|
Nokogiri::XML::NodeSet.new(node.document, [node])
|
296
383
|
end
|
297
384
|
end
|
@@ -1,18 +1,15 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "assertions/dom_assertions"
|
4
|
+
require_relative "assertions/selector_assertions"
|
3
5
|
|
4
6
|
module Rails
|
5
7
|
module Dom
|
6
8
|
module Testing
|
7
9
|
module Assertions
|
8
|
-
autoload :DomAssertions, 'rails/dom/testing/assertions/dom_assertions'
|
9
|
-
autoload :SelectorAssertions, 'rails/dom/testing/assertions/selector_assertions'
|
10
|
-
|
11
|
-
extend ActiveSupport::Concern
|
12
|
-
|
13
10
|
include DomAssertions
|
14
11
|
include SelectorAssertions
|
15
12
|
end
|
16
13
|
end
|
17
14
|
end
|
18
|
-
end
|
15
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Rails
|
4
|
+
module Dom
|
5
|
+
module Testing
|
6
|
+
class Railtie < Rails::Railtie # :nodoc:
|
7
|
+
config.after_initialize do |app|
|
8
|
+
version = app.config.try(:dom_testing_default_html_version) # Rails 7.1+
|
9
|
+
Rails::Dom::Testing.default_html_version = version if version
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|