From 29e9753e76795790c891c62204b6261338f9f694 Mon Sep 17 00:00:00 2001 From: Earlopain <14981592+Earlopain@users.noreply.github.com> Date: Mon, 19 Aug 2024 11:00:18 +0200 Subject: [PATCH] Fix an error for `Capybara/RSpec/HaveSelector` when passing no arguments. --- CHANGELOG.md | 2 ++ lib/rubocop/cop/capybara/rspec/have_selector.rb | 3 ++- spec/rubocop/cop/capybara/rspec/have_selector_spec.rb | 6 ++++++ 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1744894..a868d81 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ - Add `Capybara/AmbiguousClick` cop and make soft-deprecated `Capybara/ClickLinkOrButtonStyle` cop. If you want to use `EnforcedStyle: strict`, use `Capybara/AmbiguousClick` cop instead. ([@ydah]) - Add new `Capybara/FindAllFirst` cop. ([@ydah]) +- Fix an error for `Capybara/RSpec/HaveSelector` when passing no arguments. ([@earlopain]) ## 2.21.0 (2024-06-08) @@ -78,6 +79,7 @@ [@aried3r]: https://github.com/aried3r [@bquorning]: https://github.com/bquorning [@darhazer]: https://github.com/Darhazer +[@earlopain]: https://github.com/earlopain [@onumis]: https://github.com/onumis [@oskarsezerins]: https://github.com/OskarsEzerins [@pirj]: https://github.com/pirj diff --git a/lib/rubocop/cop/capybara/rspec/have_selector.rb b/lib/rubocop/cop/capybara/rspec/have_selector.rb index 9a47faf..a4825bb 100644 --- a/lib/rubocop/cop/capybara/rspec/have_selector.rb +++ b/lib/rubocop/cop/capybara/rspec/have_selector.rb @@ -42,7 +42,8 @@ class HaveSelector < ::RuboCop::Cop::Base SELECTORS = %i[css xpath].freeze def on_send(node) - argument = node.first_argument + return unless (argument = node.first_argument) + on_select_with_type(node, argument) if argument.sym_type? on_select_without_type(node) if %i[str dstr].include?(argument.type) end diff --git a/spec/rubocop/cop/capybara/rspec/have_selector_spec.rb b/spec/rubocop/cop/capybara/rspec/have_selector_spec.rb index 935681b..3793167 100644 --- a/spec/rubocop/cop/capybara/rspec/have_selector_spec.rb +++ b/spec/rubocop/cop/capybara/rspec/have_selector_spec.rb @@ -75,6 +75,12 @@ RUBY end + it 'registers no offense when no arguments are passed' do + expect_no_offenses(<<~RUBY) + expect(foo).to have_selector + RUBY + end + context 'when DefaultSelector is xpath' do let(:default_selector) { 'xpath' }