Allow default_set_options to be nil and add test

This commit is contained in:
Thomas Walpole 2018-05-18 10:29:53 -07:00
parent 374f64884a
commit 26ac4c13fb
6 changed files with 19 additions and 5 deletions

View File

@ -3,7 +3,7 @@ Release date: unreleased
### Added
* New global configuration `default_set_options` used in `Capybara::Node::Element#set` as default `options` hash
* New global configuration `default_set_options` used in `Capybara::Node::Element#set` as default `options` hash [Champier Cyril]
# Version 3.1.0
Release date: 2018-05-10

View File

@ -77,7 +77,7 @@ module Capybara
# @option options [String] name Match fields that match the name attribute
# @option options [String] placeholder Match fields that match the placeholder attribute
# @option options [String, Array<String>] class Match fields that match the class(es) provided
# @option options [Hash] fill_options Driver specific options regarding how to fill fields
# @option options [Hash] fill_options Driver specific options regarding how to fill fields (Defaults come from Capybara.default_set_options)
#
# @return [Capybara::Node::Element] The element filled_in
def fill_in(locator = nil, with:, fill_options: {}, **options)

View File

@ -89,7 +89,7 @@ module Capybara
# @return [Capybara::Node::Element] The element
def set(value, **options)
raise Capybara::ReadOnlyElementError, "Attempt to set readonly element with value: #{value}" if readonly?
options = session_options.default_set_options.merge(options)
options = session_options.default_set_options.to_h.merge(options)
synchronize { base.set(value, options) }
self
end

View File

@ -159,6 +159,13 @@ Capybara::SpecHelper.spec "#fill_in" do
expect(extract_results(@session)['zipcode']).to eq('12345')
end
it "fills in a field if default_set_options is nil" do
Capybara.default_set_options = nil
@session.fill_in(:form_first_name, with: 'Thomas')
@session.click_button('awesome')
expect(extract_results(@session)['first_name']).to eq('Thomas')
end
context 'on a pre-populated textfield with a reformatting onchange', requires: [:js] do
it 'should only trigger onchange once' do
@session.visit('/with_js')

View File

@ -102,9 +102,9 @@ Capybara::SpecHelper.spec "node" do
expect { @session.first('//textarea[@readonly]').set('changed') }.to raise_error(Capybara::ReadOnlyElementError)
end
it 'should use global default options', requires: [:js] do
it 'should use global default options' do
Capybara.default_set_options = { clear: :backspace }
element = @session.first('//input')
element = @session.first(:fillable_field, type: 'text')
expect(element.base).to receive(:set).with('gorilla', clear: :backspace)
element.set('gorilla')
end

View File

@ -88,6 +88,13 @@ RSpec.shared_examples "Capybara::Session" do |session, mode|
expect(session.find(:fillable_field, 'form_first_name').value).to eq('Harry')
end
it 'should fill in if the option is set via global option' do
Capybara.default_set_options = { clear: :backspace }
session.visit('/form')
session.fill_in('form_first_name', with: 'Thomas')
expect(session.find(:fillable_field, 'form_first_name').value).to eq('Thomas')
end
it 'should only trigger onchange once' do
session.visit('/with_js')
session.fill_in('with_change_event',