refactor(service): explicit call to service job to avoid test side effect

This commit is contained in:
simon lehericey 2022-10-25 14:35:36 +02:00
parent 0a1ab733b6
commit 6af4fe624a
4 changed files with 10 additions and 25 deletions

View file

@ -15,6 +15,8 @@ module Administrateurs
@service.administrateur = current_administrateur
if @service.save
@service.enqueue_api_entreprise
redirect_to admin_services_path(procedure_id: params[:procedure_id]),
notice: "#{@service.nom} créé"
else
@ -33,6 +35,10 @@ module Administrateurs
@service = service
if @service.update(service_params)
if @service.siret_previously_changed?
@service.enqueue_api_entreprise
end
redirect_to admin_services_path(procedure_id: params[:procedure_id]),
notice: "#{@service.nom} modifié"
else

View file

@ -45,8 +45,6 @@ class Service < ApplicationRecord
validates :adresse, presence: { message: 'doit être renseignée' }, allow_nil: false
validates :administrateur, presence: { message: 'doit être renseigné' }, allow_nil: false
after_commit :enqueue_api_entreprise, if: -> { siret_previously_changed? }
def clone_and_assign_to_administrateur(administrateur)
service_cloned = self.dup
service_cloned.administrateur = administrateur
@ -67,8 +65,6 @@ class Service < ApplicationRecord
[etablissement_lat, etablissement_lng]
end
private
def enqueue_api_entreprise
APIEntreprise::ServiceJob.perform_later(self.id)
end

View file

@ -34,7 +34,8 @@ describe Administrateurs::ServicesController, type: :controller do
it { expect(Service.last.telephone).to eq('1234') }
it { expect(Service.last.horaires).to eq('horaires') }
it { expect(Service.last.adresse).to eq('adresse') }
it { expect(Service.last.siret).to eq('35600082800018') }
it { expect(Service.last.siret).to eq('35600082800018') }
it { expect(APIEntreprise::ServiceJob).to have_been_enqueued.with(Service.last.id) }
it { expect(response).to redirect_to(admin_services_path(procedure_id: procedure.id)) }
end
@ -49,7 +50,7 @@ describe Administrateurs::ServicesController, type: :controller do
describe '#update' do
let!(:service) { create(:service, administrateur: admin) }
let(:service_params) { { nom: 'nom', type_organisme: Service.type_organismes.fetch(:association) } }
let(:service_params) { { nom: 'nom', type_organisme: Service.type_organismes.fetch(:association), siret: "13002526500013" } }
before do
sign_in(admin.user)
@ -67,6 +68,7 @@ describe Administrateurs::ServicesController, type: :controller do
it { expect(Service.last.nom).to eq('nom') }
it { expect(Service.last.type_organisme).to eq(Service.type_organismes.fetch(:association)) }
it { expect(response).to redirect_to(admin_services_path(procedure_id: procedure.id)) }
it { expect(APIEntreprise::ServiceJob).to have_been_enqueued.with(service.id) }
end
context 'when updating a service with invalid data' do

View file

@ -87,25 +87,6 @@ describe Service, type: :model do
end
end
describe "API Entreprise job" do
subject { create(:service) }
it "should enqueue a job when created" do
expect(APIEntreprise::ServiceJob).to have_been_enqueued.with(subject.id)
end
it "should enqueue a job when siret changed" do
subject.update(siret: "35600082800018")
expect(APIEntreprise::ServiceJob).to have_been_enqueued.with(subject.id)
end
it "should not enqueue a job when siret is unchanged" do
subject
clear_enqueued_jobs
subject.update(telephone: "09879789")
expect(APIEntreprise::ServiceJob).not_to have_been_enqueued
end
end
describe "etablissement adresse & geo coordinates" do
subject { create(:service, etablissement_lat: latitude, etablissement_lng: longitude, etablissement_infos: etablissement_infos) }