From 4c9aa96b17cff8dd3599e50c3fc13526ad4b595f Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Thu, 20 Nov 2014 09:52:19 +1100 Subject: [PATCH] have_select2 can test for an exact set of options --- spec/support/matchers/select2_matchers.rb | 46 +++++++++++++++-------- 1 file changed, 30 insertions(+), 16 deletions(-) diff --git a/spec/support/matchers/select2_matchers.rb b/spec/support/matchers/select2_matchers.rb index af7a5b4aff..8b7bc2f834 100644 --- a/spec/support/matchers/select2_matchers.rb +++ b/spec/support/matchers/select2_matchers.rb @@ -5,29 +5,21 @@ RSpec::Matchers.define :have_select2 do |id, options={}| # TODO: Instead of passing in id, use a more general locator match_for_should do |node| - @id, @options = id, options + @id, @options, @node = id, options, node #id = find_label_by_text(locator) from = "#s2id_#{id}" - found_element = node.has_selector? from + results = [] - if !found_element - false + results << node.has_selector?(from) - else - if options.key? :with_options - find(from).click - - all_options_present = options[:with_options].all? do |option| - node.has_selector? "div.select2-drop-active ul.select2-results li", text: option - end - - find(from).click - - all_options_present - end + if results.all? + results << all_options_present(from, options[:with_options]) if options.key? :with_options + results << exact_options_present(from, options[:options]) if options.key? :options end + + results.all? end failure_message_for_should do |actual| @@ -39,4 +31,26 @@ RSpec::Matchers.define :have_select2 do |id, options={}| match_for_should_not do |node| raise "Not yet implemented" end + + + def all_options_present(from, options) + with_select2_open(from) do + options.all? do |option| + @node.has_selector? "div.select2-drop-active ul.select2-results li", text: option + end + end + end + + def exact_options_present(from, options) + with_select2_open(from) do + @node.all("div.select2-drop-active ul.select2-results li").map(&:text) == options + end + end + + def with_select2_open(from) + find(from).click + r = yield + find(from).click + r + end end