diff --git a/app/graphql/types/dossier_type.rb b/app/graphql/types/dossier_type.rb index 6b20aac3d..76e65f8e0 100644 --- a/app/graphql/types/dossier_type.rb +++ b/app/graphql/types/dossier_type.rb @@ -121,9 +121,12 @@ module Types .for(object, private: false) .load(ApplicationRecord.id_from_typed_id(id)) elsif object.champs.loaded? - object.champs + object.champs.filter(&:visible?) else - Loaders::Association.for(object.class, champs: :type_de_champ).load(object) + Loaders::Association + .for(object.class, champs: :type_de_champ) + .load(object) + .then { |champs| champs.filter(&:visible?) } end end diff --git a/spec/graphql/dossier_spec.rb b/spec/graphql/dossier_spec.rb index 2190d51ac..877c21798 100644 --- a/spec/graphql/dossier_spec.rb +++ b/spec/graphql/dossier_spec.rb @@ -56,6 +56,33 @@ RSpec.describe Types::DossierType, type: :graphql do it { expect(data[:dossier][:champs][1][:__typename]).to eq "AddressChamp" } end + describe 'dossier with conditional champs' do + include Logic + let(:stable_id) { 1234 } + let(:condition) { ds_eq(champ_value(stable_id), constant(true)) } + let(:procedure) { create(:procedure, :published, types_de_champ_public: [{ type: :checkbox, stable_id: stable_id }, { type: :text, condition: condition }]) } + let(:dossier) { create(:dossier, :accepte, :with_populated_champs, procedure: procedure) } + let(:query) { DOSSIER_WITH_CHAMPS_QUERY } + let(:variables) { { number: dossier.id } } + let(:checkbox_value) { 'on' } + + before do + dossier.champs.first.update(value: checkbox_value) + end + + context 'when checkbox is true' do + it { expect(data[:dossier][:champs].size).to eq 2 } + it { expect(data[:dossier][:champs][0][:__typename]).to eq "CheckboxChamp" } + it { expect(data[:dossier][:champs][1][:__typename]).to eq "TextChamp" } + end + + context 'when checkbox is false' do + let(:checkbox_value) { 'off' } + it { expect(data[:dossier][:champs].size).to eq 1 } + it { expect(data[:dossier][:champs][0][:__typename]).to eq "CheckboxChamp" } + end + end + describe 'dossier with user' do let(:dossier) { create(:dossier, :en_construction) } let(:query) { DOSSIER_WITH_USAGER_QUERY }