From 2a3ba283438a72e3f7a9ff725f4eb64b3171b8d2 Mon Sep 17 00:00:00 2001 From: Damien Le Thiec Date: Sat, 11 Feb 2023 22:27:16 +0100 Subject: [PATCH] Fix tests possible value prefill --- .../prefill_drop_down_list_type_de_champ.rb | 10 +++--- .../prefill_pays_type_de_champ.rb | 8 ++--- .../prefill_region_type_de_champ.rb | 4 +-- .../prefill_repetition_type_de_champ.rb | 13 ++++---- .../types_de_champ/prefill_type_de_champ.rb | 32 +++++++++---------- ...efill_drop_down_list_type_de_champ_spec.rb | 11 ++++--- .../prefill_pays_type_de_champ_spec.rb | 11 +++++-- .../prefill_region_type_de_champ_spec.rb | 11 +++++-- .../prefill_repetition_type_de_champ_spec.rb | 15 ++++----- .../prefill_type_de_champ_spec.rb | 8 ++--- 10 files changed, 68 insertions(+), 55 deletions(-) diff --git a/app/models/types_de_champ/prefill_drop_down_list_type_de_champ.rb b/app/models/types_de_champ/prefill_drop_down_list_type_de_champ.rb index 648f2d17e..b5505151f 100644 --- a/app/models/types_de_champ/prefill_drop_down_list_type_de_champ.rb +++ b/app/models/types_de_champ/prefill_drop_down_list_type_de_champ.rb @@ -1,4 +1,10 @@ class TypesDeChamp::PrefillDropDownListTypeDeChamp < TypesDeChamp::PrefillTypeDeChamp + def example_value + possible_values_list.first + end + + private + def possible_values_list if drop_down_other? drop_down_list_enabled_non_empty_options.insert( @@ -9,8 +15,4 @@ class TypesDeChamp::PrefillDropDownListTypeDeChamp < TypesDeChamp::PrefillTypeDe drop_down_list_enabled_non_empty_options end end - - def example_value - possible_values_list.first - end end diff --git a/app/models/types_de_champ/prefill_pays_type_de_champ.rb b/app/models/types_de_champ/prefill_pays_type_de_champ.rb index a5e6aad21..cf0fa0278 100644 --- a/app/models/types_de_champ/prefill_pays_type_de_champ.rb +++ b/app/models/types_de_champ/prefill_pays_type_de_champ.rb @@ -1,14 +1,14 @@ class TypesDeChamp::PrefillPaysTypeDeChamp < TypesDeChamp::PrefillTypeDeChamp - def possible_values_list - countries.map { |country| "#{country[:code]} (#{country[:name]})" } - end - def example_value countries.pick(:code) end private + def possible_values_list + countries.map { |country| "#{country[:code]} (#{country[:name]})" } + end + def countries @countries ||= APIGeoService.countries.sort_by { |country| country[:code] } end diff --git a/app/models/types_de_champ/prefill_region_type_de_champ.rb b/app/models/types_de_champ/prefill_region_type_de_champ.rb index 7d52601b4..6362fccac 100644 --- a/app/models/types_de_champ/prefill_region_type_de_champ.rb +++ b/app/models/types_de_champ/prefill_region_type_de_champ.rb @@ -1,10 +1,10 @@ class TypesDeChamp::PrefillRegionTypeDeChamp < TypesDeChamp::PrefillTypeDeChamp + private + def possible_values_list regions.map { |region| "#{region[:code]} (#{region[:name]})" } end - private - def regions @regions ||= APIGeoService.regions.sort_by { |region| region[:code] } end diff --git a/app/models/types_de_champ/prefill_repetition_type_de_champ.rb b/app/models/types_de_champ/prefill_repetition_type_de_champ.rb index 906451d79..ad809b1ee 100644 --- a/app/models/types_de_champ/prefill_repetition_type_de_champ.rb +++ b/app/models/types_de_champ/prefill_repetition_type_de_champ.rb @@ -9,21 +9,22 @@ class TypesDeChamp::PrefillRepetitionTypeDeChamp < TypesDeChamp::PrefillTypeDeCh ].join("
").html_safe # rubocop:disable Rails/OutputSafety end - def subchamps_possible_values_list - "" - end def example_value [row_values_format, row_values_format].map { |row| row.to_s.gsub("=>", ":") } end + private + def too_many_possible_values? false end - private + def subchamps_possible_values_list + "" + end def row_values_format @row_example_value ||= diff --git a/app/models/types_de_champ/prefill_type_de_champ.rb b/app/models/types_de_champ/prefill_type_de_champ.rb index cc3776880..35a8fd955 100644 --- a/app/models/types_de_champ/prefill_type_de_champ.rb +++ b/app/models/types_de_champ/prefill_type_de_champ.rb @@ -2,7 +2,7 @@ class TypesDeChamp::PrefillTypeDeChamp < SimpleDelegator include ActionView::Helpers::UrlHelper include ApplicationHelper - POSSIBLE_VALUES_THRESHOLD = 1 + POSSIBLE_VALUES_THRESHOLD = 5 def self.build(type_de_champ) case type_de_champ.type_champ @@ -24,19 +24,7 @@ class TypesDeChamp::PrefillTypeDeChamp < SimpleDelegator end def possible_values - [possible_values_list_display, link_to_all_possible_values].compact.join('
').html_safe - end - - def possible_values_list - return [] unless prefillable? - - [I18n.t("views.prefill_descriptions.edit.possible_values.#{type_champ}_html")] - end - - def link_to_all_possible_values - return unless too_many_possible_values? - - link_to I18n.t("views.prefill_descriptions.edit.possible_values.link.text"), Rails.application.routes.url_helpers.prefill_type_de_champ_path(path, self), title: new_tab_suffix(I18n.t("views.prefill_descriptions.edit.possible_values.link.title")), **external_link_attributes + [possible_values_list_display, link_to_all_possible_values].compact.join('
').html_safe # rubocop:disable Rails/OutputSafety end def example_value @@ -45,12 +33,24 @@ class TypesDeChamp::PrefillTypeDeChamp < SimpleDelegator I18n.t("views.prefill_descriptions.edit.examples.#{type_champ}") end + private + + def possible_values_list + return [] unless prefillable? + + [I18n.t("views.prefill_descriptions.edit.possible_values.#{type_champ}_html")] + end + + def link_to_all_possible_values + return unless too_many_possible_values? && prefillable? + + link_to I18n.t("views.prefill_descriptions.edit.possible_values.link.text"), Rails.application.routes.url_helpers.prefill_type_de_champ_path(path, self), title: new_tab_suffix(I18n.t("views.prefill_descriptions.edit.possible_values.link.title")), **external_link_attributes + end + def too_many_possible_values? possible_values_list.count > POSSIBLE_VALUES_THRESHOLD end - private - def possible_values_list_display if too_many_possible_values? I18n.t("views.prefill_descriptions.edit.possible_values.#{type_champ}_html").html_safe # rubocop:disable Rails/OutputSafety diff --git a/spec/models/types_de_champ/prefill_drop_down_list_type_de_champ_spec.rb b/spec/models/types_de_champ/prefill_drop_down_list_type_de_champ_spec.rb index fc7d4e573..499ce5b3f 100644 --- a/spec/models/types_de_champ/prefill_drop_down_list_type_de_champ_spec.rb +++ b/spec/models/types_de_champ/prefill_drop_down_list_type_de_champ_spec.rb @@ -2,22 +2,25 @@ RSpec.describe TypesDeChamp::PrefillDropDownListTypeDeChamp do describe '#possible_values' do + let(:procedure) { create(:procedure) } subject(:possible_values) { described_class.new(type_de_champ).possible_values } + before { type_de_champ.reload } + context "when the drop down list accepts 'other'" do - let(:type_de_champ) { build(:type_de_champ_drop_down_list, :with_other) } + let(:type_de_champ) { build(:type_de_champ_drop_down_list, :with_other, procedure: procedure) } it { expect(possible_values).to match( - [I18n.t("views.prefill_descriptions.edit.possible_values.drop_down_list_other_html")] + type_de_champ.drop_down_list_enabled_non_empty_options + ([I18n.t("views.prefill_descriptions.edit.possible_values.drop_down_list_other_html")] + type_de_champ.drop_down_list_enabled_non_empty_options).to_sentence ) } end context "when the drop down list does not accept 'other'" do - let(:type_de_champ) { build(:type_de_champ_drop_down_list) } + let(:type_de_champ) { build(:type_de_champ_drop_down_list, procedure:) } - it { expect(possible_values).to match(type_de_champ.drop_down_list_enabled_non_empty_options) } + it { expect(possible_values).to match(type_de_champ.drop_down_list_enabled_non_empty_options.to_sentence) } end end diff --git a/spec/models/types_de_champ/prefill_pays_type_de_champ_spec.rb b/spec/models/types_de_champ/prefill_pays_type_de_champ_spec.rb index f651d3d60..baffe29dc 100644 --- a/spec/models/types_de_champ/prefill_pays_type_de_champ_spec.rb +++ b/spec/models/types_de_champ/prefill_pays_type_de_champ_spec.rb @@ -1,12 +1,17 @@ RSpec.describe TypesDeChamp::PrefillPaysTypeDeChamp, type: :model do - let(:type_de_champ) { build(:type_de_champ_pays) } + let(:procedure) { create(:procedure) } + let(:type_de_champ) { build(:type_de_champ_pays, procedure: procedure) } describe '#possible_values' do - let(:expected_values) { APIGeoService.countries.sort_by { |country| country[:code] }.map { |country| "#{country[:code]} (#{country[:name]})" } } + let(:expected_values) { "Un code pays ISO 3166-2
Voir toutes les valeurs possibles" } subject(:possible_values) { described_class.new(type_de_champ).possible_values } - it { expect(possible_values).to match(expected_values) } + before { type_de_champ.reload } + + it { + expect(possible_values).to match(expected_values) + } end describe '#example_value' do diff --git a/spec/models/types_de_champ/prefill_region_type_de_champ_spec.rb b/spec/models/types_de_champ/prefill_region_type_de_champ_spec.rb index 39fd75fb9..31d7b7a86 100644 --- a/spec/models/types_de_champ/prefill_region_type_de_champ_spec.rb +++ b/spec/models/types_de_champ/prefill_region_type_de_champ_spec.rb @@ -1,7 +1,8 @@ # frozen_string_literal: true RSpec.describe TypesDeChamp::PrefillRegionTypeDeChamp, type: :model do - let(:type_de_champ) { build(:type_de_champ_regions) } + let(:procedure) { create(:procedure) } + let(:type_de_champ) { create(:type_de_champ_regions, procedure: procedure) } let(:memory_store) { ActiveSupport::Cache.lookup_store(:memory_store) } before do @@ -16,9 +17,13 @@ RSpec.describe TypesDeChamp::PrefillRegionTypeDeChamp, type: :model do end describe '#possible_values', vcr: { cassette_name: 'api_geo_regions' } do - let(:expected_values) { APIGeoService.regions.sort_by { |region| region[:code] }.map { |region| "#{region[:code]} (#{region[:name]})" } } + let(:expected_values) { "Un code INSEE de région
Voir toutes les valeurs possibles" } subject(:possible_values) { described_class.new(type_de_champ).possible_values } - it { expect(possible_values).to match(expected_values) } + before { type_de_champ.reload } + + it { + expect(possible_values).to eq(expected_values) + } end end 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 0598e0740..a613b5c86 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 @@ -20,9 +20,13 @@ RSpec.describe TypesDeChamp::PrefillRepetitionTypeDeChamp, type: :model, vcr: { describe '#possible_values' do subject(:possible_values) { described_class.new(type_de_champ).possible_values } - let(:expected_value) { ["sub type de champ: Un texte court", "sub type de champ2: Un nombre entier", "region sub_champ: Voir toutes les valeurs possibles"] } + let(:expected_value) { + "Un tableau de dictionnaires avec les valeurs possibles pour chaque champ de la répétition.
" + } - it { expect(possible_values).to eq(expected_value) } + it { + expect(possible_values).to eq(expected_value) + } end describe '#example_value' do @@ -31,11 +35,4 @@ RSpec.describe TypesDeChamp::PrefillRepetitionTypeDeChamp, type: :model, vcr: { it { expect(example_value).to eq(expected_value) } end - - describe '#possible_values_sentence' do - subject(:possible_values_sentence) { described_class.new(type_de_champ).possible_values_sentence } - let(:expected_value) { "Un tableau de dictionnaires avec les valeurs possibles pour chaque champ de la répétition.
sub type de champ: Un texte court
sub type de champ2: Un nombre entier
region sub_champ: Voir toutes les valeurs possibles" } - - it { expect(possible_values_sentence).to eq(expected_value) } - end end diff --git a/spec/models/types_de_champ/prefill_type_de_champ_spec.rb b/spec/models/types_de_champ/prefill_type_de_champ_spec.rb index 3a8686c6c..34f407e0b 100644 --- a/spec/models/types_de_champ/prefill_type_de_champ_spec.rb +++ b/spec/models/types_de_champ/prefill_type_de_champ_spec.rb @@ -45,7 +45,7 @@ RSpec.describe TypesDeChamp::PrefillTypeDeChamp, type: :model do end describe '#possible_values' do - subject(:possible_values) { described_class.build(type_de_champ).possible_values } + subject(:possible_values) { described_class.new(type_de_champ).possible_values } context 'when the type de champ is not prefillable' do let(:type_de_champ) { build(:type_de_champ_mesri) } @@ -54,16 +54,16 @@ RSpec.describe TypesDeChamp::PrefillTypeDeChamp, type: :model do end context 'when there is too many possible values' do - let(:type_de_champ) { create(:type_de_champ_drop_down_list) } + let(:type_de_champ) { create(:type_de_champ_drop_down_list, procedure: create(:procedure)) } before { type_de_champ.drop_down_options = (1..described_class::POSSIBLE_VALUES_THRESHOLD + 1).map(&:to_s) } - it { expect(possible_values).to match("Un choix parmi ceux sélectionnés à la création de la procédure") } + it { expect(possible_values).to eq("Un choix parmi ceux sélectionnés à la création de la procédure") } end context 'when the type de champ is prefillable' do let(:type_de_champ) { build(:type_de_champ_email) } - it { expect(possible_values).to match("Une adresse email") } + it { expect(possible_values).to eq("Une adresse email") } end end