From 0ee236b9b4c5a44a6d3c0049155576be0364a69a Mon Sep 17 00:00:00 2001 From: Kanezoh Date: Thu, 1 Jul 2021 20:05:15 +0900 Subject: [PATCH] add select box test --- spec/form/select_list_spec.cr | 45 +++++++++++++++++++++++++ src/mechanize/form/multi_select_list.cr | 12 +++---- src/mechanize/form/option.cr | 2 +- 3 files changed, 52 insertions(+), 7 deletions(-) create mode 100644 spec/form/select_list_spec.cr diff --git a/spec/form/select_list_spec.cr b/spec/form/select_list_spec.cr new file mode 100644 index 0000000..2c63995 --- /dev/null +++ b/spec/form/select_list_spec.cr @@ -0,0 +1,45 @@ +require "../spec_helper" + +WebMock.stub(:get, "example.com/form/select_list").to_return(body: +<<-BODY + + + page_title + + +
+ +
+ + +BODY +) + +describe "Form Fields Multiple Select List" do + agent = Mechanize.new + page = agent.get("http://example.com/form/select_list") + form = page.forms[0] + + it "returns selectboxes size" do + form.selectboxes.size.should eq 1 + end + + selectbox = form.selectboxes[0] + + it "returns selectbox options size" do + selectbox.options.size.should eq 3 + end + + it "returns selected values" do + selectbox.values.empty?.should eq true + selectbox.select_all + selectbox.values.size.should eq 3 + selectbox.values.should eq ["dog", "cat", "hamster"] + selectbox.select_none + selectbox.values.empty?.should eq true + end +end diff --git a/src/mechanize/form/multi_select_list.cr b/src/mechanize/form/multi_select_list.cr index 746a536..4f4cf33 100644 --- a/src/mechanize/form/multi_select_list.cr +++ b/src/mechanize/form/multi_select_list.cr @@ -19,17 +19,17 @@ class MechanizeCr::FormContent::MultiSelectList end def select_none - @value = Array(String).new - options.each &.unclick + @values = Array(String).new + options.each &.unselect end def select_all - @value = Array(String).new - options.each &.click + @values = Array(String).new + options.each &.select end def selected_options - options.find_all(&:selected?) + options.select &.selected? end def values=(raw_values) @@ -45,7 +45,7 @@ class MechanizeCr::FormContent::MultiSelectList end def values - @values + selected_options.map(&:value) + @values + selected_options.map &.value end #def inspect # :nodoc: diff --git a/src/mechanize/form/option.cr b/src/mechanize/form/option.cr index 7f423c5..149e5c6 100644 --- a/src/mechanize/form/option.cr +++ b/src/mechanize/form/option.cr @@ -34,7 +34,7 @@ class MechanizeCr::FormContent::Option end private def unselect_peers - return unless SelectList === @select_list + return if MultiSelectList === @select_list @select_list.select_none end