From 904a3a5bd4b0fd3e55c8755472d622909507e7cd Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Wed, 15 Jul 2015 15:40:59 +1000 Subject: [PATCH 1/2] When add to cart fails, retry every 3 seconds, with no limit of the number of retries --- .../darkswarm/services/cart.js.coffee | 8 ++++- .../darkswarm/services/cart_spec.js.coffee | 29 ++++++++++++++++++- 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/app/assets/javascripts/darkswarm/services/cart.js.coffee b/app/assets/javascripts/darkswarm/services/cart.js.coffee index bbb0b5fab4..286b22c770 100644 --- a/app/assets/javascripts/darkswarm/services/cart.js.coffee +++ b/app/assets/javascripts/darkswarm/services/cart.js.coffee @@ -20,7 +20,7 @@ Darkswarm.factory 'Cart', (CurrentOrder, Variants, $timeout, $http)-> $http.post('/orders/populate', @data()).success (data, status)=> @saved() .error (response, status)=> - # TODO what shall we do here? + @scheduleRetry() data: => variants = {} @@ -30,6 +30,12 @@ Darkswarm.factory 'Cart', (CurrentOrder, Variants, $timeout, $http)-> max_quantity: li.max_quantity {variants: variants} + scheduleRetry: => + console.log "Error updating cart: #{status}. Retrying in 3 seconds..." + $timeout => + console.log "Retrying cart update" + @orderChanged() + , 3000 saved: => @dirty = false diff --git a/spec/javascripts/unit/darkswarm/services/cart_spec.js.coffee b/spec/javascripts/unit/darkswarm/services/cart_spec.js.coffee index 4be1a13dd8..cc65f043cd 100644 --- a/spec/javascripts/unit/darkswarm/services/cart_spec.js.coffee +++ b/spec/javascripts/unit/darkswarm/services/cart_spec.js.coffee @@ -3,6 +3,8 @@ describe 'Cart service', -> Variants = null variant = null order = null + $httpBackend = null + $timeout = null beforeEach -> module 'Darkswarm' @@ -16,9 +18,11 @@ describe 'Cart service', -> ] } angular.module('Darkswarm').value('currentOrder', order) - inject ($injector)-> + inject ($injector, _$httpBackend_, _$timeout_)-> Variants = $injector.get("Variants") Cart = $injector.get("Cart") + $httpBackend = _$httpBackend_ + $timeout = _$timeout_ it "backreferences line items", -> expect(Cart.line_items[0].variant.line_item).toBe Cart.line_items[0] @@ -44,6 +48,29 @@ describe 'Cart service', -> order.line_items[0].quantity = 2 expect(Cart.total_item_count()).toEqual 2 + describe "updating the cart", -> + data = {variants: {}} + + it "marks the form as saved on success", -> + spyOn(Cart, 'saved') + $httpBackend.expectPOST("/orders/populate", data).respond 200, {} + Cart.update() + $httpBackend.flush() + expect(Cart.saved).toHaveBeenCalled() + + it "retries the update on failure", -> + spyOn(Cart, 'scheduleRetry') + $httpBackend.expectPOST("/orders/populate", data).respond 404, {} + Cart.update() + $httpBackend.flush() + expect(Cart.scheduleRetry).toHaveBeenCalled() + + it "schedules retries of updates", -> + spyOn(Cart, 'orderChanged') + Cart.scheduleRetry() + $timeout.flush() + expect(Cart.orderChanged).toHaveBeenCalled() + describe "generating an extended variant name", -> it "returns the product name when it is the same as the variant name", -> variant = {product_name: 'product_name', name_to_display: 'product_name'} From f3ae812f2b0bbe72956edc64bf7884ac2390f9d6 Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Wed, 15 Jul 2015 16:02:25 +1000 Subject: [PATCH 2/2] Update cart when max_quantity value changes, not just quantity --- .../darkswarm/controllers/line_item_controller.js.coffee | 3 ++- spec/features/consumer/shopping/shopping_spec.rb | 9 +++++++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/app/assets/javascripts/darkswarm/controllers/line_item_controller.js.coffee b/app/assets/javascripts/darkswarm/controllers/line_item_controller.js.coffee index ea62163868..864f25177a 100644 --- a/app/assets/javascripts/darkswarm/controllers/line_item_controller.js.coffee +++ b/app/assets/javascripts/darkswarm/controllers/line_item_controller.js.coffee @@ -1,4 +1,5 @@ Darkswarm.controller "LineItemCtrl", ($scope)-> - $scope.$watch "line_item.quantity", (newValue, oldValue)-> + $scope.$watch '[line_item.quantity, line_item.max_quantity]', (newValue, oldValue)-> if newValue != oldValue $scope.Cart.orderChanged() + , true \ No newline at end of file diff --git a/spec/features/consumer/shopping/shopping_spec.rb b/spec/features/consumer/shopping/shopping_spec.rb index 2ab060f66f..5f2ae3d6c4 100644 --- a/spec/features/consumer/shopping/shopping_spec.rb +++ b/spec/features/consumer/shopping/shopping_spec.rb @@ -134,15 +134,20 @@ feature "As a consumer I want to shop with a distributor", js: true do end it "should save group buy data to the cart" do + # -- Quantity fill_in "variants[#{variant.id}]", with: 6 - fill_in "variant_attributes[#{variant.id}][max_quantity]", with: 7 page.should have_in_cart product.name + wait_until { !cart_dirty } + li = Spree::Order.order(:created_at).last.line_items.order(:created_at).last + li.quantity.should == 6 + + # -- Max quantity + fill_in "variant_attributes[#{variant.id}][max_quantity]", with: 7 wait_until { !cart_dirty } li = Spree::Order.order(:created_at).last.line_items.order(:created_at).last li.max_quantity.should == 7 - li.quantity.should == 6 end end end