fix(conditional): mandatory champs inside hidden repetition should not be validated

This commit is contained in:
Paul Chavard 2022-08-04 14:34:13 +02:00
parent 8e8d8d4f55
commit bbca7344e5
2 changed files with 44 additions and 27 deletions

View file

@ -978,7 +978,7 @@ class Dossier < ApplicationRecord
end
def check_mandatory_champs
(champs + champs.filter(&:repetition?).flat_map(&:champs))
(champs + champs.filter(&:repetition?).filter(&:visible?).flat_map(&:champs))
.filter(&:mandatory_blank_and_visible?)
.map do |champ|
"Le champ #{champ.libelle.truncate(200)} doit être rempli."

View file

@ -1139,15 +1139,20 @@ describe Dossier do
end
describe "#check_mandatory_champs" do
let(:procedure) { create(:procedure, :with_type_de_champ) }
include Logic
let(:procedure) { create(:procedure, types_de_champ_public: types_de_champ) }
let(:dossier) { create(:dossier, procedure: procedure) }
let(:types_de_champ) { [type_de_champ] }
let(:type_de_champ) { {} }
let(:errors) { dossier.check_mandatory_champs }
it 'no mandatory champs' do
expect(dossier.check_mandatory_champs).to be_empty
expect(errors).to be_empty
end
context "with mandatory champs" do
let(:procedure) { create(:procedure, :with_type_de_champ_mandatory) }
let(:type_de_champ) { { mandatory: true } }
let(:champ_with_error) { dossier.champs.first }
before do
@ -1156,22 +1161,29 @@ describe Dossier do
end
it 'should have errors' do
errors = dossier.check_mandatory_champs
expect(errors).not_to be_empty
expect(errors.first).to eq("Le champ #{champ_with_error.libelle} doit être rempli.")
end
context "conditionaly visible" do
let(:types_de_champ) { [{ type: :yes_no, stable_id: 99 }, type_de_champ] }
let(:type_de_champ) { { mandatory: true, condition: ds_eq(champ_value(99), constant(true)) } }
it 'should not have errors' do
expect(errors).to be_empty
end
end
end
context "with mandatory SIRET champ" do
let(:type_de_champ) { create(:type_de_champ_siret, mandatory: true, procedure: procedure) }
let(:champ_siret) { create(:champ_siret, type_de_champ: type_de_champ) }
let(:type_de_champ) { { type: :siret, mandatory: true } }
let(:champ_siret) { dossier.champs.first }
before do
dossier.champs << champ_siret
champ_siret.value = '44011762001530'
end
it 'should not have errors' do
errors = dossier.check_mandatory_champs
expect(errors).to be_empty
end
@ -1182,7 +1194,6 @@ describe Dossier do
end
it 'should have errors' do
errors = dossier.check_mandatory_champs
expect(errors).not_to be_empty
expect(errors.first).to eq("Le champ #{champ_siret.libelle} doit être rempli.")
end
@ -1190,24 +1201,16 @@ describe Dossier do
end
context "with champ repetition" do
let(:procedure) { create(:procedure, :with_repetition) }
let(:type_de_champ) { { type: :repetition, mandatory: true, children: [{ mandatory: true }] } }
let(:revision) { procedure.active_revision }
let(:type_de_champ_repetition) { revision.types_de_champ.first }
before do
type_de_champ_repetition.update(mandatory: true)
revision.children_of(type_de_champ_repetition).first.update(mandatory: true)
end
context "when no champs" do
let(:champ_with_error) do
repetition_champ = dossier.champs.first
text_champ = repetition_champ.rows.first.first
text_champ
end
let(:champ_with_error) { dossier.champs.first }
it 'should have errors' do
errors = dossier.check_mandatory_champs
dossier.champs.first.champs.destroy_all
expect(dossier.champs.first.rows).to be_empty
expect(errors).not_to be_empty
expect(errors.first).to eq("Le champ #{champ_with_error.libelle} doit être rempli.")
end
@ -1216,15 +1219,29 @@ describe Dossier do
context "when mandatory champ inside repetition" do
let(:champ_with_error) { dossier.champs.first.champs.first }
before do
dossier.champs.first.add_row(dossier.revision)
end
it 'should have errors' do
errors = dossier.check_mandatory_champs
expect(dossier.champs.first.rows).not_to be_empty
expect(errors).not_to be_empty
expect(errors.first).to eq("Le champ #{champ_with_error.libelle} doit être rempli.")
end
context "conditionaly visible" do
let(:champ_with_error) { dossier.champs.second.champs.first }
let(:types_de_champ) { [{ type: :yes_no, stable_id: 99 }, type_de_champ] }
let(:type_de_champ) { { type: :repetition, mandatory: true, children: [{ mandatory: true }], condition: ds_eq(champ_value(99), constant(true)) } }
it 'should not have errors' do
expect(dossier.champs.second.rows).not_to be_empty
expect(errors).to be_empty
end
it 'should have errors' do
dossier.champs.first.update(value: 'true')
expect(dossier.champs.second.rows).not_to be_empty
expect(errors).not_to be_empty
expect(errors.first).to eq("Le champ #{champ_with_error.libelle} doit être rempli.")
end
end
end
end
end