add possible and example values

This commit is contained in:
Sébastien Carceles 2023-01-20 10:55:07 +01:00 committed by sebastiencarceles
parent cd3a72ddce
commit a235829ddd
4 changed files with 49 additions and 0 deletions

View file

@ -0,0 +1,15 @@
class TypesDeChamp::PrefillDepartementTypeDeChamp < TypesDeChamp::PrefillTypeDeChamp
def possible_values
departements.map { |departement| "#{departement[:code]} (#{departement[:name]})" }
end
def example_value
departements.pick(:code)
end
private
def departements
@departements ||= APIGeoService.departements.sort_by { |departement| departement[:code] }
end
end

View file

@ -9,6 +9,8 @@ class TypesDeChamp::PrefillTypeDeChamp < SimpleDelegator
TypesDeChamp::PrefillPaysTypeDeChamp.new(type_de_champ) TypesDeChamp::PrefillPaysTypeDeChamp.new(type_de_champ)
when TypeDeChamp.type_champs.fetch(:regions) when TypeDeChamp.type_champs.fetch(:regions)
TypesDeChamp::PrefillRegionTypeDeChamp.new(type_de_champ) TypesDeChamp::PrefillRegionTypeDeChamp.new(type_de_champ)
when TypeDeChamp.type_champs.fetch(:departements)
TypesDeChamp::PrefillDepartementTypeDeChamp.new(type_de_champ)
else else
new(type_de_champ) new(type_de_champ)
end end

View file

@ -0,0 +1,26 @@
# frozen_string_literal: true
RSpec.describe TypesDeChamp::PrefillDepartementTypeDeChamp, type: :model do
let(:type_de_champ) { build(:type_de_champ_departements) }
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 '#possible_values', vcr: { cassette_name: 'api_geo_departements' } do
let(:expected_values) {
APIGeoService.departements.sort_by { |departement| departement[:code] }.map { |departement| "#{departement[:code]} (#{departement[:name]})" }
}
subject(:possible_values) { described_class.new(type_de_champ).possible_values }
it { expect(possible_values).to match(expected_values) }
end
describe '#example_value', vcr: { cassette_name: 'api_geo_departements' } do
subject(:example_value) { described_class.new(type_de_champ).example_value }
it { expect(example_value).to eq(APIGeoService.departements.sort_by { |departement| departement[:code] }.first[:code]) }
end
end

View file

@ -22,6 +22,12 @@ RSpec.describe TypesDeChamp::PrefillTypeDeChamp, type: :model do
it { expect(built).to be_kind_of(TypesDeChamp::PrefillRegionTypeDeChamp) } it { expect(built).to be_kind_of(TypesDeChamp::PrefillRegionTypeDeChamp) }
end end
context 'when the type de champ is a departements' do
let(:type_de_champ) { build(:type_de_champ_departements) }
it { expect(built).to be_kind_of(TypesDeChamp::PrefillDepartementTypeDeChamp) }
end
context 'when any other type de champ' do context 'when any other type de champ' do
let(:type_de_champ) { build(:type_de_champ_date) } let(:type_de_champ) { build(:type_de_champ_date) }