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
This commit is contained in:
parent
8b25503f7e
commit
f52554b5a3
11 changed files with 127 additions and 54 deletions
42
spec/models/champs/annuaire_education_champ_spec.rb
Normal file
42
spec/models/champs/annuaire_education_champ_spec.rb
Normal file
|
@ -0,0 +1,42 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe Champs::AnnuaireEducationChamp do
|
||||
describe '#update_with_external_data!' do
|
||||
let(:champ) { create(:champ_annuaire_education, data: "any data") }
|
||||
subject { champ.update_with_external_data!(data: data) }
|
||||
|
||||
shared_examples "a data updater (without updating the value)" do |data|
|
||||
it { expect { subject }.to change { champ.reload.data }.to(data) }
|
||||
it { expect { subject }.not_to change { champ.reload.value } }
|
||||
end
|
||||
|
||||
context 'when data is nil' do
|
||||
let(:data) { nil }
|
||||
it_behaves_like "a data updater (without updating the value)", nil
|
||||
end
|
||||
|
||||
context 'when data is empty' do
|
||||
let(:data) { '' }
|
||||
it_behaves_like "a data updater (without updating the value)", ''
|
||||
end
|
||||
|
||||
context 'when data is inconsistent' do
|
||||
let(:data) { { 'yo' => 'lo' } }
|
||||
it_behaves_like "a data updater (without updating the value)", { 'yo' => 'lo' }
|
||||
end
|
||||
|
||||
context 'when data is consistent' do
|
||||
let(:data) {
|
||||
{
|
||||
'nom_etablissement': "karrigel an ankou",
|
||||
'nom_commune' => 'kumun',
|
||||
'identifiant_de_l_etablissement' => '666667'
|
||||
}.with_indifferent_access
|
||||
}
|
||||
it { expect { subject }.to change { champ.reload.data }.to(data) }
|
||||
it { expect { subject }.to change { champ.reload.value }.to('karrigel an ankou, kumun (666667)') }
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Add table
Add a link
Reference in a new issue