-
-
Notifications
You must be signed in to change notification settings - Fork 278
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
178 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module RSpec | ||
module Capybara | ||
# Checks for boolean visibility in capybara finders. | ||
# | ||
# Capybara lets you find elements that match a certain visibility using | ||
# the `:visible` option. `:visible` accepts both boolean and symbols as | ||
# values, however using booleans can have unwanted effects. `visible: | ||
# false` does not find just invisible elements, but both visible and | ||
# invisible elements. For expressiveness and clarity, use one of the | ||
# symbol values, `:all`, `:hidden` or `:visible`. | ||
# (https://www.rubydoc.info/gems/capybara/Capybara%2FNode%2FFinders:all) | ||
# | ||
# @example | ||
# | ||
# # bad | ||
# expect(page).to have_selector('.foo', visible: false) | ||
# | ||
# # bad | ||
# expect(page).to have_selector('.foo', visible: true) | ||
# | ||
# # good | ||
# expect(page).to have_selector('.foo', visible: :all) | ||
# | ||
# # good | ||
# expect(page).to have_selector('.foo', visible: :hidden) | ||
# | ||
# # good | ||
# expect(page).to have_selector('.foo', visible: :visible) | ||
# | ||
class VisibilityMatcher < Cop | ||
MSG_FALSE = 'Use `:all` or `:hidden` instead of `false`.' | ||
MSG_TRUE = 'Use `:visible` instead of `true`.' | ||
|
||
def_node_matcher :visible_true?, <<~PATTERN | ||
(send nil? :have_selector ... (hash <$(pair (sym :visible) true) ...>)) | ||
PATTERN | ||
|
||
def_node_matcher :visible_false?, <<~PATTERN | ||
(send nil? :have_selector ... (hash <$(pair (sym :visible) false) ...>)) | ||
PATTERN | ||
|
||
def on_send(node) | ||
visible_false?(node) { |arg| add_offense(arg, message: MSG_FALSE) } | ||
visible_true?(node) { |arg| add_offense(arg, message: MSG_TRUE) } | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
spec/rubocop/cop/rspec/capybara/visibility_matcher_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::RSpec::Capybara::VisibilityMatcher do | ||
subject(:cop) { described_class.new } | ||
|
||
it 'registers an offense when using `visible: true`' do | ||
expect_offense(<<-RUBY) | ||
expect(page).to have_selector('.my_element', visible: true) | ||
^^^^^^^^^^^^^ Use `:visible` instead of `true`. | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when using `visible: false`' do | ||
expect_offense(<<-RUBY) | ||
expect(page).to have_selector('.my_element', 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) | ||
^^^^^^^^^^^^^^ Use `:all` or `:hidden` instead of `false`. | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when using a using multiple options`' do | ||
expect_offense(<<-RUBY) | ||
expect(page).to have_selector('.my_element', count: 1, visible: false, normalize_ws: true) | ||
^^^^^^^^^^^^^^ Use `:all` or `:hidden` instead of `false`. | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when no options are given`' do | ||
expect_no_offenses(<<~RUBY) | ||
expect(page).to have_selector('.my_element') | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using `visible: :all`' do | ||
expect_no_offenses(<<~RUBY) | ||
expect(page).to have_selector('.my_element', visible: :all) | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using `visible: :visible`' do | ||
expect_no_offenses(<<~RUBY) | ||
expect(page).to have_selector('.my_element', visible: :visible) | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using `visible: :hidden`' do | ||
expect_no_offenses(<<~RUBY) | ||
expect(page).to have_selector('.my_element', visible: :hidden) | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using other options' do | ||
expect_no_offenses(<<~RUBY) | ||
expect(page).to have_selector('.my_element', normalize_ws: true) | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using multiple options' do | ||
expect_no_offenses(<<~RUBY) | ||
expect(page).to have_selector('.my_element', count: 1, normalize_ws: true) | ||
RUBY | ||
end | ||
end |