|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'rails_helper' |
| 4 | + |
| 5 | +RSpec.describe Salesforce::ContactSyncJob do |
| 6 | + subject(:perform_job) { described_class.perform_now(school_id: school.id) } |
| 7 | + |
| 8 | + let(:school) { create(:school, creator_agree_to_ux_contact: true) } |
| 9 | + let!(:sf_contact) { create(:salesforce_contact, pi_accounts_unique_id__c: school.creator_id) } |
| 10 | + |
| 11 | + it 'sets experiencecsagreetouxcontact__c from school.creator_agree_to_ux_contact' do |
| 12 | + perform_job |
| 13 | + expect(sf_contact.reload.experiencecsagreetouxcontact__c).to be(true) |
| 14 | + end |
| 15 | + |
| 16 | + it 'saves the contact' do |
| 17 | + expect { perform_job }.not_to raise_error |
| 18 | + end |
| 19 | + |
| 20 | + context 'when the Contact is not found in Salesforce' do |
| 21 | + before { sf_contact.destroy } |
| 22 | + |
| 23 | + it 'retries the job' do |
| 24 | + expect { perform_job }.to have_enqueued_job(described_class).with(school_id: school.id) |
| 25 | + end |
| 26 | + end |
| 27 | + |
| 28 | + context 'when the Salesforce contact fails to save' do |
| 29 | + let(:sf_contact_double) { instance_double(Salesforce::Contact) } |
| 30 | + |
| 31 | + before do |
| 32 | + allow(Salesforce::Contact).to receive(:find_by) |
| 33 | + .with(pi_accounts_unique_id__c: school.creator_id) |
| 34 | + .and_return(sf_contact_double) |
| 35 | + allow(sf_contact_double).to receive(:experiencecsagreetouxcontact__c=) |
| 36 | + allow(sf_contact_double).to receive(:save!).and_raise(ActiveRecord::RecordInvalid) |
| 37 | + end |
| 38 | + |
| 39 | + it 'raises an error' do |
| 40 | + expect { perform_job }.to raise_error(ActiveRecord::RecordInvalid) |
| 41 | + end |
| 42 | + end |
| 43 | + |
| 44 | + context 'when SALESFORCE_ENABLED is false' do |
| 45 | + around do |example| |
| 46 | + ClimateControl.modify(SALESFORCE_ENABLED: 'false') do |
| 47 | + example.run |
| 48 | + end |
| 49 | + end |
| 50 | + |
| 51 | + it 'discards the job without syncing' do |
| 52 | + sf_contact.update!(experiencecsagreetouxcontact__c: false) |
| 53 | + perform_job |
| 54 | + expect(sf_contact.reload.experiencecsagreetouxcontact__c).to be(false) |
| 55 | + end |
| 56 | + end |
| 57 | +end |
0 commit comments