Skip to content

Commit

Permalink
TypeDeChamp: amélioration de la gestion des drop_down_options
Browse files Browse the repository at this point in the history
  • Loading branch information
LeSim authored and Benoit-MINT committed Oct 15, 2024
1 parent e374143 commit b588b77
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 33 deletions.
16 changes: 6 additions & 10 deletions app/models/type_de_champ.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
5 changes: 3 additions & 2 deletions spec/components/procedures/card/annotations_component_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
4 changes: 2 additions & 2 deletions spec/components/procedures/card/champs_component_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
16 changes: 10 additions & 6 deletions spec/components/procedures/errors_summary_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 }

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

Expand All @@ -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
Expand All @@ -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
Expand All @@ -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")
Expand Down
12 changes: 7 additions & 5 deletions spec/components/types_de_champ_editor/editor_component_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 9 additions & 4 deletions spec/models/procedure_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
5 changes: 4 additions & 1 deletion spec/models/type_de_champ_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 9 additions & 2 deletions spec/system/administrateurs/procedure_publish_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit b588b77

Please sign in to comment.