From a6ea975848d2d1e1c4e0112af60d2af013a48ffd Mon Sep 17 00:00:00 2001 From: luisramos0 Date: Mon, 27 May 2019 18:42:29 +0100 Subject: [PATCH] Fix editing cart when variant on_demand but on_hand is zero or negative --- app/views/spree/orders/_line_item.html.haml | 2 +- spec/features/consumer/shopping/cart_spec.rb | 50 ++++++++++++-------- 2 files changed, 32 insertions(+), 20 deletions(-) diff --git a/app/views/spree/orders/_line_item.html.haml b/app/views/spree/orders/_line_item.html.haml index ac1396fd14..14ef285565 100644 --- a/app/views/spree/orders/_line_item.html.haml +++ b/app/views/spree/orders/_line_item.html.haml @@ -22,7 +22,7 @@ %td.text-right.cart-item-price{"data-hook" => "cart_item_price"} = line_item.single_display_amount_with_adjustments.to_html %td.text-center.cart-item-quantity{"data-hook" => "cart_item_quantity"} - = item_form.number_field :quantity, :min => 0, "ofn-on-hand" => variant.on_hand, "ng-model" => "line_item_#{line_item.id}", :class => "line_item_quantity", :size => 5 + = item_form.number_field :quantity, :min => 0, "ofn-on-hand" => "#{variant.on_demand && 9999 || variant.on_hand}", "ng-model" => "line_item_#{line_item.id}", :class => "line_item_quantity", :size => 5 %td.cart-item-total.text-right{"data-hook" => "cart_item_total"} = line_item.display_amount_with_adjustments.to_html unless line_item.quantity.nil? diff --git a/spec/features/consumer/shopping/cart_spec.rb b/spec/features/consumer/shopping/cart_spec.rb index 6e1493be14..d6a15263ea 100644 --- a/spec/features/consumer/shopping/cart_spec.rb +++ b/spec/features/consumer/shopping/cart_spec.rb @@ -126,7 +126,7 @@ feature "full-page cart", js: true do end end - describe "updating quantities with insufficient stock available" do + describe "updating quantities" do let(:li) { order.line_items(true).last } let(:variant) { product_with_tax.variants.first } @@ -134,31 +134,43 @@ feature "full-page cart", js: true do add_product_to_cart order, product_with_tax end - it "prevents me from entering an invalid value" do - # Given we have 2 on hand, and we've loaded the page after that fact - variant.update_attributes!(on_hand: 2, on_demand: false) - visit main_app.cart_path + describe "when on_hand is zero but variant is on demand" do + it "allows updating the quantity" do + variant.update_attributes!(on_hand: 0, on_demand: true) + visit main_app.cart_path - accept_alert 'Insufficient stock available, only 2 remaining' do - fill_in "order_line_items_attributes_0_quantity", with: '4' + fill_in "order_line_items_attributes_0_quantity", with: '5' + expect(page).to have_field "order_line_items_attributes_0_quantity", with: '5' end - expect(page).to have_field "order_line_items_attributes_0_quantity", with: '2' end - it "shows the quantities saved, not those submitted" do - # Given we load the page with 3 on hand, then the number available drops to 2 - variant.update_attributes! on_demand: false - variant.update_attributes! on_hand: 3 - visit main_app.cart_path - variant.update_attributes! on_hand: 2 + describe "with insufficient stock available" do + it "prevents user from entering an invalid value" do + # Given we have 2 on hand, and we've loaded the page after that fact + variant.update_attributes!(on_hand: 2, on_demand: false) + visit main_app.cart_path - accept_alert do - fill_in "order_line_items_attributes_0_quantity", with: '4' + accept_alert 'Insufficient stock available, only 2 remaining' do + fill_in "order_line_items_attributes_0_quantity", with: '4' + end + expect(page).to have_field "order_line_items_attributes_0_quantity", with: '2' end - click_button 'Update' - expect(page).to have_content "Insufficient stock available, only 2 remaining" - expect(page).to have_field "order_line_items_attributes_0_quantity", with: '1' + it "shows the quantities saved, not those submitted" do + # Given we load the page with 3 on hand, then the number available drops to 2 + variant.update_attributes! on_demand: false + variant.update_attributes! on_hand: 3 + visit main_app.cart_path + variant.update_attributes! on_hand: 2 + + accept_alert do + fill_in "order_line_items_attributes_0_quantity", with: '4' + end + click_button 'Update' + + expect(page).to have_content "Insufficient stock available, only 2 remaining" + expect(page).to have_field "order_line_items_attributes_0_quantity", with: '1' + end end end