-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Raise when declared elements conflict with Capybara's RSpec matchers
What? ===== PageEz defines predicate methods for each element declared, making it easy to assert against presence or absence on a page. RSpec then takes these predicate methods to generate implicit matchers. However, concrete matchers take precedence over predicate matchers. In the case of elements that may feasibly be defined in a page object, like `title`, `button`, `field`, `select`, and `table`, this may present challenges if arguments are passed that differ from the matcher implementation. This introduces a new configuration setting, which defaults to raising an exception, when a collision occurs.
- Loading branch information
1 parent
3baad36
commit 6847911
Showing
6 changed files
with
132 additions
and
7 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
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,59 @@ | ||
module PageEz | ||
module Visitors | ||
class MatcherCollisionVisitor | ||
def initialize | ||
reset | ||
end | ||
|
||
def begin_block_evaluation | ||
@depth_visitor.begin_block_evaluation | ||
end | ||
|
||
def end_block_evaluation | ||
@depth_visitor.end_block_evaluation | ||
end | ||
|
||
def define_method(name) | ||
@depth_visitor.define_method(name) | ||
end | ||
|
||
def inherit_from(subclass) | ||
@depth_visitor.inherit_from(subclass) | ||
end | ||
|
||
def process_macro(macro, name, selector) | ||
@depth_visitor.process_macro(macro, name, selector) | ||
if existing_matchers.include?(name.to_s) | ||
rendered_macro = "#{macro} :#{name}, \"#{selector}\"" | ||
whitespace = " " * @depth_visitor.depth | ||
message = "#{whitespace}#{rendered_macro} will conflict with Capybara's `have_#{name}` matcher" | ||
|
||
case PageEz.configuration.on_matcher_collision | ||
when :warn | ||
PageEz.configuration.logger.warn(message) | ||
when :raise | ||
raise MatcherCollisionError, message | ||
end | ||
end | ||
end | ||
|
||
def reset | ||
@depth_visitor = DepthVisitor.new | ||
end | ||
|
||
private | ||
|
||
def existing_matchers | ||
if defined?(Capybara::RSpecMatchers) | ||
Capybara::RSpecMatchers.instance_methods.filter_map do |method_name| | ||
if (match = method_name.to_s.match(/^have_(?!no_)(.+)$/)) | ||
match[1] | ||
end | ||
end | ||
else | ||
[] | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
require "spec_helper" | ||
|
||
RSpec.describe "Declaring elements that overlap with Capybara matchers", type: :feature do | ||
it "warns that those matchers will be used instead of the predicate methods" do | ||
PageEz.configure do |config| | ||
config.on_matcher_collision = :warn | ||
end | ||
|
||
logger = configure_fake_logger | ||
|
||
Class.new(PageEz::Page) do | ||
has_one :thing, "section" do | ||
has_one :title, "h3" | ||
end | ||
end | ||
|
||
expect(logger.warns).to contain_in_order( | ||
" has_one :title, \"h3\" will conflict with Capybara's `have_title` matcher" | ||
) | ||
end | ||
|
||
it "raises when configured to raise" do | ||
PageEz.configure do |config| | ||
config.on_matcher_collision = :raise | ||
end | ||
|
||
expect do | ||
Class.new(PageEz::Page) do | ||
has_one :thing, "section" do | ||
has_one :title, "h3" | ||
end | ||
end | ||
end.to raise_error(PageEz::MatcherCollisionError, /will conflict/) | ||
end | ||
|
||
it "does not raise or warn when configured to ignore" do | ||
PageEz.configure do |config| | ||
config.on_matcher_collision = nil | ||
end | ||
|
||
logger = configure_fake_logger | ||
|
||
Class.new(PageEz::Page) do | ||
has_one :thing, "section" do | ||
has_one :title, "h3" | ||
end | ||
end | ||
|
||
expect(logger.warns).to be_empty | ||
end | ||
end |