From 7f5e2c17f795f0950ef6b05ceacb9072fc869f79 Mon Sep 17 00:00:00 2001 From: ydah <13041216+ydah@users.noreply.github.com> Date: Thu, 22 Sep 2022 08:55:52 +0900 Subject: [PATCH 1/2] Exclude `have_text` and `have_content` that raise `ArgumentError` with `RSpec/Capybara/VisibilityMatcher` where `:visible` is an invalid option If `:visible` is specified for `have_text` and its alias method, `have_content`, it will result in an `ArgumentError` as follows. Therefore, This PR is excluded `have_text` and `have_content` for `RSpec/Capybara/VisibilityMatcher`. ``` Failure/Error: expect(page).to have_text('a', visible: true) ArgumentError: Invalid option(s) :visible, should be one of :count, :minimum, :maximum, :between, :wait, :exact, :normalize_ws # ./spec/capybara/sandbox_spec.rb:20:in `block (2 levels) in ' Failure/Error: expect(page).to have_content('a', visible: true) ArgumentError: Invalid option(s) :visible, should be one of :count, :minimum, :maximum, :between, :wait, :exact, :normalize_ws # ./spec/capybara/sandbox_spec.rb:68:in `block (2 levels) in ' ``` --- CHANGELOG.md | 1 + lib/rubocop/cop/rspec/capybara/visibility_matcher.rb | 2 -- spec/rubocop/cop/rspec/capybara/visibility_matcher_spec.rb | 4 ---- 3 files changed, 1 insertion(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 500c09eb2..21ee6660b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ * 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][]) ## 2.13.1 (2022-09-12) diff --git a/lib/rubocop/cop/rspec/capybara/visibility_matcher.rb b/lib/rubocop/cop/rspec/capybara/visibility_matcher.rb index 57098b8d4..fa2e433e6 100644 --- a/lib/rubocop/cop/rspec/capybara/visibility_matcher.rb +++ b/lib/rubocop/cop/rspec/capybara/visibility_matcher.rb @@ -40,8 +40,6 @@ class VisibilityMatcher < Base have_table have_checked_field have_unchecked_field - have_text - have_content ].freeze RESTRICT_ON_SEND = CAPYBARA_MATCHER_METHODS diff --git a/spec/rubocop/cop/rspec/capybara/visibility_matcher_spec.rb b/spec/rubocop/cop/rspec/capybara/visibility_matcher_spec.rb index 7fd4548fb..f67dc1e27 100644 --- a/spec/rubocop/cop/rspec/capybara/visibility_matcher_spec.rb +++ b/spec/rubocop/cop/rspec/capybara/visibility_matcher_spec.rb @@ -35,10 +35,6 @@ ^^^^^^^^^^^^^^ 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) - ^^^^^^^^^^^^^^ Use `:all` or `:hidden` instead of `false`. RUBY end From 567adbe00f16e1387d206f357fa4d2ee8a12bd45 Mon Sep 17 00:00:00 2001 From: ydah <13041216+ydah@users.noreply.github.com> Date: Thu, 22 Sep 2022 20:37:12 +0900 Subject: [PATCH 2/2] Fix a false negative for `RSpec/Capybara/VisibilityMatcher` with negative matchers Fix: #1390 --- CHANGELOG.md | 1 + .../cop/rspec/capybara/visibility_matcher.rb | 26 ++++++++++--------- .../rspec/capybara/visibility_matcher_spec.rb | 23 ++++++++++++++++ 3 files changed, 38 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 21ee6660b..fb44ee2d6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ * 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) diff --git a/lib/rubocop/cop/rspec/capybara/visibility_matcher.rb b/lib/rubocop/cop/rspec/capybara/visibility_matcher.rb index fa2e433e6..c5714332b 100644 --- a/lib/rubocop/cop/rspec/capybara/visibility_matcher.rb +++ b/lib/rubocop/cop/rspec/capybara/visibility_matcher.rb @@ -29,18 +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 - ].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 diff --git a/spec/rubocop/cop/rspec/capybara/visibility_matcher_spec.rb b/spec/rubocop/cop/rspec/capybara/visibility_matcher_spec.rb index f67dc1e27..c6c493679 100644 --- a/spec/rubocop/cop/rspec/capybara/visibility_matcher_spec.rb +++ b/spec/rubocop/cop/rspec/capybara/visibility_matcher_spec.rb @@ -38,6 +38,29 @@ 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 + it 'registers an offense when using a selector`' do expect_offense(<<-RUBY) expect(page).to have_selector(:css, '.my_element', visible: false)