Skip to content

Commit

Permalink
Add :with alias for :option filter on checkbox and radiobutton selectors
Browse files Browse the repository at this point in the history
  • Loading branch information
twalpole committed May 29, 2019
1 parent 9409914 commit 4674c96
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 12 deletions.
13 changes: 8 additions & 5 deletions lib/capybara/selector/definition/checkbox.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,17 @@

filter_set(:_field, %i[checked unchecked disabled name])

node_filter(:option) do |node, value|
node_filter(%i[option with]) do |node, value|
val = node.value
(val == value.to_s).tap do |res|
add_error("Expected option value to be #{value.inspect} but it was #{val.inspect}") unless res
(value.is_a?(Regexp) ? value.match?(val) : val == value.to_s).tap do |res|
add_error("Expected value to be #{value.inspect} but it was #{val.inspect}") unless res
end
end

describe_node_filters do |option: nil, **|
" with value #{option.inspect}" if option
describe_node_filters do |option: nil, with: nil, **|
desc = +''
desc << " with value #{option.inspect}" if option
desc << " with value #{with.inspec}" if with
desc
end
end
13 changes: 8 additions & 5 deletions lib/capybara/selector/definition/radio_button.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,17 @@

filter_set(:_field, %i[checked unchecked disabled name])

node_filter(:option) do |node, value|
node_filter(%i[option with]) do |node, value|
val = node.value
(val == value.to_s).tap do |res|
add_error("Expected option value to be #{value.inspect} but it was #{val.inspect}") unless res
(value.is_a?(Regexp) ? value.match?(val) : val == value.to_s).tap do |res|
add_error("Expected value to be #{value.inspect} but it was #{val.inspect}") unless res
end
end

describe_node_filters do |option: nil, **|
" with value #{option.inspect}" if option
describe_node_filters do |option: nil, with: nil, **|
desc = +''
desc << " with value #{option.inspect}" if option
desc << " with value #{with.inspec}" if with
desc
end
end
6 changes: 4 additions & 2 deletions lib/capybara/selector/filter_set.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ def initialize(name, &block)
instance_eval(&block)
end

def node_filter(name, *types_and_options, &block)
add_filter(name, Filters::NodeFilter, *types_and_options, &block)
def node_filter(names, *types_and_options, &block)
Array(names).each do |name|
add_filter(name, Filters::NodeFilter, *types_and_options, &block)
end
end
alias_method :filter, :node_filter

Expand Down
6 changes: 6 additions & 0 deletions lib/capybara/spec/session/check_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,12 @@
expect(extract_results(@session)['pets']).to include('cat')
end

it 'should alias `with`' do
@session.check('form[pets][]', with: 'cat')
@session.click_button('awesome')
expect(extract_results(@session)['pets']).to include('cat')
end

it 'should raise an error if option not found' do
expect do
@session.check('form[pets][]', option: 'elephant')
Expand Down
6 changes: 6 additions & 0 deletions lib/capybara/spec/session/choose_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@
expect(extract_results(@session)['gender']).to eq('male')
end

it 'should alias `:with` option' do
@session.choose('form[gender]', with: 'male')
@session.click_button('awesome')
expect(extract_results(@session)['gender']).to eq('male')
end

it 'should raise an error if option not found' do
expect do
@session.choose('form[gender]', option: 'hermaphrodite')
Expand Down

0 comments on commit 4674c96

Please sign in to comment.