TypeDeChamp: amélioration de la gestion des drop_down_options

This commit is contained in:
simon lehericey 2024-10-07 14:35:23 +02:00 committed by benoitqueyron
parent e374143422
commit b588b77571
No known key found for this signature in database
GPG key ID: AD3C38C9ACA84135
9 changed files with 51 additions and 33 deletions

View file

@ -211,7 +211,7 @@ class TypeDeChamp < ApplicationRecord
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 @@ class TypeDeChamp < ApplicationRecord
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

View file

@ -15,14 +15,15 @@ describe Procedure::Card::AnnotationsComponent, type: :component do
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')

View file

@ -15,14 +15,14 @@ describe Procedure::Card::ChampsComponent, type: :component do
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')

View file

@ -5,8 +5,8 @@ describe Procedure::ErrorsSummary, type: :component do
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 @@ describe Procedure::ErrorsSummary, type: :component do
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 @@ describe Procedure::ErrorsSummary, type: :component do
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 @@ describe Procedure::ErrorsSummary, type: :component do
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 @@ describe Procedure::ErrorsSummary, type: :component do
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")

View file

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

View file

@ -393,10 +393,12 @@ describe Procedure do
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 @@ describe Procedure do
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

View file

@ -107,19 +107,22 @@ describe TypeDeChamp do
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

View file

@ -16,7 +16,7 @@ RSpec.describe TypesDeChamp::PrefillMultipleDropDownListTypeDeChamp do
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

View file

@ -66,8 +66,15 @@ describe 'Publishing a procedure', js: true do
: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