Files
openfoodnetwork/spec/serializers/api/admin/order_serializer_spec.rb
Pau Perez 20a7f2f24e Eager load payment and subs. order associations
This removes the N+1 queries caused by
`Api::Admin::OrderSerialier#ready_to_capture` when used from
`Api::OrdersController#index`. While it's fine for the single-order
controller actions, it's not for this one that deals with a collection
of orders.

Fortunately, `SearchOrders` is used only in this controller action so we
can put the `includes` calls there, otherwise, we would need to refactor
it a bit to pass in a context-specific AR relation.
2021-02-23 10:26:12 -08:00

71 lines
1.7 KiB
Ruby

# frozen_string_literal: true
require "spec_helper"
describe Api::Admin::OrderSerializer do
let(:serializer) { described_class.new order }
describe "#display_outstanding_balance" do
let(:order) { create(:order) }
it "returns empty string" do
expect(serializer.display_outstanding_balance).to eql("")
end
context "with outstanding payments" do
let(:order) { create(:order_without_full_payment, unpaid_amount: 10) }
it "generates the outstanding balance" do
expect(serializer.display_outstanding_balance).to eql("$10.00")
end
end
context "with credit owed" do
let(:order) { create(:order_with_credit_payment, credit_amount: 20) }
it "generates the outstanding balance" do
expect(serializer.display_outstanding_balance).to eql("$-20.00")
end
end
end
describe '#ready_to_capture' do
let(:order) { create(:order) }
before do
allow(order).to receive(:payment_required?) { true }
end
context "there is a pending payment requiring authorization" do
let!(:pending_payment) do
create(
:payment,
order: order,
state: 'pending',
amount: 123.45,
cvv_response_message: "https://stripe.com/redirect"
)
end
it "returns false" do
expect(serializer.ready_to_capture).to be false
end
end
context "there is a pending payment but it does not require authorization" do
let!(:pending_payment) do
create(
:payment,
order: order,
state: 'pending',
amount: 123.45,
)
end
it "returns true" do
expect(serializer.ready_to_capture).to be true
end
end
end
end