Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix a false positive and incorrect autocorrect for RSpec/Capybara/SpecificActions, RSpec/Capybara/SpecificFinders and RSpec/Capybara/SpecificMatcher #1481

Closed
wants to merge 1 commit into from

Conversation

ydah
Copy link
Member

@ydah ydah commented Nov 15, 2022

Fix: rubocop/rubocop-capybara#4


Before submitting the PR make sure the following are checked:

  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • [-] Updated documentation.
  • Added an entry to the CHANGELOG.md if the new code introduces user-observable changes.
  • The build (bundle exec rake) passes (be sure to run this locally, since it may produce updated documentation that you will need to commit).

@@ -66,14 +147,32 @@
^^^^^^^^^^^^^^^^^^^^ Prefer `find_by` over `find`.
find('[visible][id=some-id]')
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `find_by` over `find`.
find('[id=some-id][class=some-cls][focused]')
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `find_by` over `find`.
find('[id=some-id][visible][focused]')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CSS selector [visible] means the element has an attribute named visible (e.g. <span visible="yes">), NOT "the element is actually visible". CSS does not have :visible pseudo-class --- it's a jQuery's extension.
And also, [focused] means the element has an attribute named focused, NOT "the element is now focused". :focus pseudo-class should be used instead.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is indeed true. I guess I was mistaken. Thanks for pointing that out to me.

@ydah ydah changed the title Fix a false positive and incorrect autocorrect for RSpec/Capybara/SpecificFinders Fix a false positive and incorrect autocorrect for RSpec/Capybara/SpecificActions, RSpec/Capybara/SpecificFinders and RSpec/Capybara/SpecificMatcher Dec 5, 2022
@ydah
Copy link
Member Author

ydah commented Dec 5, 2022

I updated this PR.

@pirj pirj requested review from Tietew, Darhazer and bquorning and removed request for Tietew December 7, 2022 07:12
@pirj
Copy link
Member

pirj commented Dec 7, 2022

I can't tell much about those cops, just that there were no errors:

$ rubocop --only RSpec/Capybara/MatchStyle --only RSpec/Capybara/SpecificActions --only RSpec/Capybara/SpecificFinders --only RSpec/Capybara/SpecificMatcher ../real-world-rspec
...
/Users/pirj/source/real-world-rspec/solidus/backend/spec/features/admin/orders/order_details_spec.rb:175:31: C: RSpec/Capybara/SpecificMatcher: Prefer have_field over have_css.
          expect(page).not_to have_css("input")
                              ^^^^^^^^^^^^^^^^^
57497 files inspected, 63 offenses detected

Thank you!

PS In the output there were only offences from RSpec/Capybara/SpecificMatcher, so I intend to re-run it again. It might be rubocop only accepts one --only 🤔

@pirj
Copy link
Member

pirj commented Dec 7, 2022

/Users/pirj/source/real-world-rspec/solidus/frontend/spec/features/checkout_spec.rb:359:14: C: [Correctable] RSpec/Capybara/SpecificFinders: Prefer find_by over find.
      expect(find("#use_existing_card_yes")).to be_checked
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

57497 files inspected, 336 offenses detected, 336 offenses autocorrectable

for the other

@pirj
Copy link
Member

pirj commented Dec 7, 2022

/Users/pirj/source/real-world-rspec/publify/publify_core/spec/features/drafting_articles_spec.rb:25:5: C: RSpec/Capybara/SpecificActions: Prefer click_link over find('a').click.
    find('a[href="/admin/articles/1/edit"]').click
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

57497 files inspected, 63 offenses detected

@ydah ydah force-pushed the fix/1468 branch 2 times, most recently from 4bc501e to 62d7243 Compare December 13, 2022 09:29
@ydah
Copy link
Member Author

ydah commented Dec 13, 2022

I rebased this PR.

Copy link
Collaborator

@bquorning bquorning left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would really like to see some direct spec coverage of the mixins, especially lib/rubocop/cop/rspec/mixin/css_selector.rb. For a start, it could just test the mentioned @example lines of each method.

@@ -87,21 +57,11 @@ def attribute?(selector)
def attributes(selector)
selector.scan(/\[(.*?)\]/).flatten.to_h do |attr|
key, value = attr.split('=')
value << ']' if value&.include?('[')
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does not look right. Probably a shortcoming of us using regular expressions to parse CSS :-)

@pirj
Copy link
Member

pirj commented Dec 15, 2022

Please don't merge this just yet, I'm extracting rspec-capybara.

@pirj
Copy link
Member

pirj commented Dec 29, 2022

May I kindly ask to re-target this to rubocop-capybara, @ydah ?

…ecificActions`, `RSpec/Capybara/SpecificFinders` and `RSpec/Capybara/SpecificMatcher`

Fix: https://github.com/rubocop/rubocop-rspec/issues/1468
@ydah
Copy link
Member Author

ydah commented Dec 30, 2022

@pirj OK, I'll do it 👍 It may take a little time as we add mixin testing as well.

@ydah
Copy link
Member Author

ydah commented Dec 31, 2022

We are now open and PR here is closed.
rubocop/rubocop-capybara#10

@ydah ydah closed this Dec 31, 2022
@ydah ydah deleted the fix/1468 branch January 1, 2023 00:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

RSpec/Capybara/SpecificFinders autocorrection shoud remove escape
4 participants