From a88390c6d3b2953e7061249c7dca03e231bc7b8f Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Sat, 23 Jul 2022 12:32:28 +0200 Subject: [PATCH] refactor(types_de_champ): use new types_de_champ factory --- .../concern/tags_substitution_concern_spec.rb | 76 +++++++++---------- spec/models/procedure_revision_spec.rb | 30 ++++---- 2 files changed, 54 insertions(+), 52 deletions(-) diff --git a/spec/models/concern/tags_substitution_concern_spec.rb b/spec/models/concern/tags_substitution_concern_spec.rb index 85f295239..b098fbaa7 100644 --- a/spec/models/concern/tags_substitution_concern_spec.rb +++ b/spec/models/concern/tags_substitution_concern_spec.rb @@ -1,5 +1,5 @@ describe TagsSubstitutionConcern, type: :model do - let(:types_de_champ) { [] } + let(:types_de_champ_public) { [] } let(:types_de_champ_private) { [] } let(:for_individual) { false } let(:state) { Dossier.states.fetch(:accepte) } @@ -10,7 +10,7 @@ describe TagsSubstitutionConcern, type: :model do create(:procedure, :published, libelle: 'Une magnifique démarche', - types_de_champ: types_de_champ, + types_de_champ_public: types_de_champ_public, types_de_champ_private: types_de_champ_private, for_individual: for_individual, service: service, @@ -96,10 +96,10 @@ describe TagsSubstitutionConcern, type: :model do end context 'when the procedure has a type de champ named libelleA et libelleB' do - let(:types_de_champ) do + let(:types_de_champ_public) do [ - build(:type_de_champ, libelle: 'libelleA'), - build(:type_de_champ, libelle: 'libelleB') + { libelle: 'libelleA' }, + { libelle: 'libelleB' } ] end @@ -138,9 +138,9 @@ describe TagsSubstitutionConcern, type: :model do end context 'when the procedure has a type de champ with apostrophes' do - let(:types_de_champ) do + let(:types_de_champ_public) do [ - build(:type_de_champ, libelle: "Intitulé de l'‘«\"évènement\"»’") + { libelle: "Intitulé de l'‘«\"évènement\"»’" } ] end @@ -191,10 +191,8 @@ describe TagsSubstitutionConcern, type: :model do end context 'when the procedure has a linked drop down menus type de champ' do - let(:type_de_champ) do - build(:type_de_champ_linked_drop_down_list, libelle: 'libelle') - end - let(:types_de_champ) { [type_de_champ] } + let(:type_de_champ) { procedure.draft_revision.types_de_champ.first } + let(:types_de_champ_public) { [{ type: :linked_drop_down_list, libelle: 'libelle' }] } let(:template) { 'tout : --libelle--, primaire : --libelle/primaire--, secondaire : --libelle/secondaire--' } context 'and the champ has no value' do @@ -218,10 +216,10 @@ describe TagsSubstitutionConcern, type: :model do it { is_expected.to eq('tout : primo / secundo, primaire : primo, secondaire : secundo') } context 'and the same libelle is used by a header' do - let(:types_de_champ) do + let(:types_de_champ_public) do [ - type_de_champ, - build(:type_de_champ_header_section, libelle: 'libelle') + { type: :linked_drop_down_list, libelle: 'libelle' }, + { type: :header_section, libelle: 'libelle' } ] end @@ -255,7 +253,7 @@ describe TagsSubstitutionConcern, type: :model do end context 'when the procedure has a type de champ prive named libelleA' do - let(:types_de_champ_private) { [build(:type_de_champ, :private, libelle: 'libelleA')] } + let(:types_de_champ_private) { [{ libelle: 'libelleA' }] } context 'and it is used in the template' do let(:template) { '--libelleA--' } @@ -276,13 +274,13 @@ describe TagsSubstitutionConcern, type: :model do # The dossier just transitionned from brouillon to en construction, # so champs private are not valid tags yet - let(:types_de_champ_private) { [build(:type_de_champ, :private, libelle: 'libelleA')] } + let(:types_de_champ_private) { [{ libelle: 'libelleA' }] } it { is_expected.to eq('--libelleA--') } end context 'champs publics are valid tags' do - let(:types_de_champ) { [build(:type_de_champ, libelle: 'libelleA')] } + let(:types_de_champ_public) { [{ libelle: 'libelleA' }] } before { dossier.champs.first.update(value: 'libelle1') } @@ -291,10 +289,10 @@ describe TagsSubstitutionConcern, type: :model do end context 'when the procedure has 2 types de champ date and datetime' do - let(:types_de_champ) do + let(:types_de_champ_public) do [ - build(:type_de_champ_date, libelle: TypeDeChamp.type_champs.fetch(:date)), - build(:type_de_champ_datetime, libelle: TypeDeChamp.type_champs.fetch(:datetime)) + { type: :date, libelle: TypeDeChamp.type_champs.fetch(:date) }, + { type: :datetime, libelle: TypeDeChamp.type_champs.fetch(:datetime) } ] end @@ -358,13 +356,13 @@ describe TagsSubstitutionConcern, type: :model do shared_examples "treat all kinds of space as equivalent" do context 'and the champ has a non breaking space' do - let(:types_de_champ) { [build(:type_de_champ, libelle: 'mon tag')] } + let(:types_de_champ_public) { [{ libelle: 'mon tag' }] } it { is_expected.to eq('valeur') } end context 'and the champ has an ordinary space' do - let(:types_de_champ) { [build(:type_de_champ, libelle: 'mon tag')] } + let(:types_de_champ_public) { [{ libelle: 'mon tag' }] } it { is_expected.to eq('valeur') } end @@ -396,8 +394,8 @@ describe TagsSubstitutionConcern, type: :model do end context 'when procedure has revisions' do - let(:types_de_champ) { [build(:type_de_champ, libelle: 'mon ancien libellé')] } - let(:draft_type_de_champ) { procedure.draft_revision.find_and_ensure_exclusive_use(types_de_champ[0].stable_id) } + let(:types_de_champ_public) { [{ libelle: 'mon ancien libellé' }] } + let(:draft_type_de_champ) { procedure.draft_revision.find_and_ensure_exclusive_use(procedure.draft_revision.types_de_champ.first.stable_id) } before do draft_type_de_champ.update(libelle: 'mon nouveau libellé') @@ -426,14 +424,14 @@ describe TagsSubstitutionConcern, type: :model do describe 'tags' do subject { template_concern.tags } - let(:types_de_champ) do + let(:types_de_champ_public) do [ - build(:type_de_champ, libelle: 'public'), - build(:type_de_champ_header_section, libelle: 'entête de section'), - build(:type_de_champ_explication, libelle: 'explication') + { libelle: 'public' }, + { type: :header_section, libelle: 'entête de section' }, + { type: :explication, libelle: 'explication' } ] end - let(:types_de_champ_private) { [build(:type_de_champ, :private, libelle: 'privé')] } + let(:types_de_champ_private) { [{ libelle: 'privé' }] } context 'do not generate tags for champs that cannot have usager content' do it { is_expected.not_to include(include({ libelle: 'entête de section' })) } @@ -472,29 +470,29 @@ describe TagsSubstitutionConcern, type: :model do let(:text) { 'hello world --public--, --numéro du dossier--, --yolo--' } subject { template_concern.used_tags_for(text) } - let(:types_de_champ) do + let(:types_de_champ_public) do [ - build(:type_de_champ, libelle: 'public'), - build(:type_de_champ_header_section, libelle: 'entête de section'), - build(:type_de_champ_explication, libelle: 'explication') + { libelle: 'public' }, + { type: :header_section, libelle: 'entête de section' }, + { type: :explication, libelle: 'explication' } ] end - it { is_expected.to eq(["tdc#{types_de_champ.first.stable_id}", 'numéro du dossier', 'yolo']) } + it { is_expected.to eq(["tdc#{procedure.draft_revision.types_de_champ.first.stable_id}", 'numéro du dossier', 'yolo']) } end describe 'used_type_de_champ_tags' do let(:text) { 'hello world --public--, --numéro du dossier--, --yolo--' } subject { template_concern.used_type_de_champ_tags(text) } - let(:types_de_champ) do + let(:types_de_champ_public) do [ - build(:type_de_champ, libelle: 'public'), - build(:type_de_champ_header_section, libelle: 'entête de section'), - build(:type_de_champ_explication, libelle: 'explication') + { libelle: 'public' }, + { type: :header_section, libelle: 'entête de section' }, + { type: :explication, libelle: 'explication' } ] end - it { is_expected.to eq([["public", types_de_champ.first.stable_id], ['yolo']]) } + it { is_expected.to eq([["public", procedure.draft_revision.types_de_champ.first.stable_id], ['yolo']]) } end end diff --git a/spec/models/procedure_revision_spec.rb b/spec/models/procedure_revision_spec.rb index 7b7a48c94..26bca33be 100644 --- a/spec/models/procedure_revision_spec.rb +++ b/spec/models/procedure_revision_spec.rb @@ -657,14 +657,14 @@ describe ProcedureRevision do describe '#estimated_fill_duration' do let(:mandatory) { true } - let(:types_de_champ) do + let(:types_de_champ_public) do [ - build(:type_de_champ_text, position: 1, mandatory: true), - build(:type_de_champ_siret, position: 2, mandatory: true), - build(:type_de_champ_piece_justificative, position: 3, mandatory: mandatory) + { mandatory: true }, + { type: :siret, mandatory: true }, + { type: :piece_justificative, mandatory: mandatory } ] end - let(:procedure) { create(:procedure, types_de_champ: types_de_champ) } + let(:procedure) { create(:procedure, types_de_champ_public: types_de_champ_public) } subject { procedure.active_revision.estimated_fill_duration } @@ -687,13 +687,17 @@ describe ProcedureRevision do end context 'when there are repetitions' do - let(:procedure) do - procedure = create(:procedure, types_de_champ: []) - create(:type_de_champ_repetition, position: 1, mandatory: true, procedure: procedure, types_de_champ: [ - build(:type_de_champ_text, position: 1, mandatory: true), - build(:type_de_champ_piece_justificative, position: 2, mandatory: true) - ]) - procedure + let(:types_de_champ_public) do + [ + { + type: :repetition, + mandatory: true, + children: [ + { mandatory: true }, + { type: :piece_justificative, position: 2, mandatory: true } + ] + } + ] end it 'estimates that between 2 and 3 rows will be filled for each repetition' do @@ -703,7 +707,7 @@ describe ProcedureRevision do end describe 'caching behavior' do - let(:procedure) { create(:procedure, :published, types_de_champ: types_de_champ) } + let(:procedure) { create(:procedure, :published, types_de_champ_public: types_de_champ_public) } before { Rails.cache = ActiveSupport::Cache::MemoryStore.new } after { Rails.cache = ActiveSupport::Cache::NullStore.new }