lib/rubocop/cop/capybara/current_path_expectation.rb in rubocop-capybara-2.18.0 vs lib/rubocop/cop/capybara/current_path_expectation.rb in rubocop-capybara-2.19.0
- old
+ new
@@ -16,20 +16,18 @@
# This cop does not support autocorrection in some cases.
#
# @example
# # bad
# expect(current_path).to eq('/callback')
+ # expect(page.current_path).to eq('/callback')
#
# # good
- # expect(page).to have_current_path('/callback')
+ # expect(page).to have_current_path('/callback', ignore_query: true)
#
- # # bad (does not support autocorrection)
- # expect(page.current_path).to match(variable)
+ # # bad (does not support autocorrection when `match` with a variable)
+ # expect(page).to match(variable)
#
- # # good
- # expect(page).to have_current_path('/callback')
- #
class CurrentPathExpectation < ::RuboCop::Cop::Base
extend AutoCorrector
include RangeHelp
MSG = 'Do not set an RSpec expectation on `current_path` in ' \
@@ -93,11 +91,11 @@
else
'have_no_current_path'
end
corrector.replace(matcher_node.loc.selector, matcher_method)
add_argument_parentheses(corrector, matcher_node.first_argument)
- add_ignore_query_options(corrector, node)
+ add_ignore_query_options(corrector, node, matcher_node)
end
def convert_regexp_node_to_literal(corrector, matcher_node, regexp_node)
str_node = matcher_node.first_argument
regexp_expr = regexp_node_to_regexp_expr(regexp_node)
@@ -127,21 +125,16 @@
end
# `have_current_path` with no options will include the querystring
# while `page.current_path` does not.
# This ensures the option `ignore_query: true` is added
- # except when the expectation is a regexp or string
- def add_ignore_query_options(corrector, node)
+ # except when `match` matcher.
+ def add_ignore_query_options(corrector, node, matcher_node)
+ return if matcher_node.method?(:match)
+
expectation_node = node.parent.last_argument
expectation_last_child = expectation_node.children.last
- return if %i[
- regexp str dstr xstr
- ].include?(expectation_last_child.type)
-
- corrector.insert_after(
- expectation_last_child,
- ', ignore_query: true'
- )
+ corrector.insert_after(expectation_last_child, ', ignore_query: true')
end
end
end
end
end