From 80b3f0c0b3f377731f61d4dca3772f41f9288b9c Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Fri, 5 Apr 2024 10:34:01 +0200 Subject: [PATCH] fix(champ): no validate on hidden champs --- app/models/champ.rb | 2 ++ spec/models/champs/checkbox_champ_spec.rb | 4 +-- spec/models/champs/cnaf_champ_spec.rb | 2 +- spec/models/champs/dgfip_champ_spec.rb | 2 +- spec/models/champs/yes_no_champ_spec.rb | 2 +- .../concern/champ_conditional_concern_spec.rb | 35 +++++++++++++++---- 6 files changed, 36 insertions(+), 11 deletions(-) diff --git a/app/models/champ.rb b/app/models/champ.rb index dee09b37d..e11dfeb20 100644 --- a/app/models/champ.rb +++ b/app/models/champ.rb @@ -259,6 +259,8 @@ class Champ < ApplicationRecord private def validate_champ_value? + return false unless visible? + case validation_context when :champs_public_value public? diff --git a/spec/models/champs/checkbox_champ_spec.rb b/spec/models/champs/checkbox_champ_spec.rb index 0ddf973d5..04879247f 100644 --- a/spec/models/champs/checkbox_champ_spec.rb +++ b/spec/models/champs/checkbox_champ_spec.rb @@ -1,11 +1,11 @@ describe Champs::CheckboxChamp do it_behaves_like "a boolean champ" do - let(:boolean_champ) { Champs::CheckboxChamp.new(value: value) } + let(:boolean_champ) { build(:champ_checkbox, value: value) } end # TODO remove when normalize_checkbox_values is over describe '#true?' do - let(:checkbox_champ) { Champs::CheckboxChamp.new(value: value) } + let(:checkbox_champ) { build(:champ_checkbox, value: value) } subject { checkbox_champ.true? } context "when the checkbox value is 'on'" do diff --git a/spec/models/champs/cnaf_champ_spec.rb b/spec/models/champs/cnaf_champ_spec.rb index 3182ee1f8..092b90ed5 100644 --- a/spec/models/champs/cnaf_champ_spec.rb +++ b/spec/models/champs/cnaf_champ_spec.rb @@ -1,5 +1,5 @@ describe Champs::CnafChamp, type: :model do - let(:champ) { described_class.new } + let(:champ) { build(:champ_cnaf) } describe 'numero_allocataire and code_postal' do before do diff --git a/spec/models/champs/dgfip_champ_spec.rb b/spec/models/champs/dgfip_champ_spec.rb index 5563f9d62..31e8e7a7c 100644 --- a/spec/models/champs/dgfip_champ_spec.rb +++ b/spec/models/champs/dgfip_champ_spec.rb @@ -1,5 +1,5 @@ describe Champs::DgfipChamp, type: :model do - let(:champ) { described_class.new } + let(:champ) { build(:champ_dgfip) } describe 'numero_fiscal and reference_avis' do before do diff --git a/spec/models/champs/yes_no_champ_spec.rb b/spec/models/champs/yes_no_champ_spec.rb index 9304db864..6017b85c4 100644 --- a/spec/models/champs/yes_no_champ_spec.rb +++ b/spec/models/champs/yes_no_champ_spec.rb @@ -1,5 +1,5 @@ describe Champs::YesNoChamp do it_behaves_like "a boolean champ" do - let(:boolean_champ) { Champs::YesNoChamp.new(value: value) } + let(:boolean_champ) { build(:champ_yes_no, value: value) } end end diff --git a/spec/models/concern/champ_conditional_concern_spec.rb b/spec/models/concern/champ_conditional_concern_spec.rb index d8e5d9a08..b6c94b1a1 100644 --- a/spec/models/concern/champ_conditional_concern_spec.rb +++ b/spec/models/concern/champ_conditional_concern_spec.rb @@ -1,10 +1,12 @@ describe ChampConditionalConcern do include Logic - let(:procedure) { create(:procedure, types_de_champ_public: [{ type: :number }, { type: :number }]) } + let(:procedure) { create(:procedure, types_de_champ_public: [{ type: :decimal_number, stable_id: 99 }, { type: :decimal_number, condition: }]) } let(:dossier) { create(:dossier, revision: procedure.active_revision) } let(:types_de_champ) { procedure.active_revision.types_de_champ_public } - let(:champ) { create(:champ, dossier:, type_de_champ: types_de_champ.first, value: 1) } + let(:champ) { create(:champ_decimal_number, dossier:, type_de_champ: types_de_champ.first, value: '1.1234') } + let(:last_champ) { create(:champ_decimal_number, dossier:, type_de_champ: types_de_champ.last, value: '1.1234') } + let(:condition) { nil } describe '#dependent_conditions?' do context "when there are no condition" do @@ -12,12 +14,33 @@ describe ChampConditionalConcern do end context "when other tdc has a condition" do - before do - condition = ds_eq(champ_value(champ.stable_id), constant(1)) - types_de_champ.last.update!(condition:) - end + let(:condition) { ds_eq(champ_value(99), constant(1)) } it { expect(champ.dependent_conditions?).to eq(true) } end end + + describe '#visible?' do + context "when there are no condition" do + it { + expect(champ.visible?).to eq(true) + expect(champ.valid?(:champs_public_value)).to eq(false) + + expect(last_champ.visible?).to eq(true) + expect(last_champ.valid?(:champs_public_value)).to eq(false) + } + end + + context "when other tdc has a condition" do + let(:condition) { ds_eq(champ_value(99), constant(1)) } + + it { + expect(champ.visible?).to eq(true) + expect(champ.valid?(:champs_public_value)).to eq(false) + + expect(last_champ.visible?).to eq(false) + expect(last_champ.valid?(:champs_public_value)).to eq(true) + } + end + end end