rails/rails-dom-testing

Feature | Introduce `assert_not_dom` and its alias `assert_not_select`

joshuay03 opened this issue · 5 comments

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 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?

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

I got this up: #113

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.

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