Skip to content

Commit 0d12815

Browse files
authored
Merge pull request #122 from jyeharry/feature/assert_dom-ignore-whitespace
assert_dom strict parameter
2 parents 9732c58 + 16b188d commit 0d12815

File tree

3 files changed

+76
-0
lines changed

3 files changed

+76
-0
lines changed

Gemfile.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ GEM
8989

9090
PLATFORMS
9191
aarch64-linux
92+
x86_64-darwin-23
9293
x86_64-linux
9394

9495
DEPENDENCIES

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ 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+
@strict = false
2122
@tests = extract_equality_tests(refute)
2223
@message = @values.shift
2324

@@ -59,6 +60,7 @@ def filter(matches)
5960

6061
content.strip! unless NO_STRIP.include?(match.name)
6162
content.delete_prefix!("\n") if text_matches && match.name == "textarea"
63+
collapse_html_whitespace!(content) unless NO_STRIP.include?(match.name) || @strict
6264

6365
next if regex_matching ? (content =~ match_with) : (content == match_with)
6466
content_mismatch ||= diff(match_with, content)
@@ -136,8 +138,14 @@ def extract_equality_tests(refute)
136138
raise ArgumentError, "Range begin or :minimum cannot be greater than Range end or :maximum"
137139
end
138140

141+
@strict = comparisons[:strict]
142+
139143
comparisons
140144
end
145+
146+
def collapse_html_whitespace!(text)
147+
text.gsub!(/\s+/, " ")
148+
end
141149
end
142150
end
143151
end

test/selector_assertions_test.rb

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,73 @@ def test_assert_select_with_invalid_minimum_and_maximum
237237
assert_equal "Range begin or :minimum cannot be greater than Range end or :maximum", error.message
238238
end
239239

240+
def test_assert_select_not_strict_collapses_whitespace
241+
render_html "<p>Some\n line-broken\n text</p>"
242+
243+
assert_nothing_raised do
244+
assert_select "p", {
245+
text: "Some line-broken text",
246+
strict: false
247+
}, "Whitespace was not collapsed from text when not strict"
248+
249+
assert_select "p", {
250+
html: "Some line-broken text",
251+
strict: false
252+
}, "Whitespace was not collapsed from html when not strict"
253+
end
254+
255+
render_html "<p>Some<br><br>line-broken<br><br>text</p>"
256+
257+
assert_nothing_raised do
258+
assert_select "p", {
259+
text: "Someline-brokentext",
260+
strict: false
261+
}, "<br> was not removed from text when not strict"
262+
263+
assert_select "p", {
264+
html: "Some<br><br>line-broken<br><br>text",
265+
strict: false
266+
}, "<br> was removed from html when not strict"
267+
end
268+
end
269+
270+
def test_assert_select_strict_respects_whitespace
271+
render_html "<p>Some\n line-broken\n text</p>"
272+
273+
assert_nothing_raised do
274+
assert_select "p", {
275+
text: "Some\n line-broken\n text",
276+
strict: true
277+
}, "Whitespace was collapsed from text when strict"
278+
279+
assert_select "p", {
280+
html: "Some\n line-broken\n text",
281+
strict: true
282+
}, "Whitespace was collapsed from html when strict"
283+
end
284+
285+
assert_raises(Assertion) do
286+
assert_select "p", {
287+
html: "Some line-broken text",
288+
strict: true
289+
}
290+
end
291+
292+
render_html "<p>Some<br><br>line-broken<br><br>text</p>"
293+
294+
assert_nothing_raised do
295+
assert_select "p", {
296+
text: "Someline-brokentext",
297+
strict: true
298+
}, "<br> was not removed from text when strict"
299+
300+
assert_select "p", {
301+
html: "Some<br><br>line-broken<br><br>text",
302+
strict: true
303+
}, "<br> was removed from html when strict"
304+
end
305+
end
306+
240307
#
241308
# Test assert_not_select.
242309
#

0 commit comments

Comments
 (0)