demarches-normaliennes/spec/jobs/champ_fetch_external_data_job_spec.rb
sebastiencarceles f52554b5a3 review: update value with async fetch
1 - spec cover the job which fetches external data

2 - refactor the job with guard clauses

3 - delegate update operation to the champ itself

4 - annuaire education: override the update operation
to let the value be populated by the fetched data

5 - prefilling: don't fetch data synchronously
2023-02-28 14:53:08 +01:00

55 lines
1.5 KiB
Ruby

# frozen_string_literal: true
require 'rails_helper'
RSpec.describe ChampFetchExternalDataJob, type: :job do
let(:champ) { Struct.new(:external_id, :data).new(champ_external_id, data) }
let(:external_id) { "an ID" }
let(:champ_external_id) { "an ID" }
let(:data) { nil }
let(:fetched_data) { nil }
subject(:perform_job) { described_class.perform_now(champ, external_id) }
before do
allow(champ).to receive(:fetch_external_data).and_return(fetched_data)
allow(champ).to receive(:update_with_external_data!)
end
shared_examples "a champ non-updater" do
it 'does not update the champ' do
perform_job
expect(champ).not_to have_received(:update_with_external_data!)
end
end
context 'when external_id matches the champ external_id and the champ data is nil' do
it 'fetches external data' do
perform_job
expect(champ).to have_received(:fetch_external_data)
end
context 'when the fetched data is present' do
let(:fetched_data) { "data" }
it 'updates the champ' do
perform_job
expect(champ).to have_received(:update_with_external_data!).with(data: fetched_data)
end
end
context 'when the fetched data is blank' do
it_behaves_like "a champ non-updater"
end
end
context 'when external_id does not match the champ external_id' do
let(:champ_external_id) { "something else" }
it_behaves_like "a champ non-updater"
end
context 'when the champ data is present' do
let(:data) { "present" }
it_behaves_like "a champ non-updater"
end
end