|
5 | 5 | RSpec.describe Salesforce::RoleSyncJob do |
6 | 6 | subject(:perform_job) { described_class.perform_now(role_id: role.id) } |
7 | 7 |
|
8 | | - context 'when the job has run' do |
9 | | - let(:role) { create(:role) } |
10 | | - let(:sf_role) { Salesforce::Role.find(role.id) } |
| 8 | + let(:role) { create(:role) } |
| 9 | + let(:sf_role) { instance_double(Salesforce::Role) } |
| 10 | + let(:synced_attributes) { {} } |
| 11 | + |
| 12 | + before do |
| 13 | + allow(Salesforce::Role).to receive(:find_or_initialize_by).with(affiliation_id__c: role.id).and_return(sf_role) |
| 14 | + allow(sf_role).to receive(:attributes=) { |attrs| synced_attributes.merge!(attrs) } |
| 15 | + allow(sf_role).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_role.send(sf_field) |
17 | | - role_value = role.send(field) |
18 | | - column = Salesforce::Role.column_for_attribute(sf_field) |
19 | | - |
20 | | - expected = if role_value.is_a?(String) && column.limit |
21 | | - role_value.truncate(column.limit, omission: '…') |
22 | | - elsif column.type == :string |
23 | | - role_value.to_s |
24 | | - else |
25 | | - role_value |
26 | | - end |
27 | | - |
28 | | - 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 role values' do |
| 22 | + described_class::FIELD_MAPPINGS.each do |sf_field, role_field| |
| 23 | + expect(synced_attributes[sf_field]).to eq(role.send(role_field)), |
| 24 | + "Expected #{sf_field} to equal role.#{role_field}" |
29 | 25 | end |
30 | 26 | end |
| 27 | + |
| 28 | + it 'saves the Salesforce record' do |
| 29 | + expect(sf_role).to have_received(:save!) |
| 30 | + end |
31 | 31 | end |
32 | 32 |
|
33 | 33 | context 'when the Salesforce role fails to save' do |
34 | | - let(:role) { create(:role) } |
35 | | - let(:sf_role) { instance_double(Salesforce::Role) } |
36 | | - |
37 | | - before do |
38 | | - allow(Salesforce::Role).to receive(:find_or_initialize_by).with(affiliation_id__c: role.id).and_return(sf_role) |
39 | | - allow(sf_role).to receive(:attributes=) |
40 | | - allow(sf_role).to receive(:save!).and_raise(ActiveRecord::RecordInvalid) |
41 | | - end |
| 34 | + before { allow(sf_role).to receive(:save!).and_raise(ActiveRecord::RecordInvalid) } |
42 | 35 |
|
43 | 36 | it 'raises an error' do |
44 | 37 | expect { perform_job }.to raise_error ActiveRecord::RecordInvalid |
45 | 38 | end |
46 | 39 | end |
47 | 40 |
|
48 | 41 | context 'when SALESFORCE_ENABLED is false' do |
49 | | - let(:role) { create(:role) } |
50 | | - |
51 | 42 | before { stub_const('ENV', ENV.to_h.merge('SALESFORCE_ENABLED' => 'false')) } |
52 | 43 |
|
53 | 44 | it 'discards the job without syncing' do |
54 | | - allow(Salesforce::Role).to receive(:find_or_initialize_by) |
55 | 45 | perform_job |
56 | 46 | expect(Salesforce::Role).not_to have_received(:find_or_initialize_by) |
57 | 47 | end |
|
0 commit comments