diff --git a/spec/controllers/spree/admin/adjustments_controller_spec.rb b/spec/controllers/spree/admin/adjustments_controller_spec.rb index b8e1a2bb13..1e6fe5f8a5 100644 --- a/spec/controllers/spree/admin/adjustments_controller_spec.rb +++ b/spec/controllers/spree/admin/adjustments_controller_spec.rb @@ -43,71 +43,110 @@ module Spree end end - describe "setting included tax" do + describe "setting the adjustment's tax" do let(:order) { create(:order) } - let(:tax_rate) { create(:tax_rate, amount: 0.1, calculator: ::Calculator::DefaultTax.new) } + let(:zone) { create(:zone_with_member) } + let(:tax_rate) { create(:tax_rate, amount: 0.1, zone: zone, included_in_price: true ) } describe "creating an adjustment" do - it "sets included tax to zero when no tax rate is specified" do - spree_post :create, order_id: order.number, - adjustment: { label: 'Testing included tax', amount: '110' }, tax_rate_id: '' - expect(response).to redirect_to spree.admin_order_adjustments_path(order) + let(:tax_category_param) { '' } + let(:params) { + { + order_id: order.number, + adjustment: { + label: 'Testing included tax', amount: '110', tax_category_id: tax_category_param + } + } + } - a = Adjustment.last - expect(a.label).to eq('Testing included tax') - expect(a.amount).to eq(110) - expect(a.included_tax).to eq(0) - expect(a.order_id).to eq(order.id) + context "when no tax category is specified" do + it "doesn't apply tax" do + spree_post :create, params + expect(response).to redirect_to spree.admin_order_adjustments_path(order) - expect(order.reload.total).to eq 110 + new_adjustment = Adjustment.admin.last + + expect(new_adjustment.label).to eq('Testing included tax') + expect(new_adjustment.amount).to eq(110) + expect(new_adjustment.tax_category).to be_nil + expect(new_adjustment.order_id).to eq(order.id) + + expect(order.reload.total).to eq 110 + expect(order.included_tax_total).to eq 0 + end end - it "calculates included tax when a tax rate is provided" do - spree_post :create, order_id: order.number, - adjustment: { label: 'Testing included tax', amount: '110' }, tax_rate_id: tax_rate.id.to_s - expect(response).to redirect_to spree.admin_order_adjustments_path(order) + context "when a tax category is provided" do + let(:tax_category_param) { tax_rate.tax_category.id.to_s } - a = Adjustment.last - expect(a.label).to eq('Testing included tax') - expect(a.amount).to eq(110) - expect(a.included_tax).to eq(10) - expect(a.order_id).to eq(order.id) + it "applies tax" do + spree_post :create, params + expect(response).to redirect_to spree.admin_order_adjustments_path(order) - expect(order.reload.total).to eq 110 + new_adjustment = Adjustment.admin.last + + expect(new_adjustment.label).to eq('Testing included tax') + expect(new_adjustment.amount).to eq(110) + expect(new_adjustment.tax_category).to eq tax_rate.tax_category + expect(new_adjustment.order_id).to eq(order.id) + + expect(order.reload.total).to eq 110 + expect(order.included_tax_total).to eq 10 + end end end describe "updating an adjustment" do + let(:old_tax_category) { create(:tax_category) } + let(:tax_category_param) { '' } + let(:params) { + { + id: adjustment.id, + order_id: order.number, + adjustment: { + label: 'Testing included tax', amount: '110', tax_category_id: tax_category_param + } + } + } let(:adjustment) { - create(:adjustment, adjustable: order, order: order, amount: 1100, included_tax: 100) + create(:adjustment, adjustable: order, order: order, + amount: 1100, tax_category: old_tax_category) } - it "sets included tax to zero when no tax rate is specified" do - spree_put :update, order_id: order.number, id: adjustment.id, - adjustment: { label: 'Testing included tax', amount: '110' }, tax_rate_id: '' - expect(response).to redirect_to spree.admin_order_adjustments_path(order) + context "when no tax category is specified" do + it "doesn't apply tax" do + spree_put :update, params + expect(response).to redirect_to spree.admin_order_adjustments_path(order) - a = Adjustment.last - expect(a.label).to eq('Testing included tax') - expect(a.amount).to eq(110) - expect(a.included_tax).to eq(0) - expect(a.order_id).to eq(order.id) + adjustment = Adjustment.admin.last - expect(order.reload.total).to eq 110 + expect(adjustment.label).to eq('Testing included tax') + expect(adjustment.amount).to eq(110) + expect(adjustment.tax_category).to be_nil + expect(adjustment.order_id).to eq(order.id) + + expect(order.reload.total).to eq 110 + expect(order.included_tax_total).to eq 0 + end end - it "calculates included tax when a tax rate is provided" do - spree_put :update, order_id: order.number, id: adjustment.id, - adjustment: { label: 'Testing included tax', amount: '110' }, tax_rate_id: tax_rate.id.to_s - expect(response).to redirect_to spree.admin_order_adjustments_path(order) + context "when a tax category is provided" do + let(:tax_category_param) { tax_rate.tax_category.id.to_s } - a = Adjustment.last - expect(a.label).to eq('Testing included tax') - expect(a.amount).to eq(110) - expect(a.included_tax).to eq(10) - expect(a.order_id).to eq(order.id) + it "applies tax" do + spree_put :update, params + expect(response).to redirect_to spree.admin_order_adjustments_path(order) - expect(order.reload.total).to eq 110 + adjustment = Adjustment.admin.last + + expect(adjustment.label).to eq('Testing included tax') + expect(adjustment.amount).to eq(110) + expect(adjustment.tax_category).to eq tax_rate.tax_category + expect(adjustment.order_id).to eq(order.id) + + expect(order.reload.total).to eq 110 + expect(order.included_tax_total).to eq 10 + end end end end @@ -151,7 +190,7 @@ module Spree let(:order) { create(:completed_order_with_totals) } let(:tax_rate) { create(:tax_rate, amount: 0.1, calculator: ::Calculator::DefaultTax.new) } let(:adjustment) { - create(:adjustment, adjustable: order, order: order, amount: 1100, included_tax: 100) + create(:adjustment, adjustable: order, order: order, amount: 1100) } before do @@ -161,7 +200,7 @@ module Spree it "doesn't create adjustments" do expect { spree_post :create, order_id: order.number, - adjustment: { label: "Testing", amount: "110" }, tax_rate_id: "" + adjustment: { label: "Testing", amount: "110" } }.to_not change { [Adjustment.count, order.reload.total] } expect(response).to redirect_to spree.admin_order_adjustments_path(order) @@ -170,7 +209,7 @@ module Spree it "doesn't change adjustments" do expect { spree_put :update, order_id: order.number, id: adjustment.id, - adjustment: { label: "Testing", amount: "110" }, tax_rate_id: "" + adjustment: { label: "Testing", amount: "110" } }.to_not change { [adjustment.reload.amount, order.reload.total] } expect(response).to redirect_to spree.admin_order_adjustments_path(order)