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

Feature | Introduce assert_not_dom and its alias assert_not_select #112

Closed
joshuay03 opened this issue Dec 12, 2023 · 5 comments
Closed

Comments

@joshuay03
Copy link
Contributor

joshuay03 commented Dec 12, 2023

There are occasions when you need to assert that there were no specific elements rendered, currently this can be achieved with any of:

assert_select "element[attribute_1=?][attribute_2=?]", value_1, value_2, false
assert_select "element[attribute_1=?][attribute_2=?]", value_1, value_2, 0
assert_select "element[attribute_1=?][attribute_2=?]", value_1, value_2, 0..0
assert_select "element[attribute_1=?][attribute_2=?]", value_1, value_2, count: 0
assert_select "element[attribute_1=?][attribute_2=?]", value_1, value_2, maximum: 0

One downside of this is that it's not clear until you get to the end of the line that the assertion is for the inverse / lack of. This isn't as much of an issue when using Integer, Range, :count or :maximum with positive integers, as the assertion itself is for existence.

I'd like to propose introducing assert_not_dom and its alias assert_not_select which return true if no elements are found ie they are an optional drop in replacement for my examples above:

assert_not_select "element[attribute_1=?][attribute_2=?]", value_1, value_2

I believe this pattern is consistent with other test helper pairs provided by ActiveSupport (assert_not, assert_not_nil, assert_no_match etc.).

Some vanity metrics:

I would like to work on this if someone can confirm if the feature will be accepted.

@joshuay03 joshuay03 moved this to In Progress / Pending Review in Open Source Dec 12, 2023
@flavorjones
Copy link
Member

@joshuay03 Thanks for suggesting this! I think this would be a nice addition to the API. If you'd be willing to work on this, I would love that! and would definitely welcome the help.

Side note: I'd love if we also supported the minitest style of refute_x as an alias for assert_not_x.

So tell me if this makes sense:

  • introduce assert_not_dom and assert_not_select
    • alias refute_dom and refute_select to those
  • alias refute_dom_equal to existing assert_dom_not_equal
  • update the README

WDYT?

@joshuay03
Copy link
Contributor Author

@flavorjones Thank you for your reply. All that makes sense to me. I'll update here when I've got a PR up 👍🏽

@joshuay03
Copy link
Contributor Author

I got this up: #113

@joshuay03
Copy link
Contributor Author

joshuay03 commented Dec 13, 2023

Also, when working on this I realised you can do:

assert_select "div", 0 do |matches|
  puts "we never get here but I think we should raise an error if given a block in such cases"
end

I'll open a separate issue for this.

@flavorjones
Copy link
Member

#113 has been merged (thanks, @rafaelfranca!)

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

No branches or pull requests

2 participants