have_select2 can test for an exact set of options

This commit is contained in:
Rohan Mitchell
2014-11-20 09:52:19 +11:00
parent 2c74a94e31
commit 4c9aa96b17

View File

@@ -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