Add missing tests repetition prefill

This commit is contained in:
Damien Le Thiec 2023-02-06 17:38:59 +01:00
parent 63e7c17fda
commit b9c2d1251b
7 changed files with 89 additions and 8 deletions

View file

@ -128,6 +128,7 @@ en:
checkbox_html: '"true" to check, "false" to uncheck'
pays_html: An <a href="https://en.wikipedia.org/wiki/ISO_3166-2" target="_blank">ISO 3166-2 country code</a>
regions_html: An <a href="https://fr.wikipedia.org/wiki/R%C3%A9gion_fran%C3%A7aise" target="_blank">INSEE region code</a>
drop_down_list_html: A choice among those selected when the procedure was created
date_html: ISO8601 date
datetime_html: ISO8601 datetime
drop_down_list_other_html: Any value

View file

@ -119,6 +119,7 @@ fr:
checkbox_html: '"true" pour coché, "false" pour décoché'
pays_html: Un <a href="https://en.wikipedia.org/wiki/ISO_3166-2" target="_blank">code pays ISO 3166-2</a>
regions_html: Un <a href="https://fr.wikipedia.org/wiki/R%C3%A9gion_fran%C3%A7aise" target="_blank">code INSEE de région</a>
drop_down_list_html: Un choix parmi ceux sélectionnés à la création de la procédure
datetime_html: Datetime au format ISO8601
date_html: Date au format ISO8601
drop_down_list_other_html: Toute valeur

View file

@ -201,6 +201,15 @@ FactoryBot.define do
build(:type_de_champ, type_champ: TypeDeChamp.type_champs.fetch(:integer_number), procedure: evaluator.procedure, libelle: 'sub type de champ2', parent: parent, position: 1)
end
end
trait :with_region_types_de_champ do
after(:build) do |type_de_champ_repetition, evaluator|
revision = evaluator.procedure.active_revision
parent = revision.revision_types_de_champ.find { |rtdc| rtdc.type_de_champ == type_de_champ_repetition }
build(:type_de_champ, type_champ: TypeDeChamp.type_champs.fetch(:regions), procedure: evaluator.procedure, libelle: 'region sub_champ', parent: parent, position: 10)
end
end
end
end
end

View file

@ -88,36 +88,42 @@ RSpec.describe PrefillDescription, type: :model do
end
end
describe '#prefill_link' do
describe '#prefill_link', vcr: { cassette_name: 'api_geo_regions' } do
let(:procedure) { create(:procedure) }
let(:type_de_champ) { create(:type_de_champ_text, procedure: procedure) }
let(:type_de_champ_repetition) { build(:type_de_champ_repetition, :with_types_de_champ, :with_region_types_de_champ, procedure: procedure) }
let(:prefillable_subchamps) { TypesDeChamp::PrefillRepetitionTypeDeChamp.new(type_de_champ_repetition).send(:prefillable_subchamps) }
let(:region_repetition) { prefillable_subchamps.third }
let(:prefill_description) { described_class.new(procedure) }
before { prefill_description.update(selected_type_de_champ_ids: [type_de_champ.id]) }
before { prefill_description.update(selected_type_de_champ_ids: [type_de_champ.id, type_de_champ_repetition.id]) }
it "builds the URL to create a new prefilled dossier" do
expect(prefill_description.prefill_link).to eq(
commencer_url(
path: procedure.path,
"champ_#{type_de_champ.to_typed_id}" => I18n.t("views.prefill_descriptions.edit.examples.#{type_de_champ.type_champ}")
"champ_#{type_de_champ.to_typed_id}" => I18n.t("views.prefill_descriptions.edit.examples.#{type_de_champ.type_champ}"),
"champ_#{type_de_champ_repetition.to_typed_id}" => TypesDeChamp::PrefillTypeDeChamp.build(type_de_champ_repetition).example_value
)
)
end
end
describe '#prefill_query' do
describe '#prefill_query', vcr: { cassette_name: 'api_geo_regions' } do
let(:procedure) { create(:procedure) }
let(:type_de_champ) { create(:type_de_champ_text, procedure: procedure) }
let(:type_de_champ_repetition) { build(:type_de_champ_repetition, :with_types_de_champ, :with_region_types_de_champ, procedure: procedure) }
let(:prefillable_subchamps) { TypesDeChamp::PrefillRepetitionTypeDeChamp.new(type_de_champ_repetition).send(:prefillable_subchamps) }
let(:region_repetition) { prefillable_subchamps.third }
let(:prefill_description) { described_class.new(procedure) }
let(:expected_query) do
<<~TEXT
curl --request POST '#{api_public_v1_dossiers_url(procedure)}' \\
--header 'Content-Type: application/json' \\
--data '{"champ_#{type_de_champ.to_typed_id}": "#{I18n.t("views.prefill_descriptions.edit.examples.#{type_de_champ.type_champ}")}"}'
--data '{"champ_#{type_de_champ.to_typed_id}": "#{I18n.t("views.prefill_descriptions.edit.examples.#{type_de_champ.type_champ}")}", "champ_#{type_de_champ_repetition.to_typed_id}": #{TypesDeChamp::PrefillTypeDeChamp.build(type_de_champ_repetition).example_value}}'
TEXT
end
before { prefill_description.update(selected_type_de_champ_ids: [type_de_champ.id]) }
before { prefill_description.update(selected_type_de_champ_ids: [type_de_champ.id, type_de_champ_repetition.id]) }
it "builds the query to create a new prefilled dossier" do
expect(prefill_description.prefill_query).to eq(expected_query)

View file

@ -144,6 +144,7 @@ RSpec.describe PrefillParams do
it_behaves_like "a champ private value that is authorized", :checkbox, "false"
it_behaves_like "a champ private value that is authorized", :drop_down_list, "value"
it_behaves_like "a champ private value that is authorized", :regions, "93"
it_behaves_like "a champ public value that is unauthorized", :repetition, "[{\"name\":\"value\"}, {\"name\":\"value2\"}]"
it_behaves_like "a champ public value that is unauthorized", :decimal_number, "non decimal string"
it_behaves_like "a champ public value that is unauthorized", :integer_number, "non integer string"
@ -160,7 +161,6 @@ RSpec.describe PrefillParams do
it_behaves_like "a champ public value that is unauthorized", :header_section, "value"
it_behaves_like "a champ public value that is unauthorized", :explication, "value"
it_behaves_like "a champ public value that is unauthorized", :piece_justificative, "value"
it_behaves_like "a champ public value that is unauthorized", :repetition, "value"
it_behaves_like "a champ public value that is unauthorized", :cnaf, "value"
it_behaves_like "a champ public value that is unauthorized", :dgfip, "value"
it_behaves_like "a champ public value that is unauthorized", :pole_emploi, "value"

View file

@ -0,0 +1,41 @@
# frozen_string_literal: true
RSpec.describe TypesDeChamp::PrefillRepetitionTypeDeChamp, type: :model, vcr: { cassette_name: 'api_geo_regions' } do
let(:procedure) { build(:procedure) }
let(:type_de_champ) { build(:type_de_champ_repetition, :with_types_de_champ, :with_region_types_de_champ, procedure: procedure) }
let(:prefillable_subchamps) { TypesDeChamp::PrefillRepetitionTypeDeChamp.new(type_de_champ).send(:prefillable_subchamps) }
let(:region_repetition) { prefillable_subchamps.third }
let(:memory_store) { ActiveSupport::Cache.lookup_store(:memory_store) }
before do
allow(Rails).to receive(:cache).and_return(memory_store)
Rails.cache.clear
end
describe 'ancestors' do
subject { described_class.build(type_de_champ) }
it { is_expected.to be_kind_of(TypesDeChamp::PrefillTypeDeChamp) }
end
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>"] }
it { expect(possible_values).to eq(expected_value) }
end
describe '#example_value' do
subject(:example_value) { described_class.new(type_de_champ).example_value }
let(:expected_value) { ["{\"sub type de champ\":\"Texte court\", \"sub type de champ2\":\"42\", \"region sub_champ\":\"53\"}", "{\"sub type de champ\":\"Texte court\", \"sub type de champ2\":\"42\", \"region sub_champ\":\"53\"}"] }
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 array de hash 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

@ -60,6 +60,29 @@ RSpec.describe TypesDeChamp::PrefillTypeDeChamp, type: :model do
end
end
describe '#possible_values_sentence' do
subject(:possible_values_sentence) { described_class.build(type_de_champ).possible_values_sentence }
context 'when the type de champ is not prefillable' do
let(:type_de_champ) { build(:type_de_champ_mesri) }
it { expect(possible_values_sentence).to be_empty }
end
context 'when there is too many possible values' do
let(:type_de_champ) { build(:type_de_champ_drop_down_list) }
before { type_de_champ.drop_down_options = (1..described_class::POSSIBLE_VALUES_THRESHOLD + 1).map(&:to_s) }
it { expect(possible_values_sentence).to match("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_sentence).to match("Une adresse email") }
end
end
describe '#example_value' do
subject(:example_value) { described_class.build(type_de_champ).example_value }