From deb19177f72cc513b24018a2a89578ed2f84d988 Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Tue, 28 Nov 2023 16:33:43 +0000 Subject: [PATCH] refactor(test): update tests --- .../editable_champ/section_component_spec.rb | 5 +- spec/factories/dossier.rb | 16 +++- spec/factories/procedure.rb | 8 +- spec/models/champ_spec.rb | 5 - .../champs/header_section_champ_spec.rb | 38 -------- spec/models/concern/treeable_concern_spec.rb | 46 ++++----- spec/models/dossier_spec.rb | 15 +-- .../prefill_repetition_type_de_champ_spec.rb | 7 +- .../shared/dossiers/_champs.html.haml_spec.rb | 89 +++++++++++------- .../shared/dossiers/_edit.html.haml_spec.rb | 93 +++++++++---------- 10 files changed, 160 insertions(+), 162 deletions(-) delete mode 100644 spec/models/champs/header_section_champ_spec.rb diff --git a/spec/components/editable_champ/section_component_spec.rb b/spec/components/editable_champ/section_component_spec.rb index da5283530..f3f474297 100644 --- a/spec/components/editable_champ/section_component_spec.rb +++ b/spec/components/editable_champ/section_component_spec.rb @@ -1,6 +1,8 @@ describe EditableChamp::SectionComponent, type: :component do include TreeableConcern - let(:component) { described_class.new(champs: champs) } + let(:types_de_champ) { champs.map(&:type_de_champ) } + let(:champs_by_stable_id_with_row) { champs.index_by(&:stable_id_with_row) } + let(:component) { described_class.new(types_de_champ:, champs_by_stable_id_with_row:) } before { render_inline(component).to_html } context 'list of champs without an header_section' do @@ -98,6 +100,7 @@ describe EditableChamp::SectionComponent, type: :component do end let(:dossier) { create(:dossier, :with_populated_champs, procedure: procedure) } let(:champs) { dossier.champs_public } + let(:champs_by_stable_id_with_row) { dossier.champs_by_stable_id_with_row } it 'render nested fieldsets, increase heading level for repetition header_section' do expect(page).to have_selector("fieldset") diff --git a/spec/factories/dossier.rb b/spec/factories/dossier.rb index f934364bf..bf387fe6c 100644 --- a/spec/factories/dossier.rb +++ b/spec/factories/dossier.rb @@ -280,7 +280,13 @@ FactoryBot.define do after(:create) do |dossier, _evaluator| dossier.champs_to_destroy.where(private: false).destroy_all dossier.types_de_champ.each do |type_de_champ| - create(:"champ_#{type_de_champ.type_champ}", dossier:, type_de_champ:) + if type_de_champ.simple_drop_down_list? + create(:"champ_#{type_de_champ.type_champ}", dossier:, type_de_champ:, value: type_de_champ.drop_down_list_enabled_non_empty_options.first) + elsif type_de_champ.multiple_drop_down_list? + create(:"champ_#{type_de_champ.type_champ}", dossier:, type_de_champ:, value: type_de_champ.drop_down_list_enabled_non_empty_options.first(2).to_json) + else + create(:"champ_#{type_de_champ.type_champ}", dossier:, type_de_champ:) + end end dossier.reload end @@ -290,7 +296,13 @@ FactoryBot.define do after(:create) do |dossier, _evaluator| dossier.champs_to_destroy.where(private: true).destroy_all dossier.types_de_champ_private.each do |type_de_champ| - create(:"champ_#{type_de_champ.type_champ}", private: true, dossier:, type_de_champ:) + if type_de_champ.simple_drop_down_list? + create(:"champ_#{type_de_champ.type_champ}", private: true, dossier:, type_de_champ:, value: type_de_champ.drop_down_list_enabled_non_empty_options.first) + elsif type_de_champ.multiple_drop_down_list? + create(:"champ_#{type_de_champ.type_champ}", private: true, dossier:, type_de_champ:, value: type_de_champ.drop_down_list_enabled_non_empty_options.first(2).to_json) + else + create(:"champ_#{type_de_champ.type_champ}", private: true, dossier:, type_de_champ:) + end end dossier.reload end diff --git a/spec/factories/procedure.rb b/spec/factories/procedure.rb index 6d3b76c6e..10dbffecf 100644 --- a/spec/factories/procedure.rb +++ b/spec/factories/procedure.rb @@ -300,7 +300,13 @@ def build_types_de_champ(types_de_champ, revision:, scope: :public, parent: nil) end if type.in?([:drop_down_list, :multiple_drop_down_list, :linked_drop_down_list]) - type_de_champ_attributes[:drop_down_list_value] = options.join("\r\n") + if options.delete(:short).present? + type_de_champ_attributes[:drop_down_list_value] = "val1\r\nval2\r\n--separateur--\r\nval3" + elsif options.delete(:long).present? + type_de_champ_attributes[:drop_down_list_value] = "alpha\r\nbravo\r\n--separateur--\r\ncharly\r\ndelta\r\necho\r\nfox-trot\r\ngolf" + else + type_de_champ_attributes[:drop_down_list_value] = options.join("\r\n") + end end end diff --git a/spec/models/champ_spec.rb b/spec/models/champ_spec.rb index 9ba045034..ca7e32bc2 100644 --- a/spec/models/champ_spec.rb +++ b/spec/models/champ_spec.rb @@ -83,11 +83,6 @@ describe Champ do expect(public_sections).not_to be_empty expect(private_sections).not_to be_empty expect(sections_in_repetition).not_to be_empty - - expect(public_champ.sections).to eq(public_sections) - expect(private_champ.sections).to eq(private_sections) - expect(champ_in_repetition.sections).to eq(sections_in_repetition) - expect(standalone_champ.sections).to eq([]) end end diff --git a/spec/models/champs/header_section_champ_spec.rb b/spec/models/champs/header_section_champ_spec.rb deleted file mode 100644 index 7e3cbee4c..000000000 --- a/spec/models/champs/header_section_champ_spec.rb +++ /dev/null @@ -1,38 +0,0 @@ -describe Champs::HeaderSectionChamp do - describe '#section_index' do - let(:types_de_champ) do - [ - { type: :header_section }, - { type: :civilite }, - { type: :text }, - { type: :header_section }, - { type: :email } - ] - end - let(:types_de_champ_public) { types_de_champ } - let(:procedure) { create(:procedure, types_de_champ_public: types_de_champ_public) } - let(:dossier) { create(:dossier, procedure: procedure) } - - context 'for root-level champs' do - let(:first_header) { dossier.champs_public.first } - let(:second_header) { dossier.champs_public.fourth } - - it 'returns the index of the section (starting from 1)' do - expect(first_header.section_index).to eq 1 - expect(second_header.section_index).to eq 2 - end - end - - context 'for repetition champs' do - let(:types_de_champ_public) { [{ type: :repetition, mandatory: true, children: types_de_champ }] } - - let(:first_header) { dossier.champs_public.first.champs.first } - let(:second_header) { dossier.champs_public.first.champs.fourth } - - it 'returns the index of the section in the repetition (starting from 1)' do - expect(first_header.section_index).to eq 1 - expect(second_header.section_index).to eq 2 - end - end - end -end diff --git a/spec/models/concern/treeable_concern_spec.rb b/spec/models/concern/treeable_concern_spec.rb index d3f12d5d9..674720e92 100644 --- a/spec/models/concern/treeable_concern_spec.rb +++ b/spec/models/concern/treeable_concern_spec.rb @@ -3,35 +3,35 @@ describe TreeableConcern do include TreeableConcern attr_reader :root - def initialize(champs:) - @root = to_tree(champs:) + def initialize(types_de_champ:) + @root = to_tree(types_de_champ:) end end - subject { ChampsToTree.new(champs: champs).root } + subject { ChampsToTree.new(types_de_champ:).root } describe "to_tree" do - let(:header_1) { build(:champ_header_section_level_1) } - let(:header_1_2) { build(:champ_header_section_level_2) } - let(:header_2) { build(:champ_header_section_level_1) } - let(:champ_text) { build(:champ_text) } - let(:champ_textarea) { build(:champ_textarea) } - let(:champ_explication) { build(:champ_explication) } - let(:champ_communes) { build(:champ_communes) } + let(:header_1) { build(:champ_header_section_level_1).type_de_champ } + let(:header_1_2) { build(:champ_header_section_level_2).type_de_champ } + let(:header_2) { build(:champ_header_section_level_1).type_de_champ } + let(:champ_text) { build(:champ_text).type_de_champ } + let(:champ_textarea) { build(:champ_textarea).type_de_champ } + let(:champ_explication) { build(:champ_explication).type_de_champ } + let(:champ_communes) { build(:champ_communes).type_de_champ } context 'without section' do - let(:champs) do + let(:types_de_champ) do [ champ_text, champ_textarea ] end it 'inlines champs at root level' do - expect(subject.size).to eq(champs.size) - expect(subject).to eq(champs) + expect(subject.size).to eq(types_de_champ.size) + expect(subject).to eq(types_de_champ) end end context 'with header_section and champs' do - let(:champs) do + let(:types_de_champ) do [ header_1, champ_explication, @@ -51,7 +51,7 @@ describe TreeableConcern do end context 'leading champs, and in between sections only' do - let(:champs) do + let(:types_de_champ) do [ champ_text, champ_textarea, @@ -74,7 +74,7 @@ describe TreeableConcern do end context 'with one sub sections' do - let(:champs) do + let(:types_de_champ) do [ header_1, champ_explication, @@ -94,11 +94,11 @@ describe TreeableConcern do end context 'with consecutive subsection' do - let(:header_1) { build(:champ_header_section_level_1) } - let(:header_1_2_1) { build(:champ_header_section_level_2) } - let(:header_1_2_2) { build(:champ_header_section_level_2) } - let(:header_1_2_3) { build(:champ_header_section_level_2) } - let(:champs) do + let(:header_1) { build(:champ_header_section_level_1).type_de_champ } + let(:header_1_2_1) { build(:champ_header_section_level_2).type_de_champ } + let(:header_1_2_2) { build(:champ_header_section_level_2).type_de_champ } + let(:header_1_2_3) { build(:champ_header_section_level_2).type_de_champ } + let(:types_de_champ) do [ header_1, header_1_2_1, @@ -123,9 +123,9 @@ describe TreeableConcern do end context 'with one sub sections and one subsub section' do - let(:header_1_2_3) { build(:champ_header_section_level_3) } + let(:header_1_2_3) { build(:champ_header_section_level_3).type_de_champ } - let(:champs) do + let(:types_de_champ) do [ header_1, champ_explication, diff --git a/spec/models/dossier_spec.rb b/spec/models/dossier_spec.rb index 466c5aba4..170cf02b1 100644 --- a/spec/models/dossier_spec.rb +++ b/spec/models/dossier_spec.rb @@ -1540,10 +1540,10 @@ describe Dossier, type: :model do context "with mandatory SIRET champ" do let(:type_de_champ) { { type: :siret, mandatory: true } } - let(:champ_siret) { dossier.champs_public.first } + let(:champ_siret) { dossier.champs.first } before do - champ_siret.value = '44011762001530' + champ_siret.update(value: '44011762001530') end it 'should not have errors' do @@ -1776,6 +1776,7 @@ describe Dossier, type: :model do it "should solve N+1 problem" do dossier.champs_public << create_list(:champ_carte, 3, type_de_champ: type_de_champ_carte, geo_areas: [create(:geo_area)]) + dossier.champs_for_revision count = 0 @@ -1928,9 +1929,9 @@ describe Dossier, type: :model do let(:repetition_second_revision_champ) { dossier_second_revision.champs_public.find(&:repetition?) } let(:dossier) { create(:dossier, procedure: procedure) } let(:dossier_second_revision) { create(:dossier, procedure: procedure) } - let(:dossier_champs_for_export) { Dossier.champs_for_export(dossier.champs_public, procedure.types_de_champ_for_procedure_presentation.not_repetition) } - let(:dossier_second_revision_champs_for_export) { Dossier.champs_for_export(dossier_second_revision.champs_public, procedure.types_de_champ_for_procedure_presentation.not_repetition) } - let(:repetition_second_revision_champs_for_export) { Dossier.champs_for_export(repetition_second_revision_champ.champs, procedure.types_de_champ_for_procedure_presentation.repetition) } + let(:dossier_champs_for_export) { Dossier.champs_for_export(procedure.types_de_champ_for_procedure_presentation.not_repetition, dossier.champs_by_stable_id_with_row) } + let(:dossier_second_revision_champs_for_export) { Dossier.champs_for_export(procedure.types_de_champ_for_procedure_presentation.not_repetition, dossier_second_revision.champs_by_stable_id_with_row) } + let(:repetition_second_revision_champs_for_export) { Dossier.champs_for_export(procedure.types_de_champ_for_procedure_presentation.repetition, dossier.champs_by_stable_id_with_row) } context "when procedure published" do before do @@ -1968,7 +1969,7 @@ describe Dossier, type: :model do repetition = proc_test.types_de_champ_for_procedure_presentation.repetition.first type_champs = proc_test.types_de_champ_for_procedure_presentation(repetition).to_a expect(type_champs.size).to eq(1) - expect(Dossier.champs_for_export(dossier.champs_public, type_champs).size).to eq(3) + expect(Dossier.champs_for_export(type_champs, dossier.champs_by_stable_id_with_row).size).to eq(3) end end end @@ -1991,7 +1992,7 @@ describe Dossier, type: :model do let(:text_tdc) { procedure.active_revision.types_de_champ_public.second } let(:tdcs) { dossier.champs_public.map(&:type_de_champ) } - subject { Dossier.champs_for_export(dossier.champs_public, tdcs) } + subject { Dossier.champs_for_export(tdcs, dossier.champs_by_stable_id_with_row) } before do text_tdc.update(condition: ds_eq(champ_value(yes_no_tdc.stable_id), constant(true))) diff --git a/spec/models/types_de_champ/prefill_repetition_type_de_champ_spec.rb b/spec/models/types_de_champ/prefill_repetition_type_de_champ_spec.rb index c02a7cf15..123b9f099 100644 --- a/spec/models/types_de_champ/prefill_repetition_type_de_champ_spec.rb +++ b/spec/models/types_de_champ/prefill_repetition_type_de_champ_spec.rb @@ -1,9 +1,10 @@ # frozen_string_literal: true RSpec.describe TypesDeChamp::PrefillRepetitionTypeDeChamp, type: :model do - let(:procedure) { create(:procedure) } - let(:type_de_champ) { build(:type_de_champ_repetition, :with_types_de_champ, :with_region_types_de_champ, procedure: procedure) } - let(:champ) { create(:champ_repetition, type_de_champ: type_de_champ) } + let(:procedure) { create(:procedure, types_de_champ_public: [{ type: :repetition, children: [{}, { type: :integer_number }, { type: :regions }] }]) } + let(:dossier) { create(:dossier, procedure: procedure) } + let(:type_de_champ) { champ.type_de_champ } + let(:champ) { dossier.champs.first } let(:prefillable_subchamps) { TypesDeChamp::PrefillRepetitionTypeDeChamp.new(type_de_champ, procedure.active_revision).send(:prefillable_subchamps) } let(:text_repetition) { prefillable_subchamps.first } let(:integer_repetition) { prefillable_subchamps.second } diff --git a/spec/views/shared/dossiers/_champs.html.haml_spec.rb b/spec/views/shared/dossiers/_champs.html.haml_spec.rb index 441ab1a4c..322c80db0 100644 --- a/spec/views/shared/dossiers/_champs.html.haml_spec.rb +++ b/spec/views/shared/dossiers/_champs.html.haml_spec.rb @@ -2,6 +2,9 @@ describe 'shared/dossiers/champs', type: :view do let(:instructeur) { create(:instructeur) } let(:demande_seen_at) { nil } let(:profile) { "instructeur" } + let(:procedure) { create(:procedure, types_de_champ_public:) } + let(:dossier) { create(:dossier, :with_populated_champs, procedure:) } + let(:types_de_champ) { dossier.revision.types_de_champ_public } before do view.extend DossierHelper @@ -12,17 +15,23 @@ describe 'shared/dossiers/champs', type: :view do end end - subject { render 'shared/dossiers/champs', champs:, dossier:, demande_seen_at:, profile: } + subject { render 'shared/dossiers/champs', types_de_champ:, dossier:, demande_seen_at:, profile: } context "there are some champs" do - let(:dossier) { create(:dossier) } - let(:champ1) { create(:champ_checkbox, dossier: dossier, value: 'true') } - let(:champ2) { create(:champ_header_section, dossier: dossier, value: "Section") } - let(:champ3) { create(:champ_explication, dossier: dossier, value: "mazette") } - let(:champ4) { create(:champ_dossier_link, dossier: dossier, value: dossier.id) } - let(:champ5) { create(:champ_textarea, dossier: dossier, value: "Some long text in a textarea.") } - let(:champ6) { create(:champ_rna, value: "W173847273") } - let(:champs) { [champ1, champ2, champ3, champ4, champ5, champ6] } + let(:types_de_champ_public) { [{ type: :checkbox }, { type: :header_section }, { type: :explication }, { type: :dossier_link }, { type: :textarea }, { type: :rna }] } + let(:champ1) { dossier.champs[0] } + let(:champ2) { dossier.champs[1] } + let(:champ3) { dossier.champs[2] } + let(:champ4) { dossier.champs[3] } + let(:champ5) { dossier.champs[4] } + let(:champ6) { dossier.champs[5] } + + before do + champ1.update(value: 'true') + champ4.update(value: dossier.id) + champ5.update(value: "Some long text in a textarea.") + champ6.update(value: "W173847273") + end it "renders titles and values of champs" do expect(subject).to include(champ1.libelle) @@ -41,26 +50,34 @@ describe 'shared/dossiers/champs', type: :view do it "doesn't render explication champs" do expect(subject).not_to include(champ3.libelle) - expect(subject).not_to include(champ3.value) + end + end + + context "with auto-link" do + let(:types_de_champ_public) { [{ type: :text }, { type: :textarea }] } + let(:champ1) { dossier.champs[0] } + let(:champ2) { dossier.champs[1] } + + before do + champ1.update(value: 'https://github.com/tchak') + champ2.update(value: "https://github.com/LeSim") end - context "with auto-link" do - let(:champ1) { create(:champ_text, value: "https://github.com/tchak") } - let(:champ2) { create(:champ_textarea, value: "https://github.com/LeSim") } - let(:link1) { 'https://github.com/tchak' } - let(:link2) { 'https://github.com/LeSim' } + let(:link1) { 'https://github.com/tchak' } + let(:link2) { 'https://github.com/LeSim' } - it "render links" do - expect(subject).to include(link1) - expect(subject).to include(link2) - end + it "render links" do + expect(subject).to include(link1) + expect(subject).to include(link2) end end context "with a dossier champ, but we are not authorized to acces the dossier" do - let(:dossier) { create(:dossier) } - let(:champ) { create(:champ_dossier_link, dossier: dossier, value: dossier.id) } - let(:champs) { [champ] } + let(:types_de_champ_public) { [{ type: :dossier_link }] } + + before do + dossier.champs.first.update(value: dossier.id) + end it { is_expected.not_to have_link("Dossier nº #{dossier.id}") } it { is_expected.to include("Dossier nº #{dossier.id}") } @@ -68,9 +85,11 @@ describe 'shared/dossiers/champs', type: :view do end context "with a dossier_link champ but without value" do - let(:dossier) { create(:dossier) } - let(:champ) { create(:champ_dossier_link, dossier: dossier, value: nil) } - let(:champs) { [champ] } + let(:types_de_champ_public) { [{ type: :dossier_link }] } + + before do + dossier.champs.first.update(value: nil) + end it { is_expected.not_to include("non saisi") } @@ -81,9 +100,11 @@ describe 'shared/dossiers/champs', type: :view do end context "with a piece justificative without value" do - let(:dossier) { create(:dossier) } - let(:champ) { create(:champ_without_piece_justificative, dossier:) } - let(:champs) { [champ] } + let(:types_de_champ_public) { [{ type: :piece_justificative }] } + + before do + dossier.champs.first.piece_justificative_file.purge + end it { is_expected.not_to include("pièce justificative non saisie") } @@ -94,9 +115,9 @@ describe 'shared/dossiers/champs', type: :view do end context "with seen_at" do - let(:dossier) { create(:dossier, :en_construction, depose_at: 1.day.ago) } - let(:champ1) { create(:champ_checkbox, dossier: dossier, value: 'true') } - let(:champs) { [champ1] } + let(:types_de_champ_public) { [{ type: :checkbox }] } + let(:dossier) { create(:dossier, :en_construction, :with_populated_champs, procedure:, depose_at: 1.day.ago) } + let(:champ1) { dossier.champs[0] } context "with a demande_seen_at after champ updated_at" do let(:demande_seen_at) { champ1.updated_at + 1.hour } @@ -105,9 +126,13 @@ describe 'shared/dossiers/champs', type: :view do end context "with champ updated_at at depose_at" do - let(:champ1) { create(:champ_checkbox, dossier: dossier, value: 'true', updated_at: dossier.depose_at) } + let(:champ1) { dossier.champs[0] } let(:demande_seen_at) { champ1.updated_at - 1.hour } + before do + champ1.update(value: 'false', updated_at: dossier.depose_at) + end + it { is_expected.not_to have_css(".fr-badge--new") } end diff --git a/spec/views/shared/dossiers/_edit.html.haml_spec.rb b/spec/views/shared/dossiers/_edit.html.haml_spec.rb index 7814425fd..67f35ed3d 100644 --- a/spec/views/shared/dossiers/_edit.html.haml_spec.rb +++ b/spec/views/shared/dossiers/_edit.html.haml_spec.rb @@ -6,29 +6,34 @@ describe 'shared/dossiers/edit', type: :view do subject { render 'shared/dossiers/edit', dossier: dossier, apercu: false } - context 'when there are some champs' do - let(:dossier) { create(:dossier) } - let(:champ_checkbox) { create(:champ_checkbox, dossier: dossier, value: 'true') } - let(:champ_header_section) { create(:champ_header_section, dossier: dossier, value: 'Section') } - let(:champ_explication) { create(:champ_explication, dossier: dossier, value: 'mazette') } - let(:champ_dossier_link) { create(:champ_dossier_link, dossier: dossier, value: dossier.id) } - let(:champ_textarea) { create(:champ_textarea, dossier: dossier, value: 'Some long text in a textarea.') } - let(:champs) { [champ_checkbox, champ_header_section, champ_explication, champ_dossier_link, champ_textarea] } + let(:procedure) { create(:procedure, types_de_champ_public:) } + let(:dossier) { create(:dossier, :with_populated_champs, procedure:) } - before { dossier.champs_public << champs } + context 'when there are some champs' do + let(:champs_by_stable_id_with_row) { dossier.champs_by_stable_id_with_row } + + let(:type_de_champ_header_section) { procedure.draft_types_de_champ_public.find(&:header_section?) } + let(:type_de_champ_explication) { procedure.draft_types_de_champ_public.find(&:explication?) } + let(:type_de_champ_dossier_link) { procedure.draft_types_de_champ_public.find(&:dossier_link?) } + let(:type_de_champ_checkbox) { procedure.draft_types_de_champ_public.find(&:checkbox?) } + let(:type_de_champ_textarea) { procedure.draft_types_de_champ_public.find(&:textarea?) } + + let(:champ_checkbox) { champs_by_stable_id_with_row[[type_de_champ_checkbox.stable_id]] } + let(:champ_dossier_link) { champs_by_stable_id_with_row[[type_de_champ_dossier_link.stable_id]] } + let(:champ_textarea) { champs_by_stable_id_with_row[[type_de_champ_textarea.stable_id]] } + + let(:types_de_champ_public) { [{ type: :checkbox }, { type: :header_section }, { type: :explication }, { type: :dossier_link }, { type: :textarea }] } it 'renders labels and editable values of champs' do expect(subject).to have_field(champ_checkbox.libelle, checked: true) - expect(subject).to have_css(".header-section", text: champ_header_section.libelle) - expect(subject).to have_text(champ_explication.libelle) - expect(subject).to have_field(champ_dossier_link.libelle, with: champ_dossier_link.value) + expect(subject).to have_css(".header-section", text: type_de_champ_header_section.libelle) + expect(subject).to have_text(type_de_champ_explication.libelle) + expect(subject).to have_field(type_de_champ_dossier_link.libelle, with: champ_dossier_link.value) expect(subject).to have_field(champ_textarea.libelle, with: champ_textarea.value) end context "with standard champs" do - let(:champ_email) { create(:champ_email, dossier: dossier) } - let(:champ_phone) { create(:champ_phone, dossier: dossier) } - let(:champs) { [champ_email, champ_phone] } + let(:types_de_champ_public) { [{ type: :email }, { type: :phone }] } it "does not render basic placeholders" do expect(subject).not_to have_css('input[type="email"][placeholder$="exemple.fr"]') @@ -38,17 +43,18 @@ describe 'shared/dossiers/edit', type: :view do end context 'with a single-value list' do - let(:dossier) { create(:dossier) } - let(:type_de_champ) { create(:type_de_champ_drop_down_list, mandatory: mandatory, procedure: dossier.procedure) } - let(:champ) { create(:champ_drop_down_list, dossier: dossier, type_de_champ: type_de_champ, value: value) } + let(:types_de_champ_public) { [{ type: :drop_down_list, options:, mandatory: }] } + let(:champ) { dossier.champs_public.first } + let(:type_de_champ) { champ.type_de_champ } let(:enabled_options) { type_de_champ.drop_down_list_enabled_non_empty_options } let(:mandatory) { true } - - before { dossier.champs_public << champ } + let(:options) { nil } context 'when the list is short' do let(:value) { 'val1' } + before { champ.update(value:) } + it 'renders the list as radio buttons' do expect(subject).to have_selector('input[type=radio]', count: enabled_options.count) end @@ -65,7 +71,9 @@ describe 'shared/dossiers/edit', type: :view do context 'when the list is long' do let(:value) { 'alpha' } - let(:type_de_champ) { create(:type_de_champ_drop_down_list, :long, procedure: dossier.procedure) } + let(:options) { [:long] } + + before { champ.update(value:) } it 'renders the list as a dropdown' do expect(subject).to have_select(type_de_champ.libelle, options: enabled_options + ['']) @@ -74,21 +82,18 @@ describe 'shared/dossiers/edit', type: :view do end context 'with a multiple-values list' do - let(:dossier) { create(:dossier) } - let(:type_de_champ) { create(:type_de_champ_multiple_drop_down_list, procedure: dossier.procedure, drop_down_list_value: drop_down_list_value) } - let(:champ) { create(:champ_multiple_drop_down_list, dossier: dossier, type_de_champ: type_de_champ, value: champ_value) } + let(:types_de_champ_public) { [{ type: :multiple_drop_down_list, options: }] } + let(:champ) { dossier.champs.first } + let(:type_de_champ) { champ.type_de_champ } let(:options) { type_de_champ.drop_down_list_options } let(:enabled_options) { type_de_champ.drop_down_list_enabled_non_empty_options } - before { dossier.champs_public << champ } - context 'when the list is short' do - let(:drop_down_list_value) { ['valid', 'invalid', 'not sure yet'].join("\r\n") } - let(:champ_value) { ['invalid'].to_json } + let(:options) { ['valid', 'invalid', 'not sure yet'] } it 'renders the list as checkboxes' do expect(subject).to have_selector('input[type=checkbox]', count: enabled_options.count) - expect(subject).to have_selector('input[type=checkbox][checked=checked]', count: 1) + expect(subject).to have_selector('input[type=checkbox][checked=checked]', count: 2) end it 'adds an extra hidden input, to send a blank value even when all checkboxes are unchecked' do @@ -97,8 +102,7 @@ describe 'shared/dossiers/edit', type: :view do end context 'when the list is long' do - let(:drop_down_list_value) { ['peach', 'banana', 'pear', 'apricot', 'apple', 'grapefruit'].join("\r\n") } - let(:champ_value) { ['banana', 'grapefruit'].to_json } + let(:options) { ['peach', 'banana', 'pear', 'apricot', 'apple', 'grapefruit'] } it 'renders the list as a multiple-selection dropdown' do expect(subject).to have_selector('select') @@ -107,13 +111,11 @@ describe 'shared/dossiers/edit', type: :view do end context 'with a mandatory piece justificative' do - let(:dossier) { create(:dossier) } - let(:type_de_champ) { create(:type_de_champ_piece_justificative, procedure: dossier.procedure, mandatory: true) } - let(:champ) { create(:champ_piece_justificative, dossier: dossier, type_de_champ: type_de_champ) } + let(:types_de_champ_public) { [{ type: :piece_justificative, mandatory: true }] } + let(:champ) { dossier.champs.first } context 'when dossier is en construction' do - let(:dossier) { create(:dossier, :en_construction) } - before { dossier.champs_public << champ } + let(:dossier) { create(:dossier, :en_construction, :with_populated_champs, procedure:) } it 'can delete a piece justificative' do expect(subject).to have_selector("[title='Supprimer le fichier #{champ.piece_justificative_file.attachments[0].filename}']") @@ -121,10 +123,6 @@ describe 'shared/dossiers/edit', type: :view do end context 'when dossier is brouillon' do - before do - dossier.champs_public << champ - end - it 'can delete a piece justificative' do expect(subject).to have_selector("[title='Supprimer le fichier #{champ.piece_justificative_file.attachments[0].filename}']") end @@ -132,16 +130,11 @@ describe 'shared/dossiers/edit', type: :view do end context 'with a routed procedure' do - let(:procedure_routee) do - create(:procedure, :routee) - end - let!(:drop_down_tdc) { create(:type_de_champ_drop_down_list, procedure: procedure_routee, drop_down_options: options) } - let(:options) { procedure_routee.groupe_instructeurs.pluck(:label) } - let(:dossier) { create(:dossier, procedure: procedure_routee) } - let(:champs) { [champ_drop_down] } - let(:champ_drop_down) { create(:champ_drop_down_list, dossier: dossier, type_de_champ: drop_down_tdc, value: options.first) } - - before { dossier.champs_public << champs } + let(:groupe_instructeur) { create(:groupe_instructeur) } + let(:procedure) { create(:procedure, :routee, groupe_instructeurs: [groupe_instructeur], types_de_champ_public: [{ type: :drop_down_list, options: }]) } + let(:options) { [groupe_instructeur.label] } + let(:dossier) { create(:dossier, procedure:) } + let(:champ_drop_down) { dossier.champs.first } it 'renders the libelle of the type de champ used for routing' do expect(subject).to include(champ_drop_down.libelle)