Fix tests possible value prefill

This commit is contained in:
Damien Le Thiec 2023-02-11 22:27:16 +01:00
parent a4d707f942
commit 2a3ba28343
10 changed files with 68 additions and 55 deletions

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -9,21 +9,22 @@ class TypesDeChamp::PrefillRepetitionTypeDeChamp < TypesDeChamp::PrefillTypeDeCh
].join("</br>").html_safe # rubocop:disable Rails/OutputSafety
end
def subchamps_possible_values_list
"<ul>" + prefillable_subchamps.map do |prefill_type_de_champ|
"<li>#{prefill_type_de_champ.libelle}: #{prefill_type_de_champ.possible_values}</li>"
end.join + "</ul>"
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
"<ul>" + prefillable_subchamps.map do |prefill_type_de_champ|
"<li>#{prefill_type_de_champ.libelle}: #{prefill_type_de_champ.possible_values}</li>"
end.join + "</ul>"
end
def row_values_format
@row_example_value ||=

View file

@ -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('<br>').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('<br>').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

View file

@ -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

View file

@ -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 <a href=\"https://en.wikipedia.org/wiki/ISO_3166-2\" target=\"_blank\">code pays ISO 3166-2</a><br><a title=\"Toutes les valeurs possibles — Nouvel onglet\" target=\"_blank\" rel=\"noopener noreferrer\" href=\"/procedures/#{procedure.path}/prefill_type_de_champs/#{type_de_champ.id}\">Voir toutes les valeurs possibles</a>" }
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

View file

@ -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 <a href=\"https://fr.wikipedia.org/wiki/R%C3%A9gion_fran%C3%A7aise\" target=\"_blank\">code INSEE de région</a><br><a title=\"Toutes les valeurs possibles — Nouvel onglet\" target=\"_blank\" rel=\"noopener noreferrer\" href=\"/procedures/#{procedure.path}/prefill_type_de_champs/#{type_de_champ.id}\">Voir toutes les valeurs possibles</a>" }
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

View file

@ -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: <a title=\"Toutes les valeurs possibles — Nouvel onglet\" target=\"_blank\" rel=\"noopener noreferrer\" href=\"/procedures/#{procedure.path}/prefill_type_de_champs/#{region_repetition.id}\">Voir toutes les valeurs possibles</a>"] }
let(:expected_value) {
"Un tableau de dictionnaires avec les valeurs possibles pour chaque champ de la répétition.</br><ul><li>sub type de champ: Un texte court</li><li>sub type de champ2: Un nombre entier</li><li>region sub_champ: Un <a href=\"https://fr.wikipedia.org/wiki/R%C3%A9gion_fran%C3%A7aise\" target=\"_blank\">code INSEE de région</a><br><a title=\"Toutes les valeurs possibles — Nouvel onglet\" target=\"_blank\" rel=\"noopener noreferrer\" href=\"/procedures/#{procedure.path}/prefill_type_de_champs/#{region_repetition.id}\">Voir toutes les valeurs possibles</a></li></ul>"
}
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.<br>sub type de champ: Un texte court<br>sub type de champ2: Un nombre entier<br>region sub_champ: <a title=\"Toutes les valeurs possibles — Nouvel onglet\" target=\"_blank\" rel=\"noopener noreferrer\" href=\"/procedures/#{procedure.path}/prefill_type_de_champs/#{region_repetition.id}\">Voir toutes les valeurs possibles</a>" }
it { expect(possible_values_sentence).to eq(expected_value) }
end
end

View file

@ -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