Skip to content

Commit

Permalink
Refactor mailer tests
Browse files Browse the repository at this point in the history
  • Loading branch information
floganz committed Dec 16, 2024
1 parent 3fb776d commit 32d13e3
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 26 deletions.
2 changes: 1 addition & 1 deletion app/services/data_exports/csv/invoice_fees.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def serialized_item(fee)

serialized_subscription = {
external_id: fee.subscription&.external_id,
plan_code: fee.subscription&.plan&.code,
plan_code: fee.subscription&.plan&.code
}

[
Expand Down
66 changes: 41 additions & 25 deletions spec/mailers/data_export_mailer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,48 +3,64 @@
require 'rails_helper'

RSpec.describe DataExportMailer, type: :mailer do
subject(:data_export_mailer) { described_class }

let(:data_export) { create(:data_export, :completed) }

describe '#completed' do
let(:mailer) { data_export_mailer.with(data_export:).completed }
let(:mail) { described_class.with(data_export:).completed }
let(:file_url) { "https://api.lago.dev/rails/active_storage/blobs/redirect/eyJf" }

before do
allow(data_export).to receive(:file_url).and_return(file_url)
before { allow(data_export).to receive(:file_url).and_return(file_url) }

describe 'subject' do
subject { mail.subject }

context 'with invoice data export' do
let(:data_export) { create(:data_export, :completed, resource_type: 'invoices') }

it { is_expected.to eq 'Your Lago invoices export is ready!' }
end

context 'with invoice fee data export' do
let(:data_export) { create(:data_export, :completed, resource_type: 'invoice_fees') }

it { is_expected.to eq 'Your Lago invoice fees export is ready!' }
end
end

specify do
expect(mailer.to).to eq([data_export.user.email])
expect(mailer.subject).to eq("Your Lago invoices export is ready!")
expect(mailer.body.encoded).to match("Your invoices export is ready!")
expect(mailer.body.encoded).to match("will be available for 7 days")
expect(mailer.body.encoded).to match(data_export.file_url)
describe 'recipients' do
subject { mail.to }

it { is_expected.to eq [data_export.user.email] }
end

context "when the resource type is invoice_fees" do
let(:data_export) { create(:data_export, :completed, resource_type: 'invoice_fees') }
describe 'body' do
subject { mail.body.to_s }

specify do
expect(mailer.subject).to eq("Your Lago invoice fees export is ready!")
expect(mailer.body.encoded).to match("Your invoice fees export is ready!")
it 'includes expiration notice and link to file', :aggregate_failures do
expect(subject).to match('will be available for 7 days')
expect(subject).to match(data_export.file_url)
end
end

context 'when data export is expired' do
let(:data_export) { create(:data_export, :expired) }
describe 'delivery' do
subject { mail.deliver_now }

it 'returns a mailer with nil values' do
expect(mailer.to).to be_nil
let(:deliveries) { ActionMailer::Base.deliveries }

context 'when data export is not completed' do
let(:data_export) { create(:data_export, :processing) }

it 'is not performed' do
expect { subject }.not_to change(deliveries, :count)
end
end
end

context 'when data export is not completed' do
let(:data_export) { create(:data_export, :processing) }
context 'when data export is completed' do
let(:data_export) { create(:data_export, :completed) }

it 'returns a mailer with nil values' do
expect(mailer.to).to be_nil
it 'is performed' do
expect { subject }.to change(deliveries, :count).by(1)
end
end
end
end
Expand Down

0 comments on commit 32d13e3

Please sign in to comment.