Compare commits

...

1 Commits

Author SHA1 Message Date
Matt-Yorkley
f7b3813fbe Defend against nils in variant serializer
If unit_price.denominator ever returns nil, the serializer won't explode now.
2021-03-30 15:26:48 +01:00
2 changed files with 19 additions and 1 deletions

View File

@@ -41,7 +41,7 @@ class Api::VariantSerializer < ActiveModel::Serializer
end
def unit_price_price
price_with_fees / unit_price.denominator
price_with_fees / (unit_price.denominator || 1)
end
def unit_price_unit

View File

@@ -25,4 +25,22 @@ describe Api::VariantSerializer do
:tag_list # Used to apply tag rules
)
end
describe "#unit_price_price" do
context "without fees" do
it "displays the price divided by the unit price denominator" do
allow(subject).to receive_message_chain(:unit_price, :denominator) { 1000 }
expect(subject.unit_price_price).to eq(variant.price / 1000)
end
end
context "when the denominator returns nil" do
it "returns the price" do
allow(subject).to receive_message_chain(:unit_price, :denominator) { nil }
expect(subject.unit_price_price).to eq(variant.price)
end
end
end
end