|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'rails_helper' |
| 4 | + |
| 5 | +RSpec.describe Salesforce::SchoolSyncJob do |
| 6 | + subject(:perform_job) { described_class.perform_now(school_id: school.id) } |
| 7 | + |
| 8 | + let(:school) { create(:school) } |
| 9 | + |
| 10 | + context 'when the job has run' do |
| 11 | + before { perform_job } |
| 12 | + |
| 13 | + it 'syncs all FIELD_MAPPINGS to the correct school values' do |
| 14 | + sf_school = Salesforce::School.find_by(editoruuid__c: school.id) |
| 15 | + described_class::FIELD_MAPPINGS.each do |sf_field, school_field| |
| 16 | + expected = Salesforce::School.type_for_attribute(sf_field).cast(school.send(school_field)) |
| 17 | + expect(sf_school.send(sf_field)).to eq(expected), |
| 18 | + "Expected #{sf_field} to equal school.#{school_field}" |
| 19 | + end |
| 20 | + end |
| 21 | + |
| 22 | + context 'when an address field is very long' do |
| 23 | + let(:school) { create(:school, address_line_1: '❌' * 300) } |
| 24 | + |
| 25 | + it 'truncates addressline1__c' do |
| 26 | + sf_school = Salesforce::School.find_by(editoruuid__c: school.id) |
| 27 | + expect(sf_school.addressline1__c).to end_with('…') |
| 28 | + expect(sf_school.addressline1__c.length).to be < school.address_line_1.length |
| 29 | + end |
| 30 | + end |
| 31 | + |
| 32 | + context 'when the school is verified' do |
| 33 | + let(:school) { create(:verified_school) } |
| 34 | + |
| 35 | + it 'syncs verifiedat__c' do |
| 36 | + sf_school = Salesforce::School.find_by(editoruuid__c: school.id) |
| 37 | + expect(sf_school.verifiedat__c).to eq(school.verified_at) |
| 38 | + end |
| 39 | + end |
| 40 | + |
| 41 | + context 'when the school is rejected' do |
| 42 | + let(:school) { create(:school, rejected_at: Time.current) } |
| 43 | + |
| 44 | + it 'syncs rejectedat__c' do |
| 45 | + sf_school = Salesforce::School.find_by(editoruuid__c: school.id) |
| 46 | + expect(sf_school.rejectedat__c).to eq(school.rejected_at) |
| 47 | + end |
| 48 | + end |
| 49 | + end |
| 50 | + |
| 51 | + context 'when the Salesforce school fails to save' do |
| 52 | + let(:sf_school) { instance_double(Salesforce::School) } |
| 53 | + |
| 54 | + before do |
| 55 | + allow(Salesforce::School).to receive(:find_or_initialize_by).with(editoruuid__c: school.id).and_return(sf_school) |
| 56 | + allow(sf_school).to receive(:attributes=) |
| 57 | + allow(sf_school).to receive(:save!).and_raise(ActiveRecord::RecordInvalid) |
| 58 | + end |
| 59 | + |
| 60 | + it 'raises an error' do |
| 61 | + expect { perform_job }.to raise_error ActiveRecord::RecordInvalid |
| 62 | + end |
| 63 | + end |
| 64 | + |
| 65 | + context 'when SALESFORCE_ENABLED is false' do |
| 66 | + around do |example| |
| 67 | + ClimateControl.modify(SALESFORCE_ENABLED: 'false') do |
| 68 | + example.run |
| 69 | + end |
| 70 | + end |
| 71 | + |
| 72 | + it 'discards the job without syncing' do |
| 73 | + perform_job |
| 74 | + expect(Salesforce::School.find_by(editoruuid__c: school.id)).to be_nil |
| 75 | + end |
| 76 | + end |
| 77 | +end |
0 commit comments