Skip to content

Commit

Permalink
Merge pull request #79 from rubocop/fix78-1
Browse files Browse the repository at this point in the history
Fix a false negative for `RSpec/HaveSelector` when first argument is dstr node
  • Loading branch information
ydah authored Nov 4, 2023
2 parents 355d09b + 9c763ab commit 17bb784
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Edge (Unreleased)

- Change to default `EnforcedStyle: link_or_button` for `Capybara/ClickLinkOrButtonStyle` cop. ([@ydah])
- Fix a false negative for `RSpec/HaveSelector` when first argument is dstr node. ([@ydah])

## 2.19.0 (2023-09-20)

Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/capybara/rspec/have_selector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class HaveSelector < ::RuboCop::Cop::Base
def on_send(node)
argument = node.first_argument
on_select_with_type(node, argument) if argument.sym_type?
on_select_without_type(node) if argument.str_type?
on_select_without_type(node) if %i[str dstr].include?(argument.type)
end

private
Expand Down
24 changes: 24 additions & 0 deletions spec/rubocop/cop/capybara/rspec/have_selector_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,30 @@
RUBY
end

it 'registers an offense when using `have_selector` with `:css` ' \
'and "#{bar}"' do
expect_offense(<<~'RUBY')
expect(foo).to have_selector(:css, "#{bar}")
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `have_css` instead of `have_selector`.
RUBY

expect_correction(<<~'RUBY')
expect(foo).to have_css("#{bar}")
RUBY
end

it 'registers an offense when using `have_selector` with ' \
'"input[name=\'#{title}\']"' do
expect_offense(<<~'RUBY')
expect(foo).to have_selector("input[name='#{title}']")
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `have_css` instead of `have_selector`.
RUBY

expect_correction(<<~'RUBY')
expect(foo).to have_css("input[name='#{title}']")
RUBY
end

context 'when DefaultSelector is xpath' do
let(:default_selector) { 'xpath' }

Expand Down

0 comments on commit 17bb784

Please sign in to comment.