Sha256: 39ca6533f907c5b66c25eb44c0b49e0517832648613217f4fa5a8067a077be9c

Contents?: true

Size: 1.89 KB

Versions: 1

Compression:

Stored size: 1.89 KB

Contents

# 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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rubocop-rspec-1.39.0 lib/rubocop/cop/rspec/capybara/visibility_matcher.rb