demarches-normaliennes/spec/models/expert_spec.rb

85 lines
2.3 KiB
Ruby
Raw Normal View History

2021-01-07 15:45:02 +01:00
RSpec.describe Expert, type: :model do
describe 'an expert could be add to a procedure' do
let(:procedure) { create(:procedure) }
2021-01-15 16:33:36 +01:00
let(:expert) { create(:expert) }
2021-01-07 15:45:02 +01:00
before do
procedure.experts << expert
procedure.reload
end
it { expect(procedure.experts).to eq([expert]) }
it { expect(ExpertsProcedure.where(expert: expert, procedure: procedure).count).to eq(1) }
it { expect(ExpertsProcedure.where(expert: expert, procedure: procedure).first.allow_decision_access).to be_falsy }
2021-01-07 15:45:02 +01:00
end
2021-10-07 14:34:01 +02:00
describe '#merge' do
let(:old_expert) { create(:expert) }
let(:new_expert) { create(:expert) }
subject { new_expert.merge(old_expert) }
context 'when the old expert does not exist' do
let(:old_expert) { nil }
it { expect { subject }.not_to raise_error }
end
2021-10-07 14:34:01 +02:00
context 'when an old expert access a procedure' do
let(:procedure) { create(:procedure) }
before do
procedure.experts << old_expert
subject
end
it 'transfers the access to the new expert' do
2021-10-12 11:03:34 +02:00
expect(procedure.reload.experts).to match_array(new_expert)
2021-10-07 14:34:01 +02:00
end
end
context 'when both expert access a procedure' do
let(:procedure) { create(:procedure) }
before do
procedure.experts << old_expert
procedure.experts << new_expert
subject
end
it 'removes the old one' do
expect(procedure.reload.experts). to match_array(new_expert)
end
end
context 'when an old expert has a commentaire' do
let(:dossier) { create(:dossier) }
2021-11-30 18:56:12 +01:00
let(:commentaire) { CommentaireService.create(old_expert, dossier, body: "Mon commentaire") }
2021-10-07 14:34:01 +02:00
before do
2021-11-30 18:56:12 +01:00
commentaire
2021-10-07 14:34:01 +02:00
subject
end
it 'transfers the commentaire to the new expert' do
expect(new_expert.reload.commentaires).to match_array(commentaire)
end
end
context 'when an old expert claims for an avis' do
let!(:avis) { create(:avis, dossier: create(:dossier), claimant: old_expert) }
before do
subject
end
it 'transfers the claim to the new expert' do
avis_claimed_by_new_expert = Avis
.where(claimant_id: new_expert.id, claimant_type: Expert.name)
expect(avis_claimed_by_new_expert).to match_array(avis)
end
end
end
2021-01-07 15:45:02 +01:00
end