|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'rails_helper' |
| 4 | + |
| 5 | +RSpec.describe Pwb::Zoho::SyncActivityJob, type: :job do |
| 6 | + include ActiveJob::TestHelper |
| 7 | + |
| 8 | + let(:website) { create(:pwb_website) } |
| 9 | + let(:user) { create(:pwb_user, website: website, metadata: { 'zoho_lead_id' => 'lead_123' }) } |
| 10 | + let(:mock_client) { instance_double(Pwb::Zoho::Client) } |
| 11 | + let(:mock_service) { instance_double(Pwb::Zoho::LeadSyncService) } |
| 12 | + |
| 13 | + before do |
| 14 | + allow(Pwb::Zoho::Client).to receive(:instance).and_return(mock_client) |
| 15 | + allow(Pwb::Zoho::LeadSyncService).to receive(:new).and_return(mock_service) |
| 16 | + end |
| 17 | + |
| 18 | + describe '#perform' do |
| 19 | + context 'when Zoho is enabled' do |
| 20 | + before do |
| 21 | + allow(mock_client).to receive(:configured?).and_return(true) |
| 22 | + allow(mock_service).to receive(:log_activity) |
| 23 | + end |
| 24 | + |
| 25 | + it 'calls the lead sync service with activity details' do |
| 26 | + expect(mock_service).to receive(:log_activity) |
| 27 | + .with(user, 'property_added', { title: 'Beach House', reference: 'BH-001' }) |
| 28 | + |
| 29 | + described_class.perform_now(user.id, 'property_added', { 'title' => 'Beach House', 'reference' => 'BH-001' }) |
| 30 | + end |
| 31 | + |
| 32 | + it 'symbolizes string keys in details hash' do |
| 33 | + expect(mock_service).to receive(:log_activity) |
| 34 | + .with(user, 'logo_uploaded', { filename: 'logo.png' }) |
| 35 | + |
| 36 | + described_class.perform_now(user.id, 'logo_uploaded', { 'filename' => 'logo.png' }) |
| 37 | + end |
| 38 | + |
| 39 | + it 'handles empty details hash' do |
| 40 | + expect(mock_service).to receive(:log_activity) |
| 41 | + .with(user, 'first_property', {}) |
| 42 | + |
| 43 | + described_class.perform_now(user.id, 'first_property', {}) |
| 44 | + end |
| 45 | + |
| 46 | + it 'handles nil details by converting to empty hash' do |
| 47 | + # nil.to_h returns {} but nil&.symbolize_keys raises |
| 48 | + # The job handles this by defaulting to {} in the method signature |
| 49 | + expect(mock_service).to receive(:log_activity) |
| 50 | + .with(user, 'login', {}) |
| 51 | + |
| 52 | + # Pass empty hash instead of nil since nil&.symbolize_keys would fail |
| 53 | + described_class.perform_now(user.id, 'login', {}) |
| 54 | + end |
| 55 | + end |
| 56 | + |
| 57 | + context 'when Zoho is not enabled' do |
| 58 | + before do |
| 59 | + allow(mock_client).to receive(:configured?).and_return(false) |
| 60 | + end |
| 61 | + |
| 62 | + it 'does not call the sync service' do |
| 63 | + expect(mock_service).not_to receive(:log_activity) |
| 64 | + |
| 65 | + described_class.perform_now(user.id, 'property_added', {}) |
| 66 | + end |
| 67 | + end |
| 68 | + |
| 69 | + context 'when user is not found' do |
| 70 | + before do |
| 71 | + allow(mock_client).to receive(:configured?).and_return(true) |
| 72 | + end |
| 73 | + |
| 74 | + it 'logs a warning and returns early' do |
| 75 | + expect(mock_service).not_to receive(:log_activity) |
| 76 | + |
| 77 | + described_class.perform_now(999_999, 'property_added', {}) |
| 78 | + end |
| 79 | + end |
| 80 | + |
| 81 | + context 'when user has no Zoho lead ID' do |
| 82 | + let(:user_without_zoho) { create(:pwb_user, website: website, metadata: {}) } |
| 83 | + |
| 84 | + before do |
| 85 | + allow(mock_client).to receive(:configured?).and_return(true) |
| 86 | + end |
| 87 | + |
| 88 | + it 'skips activity logging' do |
| 89 | + expect(mock_service).not_to receive(:log_activity) |
| 90 | + |
| 91 | + described_class.perform_now(user_without_zoho.id, 'property_added', {}) |
| 92 | + end |
| 93 | + end |
| 94 | + end |
| 95 | + |
| 96 | + describe 'job enqueueing' do |
| 97 | + it 'can be enqueued with user ID, activity type, and details' do |
| 98 | + expect do |
| 99 | + described_class.perform_later(user.id, 'property_added', { title: 'Test Property' }) |
| 100 | + end.to have_enqueued_job(described_class).with(user.id, 'property_added', { title: 'Test Property' }) |
| 101 | + end |
| 102 | + |
| 103 | + it 'uses the zoho_sync queue' do |
| 104 | + expect do |
| 105 | + described_class.perform_later(user.id, 'property_added', {}) |
| 106 | + end.to have_enqueued_job.on_queue('zoho_sync') |
| 107 | + end |
| 108 | + end |
| 109 | +end |
0 commit comments