From 959e2308ddd61fa842a39962e91f2cceab5fe312 Mon Sep 17 00:00:00 2001 From: Gaetan Craig-Riou Date: Tue, 27 Jun 2023 14:50:48 +1000 Subject: [PATCH] Add system specs for percentage based voucher Similar to tax included in price scenario, adds a test for percentage based voucher to check the adjustments are recalculated when needed. Plus fix tax incluced in price specs to use new factory --- .../consumer/split_checkout_tax_incl_spec.rb | 4 +- .../split_checkout_tax_not_incl_spec.rb | 80 +++++++++++++++++-- 2 files changed, 76 insertions(+), 8 deletions(-) diff --git a/spec/system/consumer/split_checkout_tax_incl_spec.rb b/spec/system/consumer/split_checkout_tax_incl_spec.rb index ffff4e6e9b..bc0e9aa7e0 100644 --- a/spec/system/consumer/split_checkout_tax_incl_spec.rb +++ b/spec/system/consumer/split_checkout_tax_incl_spec.rb @@ -112,7 +112,7 @@ describe "As a consumer, I want to see adjustment breakdown" do before { Flipper.enable :vouchers } let!(:voucher) do - create(:voucher, code: 'some_code', enterprise: distributor, amount: 10) + create(:voucher_flat_rate, code: 'some_code', enterprise: distributor, amount: 10) end it "will include a tax included amount on the voucher adjustment" do @@ -148,7 +148,7 @@ describe "As a consumer, I want to see adjustment breakdown" do describe "moving between summary to summary via edit cart" do let!(:voucher) do - create(:voucher, code: 'good_code', enterprise: distributor, amount: 15) + create(:voucher_flat_rate, code: 'good_code', enterprise: distributor, amount: 15) end it "recalculate the tax component properly" do diff --git a/spec/system/consumer/split_checkout_tax_not_incl_spec.rb b/spec/system/consumer/split_checkout_tax_not_incl_spec.rb index 3e9982d26b..fc467ed7fa 100644 --- a/spec/system/consumer/split_checkout_tax_not_incl_spec.rb +++ b/spec/system/consumer/split_checkout_tax_not_incl_spec.rb @@ -118,7 +118,7 @@ describe "As a consumer, I want to see adjustment breakdown" do context "when using a voucher" do let!(:voucher) do - create(:voucher, code: 'some_code', enterprise: distributor, amount: 10) + create(:voucher_flat_rate, code: 'some_code', enterprise: distributor, amount: 10) end it "will include a tax included amount on the voucher adjustment" do @@ -151,12 +151,71 @@ describe "As a consumer, I want to see adjustment breakdown" do end # DB check - order_within_zone.reload - voucher_adjustment = order_within_zone.voucher_adjustments.first - voucher_tax_adjustment = order_within_zone.voucher_adjustments.second + #order_within_zone.reload + #voucher_adjustment = order_within_zone.voucher_adjustments.first + #voucher_tax_adjustment = order_within_zone.voucher_adjustments.second - expect(voucher_adjustment.amount.to_f).to eq(-8.85) - expect(voucher_tax_adjustment.amount.to_f).to eq(-1.15) + assert_db_voucher_adjustment(-8.85, -1.15) + end + + describe "moving between summary to summary via edit cart" do + let!(:voucher) do + create(:voucher_percentage, code: 'good_code', enterprise: distributor, amount: 20) + end + + it "recalculate the tax component properly" do + visit checkout_step_path(:details) + proceed_to_payment + + # add Voucher + fill_in "Enter voucher code", with: voucher.code + click_button("Apply") + + #pause + proceed_to_summary + assert_db_voucher_adjustment(-2.00, -0.26) + + # Click on edit link + within "div", text: /Order details/ do + # It's a bit brittle, but the scoping doesn't seem to work + all(".summary-edit").last.click + end + + # Update quantity + within ".cart-item-quantity" do + input = find(".line_item_quantity") + input.send_keys :up + end + + click_button("Update") + + # Check adjustment has been recalculated + assert_db_voucher_adjustment(-4.00, -0.52) + + within "#cart-container" do + click_link("Checkout") + end + + # Go back to payment step + proceed_to_payment + + # Check voucher is still there + expect(page).to have_content("20.0 % Voucher") + + # Go to summary + proceed_to_summary + + # Check voucher value + within ".summary-right" do + expect(page).to have_content "good_code" + expect(page).to have_content "-4" + expect(page).to have_content "Tax good_code" + expect(page).to have_content "-0.52" + end + + # Check adjustment has been recalculated, we are not expecting any changes here + assert_db_voucher_adjustment(-4.00, -0.52) + end end end end @@ -233,4 +292,13 @@ describe "As a consumer, I want to see adjustment breakdown" do end end end + + private + + def assert_db_voucher_adjustment(amount, tax_amount) + adjustment = order_within_zone.voucher_adjustments.first + tax_adjustment = order_within_zone.voucher_adjustments.second + expect(adjustment.amount.to_f).to eq(amount) + expect(tax_adjustment.amount.to_f).to eq(tax_amount) + end end