Skip to content

Commit 6a4b9db

Browse files
authored
Merge pull request #123 from jyeharry/feature/assert_dom-ignore-whitespace-v2
assert_dom :text collapses whitespace
2 parents 0d12815 + 4241aa0 commit 6a4b9db

File tree

2 files changed

+9
-40
lines changed

2 files changed

+9
-40
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ 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
2221
@tests = extract_equality_tests(refute)
2322
@message = @values.shift
2423

@@ -52,15 +51,16 @@ def filter(matches)
5251

5352
content_mismatch = nil
5453
text_matches = tests.has_key?(:text)
54+
html_matches = tests.has_key?(:html)
5555
regex_matching = match_with.is_a?(Regexp)
5656

5757
remaining = matches.reject do |match|
5858
# Preserve markup with to_s for html elements
59-
content = text_matches ? match.text : match.children.to_s
59+
content = text_matches ? match.text : match.inner_html
6060

6161
content.strip! unless NO_STRIP.include?(match.name)
6262
content.delete_prefix!("\n") if text_matches && match.name == "textarea"
63-
collapse_html_whitespace!(content) unless NO_STRIP.include?(match.name) || @strict
63+
collapse_html_whitespace!(content) unless NO_STRIP.include?(match.name) || html_matches
6464

6565
next if regex_matching ? (content =~ match_with) : (content == match_with)
6666
content_mismatch ||= diff(match_with, content)

test/selector_assertions_test.rb

Lines changed: 6 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -237,70 +237,39 @@ 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
240+
def test_assert_select_text_equality_collapses_whitespace
241241
render_html "<p>Some\n line-broken\n text</p>"
242242

243243
assert_nothing_raised do
244244
assert_select "p", {
245245
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"
246+
}, "Whitespace was not collapsed from text"
253247
end
254248

255249
render_html "<p>Some<br><br>line-broken<br><br>text</p>"
256250

257251
assert_nothing_raised do
258252
assert_select "p", {
259253
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"
254+
}, "<br> was not removed from text"
267255
end
268256
end
269257

270-
def test_assert_select_strict_respects_whitespace
258+
def test_assert_select_html_equality_respects_whitespace
271259
render_html "<p>Some\n line-broken\n text</p>"
272260

273261
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-
279262
assert_select "p", {
280263
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-
}
264+
}, "Whitespace was collapsed from html"
290265
end
291266

292267
render_html "<p>Some<br><br>line-broken<br><br>text</p>"
293268

294269
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-
300270
assert_select "p", {
301271
html: "Some<br><br>line-broken<br><br>text",
302-
strict: true
303-
}, "<br> was removed from html when strict"
272+
}, "<br> was removed from html"
304273
end
305274
end
306275

0 commit comments

Comments
 (0)