|
5 | 5 | RSpec.describe Salesforce::SchoolSyncJob do |
6 | 6 | subject(:perform_job) { described_class.perform_now(school_id: school.id) } |
7 | 7 |
|
8 | | - context 'when the job has run' do |
9 | | - let(:school) { create(:school) } |
10 | | - let(:sf_school) { Salesforce::School.find(school.id) } |
| 8 | + let(:school) { create(:school) } |
| 9 | + let(:sf_school) { instance_double(Salesforce::School) } |
| 10 | + let(:synced_attributes) { {} } |
| 11 | + |
| 12 | + before do |
| 13 | + allow(Salesforce::School).to receive(:find_or_initialize_by).with(editoruuid__c: school.id).and_return(sf_school) |
| 14 | + allow(sf_school).to receive(:attributes=) { |attrs| synced_attributes.merge!(attrs) } |
| 15 | + allow(sf_school).to receive(:save!) |
| 16 | + end |
11 | 17 |
|
| 18 | + context 'when the job has run' do |
12 | 19 | before { perform_job } |
13 | 20 |
|
14 | | - it 'syncs the standard mappings' do |
15 | | - described_class::FIELD_MAPPINGS.each do |sf_field, field| |
16 | | - sf_value = sf_school.send(sf_field) |
17 | | - school_value = school.send(field) |
18 | | - column = Salesforce::School.column_for_attribute(sf_field) |
19 | | - ar_type = Salesforce::School.type_for_attribute(sf_field.to_s) |
20 | | - |
21 | | - expected = if school_value.is_a?(String) && column.limit |
22 | | - school_value.truncate(column.limit, omission: '…') |
23 | | - else |
24 | | - ar_type.cast(school_value) |
25 | | - end |
26 | | - |
27 | | - expect(sf_value).to eq(expected), "Expected #{sf_field} to be #{expected.inspect} but was #{sf_value.inspect}" |
| 21 | + it 'syncs all FIELD_MAPPINGS to the correct school values' do |
| 22 | + described_class::FIELD_MAPPINGS.each do |sf_field, school_field| |
| 23 | + expect(synced_attributes[sf_field]).to eq(school.send(school_field)), |
| 24 | + "Expected #{sf_field} to equal school.#{school_field}" |
28 | 25 | end |
29 | 26 | end |
30 | 27 |
|
31 | | - context 'when address fields are very long' do |
32 | | - let(:max_field_size) { Salesforce::School.column_for_attribute(:addressline1__c).limit } |
33 | | - let(:school) { create(:school, address_line_1: '❌' * (max_field_size + 10)) } |
| 28 | + it 'saves the Salesforce record' do |
| 29 | + expect(sf_school).to have_received(:save!) |
| 30 | + end |
| 31 | + |
| 32 | + context 'when an address field is very long' do |
| 33 | + let(:school) { create(:school, address_line_1: '❌' * 300) } |
34 | 34 |
|
35 | | - it 'truncates addressline1__c to the max field length' do |
36 | | - expect(sf_school.addressline1__c.size).to be <= max_field_size |
| 35 | + it 'truncates addressline1__c' do |
| 36 | + expect(synced_attributes[:addressline1__c]).to end_with('…') |
| 37 | + expect(synced_attributes[:addressline1__c].length).to be < school.address_line_1.length |
37 | 38 | end |
38 | 39 | end |
39 | 40 |
|
40 | 41 | context 'when the school is verified' do |
41 | 42 | let(:school) { create(:verified_school) } |
42 | 43 |
|
43 | 44 | it 'syncs verifiedat__c' do |
44 | | - expect(sf_school.verifiedat__c).to eq(school.verified_at) |
| 45 | + expect(synced_attributes[:verifiedat__c]).to eq(school.verified_at) |
45 | 46 | end |
46 | 47 | end |
47 | 48 |
|
48 | 49 | context 'when the school is rejected' do |
49 | 50 | let(:school) { create(:school, rejected_at: Time.current) } |
50 | 51 |
|
51 | 52 | it 'syncs rejectedat__c' do |
52 | | - expect(sf_school.rejectedat__c).to eq(school.rejected_at) |
| 53 | + expect(synced_attributes[:rejectedat__c]).to eq(school.rejected_at) |
53 | 54 | end |
54 | 55 | end |
55 | 56 | end |
56 | 57 |
|
57 | 58 | context 'when the Salesforce school fails to save' do |
58 | | - let(:school) { create(:school) } |
59 | | - let(:sf_school) { instance_double(Salesforce::School) } |
60 | | - |
61 | | - before do |
62 | | - allow(Salesforce::School).to receive(:find_or_initialize_by).with(editoruuid__c: school.id).and_return(sf_school) |
63 | | - allow(sf_school).to receive(:attributes=) |
64 | | - allow(sf_school).to receive(:save!).and_raise(ActiveRecord::RecordInvalid) |
65 | | - end |
| 59 | + before { allow(sf_school).to receive(:save!).and_raise(ActiveRecord::RecordInvalid) } |
66 | 60 |
|
67 | 61 | it 'raises an error' do |
68 | 62 | expect { perform_job }.to raise_error ActiveRecord::RecordInvalid |
69 | 63 | end |
70 | 64 | end |
71 | 65 |
|
72 | 66 | context 'when SALESFORCE_ENABLED is false' do |
73 | | - let(:school) { create(:school) } |
74 | | - |
75 | 67 | before { stub_const('ENV', ENV.to_h.merge('SALESFORCE_ENABLED' => 'false')) } |
76 | 68 |
|
77 | 69 | it 'discards the job without syncing' do |
78 | | - allow(Salesforce::School).to receive(:find_or_initialize_by) |
79 | 70 | perform_job |
80 | 71 | expect(Salesforce::School).not_to have_received(:find_or_initialize_by) |
81 | 72 | end |
|
0 commit comments