From 9c330f9616bcff313956b39093c341419c4da0ea Mon Sep 17 00:00:00 2001 From: Gaetan Craig-Riou Date: Fri, 30 Jun 2023 14:22:42 +1000 Subject: [PATCH] Add specs to cover re calculation 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 --- .../voucher_adjustments_service_spec.rb | 67 +++++++++++++++++-- 1 file changed, 61 insertions(+), 6 deletions(-) diff --git a/spec/services/voucher_adjustments_service_spec.rb b/spec/services/voucher_adjustments_service_spec.rb index e84286c3b735..b142821983a8 100644 --- a/spec/services/voucher_adjustments_service_spec.rb +++ b/spec/services/voucher_adjustments_service_spec.rb @@ -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 @@ -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 @@ -84,7 +118,7 @@ end it 'includes amount without tax' do - adjustment = order.voucher_adjustments.first + #adjustment = order.voucher_adjustments.first # voucher_rate = amount / order.total # -10 / 171 = -0.058479532 # amount = voucher_rate * (order.total - order.additional_tax_total) @@ -97,19 +131,40 @@ # -10 / 171 = -0.058479532 # amount = voucher_rate * order.additional_tax_total # -0.058479532 * 11 = -0.64 - tax_adjustment = order.voucher_adjustments.second + #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