lib/decidim/dev/test/rspec_support/capybara_select2.rb in decidim-dev-0.6.8 vs lib/decidim/dev/test/rspec_support/capybara_select2.rb in decidim-dev-0.7.0

- old
+ new

@@ -1,35 +1,32 @@ # frozen_string_literal: true -# extracted from https://github.com/goodwill/capybara-select2 +# Adapted from https://github.com/goodwill/capybara-select2 module Capybara module Select2 - def select2(value, options = {}) - raise "Must pass a hash containing 'from' or 'xpath' or 'css'" unless options.is_a?(Hash) && [:from, :xpath, :css].any? { |k| options.has_key? k } + def select2(value, xpath:, search:) + expect(page).to have_xpath(xpath) + select2_container = find(:xpath, xpath) - if options.has_key? :xpath - select2_container = find(:xpath, options[:xpath]) - elsif options.has_key? :css - select2_container = find(:css, options[:css]) - else - select_name = options[:from] - select2_container = find("label", text: select_name).find(:xpath, "..").find(".select2-container") - end - - # Open select2 field + expect(select2_container).to have_selector(".select2-selection") select2_container.find(".select2-selection").click - if options.has_key? :search - find(:xpath, "//body").find(".select2-search input.select2-search__field").set(value) + if search + body = find(:xpath, "//body") + expect(body).to have_selector(".select2-search input.select2-search__field") + body.find(".select2-search input.select2-search__field").set(value) + page.execute_script(%|$("input.select2-search__field:visible").keyup();|) drop_container = ".select2-results" else drop_container = ".select2-dropdown" end - [value].flatten.each do |val| - find(:xpath, "//body").find("#{drop_container} li.select2-results__option", text: val).click - end + expect(page).to have_no_content("Searching...") + + body = find(:xpath, "//body") + expect(body).to have_selector("#{drop_container} li.select2-results__option", text: value) + body.find("#{drop_container} li.select2-results__option", text: value).click end end end