Skip to content

Commit

Permalink
implement unselect in select2 plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
twalpole committed Oct 2, 2018
1 parent ab6135a commit 3687087
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 5 deletions.
1 change: 1 addition & 0 deletions lib/capybara.rb
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,7 @@ module Selenium; end
config.test_id = nil
config.predicates_wait = true
config.default_normalize_ws = false
config.default_plugin = { select: nil, unselect: nil, check: nil, uncheck: nil, choose: nil }
end

Capybara.register_driver :rack_test do |app|
Expand Down
22 changes: 18 additions & 4 deletions lib/capybara/plugins/select2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,29 @@
module Capybara
module Plugins
class Select2
def select(scope, value, from: nil, **options)
select2 = if from
def select(scope, value, **options)
select2 = find_select2(scope, value, options).click
option = scope.find(:select2_option, value)
option[:"aria-selected"] != 'true' ? option.click : select2.click
end

def unselect(scope, value, **options)
select2 = find_select2(scope, value, options)
raise Capybara::UnselectNotAllowed, 'Cannot unselect option from single select box.' unless select2.has_css?('.select2-selection--multiple')
select2.click
option = scope.find(:select2_option, value)
option[:"aria-selected"] == 'true' ? option.click : select2.click
end

private

def find_select2(scope, value, from: nil, **options)
if from
scope.find(:select2, from, options.merge(visible: false))
else
select = scope.find(:option, value, options).ancestor(:css, 'select', visible: false)
select.find(:xpath, XPath.next_sibling(:span)[XPath.attr(:class).contains_word('select2')][XPath.attr(:class).contains_word('select2-container')])
end
select2.click
scope.find(:select2_option, value).click
end
end
end
Expand Down
20 changes: 19 additions & 1 deletion lib/capybara/spec/session/plugin_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,29 @@
expect(@session).to have_field(type: 'select', with: 'FL', visible: false)
end

it 'should remain selected if called twice on a single select' do
@session.select 'Florida', from: 'Click this to focus the single select element', using: :select2
@session.select 'Florida', from: 'Click this to focus the single select element', using: :select2
expect(@session).to have_field(type: 'select', with: 'FL', visible: false)
end

it 'should work with multiple select' do
@session.select 'Pennsylvania', from: 'Click this to focus the multiple select element', using: :select2
@session.select 'California', from: 'Click this to focus the multiple select element', using: :select2

expect(@session).to have_select(multiple: true, selected: %w[Pennsylvania California], visible: false)
@session.unselect 'Pennsylvania', from: 'Click this to focus the multiple select element', using: :select2
expect(@session).to have_select(multiple: true, selected: %w[California], visible: false)
@session.unselect 'California', from: 'Click this to focus the multiple select element', using: :select2
expect(@session).to have_select(multiple: true, selected: %w[], visible: false)
end

it 'should not reselect if already selected' do
@session.select 'Pennsylvania', from: 'Click this to focus the multiple select element', using: :select2
@session.select 'Pennsylvania', from: 'Click this to focus the multiple select element', using: :select2
expect(@session).to have_select(multiple: true, selected: %w[Pennsylvania], visible: false)
@session.unselect 'Pennsylvania', from: 'Click this to focus the multiple select element', using: :select2
@session.unselect 'Pennsylvania', from: 'Click this to focus the multiple select element', using: :select2
expect(@session).to have_select(multiple: true, selected: %w[], visible: false)
end

it 'should work with id' do
Expand Down

0 comments on commit 3687087

Please sign in to comment.