Skip to content

Commit

Permalink
Add specs to cover re calculation
Browse files Browse the repository at this point in the history
It is important that the calculated voucher adjustments don't change
if they are recalculated and it is equally important that they are
updated if the order has changed
  • Loading branch information
rioug committed Jun 30, 2023
1 parent 82595e3 commit dddc886
Showing 1 changed file with 59 additions and 6 deletions.
65 changes: 59 additions & 6 deletions spec/services/voucher_adjustments_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,38 @@
# -0.0625 * 10 = -0.625
expect(subject.included_tax.to_f).to eq(-0.63)
end

context "when re calculating" do
it "does not update the adjustment amount" do
expect do
VoucherAdjustmentsService.calculate(order)
end.not_to change { subject.reload.amount }
end

it "does not update the tax adjustment" do
expect do
VoucherAdjustmentsService.calculate(order)
end.not_to change { subject.reload.included_tax }
end

context "when the order changed" do
before do
order.update_columns(item_total: 200)
end

it "does update the adjustment amount" do
expect do
VoucherAdjustmentsService.calculate(order)
end.not_to change { subject.reload.amount }
end

it "updates the tax adjustment" do
expect do
VoucherAdjustmentsService.calculate(order)
end.to change { subject.reload.included_tax }
end
end
end
end

context 'with tax not included in order price' do
Expand All @@ -70,6 +102,8 @@
tax_rate_name: "Tax 1"
)
end
let(:adjustment) { order.voucher_adjustments.first }
let(:tax_adjustment) { order.voucher_adjustments.second }

before do
# create adjustment before tax are set
Expand All @@ -84,7 +118,6 @@
end

it 'includes amount without tax' do
adjustment = order.voucher_adjustments.first
# voucher_rate = amount / order.total
# -10 / 171 = -0.058479532
# amount = voucher_rate * (order.total - order.additional_tax_total)
Expand All @@ -97,19 +130,39 @@
# -10 / 171 = -0.058479532
# amount = voucher_rate * order.additional_tax_total
# -0.058479532 * 11 = -0.64
tax_adjustment = order.voucher_adjustments.second
expect(tax_adjustment.amount.to_f).to eq(-0.64)
expect(tax_adjustment.label).to match("Tax")
end

context "when re calculating" do
it "updates the tax adjustment" do
order.update_columns(item_total: 200)
tax_adjustment = order.voucher_adjustments.second
it "does not update the adjustment amount" do
expect do
VoucherAdjustmentsService.calculate(order)
end.not_to change { adjustment.reload.amount }
end

it "does not update the tax adjustment" do
expect do
VoucherAdjustmentsService.calculate(order)
end.to change { tax_adjustment.reload.amount }
end.not_to change { tax_adjustment.reload.amount }
end

context "when the order changed" do
before do
order.update_columns(item_total: 200)
end

it "updates the adjustment amount" do
expect do
VoucherAdjustmentsService.calculate(order)
end.to change { adjustment.reload.amount }
end

it "updates the tax adjustment" do
expect do
VoucherAdjustmentsService.calculate(order)
end.to change { tax_adjustment.reload.amount }
end
end
end
end
Expand Down

0 comments on commit dddc886

Please sign in to comment.