Skip to content

Commit

Permalink
Change to default EnforcedStyle: link_or_button for `Capybara/Click…
Browse files Browse the repository at this point in the history
…LinkOrButtonStyle` cop

Fix: #61 (comment)
Fix: #61 (comment)
  • Loading branch information
ydah committed Oct 2, 2023
1 parent c449767 commit f94468d
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 26 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Edge (Unreleased)

- Change to default `EnforcedStyle: link_or_button` for `Capybara/ClickLinkOrButtonStyle` cop. ([@ydah])

## 2.19.0 (2023-09-20)

- Add new `Capybara/RSpec/PredicateMatcher` cop. ([@ydah])
Expand Down
7 changes: 4 additions & 3 deletions config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@ Capybara:
- "**/features/step_definitions/**/*"

Capybara/ClickLinkOrButtonStyle:
Description: Checks for click button or link style.
Description: Checks for methods of button or link clicks.
Enabled: pending
VersionAdded: '2.19'
EnforcedStyle: strict
VersionChanged: "<<next>>"
EnforcedStyle: link_or_button
SupportedStyles:
- strict
- link_or_button
- strict
Reference: https://www.rubydoc.info/gems/rubocop-capybara/RuboCop/Cop/Capybara/ClickLinkOrButtonStyle

Capybara/CurrentPathExpectation:
Expand Down
35 changes: 21 additions & 14 deletions docs/modules/ROOT/pages/cops_capybara.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -9,37 +9,44 @@
| Yes
| No
| 2.19
| -
| <<next>>
|===

Checks for click button or link style.
Checks for methods of button or link clicks.

By default, prefer to use `click_link_or_button` or `click_on`.
These methods offer a weaker coupling between the test and HTML,
allowing for a more faithful reflection of how the user behaves.

You can set `EnforcedStyle: strict` to prefer the use of
`click_link` and `click_button`, but this is a deprecated setting.

=== Examples

==== EnforcedStyle: strict (default)
==== EnforcedStyle: link_or_button (default)

[source,ruby]
----
# bad
click_link_or_button('foo')
click_on('foo')
# good
click_link('foo')
click_button('foo')
# good
click_link_or_button('foo')
click_on('foo')
----

==== EnforcedStyle: link_or_button
==== EnforcedStyle: strict

[source,ruby]
----
# bad
click_link('foo')
click_button('foo')
# good
click_link_or_button('foo')
click_on('foo')
# good
click_link('foo')
click_button('foo')
----

=== Configurable attributes
Expand All @@ -48,8 +55,8 @@ click_on('foo')
| Name | Default value | Configurable values

| EnforcedStyle
| `strict`
| `strict`, `link_or_button`
| `link_or_button`
| `link_or_button`, `strict`
|===

=== References
Expand Down
25 changes: 16 additions & 9 deletions lib/rubocop/cop/capybara/click_link_or_button_style.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,16 @@
module RuboCop
module Cop
module Capybara
# Checks for click button or link style.
# Checks for methods of button or link clicks.
#
# @example EnforcedStyle: strict (default)
# # bad
# click_link_or_button('foo')
# click_on('foo')
# By default, prefer to use `click_link_or_button` or `click_on`.
# These methods offer a weaker coupling between the test and HTML,
# allowing for a more faithful reflection of how the user behaves.
#
# # good
# click_link('foo')
# click_button('foo')
# You can set `EnforcedStyle: strict` to prefer the use of
# `click_link` and `click_button`, but this is a deprecated setting.
#
# @example EnforcedStyle: link_or_button
# @example EnforcedStyle: link_or_button (default)
# # bad
# click_link('foo')
# click_button('foo')
Expand All @@ -23,6 +21,15 @@ module Capybara
# click_link_or_button('foo')
# click_on('foo')
#
# @example EnforcedStyle: strict
# # bad
# click_link_or_button('foo')
# click_on('foo')
#
# # good
# click_link('foo')
# click_button('foo')
#
class ClickLinkOrButtonStyle < ::RuboCop::Cop::Base
include ConfigurableEnforcedStyle

Expand Down

0 comments on commit f94468d

Please sign in to comment.