From cc55e9eeda2b2aea168853e4ce0e91928f945428 Mon Sep 17 00:00:00 2001 From: Matt-Yorkley <9029026+Matt-Yorkley@users.noreply.github.com> Date: Fri, 19 Feb 2021 00:02:16 +0000 Subject: [PATCH] Improve coverage in orders_controller_spec --- .../spree/orders_controller_spec.rb | 37 ++++++++++++++++--- 1 file changed, 31 insertions(+), 6 deletions(-) diff --git a/spec/controllers/spree/orders_controller_spec.rb b/spec/controllers/spree/orders_controller_spec.rb index 55cf94470f..2f77809ff1 100644 --- a/spec/controllers/spree/orders_controller_spec.rb +++ b/spec/controllers/spree/orders_controller_spec.rb @@ -257,16 +257,19 @@ describe Spree::OrdersController, type: :controller do context "with enterprise fees" do let(:user) { create(:user) } - let(:variant) { create(:variant) } + let(:variant1) { create(:variant) } + let(:variant2) { create(:variant) } let(:distributor) { create(:distributor_enterprise, allow_order_changes: true) } let(:order_cycle) { create(:simple_order_cycle, distributors: [distributor]) } let(:enterprise_fee) { create(:enterprise_fee, calculator: build(:calculator_per_item) ) } - let!(:exchange) { create(:exchange, incoming: true, sender: variant.product.supplier, receiver: order_cycle.coordinator, variants: [variant], enterprise_fees: [enterprise_fee]) } + let!(:exchange) { create(:exchange, incoming: true, sender: variant1.product.supplier, receiver: order_cycle.coordinator, variants: [variant1, variant2], enterprise_fees: [enterprise_fee]) } let!(:order) do - order = create(:completed_order_with_totals, line_items_count: 1, user: user, distributor: distributor, order_cycle: order_cycle) - order.reload.line_items.first.update(variant_id: variant.id) + order = create(:completed_order_with_totals, line_items_count: 2, user: user, distributor: distributor, order_cycle: order_cycle) + order.reload.line_items.first.update(variant_id: variant1.id) + order.reload.line_items.last.update(variant_id: variant2.id) while !order.completed? do break unless order.next! end order.recreate_all_fees! + order.update! order end let(:params) { @@ -281,12 +284,34 @@ describe Spree::OrdersController, type: :controller do end it "updates the fees" do - expect(order.reload.adjustment_total).to eq enterprise_fee.calculator.preferred_amount + expect(order.total).to eq order.item_total + (enterprise_fee.calculator.preferred_amount * 2) + expect(order.adjustment_total).to eq enterprise_fee.calculator.preferred_amount * 2 allow(controller).to receive_messages spree_current_user: user spree_post :update, params - expect(order.reload.adjustment_total).to eq enterprise_fee.calculator.preferred_amount * 2 + expect(order.total).to eq order.item_total + (enterprise_fee.calculator.preferred_amount * 3) + expect(order.adjustment_total).to eq enterprise_fee.calculator.preferred_amount * 3 + end + + context "when a line item is removed" do + let(:params) { + { order: { line_items_attributes: { + "0" => { id: order.line_items.first.id, quantity: 0 }, + "1" => { id: order.line_items.last.id, quantity: 1 } + } } } + } + + it "updates the fees" do + expect(order.total).to eq order.item_total + (enterprise_fee.calculator.preferred_amount * 2) + expect(order.adjustment_total).to eq enterprise_fee.calculator.preferred_amount * 2 + + allow(controller).to receive_messages spree_current_user: user + spree_post :update, params + + expect(order.total).to eq order.item_total + (enterprise_fee.calculator.preferred_amount * 1) + expect(order.adjustment_total).to eq enterprise_fee.calculator.preferred_amount * 1 + end end end end