Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

assert_dom should ignore whitespace just like assert_dom_equal #121

Open
jyeharry opened this issue Feb 28, 2025 · 4 comments · May be fixed by #122 or #123
Open

assert_dom should ignore whitespace just like assert_dom_equal #121

jyeharry opened this issue Feb 28, 2025 · 4 comments · May be fixed by #122 or #123

Comments

@jyeharry
Copy link

In one of my views I have this:

<li><%= item.quantity %> &times; <%= item.product.title %></li>

My formatter automatically breaks this onto new lines like so:

      <li><%= item.quantity %>
        &times;
        <%= item.product.title %></li>

Because of this, running a test that contains this assertion fails:

assert_select "ul li", "#{item.quantity} \u00D7 #{products(:one).title}"

This is due to the text I'm passing to assert_select having no newlines in it, but in the view code there are newlines even though the browser actually drops those newlines.

So the test output is as follow:

Failure:
LineItemsControllerTest#test_should_create_line_item [test/controllers/line_items_controller_test.rb:27]:
--- expected
+++ actual
@@ -1 +1,3 @@
-"1 × The Pragmatic Programmer"
+"1
+        ×
+        The Pragmatic Programmer"
.
Expected 0 to be >= 1.

My current fix is to either turn off my formatter for that file, or to update the assertion to use regex and replace the spaces with \s+ like this:

assert_select "ul li", /#{item.quantity}\s+\u00D7\s+#{products(:one).title}/

However, the test should really treat excess whitespace the same as the browser does by dropping it.

Sounds like the same thing was done for assert_dom_equal in #84.

@flavorjones
Copy link
Member

@jyeharry Thanks for opening this issue. I tend to agree that this is how the assert_select/assert_dom methods should work, though any PR addressing this need should provide a strict: true argument like the one in #84 for folks who want to be exact.

Is this something you'd be interested in working on and contributing? I'd be happy to help you land a pull request.

But if you don't think you're able to do that, that's fine, I'm sure I can find time to work on this at some point.

@jyeharry
Copy link
Author

jyeharry commented Mar 3, 2025

@flavorjones I'd love to contribute!

I don't actually have much rails experience though, I've just been teaching myself lately. My experience (four years) is primarily in node. However I've wanted to contribute to open source for ages so I'd love to help here.

Though since I'm not so familiar with this library and contributing to open source in general it would be great if you could point me in the right direction and I'll get started.

@flavorjones
Copy link
Member

@jyeharry I'm glad you're game to help! I'm not totally sure how I want to approach the solution yet, so I don't have much advice to give yet (and I'd be happy to take the responsibility of making it work), but a great start would be opening a pull request that has a failing test in it that describes your scenario above!

Would you be up for adding that test in a pull request?

@jyeharry
Copy link
Author

jyeharry commented Mar 8, 2025

@flavorjones yeah sure! I'll give it a go over the coming week and hopefully have something for you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants