demarches-normaliennes/spec/models/concern/dossier_prefillable_concern_spec.rb

65 lines
2.3 KiB
Ruby
Raw Normal View History

2022-11-28 15:54:14 +01:00
# frozen_string_literal: true
RSpec.describe DossierPrefillableConcern do
2022-11-28 15:54:14 +01:00
describe '.prefill!' do
let(:procedure) { create(:procedure, :published) }
let(:dossier) { create(:dossier, :brouillon, procedure: procedure) }
subject(:fill) { dossier.prefill!(values) }
context 'when champs_public_attributes is empty' do
let(:values) { [] }
it "does nothing" do
expect(dossier).not_to receive(:save)
fill
end
end
context 'when champs_public_attributes has values' do
context 'when the champs are valid' do
let!(:type_de_champ_1) { create(:type_de_champ_text, procedure: procedure) }
let(:value_1) { "any value" }
2022-12-01 11:17:48 +01:00
let(:champ_id_1) { find_champ_by_stable_id(dossier, type_de_champ_1.stable_id).id }
2022-11-28 15:54:14 +01:00
let!(:type_de_champ_2) { create(:type_de_champ_phone, procedure: procedure) }
let(:value_2) { "33612345678" }
2022-12-01 11:17:48 +01:00
let(:champ_id_2) { find_champ_by_stable_id(dossier, type_de_champ_2.stable_id).id }
2022-11-28 15:54:14 +01:00
let(:values) { [{ id: champ_id_1, value: value_1 }, { id: champ_id_2, value: value_2 }] }
2022-12-01 12:00:21 +01:00
it "updates the champs with the new values and mark them as prefilled" do
2022-11-28 15:54:14 +01:00
fill
2022-12-01 12:00:21 +01:00
2022-11-28 15:54:14 +01:00
expect(dossier.champs_public.first.value).to eq(value_1)
2022-12-01 12:00:21 +01:00
expect(dossier.champs_public.first.prefilled).to eq(true)
2022-11-28 15:54:14 +01:00
expect(dossier.champs_public.last.value).to eq(value_2)
2022-12-01 12:00:21 +01:00
expect(dossier.champs_public.last.prefilled).to eq(true)
2022-11-28 15:54:14 +01:00
end
end
context 'when a champ is invalid' do
let!(:type_de_champ) { create(:type_de_champ_phone, procedure: procedure) }
let(:value) { "a non phone value" }
2022-12-01 11:17:48 +01:00
let(:champ_id) { find_champ_by_stable_id(dossier, type_de_champ.stable_id).id }
2022-11-28 15:54:14 +01:00
let(:values) { [{ id: champ_id, value: value }] }
it "still updates the champ" do
expect { fill }.to change { dossier.champs_public.first.value }.from(nil).to(value)
end
2022-12-01 12:00:21 +01:00
it "still marks it as prefilled" do
2022-12-14 10:15:30 +01:00
expect { fill }.to change { dossier.champs_public.first.prefilled }.from(nil).to(true)
2022-12-01 12:00:21 +01:00
end
2022-11-28 15:54:14 +01:00
end
end
end
2022-12-01 11:17:48 +01:00
private
def find_champ_by_stable_id(dossier, stable_id)
dossier.champs_public.joins(:type_de_champ).find_by(types_de_champ: { stable_id: stable_id })
end
2022-11-28 15:54:14 +01:00
end