From b588b775718c1531b55ea4877cdf243054a4ba6a Mon Sep 17 00:00:00 2001 From: simon lehericey Date: Mon, 7 Oct 2024 14:35:23 +0200 Subject: [PATCH] =?UTF-8?q?TypeDeChamp:=20am=C3=A9lioration=20de=20la=20ge?= =?UTF-8?q?stion=20des=20drop=5Fdown=5Foptions?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/models/type_de_champ.rb | 16 ++++++---------- .../card/annotations_component_spec.rb | 5 +++-- .../procedures/card/champs_component_spec.rb | 4 ++-- .../components/procedures/errors_summary_spec.rb | 16 ++++++++++------ .../editor_component_spec.rb | 12 +++++++----- spec/models/procedure_spec.rb | 13 +++++++++---- spec/models/type_de_champ_spec.rb | 5 ++++- ...multiple_drop_down_list_type_de_champ_spec.rb | 2 +- .../administrateurs/procedure_publish_spec.rb | 11 +++++++++-- 9 files changed, 51 insertions(+), 33 deletions(-) diff --git a/app/models/type_de_champ.rb b/app/models/type_de_champ.rb index 015721c60ee..6f28c36a15f 100644 --- a/app/models/type_de_champ.rb +++ b/app/models/type_de_champ.rb @@ -211,7 +211,7 @@ def self.dump(options) before_validation :normalize_libelle before_save :remove_piece_justificative_template, if: -> { type_champ_changed? } - before_validation :remove_drop_down_list, if: -> { type_champ_changed? } + before_validation :set_drop_down_list_options, if: -> { type_champ_changed? } before_save :remove_block, if: -> { type_champ_changed? } after_save if: -> { @remove_piece_justificative_template } do @@ -773,15 +773,11 @@ def remove_piece_justificative_template end end - def remove_drop_down_list - if !drop_down_list? - self.drop_down_options = nil - elsif !drop_down_options_changed? - self.drop_down_options = if linked_drop_down_list? - ['--Fromage--', 'bleu de sassenage', 'picodon', '--Dessert--', 'éclair', 'tarte aux pommes'] - else - ['Premier choix', 'Deuxième choix'] - end + def set_drop_down_list_options + if (simple_drop_down_list? || multiple_drop_down_list?) && drop_down_options.empty? + self.drop_down_options = ['Fromage', 'Dessert'] + elsif linked_drop_down_list? && drop_down_options.none?(/^--.*--$/) + self.drop_down_options = ['--Fromage--', 'bleu de sassenage', 'picodon', '--Dessert--', 'éclair', 'tarte aux pommes'] end end diff --git a/spec/components/procedures/card/annotations_component_spec.rb b/spec/components/procedures/card/annotations_component_spec.rb index 26897a85bdd..772e80e9573 100644 --- a/spec/components/procedures/card/annotations_component_spec.rb +++ b/spec/components/procedures/card/annotations_component_spec.rb @@ -15,14 +15,15 @@ end context 'when errors on types_de_champs_public' do - let(:types_de_champ_public) { [{ type: :drop_down_list, options: [] }] } + let(:types_de_champ_public) { [{ type: :repetition, children: [] }] } + it 'does not render' do expect(subject).to have_selector('.fr-badge--info', text: 'À configurer') end end context 'when errors on types_de_champs_private' do - let(:types_de_champ_private) { [{ type: :drop_down_list, options: [] }] } + let(:types_de_champ_private) { [{ type: :repetition, children: [] }] } it 'render the template' do expect(subject).to have_selector('.fr-badge--error', text: 'À modifier') diff --git a/spec/components/procedures/card/champs_component_spec.rb b/spec/components/procedures/card/champs_component_spec.rb index 40412d05552..f986f3d0b87 100644 --- a/spec/components/procedures/card/champs_component_spec.rb +++ b/spec/components/procedures/card/champs_component_spec.rb @@ -15,14 +15,14 @@ end context 'when errors on types_de_champs_public' do - let(:types_de_champ_public) { [{ type: :drop_down_list, options: [] }] } + let(:types_de_champ_public) { [{ type: :repetition, children: [] }] } it 'does not render' do expect(subject).to have_selector('.fr-badge--error', text: 'À modifier') end end context 'when errors on types_de_champs_private' do - let(:types_de_champ_private) { [{ type: :drop_down_list, options: [] }] } + let(:types_de_champ_private) { [{ type: :repetition, children: [] }] } it 'render the template' do expect(subject).to have_selector('.fr-badge--warning', text: 'À faire') diff --git a/spec/components/procedures/errors_summary_spec.rb b/spec/components/procedures/errors_summary_spec.rb index 9ff3edadb9a..204bea9dffa 100644 --- a/spec/components/procedures/errors_summary_spec.rb +++ b/spec/components/procedures/errors_summary_spec.rb @@ -5,8 +5,8 @@ describe 'validations context' do let(:procedure) { create(:procedure, types_de_champ_private:, types_de_champ_public:) } - let(:types_de_champ_private) { [{ type: :drop_down_list, options: [], libelle: 'private' }] } - let(:types_de_champ_public) { [{ type: :drop_down_list, options: [], libelle: 'public' }] } + let(:types_de_champ_private) { [{ type: :repetition, children: [], libelle: 'private' }] } + let(:types_de_champ_public) { [{ type: :repetition, children: [], libelle: 'public' }] } before { subject } @@ -17,7 +17,7 @@ expect(page).to have_content("Erreur : Des problèmes empêchent la publication de la démarche") expect(page).to have_selector("a", text: "public") expect(page).to have_selector("a", text: "private") - expect(page).to have_text("doit comporter au moins un choix sélectionnable", count: 2) + expect(page).to have_text("doit comporter au moins un champ répétable", count: 2) end end @@ -27,7 +27,7 @@ it 'shows errors and links for public only tdc' do expect(page).to have_text("Erreur : Les champs formulaire contiennent des erreurs") expect(page).to have_selector("a", text: "public") - expect(page).to have_text("doit comporter au moins un choix sélectionnable", count: 1) + expect(page).to have_text("doit comporter au moins un champ répétable", count: 1) expect(page).not_to have_selector("a", text: "private") end end @@ -38,7 +38,7 @@ it 'shows errors and links for private only tdc' do expect(page).to have_text("Erreur : Les annotations privées contiennent des erreurs") expect(page).to have_selector("a", text: "private") - expect(page).to have_text("doit comporter au moins un choix sélectionnable") + expect(page).to have_text("doit comporter au moins un champ répétable") expect(page).not_to have_selector("a", text: "public") end end @@ -59,7 +59,11 @@ let(:validation_context) { :types_de_champ_public_editor } - before { subject } + before do + drop_down_public = procedure.draft_revision.types_de_champ_public.find(&:drop_down_list?) + drop_down_public.update!(drop_down_options: []) + subject + end it 'renders all errors and links on champ' do expect(page).to have_selector("a", text: "drop down list requires options") diff --git a/spec/components/types_de_champ_editor/editor_component_spec.rb b/spec/components/types_de_champ_editor/editor_component_spec.rb index ae69471765b..3e6bc205816 100644 --- a/spec/components/types_de_champ_editor/editor_component_spec.rb +++ b/spec/components/types_de_champ_editor/editor_component_spec.rb @@ -3,26 +3,28 @@ describe TypesDeChampEditor::EditorComponent, type: :component do let(:revision) { procedure.draft_revision } let(:procedure) { create(:procedure, id: 1, types_de_champ_private:, types_de_champ_public:) } - - let(:types_de_champ_private) { [{ type: :drop_down_list, options: [], libelle: 'private' }] } - let(:types_de_champ_public) { [{ type: :drop_down_list, options: [], libelle: 'public' }] } + let(:types_de_champ_private) { [{ type: :repetition, children: [], libelle: 'private' }] } + let(:types_de_champ_public) { [{ type: :repetition, children: [], libelle: 'public' }] } describe 'render' do subject { render_inline(described_class.new(revision:, is_annotation:)) } + context 'types_de_champ_public' do let(:is_annotation) { false } + it 'does not render private champs errors' do expect(subject).not_to have_text("private") expect(subject).to have_selector("a", text: "public") - expect(subject).to have_text("doit comporter au moins un choix sélectionnable") + expect(subject).to have_text("doit comporter au moins un champ répétable") end end context 'types_de_champ_private' do let(:is_annotation) { true } + it 'does not render public champs errors' do expect(subject).to have_selector("a", text: "private") - expect(subject).to have_text("doit comporter au moins un choix sélectionnable") + expect(subject).to have_text("doit comporter au moins un champ répétable") expect(subject).not_to have_text("public") end end diff --git a/spec/models/procedure_spec.rb b/spec/models/procedure_spec.rb index cda2f833054..68d6bddec0a 100644 --- a/spec/models/procedure_spec.rb +++ b/spec/models/procedure_spec.rb @@ -393,10 +393,12 @@ end it 'validates that no drop-down type de champ is empty' do - procedure.validate(:publication) + drop_down = procedure.draft_revision.types_de_champ_public.find(&:drop_down_list?) + + drop_down.update!(drop_down_options: []) + procedure.reload.validate(:publication) expect(procedure.errors.messages_for(:draft_types_de_champ_public)).to include(invalid_drop_down_error_message) - drop_down = procedure.draft_revision.types_de_champ_public.find(&:drop_down_list?) drop_down.update!(drop_down_options: ["--title--", "some value"]) procedure.reload.validate(:publication) expect(procedure.errors.messages_for(:draft_types_de_champ_public)).not_to include(invalid_drop_down_error_message) @@ -418,14 +420,17 @@ it 'validates that no repetition type de champ is empty' do procedure.validate(:publication) expect(procedure.errors.messages_for(:draft_types_de_champ_private)).to include(invalid_repetition_error_message) + repetition = procedure.draft_revision.types_de_champ_private.find(&:repetition?) expect(procedure.errors.to_enum.to_a.map { _1.options[:type_de_champ] }).to include(repetition) end it 'validates that no drop-down type de champ is empty' do - procedure.validate(:publication) - expect(procedure.errors.messages_for(:draft_types_de_champ_private)).to include(invalid_drop_down_error_message) drop_down = procedure.draft_revision.types_de_champ_private.find(&:drop_down_list?) + drop_down.update!(drop_down_options: []) + procedure.reload.validate(:publication) + + expect(procedure.errors.messages_for(:draft_types_de_champ_private)).to include(invalid_drop_down_error_message) expect(procedure.errors.to_enum.to_a.map { _1.options[:type_de_champ] }).to include(drop_down) end end diff --git a/spec/models/type_de_champ_spec.rb b/spec/models/type_de_champ_spec.rb index 94e5ad97474..5fb9d8f2a95 100644 --- a/spec/models/type_de_champ_spec.rb +++ b/spec/models/type_de_champ_spec.rb @@ -107,19 +107,22 @@ context 'when the target type_champ is not drop_down_list' do let(:target_type_champ) { TypeDeChamp.type_champs.fetch(:text) } - it { expect(tdc.drop_down_options).to be_empty } + it { expect(tdc.drop_down_options).to be_present } + it { expect(tdc.drop_down_options).to eq(["val1", "val2", "val3"]) } end context 'when the target type_champ is linked_drop_down_list' do let(:target_type_champ) { TypeDeChamp.type_champs.fetch(:linked_drop_down_list) } it { expect(tdc.drop_down_options).to be_present } + it { expect(tdc.drop_down_options).to eq(['--Fromage--', 'bleu de sassenage', 'picodon', '--Dessert--', 'éclair', 'tarte aux pommes']) } end context 'when the target type_champ is multiple_drop_down_list' do let(:target_type_champ) { TypeDeChamp.type_champs.fetch(:multiple_drop_down_list) } it { expect(tdc.drop_down_options).to be_present } + it { expect(tdc.drop_down_options).to eq(["val1", "val2", "val3"]) } end end 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 index a4d60e3f45f..416bdc87f95 100644 --- 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 @@ -16,7 +16,7 @@ context 'when the multiple drop down list has no option' do let(:drop_down_options_from_text) { "" } - it { expect(example_value).to eq(nil) } + it { expect(example_value).to eq(["Fromage", "Dessert"]) } end context 'when the multiple drop down list only has one option' do diff --git a/spec/system/administrateurs/procedure_publish_spec.rb b/spec/system/administrateurs/procedure_publish_spec.rb index ede1086fa56..aac700f8d27 100644 --- a/spec/system/administrateurs/procedure_publish_spec.rb +++ b/spec/system/administrateurs/procedure_publish_spec.rb @@ -66,8 +66,15 @@ :with_zone, instructeurs: instructeurs, administrateur: administrateur, - types_de_champ_public: [{ type: :repetition, libelle: 'Enfants', children: [] }, { type: :drop_down_list, libelle: 'Civilité', options: [] }], - types_de_champ_private: [{ type: :drop_down_list, libelle: 'Civilité', options: [] }]) + types_de_champ_public: [{ type: :repetition, libelle: 'Enfants', children: [] }, { type: :drop_down_list, libelle: 'Civilité' }], + types_de_champ_private: [{ type: :drop_down_list, libelle: 'Civilité' }]) + end + + before do + drop_down = procedure.draft_revision.types_de_champ_public.find(&:drop_down_list?) + drop_down.update!(drop_down_options: []) + drop_down = procedure.draft_revision.types_de_champ_private.find(&:drop_down_list?) + drop_down.update!(drop_down_options: []) end scenario 'an error message prevents the publication' do