From d581fc4863aa76e988809806a53a1ee23b29a126 Mon Sep 17 00:00:00 2001 From: Enrico Stano Date: Fri, 14 Jul 2017 10:06:51 +0200 Subject: [PATCH] Fix specs to contemplate XHR requests and more coverage --- .../spree/admin/line_items_controller_spec.rb | 68 ++++++++++++++----- 1 file changed, 51 insertions(+), 17 deletions(-) diff --git a/spec/controllers/spree/admin/line_items_controller_spec.rb b/spec/controllers/spree/admin/line_items_controller_spec.rb index 0838e83b64..dc82b005b5 100644 --- a/spec/controllers/spree/admin/line_items_controller_spec.rb +++ b/spec/controllers/spree/admin/line_items_controller_spec.rb @@ -143,14 +143,15 @@ describe Spree::Admin::LineItemsController do end end - describe "#update" do + describe '#update' do let(:supplier) { create(:supplier_enterprise) } let(:distributor1) { create(:distributor_enterprise) } let(:coordinator) { create(:distributor_enterprise) } let(:order_cycle) { create(:simple_order_cycle, coordinator: coordinator) } let!(:order1) { FactoryGirl.create(:order, order_cycle: order_cycle, state: 'complete', completed_at: Time.zone.now, distributor: distributor1, billing_address: FactoryGirl.create(:address) ) } let!(:line_item1) { FactoryGirl.create(:line_item, order: order1, product: FactoryGirl.create(:product, supplier: supplier)) } - let(:params) { { format: :json, id: line_item1.id, order_id: order1.number, line_item: { quantity: 3, final_weight_volume: 3000, price: 3.00 } } } + let(:line_item_params) { { quantity: 3, final_weight_volume: 3000, price: 3.00 } } + let(:params) { { id: line_item1.id, order_id: order1.number, line_item: line_item_params } } context "as an enterprise user" do context "producer enterprise" do @@ -172,7 +173,7 @@ describe Spree::Admin::LineItemsController do end it "updates the line item" do - spree_put :update, params + xhr :put, :update, params line_item1.reload expect(line_item1.quantity).to eq 3 expect(line_item1.final_weight_volume).to eq 3000 @@ -180,20 +181,44 @@ describe Spree::Admin::LineItemsController do end it "returns an empty JSON response" do - spree_put :update, params.merge(format: :json) + xhr :put, :update, params expect(response.body).to eq ' ' end - it "returns an HTML response with the order form" do - spree_put :update, params.merge(format: :html) - expect(response.body).to match /admin_order_form_fields/ + it 'returns a 204 response' do + xhr :put, :update, params + expect(response.status).to eq 204 + end + + context 'when the line item params are not correct' do + let(:line_item_params) { { price: 'hola' } } + let(:errors) { { 'price' => ['is not a number'] } } + + it 'returns a JSON with the errors' do + xhr :put, :update, params + expect(JSON.parse(response.body)['errors']).to eq(errors) + end + + it 'returns a 412 response' do + xhr :put, :update, params + expect(response.status).to eq 412 + end + end + + context 'when the request is HTML' do + before { params.merge(format: :html) } + + it 'returns an HTML response with the order form' do + spree_put :update, params + expect(response.body).to match /admin_order_form_fields/ + end end end context "hub enterprise" do before do controller.stub spree_current_user: distributor1.owner - spree_put :update, params + xhr :put, :update, params end it "updates the line item" do @@ -206,7 +231,7 @@ describe Spree::Admin::LineItemsController do end end - describe "#destroy" do + describe '#destroy' do render_views let(:supplier) { create(:supplier_enterprise) } @@ -215,26 +240,35 @@ describe Spree::Admin::LineItemsController do let(:order_cycle) { create(:simple_order_cycle, coordinator: coordinator) } let!(:order1) { FactoryGirl.create(:order, order_cycle: order_cycle, state: 'complete', completed_at: Time.zone.now, distributor: distributor1, billing_address: FactoryGirl.create(:address) ) } let!(:line_item1) { FactoryGirl.create(:line_item, order: order1, product: FactoryGirl.create(:product, supplier: supplier)) } - let(:params) { { format: :json, id: line_item1.id, order_id: order1.number } } + let(:params) { { id: line_item1.id, order_id: order1.number } } before do controller.stub spree_current_user: coordinator.owner end - it "destroys the line item" do + it 'destroys the line item' do expect { - spree_delete :destroy, params + xhr :delete, :destroy, params }.to change { Spree::LineItem.where(id: line_item1).count }.from(1).to(0) end - it "returns an empty JSON response" do - spree_delete :destroy, params.merge(format: :json) + it 'returns an empty JSON response' do + xhr :delete, :destroy, params expect(response.body).to eq ' ' end - it "returns an HTML response with the order form" do - spree_delete :destroy, params.merge(format: :html) - expect(response.body).to match /admin_order_form_fields/ + it 'returns a 204 response' do + xhr :delete, :destroy, params + expect(response.status).to eq 204 + end + + context 'when the request is HTML' do + before { params.merge(format: :html) } + + it 'returns an HTML response with the order form' do + spree_delete :destroy, params + expect(response.body).to match /admin_order_form_fields/ + end end end end