Skip to content

Commit

Permalink
Merge pull request #1391 from ydah/fix/visibilly_matcher
Browse files Browse the repository at this point in the history
Fix a false negative for `RSpec/Capybara/VisibilityMatcher` with negative matcher
  • Loading branch information
Darhazer authored Sep 22, 2022
2 parents 3e05139 + 567adbe commit b6a1ec0
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 17 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

* Fix an error for `RSpec/Capybara/SpecificFinders` with no parentheses. ([@ydah][])
* Fix a false positive for `RSpec/NoExpectationExample` with pending using `skip` or `pending` inside an example. ([@ydah][])
* Exclude `have_text` and `have_content` that raise `ArgumentError` with `RSpec/Capybara/VisibilityMatcher` where `:visible` is an invalid option. ([@ydah][])
* Fix a false negative for `RSpec/Capybara/VisibilityMatcher` with negative matchers. ([@ydah][])

## 2.13.1 (2022-09-12)

Expand Down
28 changes: 14 additions & 14 deletions lib/rubocop/cop/rspec/capybara/visibility_matcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,20 @@ module Capybara
class VisibilityMatcher < Base
MSG_FALSE = 'Use `:all` or `:hidden` instead of `false`.'
MSG_TRUE = 'Use `:visible` instead of `true`.'
CAPYBARA_MATCHER_METHODS = %i[
have_selector
have_css
have_xpath
have_link
have_button
have_field
have_select
have_table
have_checked_field
have_unchecked_field
have_text
have_content
].freeze
CAPYBARA_MATCHER_METHODS = %w[
button
checked_field
css
field
link
select
selector
table
unchecked_field
xpath
].flat_map do |element|
["have_#{element}".to_sym, "have_no_#{element}".to_sym]
end

RESTRICT_ON_SEND = CAPYBARA_MATCHER_METHODS

Expand Down
25 changes: 22 additions & 3 deletions spec/rubocop/cop/rspec/capybara/visibility_matcher_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,29 @@
^^^^^^^^^^^^^^ Use `:all` or `:hidden` instead of `false`.
expect(page).to have_unchecked_field('cat', visible: false)
^^^^^^^^^^^^^^ Use `:all` or `:hidden` instead of `false`.
expect(page).to have_text('My homepage', visible: false)
^^^^^^^^^^^^^^ Use `:all` or `:hidden` instead of `false`.
expect(page).to have_content('Success', visible: false)
RUBY
end

it 'recognizes multiple negative matchers' do
expect_offense(<<-RUBY)
expect(page).to have_no_css('.profile', visible: false)
^^^^^^^^^^^^^^ Use `:all` or `:hidden` instead of `false`.
expect(page).to have_no_xpath('.//profile', visible: false)
^^^^^^^^^^^^^^ Use `:all` or `:hidden` instead of `false`.
expect(page).to have_no_link('news', visible: false)
^^^^^^^^^^^^^^ Use `:all` or `:hidden` instead of `false`.
expect(page).to have_no_button('login', visible: false)
^^^^^^^^^^^^^^ Use `:all` or `:hidden` instead of `false`.
expect(page).to have_no_field('name', visible: false)
^^^^^^^^^^^^^^ Use `:all` or `:hidden` instead of `false`.
expect(page).to have_no_select('sauce', visible: false)
^^^^^^^^^^^^^^ Use `:all` or `:hidden` instead of `false`.
expect(page).to have_no_table('arrivals', visible: false)
^^^^^^^^^^^^^^ Use `:all` or `:hidden` instead of `false`.
expect(page).to have_no_checked_field('cat', visible: false)
^^^^^^^^^^^^^^ Use `:all` or `:hidden` instead of `false`.
expect(page).to have_no_unchecked_field('cat', visible: false)
^^^^^^^^^^^^^^ Use `:all` or `:hidden` instead of `false`.
RUBY
end

Expand Down

0 comments on commit b6a1ec0

Please sign in to comment.