From aa5511decd0dd418fe48b2e412ea62aa221a0f82 Mon Sep 17 00:00:00 2001 From: Roman Blanco Date: Tue, 10 Apr 2018 15:56:56 +0200 Subject: [PATCH 1/2] Forcing default_value to an array, if the dynamic dropdown is multiselect Fixes: https://bugzilla.redhat.com/show_bug.cgi?id=1559030 --- app/models/dialog_field_radio_button.rb | 4 ++++ app/models/dialog_field_sorted_item.rb | 3 +++ 2 files changed, 7 insertions(+) diff --git a/app/models/dialog_field_radio_button.rb b/app/models/dialog_field_radio_button.rb index dbb05053e4d..bf38975c508 100644 --- a/app/models/dialog_field_radio_button.rb +++ b/app/models/dialog_field_radio_button.rb @@ -7,6 +7,10 @@ def initial_values [[nil, ""]] end + def force_multi_value + false + end + private def raw_values diff --git a/app/models/dialog_field_sorted_item.rb b/app/models/dialog_field_sorted_item.rb index 752e8ae5300..a2f4d1d0dd7 100644 --- a/app/models/dialog_field_sorted_item.rb +++ b/app/models/dialog_field_sorted_item.rb @@ -96,6 +96,9 @@ def sort_data(data_to_sort) end def determine_selected_default_value + if dynamic? && force_multi_value && !default_value.kind_of?(Array) + self.default_value = Array.wrap(default_value) + end use_first_value_as_default unless default_value_included?(@raw_values) self.value ||= default_value.nil? && data_type == "integer" ? nil : default_value.send(value_modifier) end From 2cde07d52fb01f0b7fd0188808cf137cf36c568f Mon Sep 17 00:00:00 2001 From: Roman Blanco Date: Sat, 14 Apr 2018 23:39:21 +0200 Subject: [PATCH 2/2] Adding specs for modified method Testing that default_value is wrapped into an array, if the dynamic dialog field is multiselect --- spec/models/dialog_field_sorted_item_spec.rb | 48 +++++++++++++++++++- 1 file changed, 46 insertions(+), 2 deletions(-) diff --git a/spec/models/dialog_field_sorted_item_spec.rb b/spec/models/dialog_field_sorted_item_spec.rb index 07889bc2e8d..b3d40561866 100644 --- a/spec/models/dialog_field_sorted_item_spec.rb +++ b/spec/models/dialog_field_sorted_item_spec.rb @@ -13,6 +13,9 @@ let(:default_value) { "test2" } context "when show_refresh_button is true" do + before(:each) do + allow(dialog_field).to receive(:force_multi_value).and_return(false) + end let(:show_refresh_button) { true } context "when load_values_on_init is true" do @@ -59,6 +62,9 @@ context "when show_refresh_button is false" do let(:show_refresh_button) { false } + before(:each) do + allow(dialog_field).to receive(:force_multi_value).and_return(false) + end context "when load_values_on_init is true" do let(:load_values_on_init) { true } @@ -148,13 +154,47 @@ allow(DynamicDialogFieldValueProcessor).to receive(:values_from_automate).with(dialog_field).and_return(values) end - context "when the default_value is included in the list of returned values" do + context "when the force_multi_value is set to false" do let(:default_value) { "abc" } + before do + allow(dialog_field).to receive(:force_multi_value).and_return(false) + end - it "sets the default value" do + it "sets the default_value" do dialog_field.values expect(dialog_field.default_value).to eq("abc") end + end + + context "when the force_multi_value is set to true" do + let(:default_value) { "abc" } + before do + allow(dialog_field).to receive(:force_multi_value).and_return(true) + # FIXME (rblanco): this is a workaround for the tested class being + # DialogFieldSortedItem and not DialogFieldDropDownList as it is + # in real usecase + # + # The test should be moved to dialog_field_drop_down_list_spec.rb, as + # https://github.com/ManageIQ/manageiq/pull/17272#discussion_r180468970 + # says. + allow(dialog_field).to receive(:default_value_included?).and_return(true) + end + + it "sets the default_value" do + dialog_field.values + # while reproducing the BZ, the method + # `determine_selected_default_value` is called twice, + # not just once - could be a difference here + expect(dialog_field.default_value).to eq("[\"abc\"]") + end + end + + context "when the default_value is included in the list of returned values" do + before do + allow(dialog_field).to receive(:force_multi_value).and_return(false) + end + + let(:default_value) { "abc" } it "sets the value to the default value" do dialog_field.values @@ -163,6 +203,10 @@ end context "when the default_value is not included in the list of returned values" do + before do + allow(dialog_field).to receive(:force_multi_value).and_return(false) + end + let(:default_value) { "123" } it "sets the default value to the first value" do