From d648ac31c2d25b7d18b2421c6e87583a263b908b Mon Sep 17 00:00:00 2001 From: sebastiencarceles Date: Mon, 23 Jan 2023 14:14:44 +0100 Subject: [PATCH] add prefill decorator for multiple drop down list --- ...l_multiple_drop_down_list_type_de_champ.rb | 8 +++++ .../types_de_champ/prefill_type_de_champ.rb | 2 ++ ...tiple_drop_down_list_type_de_champ_spec.rb | 32 +++++++++++++++++++ .../prefill_type_de_champ_spec.rb | 6 ++++ 4 files changed, 48 insertions(+) create mode 100644 app/models/types_de_champ/prefill_multiple_drop_down_list_type_de_champ.rb create mode 100644 spec/models/types_de_champ/prefill_multiple_drop_down_list_type_de_champ_spec.rb diff --git a/app/models/types_de_champ/prefill_multiple_drop_down_list_type_de_champ.rb b/app/models/types_de_champ/prefill_multiple_drop_down_list_type_de_champ.rb new file mode 100644 index 000000000..11992f53d --- /dev/null +++ b/app/models/types_de_champ/prefill_multiple_drop_down_list_type_de_champ.rb @@ -0,0 +1,8 @@ +class TypesDeChamp::PrefillMultipleDropDownListTypeDeChamp < TypesDeChamp::PrefillDropDownListTypeDeChamp + def example_value + return nil if possible_values.empty? + return possible_values.first if possible_values.one? + + [possible_values.first, possible_values.second] + end +end diff --git a/app/models/types_de_champ/prefill_type_de_champ.rb b/app/models/types_de_champ/prefill_type_de_champ.rb index a385ea653..1dc57e46f 100644 --- a/app/models/types_de_champ/prefill_type_de_champ.rb +++ b/app/models/types_de_champ/prefill_type_de_champ.rb @@ -5,6 +5,8 @@ class TypesDeChamp::PrefillTypeDeChamp < SimpleDelegator case type_de_champ.type_champ when TypeDeChamp.type_champs.fetch(:drop_down_list) TypesDeChamp::PrefillDropDownListTypeDeChamp.new(type_de_champ) + when TypeDeChamp.type_champs.fetch(:multiple_drop_down_list) + TypesDeChamp::PrefillMultipleDropDownListTypeDeChamp.new(type_de_champ) when TypeDeChamp.type_champs.fetch(:pays) TypesDeChamp::PrefillPaysTypeDeChamp.new(type_de_champ) when TypeDeChamp.type_champs.fetch(:regions) diff --git a/spec/models/types_de_champ/prefill_multiple_drop_down_list_type_de_champ_spec.rb b/spec/models/types_de_champ/prefill_multiple_drop_down_list_type_de_champ_spec.rb new file mode 100644 index 000000000..d44fd1274 --- /dev/null +++ b/spec/models/types_de_champ/prefill_multiple_drop_down_list_type_de_champ_spec.rb @@ -0,0 +1,32 @@ +# frozen_string_literal: true + +RSpec.describe TypesDeChamp::PrefillMultipleDropDownListTypeDeChamp do + describe 'ancestors' do + subject { described_class.new(build(:type_de_champ_multiple_drop_down_list)) } + + it { is_expected.to be_kind_of(TypesDeChamp::PrefillDropDownListTypeDeChamp) } + end + + describe '#example_value' do + let(:type_de_champ) { build(:type_de_champ_multiple_drop_down_list, drop_down_list_value: drop_down_list_value) } + subject(:example_value) { described_class.new(type_de_champ).example_value } + + context 'when the multiple drop down list has no option' do + let(:drop_down_list_value) { "" } + + it { expect(example_value).to eq(nil) } + end + + context 'when the multiple drop down list only has one option' do + let(:drop_down_list_value) { "value" } + + it { expect(example_value).to eq("value") } + end + + context 'when the multiple drop down list has two options or more' do + let(:drop_down_list_value) { "value1\r\nvalue2\r\nvalue3" } + + it { expect(example_value).to eq(["value1", "value2"]) } + end + end +end diff --git a/spec/models/types_de_champ/prefill_type_de_champ_spec.rb b/spec/models/types_de_champ/prefill_type_de_champ_spec.rb index 7477b3789..3f892f907 100644 --- a/spec/models/types_de_champ/prefill_type_de_champ_spec.rb +++ b/spec/models/types_de_champ/prefill_type_de_champ_spec.rb @@ -10,6 +10,12 @@ RSpec.describe TypesDeChamp::PrefillTypeDeChamp, type: :model do it { expect(built).to be_kind_of(TypesDeChamp::PrefillDropDownListTypeDeChamp) } end + context 'when the type de champ is a multiple_drop_down_list' do + let(:type_de_champ) { build(:type_de_champ_multiple_drop_down_list) } + + it { expect(built).to be_kind_of(TypesDeChamp::PrefillMultipleDropDownListTypeDeChamp) } + end + context 'when the type de champ is a pays' do let(:type_de_champ) { build(:type_de_champ_pays) }