Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Forward given filter block to matcher #2617

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 20 additions & 8 deletions lib/capybara/rspec/matchers/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,19 @@ def session_options
end

class WrappedElementMatcher < Base
def matches?(actual)
element_matches?(wrap(actual))
def matches?(actual, &filter_block)
with_filter(filter_block) do
element_matches?(wrap(actual))
end
rescue Capybara::ExpectationNotMet => e
@failure_message = e.message
false
end

def does_not_match?(actual)
element_does_not_match?(wrap(actual))
def does_not_match?(actual, &filter_block)
with_filter(filter_block) do
element_does_not_match?(wrap(actual))
end
rescue Capybara::ExpectationNotMet => e
@failure_message_when_negated = e.message
false
Expand All @@ -71,6 +75,14 @@ def wrap(actual)
Capybara.string(actual.to_s)
end
end

def with_filter(filter_block)
previous_filter_block = @filter_block
@filter_block = filter_block if filter_block
yield
ensure
@filter_block = previous_filter_block
end
end

class CountableWrappedElementMatcher < WrappedElementMatcher
Expand All @@ -86,12 +98,12 @@ def initialize(matcher)
@matcher = matcher
end

def matches?(actual)
@matcher.does_not_match?(actual)
def matches?(actual, &filter_block)
@matcher.does_not_match?(actual, &filter_block)
end

def does_not_match?(actual)
@matcher.matches?(actual)
def does_not_match?(actual, &filter_block)
@matcher.matches?(actual, &filter_block)
end

def description
Expand Down
23 changes: 23 additions & 0 deletions spec/rspec_matchers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,29 @@
end
end

context 'with a filter block' do
it 'applies the filter' do
visit('/with_html')
expect(page).to have_selector(:css, 'input#test_field') { |input| input.value == 'monkey' }
expect(page).to have_selector(:css, 'input#test_field') do |input|
input.value == 'monkey'
end
expect(page).to have_no_selector(:css, 'input#test_field') { |input| input.value == 'not a monkey' }
expect(page).to have_no_selector(:css, 'input#test_field') do |input|
input.value == 'not a monkey'
end

expect(page).not_to have_selector(:css, 'input#test_field') { |input| input.value == 'not a monkey' }
expect(page).not_to have_selector(:css, 'input#test_field') do |input|
input.value == 'not a monkey'
end
expect(page).not_to have_no_selector(:css, 'input#test_field') { |input| input.value == 'monkey' }
expect(page).not_to have_no_selector(:css, 'input#test_field') do |input|
input.value == 'monkey'
end
end
end

def error_msg_for(&block)
expect(&block).to(raise_error { |e| return e.message })
end
Expand Down