mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-24 20:36:49 +00:00
import BulkInvoiceJob#perform spec
1. remove the mocks 2. test the content of the generated pdf 3. remove the test of private methods
This commit is contained in:
@@ -4108,6 +4108,11 @@ See the %{link} to find out more about %{sitename}'s features and to start using
|
||||
shipping: "Shipping"
|
||||
order_number: "Order Number"
|
||||
invoice_number: "Invoice Number"
|
||||
payments_list:
|
||||
date_time: "Date/time"
|
||||
payment_method: "Payment method"
|
||||
payment_state: "Payment state"
|
||||
amount: "Amount"
|
||||
note:
|
||||
note_label: "Note:"
|
||||
no_note_present: "No note provided."
|
||||
|
||||
@@ -24,51 +24,29 @@ describe BulkInvoiceJob do
|
||||
end
|
||||
|
||||
describe "#perform" do
|
||||
let(:order1) { create(:order) }
|
||||
let(:order2) { create(:order) }
|
||||
let(:order3) { create(:order) }
|
||||
let!(:order1) { create(:shipped_order) }
|
||||
let!(:order2) { create(:order_with_line_items) }
|
||||
let!(:order3) { create(:order_ready_to_ship) }
|
||||
let(:order_ids) { [order1.id, order2.id, order3.id] }
|
||||
it "should generate invoices for invoiceable orders only" do
|
||||
expect(subject).to receive(:sorted_orders).with(order_ids).and_return([order1, order2,
|
||||
order3])
|
||||
expect(order1).to receive(:invoiceable?).and_return(true)
|
||||
expect(order2).to receive(:invoiceable?).and_return(false)
|
||||
expect(order3).to receive(:invoiceable?).and_return(true)
|
||||
|
||||
[order1, order3].each do |order|
|
||||
expect(subject).to receive(:generate_invoice).with(order)
|
||||
end
|
||||
expect(subject).not_to receive(:generate_invoice).with(order2)
|
||||
|
||||
subject.perform(order_ids, "/tmp/file/path")
|
||||
end
|
||||
end
|
||||
|
||||
describe "#generate_invoice" do
|
||||
let(:order) { create(:completed_order_with_totals) }
|
||||
let(:order_ids){ [order.id] }
|
||||
let!(:invoice_data_generator){ InvoiceDataGenerator.new(order) }
|
||||
let!(:invoice){
|
||||
create(:invoice, order:,
|
||||
data: invoice_data_generator.serialize_for_invoice)
|
||||
}
|
||||
let(:generator){ double("generator") }
|
||||
let(:renderer){ double("renderer") }
|
||||
let(:printed_invoice_string){ "printed invoice string" }
|
||||
|
||||
let(:path){ "/tmp/file/path.pdf" }
|
||||
before do
|
||||
expect(OrderInvoiceGenerator).to receive(:new).with(order).and_return(generator)
|
||||
expect(subject).to receive(:renderer).and_return(renderer)
|
||||
order3.cancel
|
||||
order3.resume
|
||||
end
|
||||
it "should generate invoices for invoiceable orders only" do
|
||||
expect{
|
||||
subject.perform(order_ids, path)
|
||||
}.to change{ order1.invoices.count }.from(0).to(1)
|
||||
.and change{ order2.invoices.count }.by(0)
|
||||
.and change{ order3.invoices.count }.from(0).to(1)
|
||||
|
||||
it "should call the renderer with the invoice presenter" do
|
||||
expect(generator).to receive(:generate_or_update_latest_invoice)
|
||||
expect(renderer).to receive(:render_to_string).with(invoice.presenter)
|
||||
.and_return(printed_invoice_string)
|
||||
expect(order).to receive(:invoices).and_return([invoice])
|
||||
expect(CombinePDF).to receive(:parse).with(printed_invoice_string)
|
||||
|
||||
subject.__send__(:generate_invoice, order)
|
||||
File.open(path, "rb") do |io|
|
||||
reader = PDF::Reader.new(io)
|
||||
content = reader.pages.map(&:text).join("\n")
|
||||
expect(content).to include(order1.number)
|
||||
expect(content).to include(order3.number)
|
||||
expect(content).not_to include(order2.number)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user