From faa1d0d1c5809f4b6abbaefa4d8627d5c03ca9de Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Fri, 31 Jul 2015 11:35:29 +1000 Subject: [PATCH] When cart clear is declined, revert order cycle selection change --- .../order_cycle_controller.js.coffee | 8 +++ .../directives/change_order_cycle.js.coffee | 3 + .../consumer/shopping/shopping_spec.rb | 69 +++++++++++++------ .../order_cycle_controller_spec.js.coffee | 10 ++- 4 files changed, 62 insertions(+), 28 deletions(-) diff --git a/app/assets/javascripts/darkswarm/controllers/order_cycle_controller.js.coffee b/app/assets/javascripts/darkswarm/controllers/order_cycle_controller.js.coffee index 9c2baccda9..42d4c1c44d 100644 --- a/app/assets/javascripts/darkswarm/controllers/order_cycle_controller.js.coffee +++ b/app/assets/javascripts/darkswarm/controllers/order_cycle_controller.js.coffee @@ -11,11 +11,19 @@ Darkswarm.controller "OrderCycleCtrl", ($scope, $timeout, OrderCycle) -> Darkswarm.controller "OrderCycleChangeCtrl", ($scope, $timeout, OrderCycle, Products, Variants, Cart) -> + # Track previous order cycle id for use with revertOrderCycle() + $scope.previous_order_cycle_id = OrderCycle.order_cycle.order_cycle_id + $scope.$watch 'order_cycle.order_cycle_id', (newValue, oldValue)-> + $scope.previous_order_cycle_id = oldValue + $scope.changeOrderCycle = -> OrderCycle.push_order_cycle $scope.orderCycleChanged $timeout -> $("#order_cycle_id").trigger("closeTrigger") + $scope.revertOrderCycle = -> + $scope.order_cycle.order_cycle_id = $scope.previous_order_cycle_id + $scope.orderCycleChanged = -> # push_order_cycle clears the cart server-side. Here we call Cart.clear() to clear the # client-side cart. diff --git a/app/assets/javascripts/darkswarm/directives/change_order_cycle.js.coffee b/app/assets/javascripts/darkswarm/directives/change_order_cycle.js.coffee index a98b5508e8..a747e5c219 100644 --- a/app/assets/javascripts/darkswarm/directives/change_order_cycle.js.coffee +++ b/app/assets/javascripts/darkswarm/directives/change_order_cycle.js.coffee @@ -15,5 +15,8 @@ Darkswarm.directive "ofnChangeOrderCycle", (OrderCycle, Cart, storage) -> if confirm "Are you sure? This will change your selected order cycle and remove any items in your shopping cart." Cart.clear() scope.changeOrderCycle() + else + scope.$apply -> + scope.revertOrderCycle() else scope.changeOrderCycle() diff --git a/spec/features/consumer/shopping/shopping_spec.rb b/spec/features/consumer/shopping/shopping_spec.rb index dafc8daad9..f1bed3a276 100644 --- a/spec/features/consumer/shopping/shopping_spec.rb +++ b/spec/features/consumer/shopping/shopping_spec.rb @@ -85,32 +85,57 @@ feature "As a consumer I want to shop with a distributor", js: true do modal_should_be_open_for product end - it "shows the correct fees after selecting and changing an order cycle" do - enterprise_fee = create(:enterprise_fee, amount: 1001) - exchange2.enterprise_fees << enterprise_fee - exchange2.variants << variant - exchange1.variants << variant + describe "changing order cycle" do + it "shows the correct fees after selecting and changing an order cycle" do + enterprise_fee = create(:enterprise_fee, amount: 1001) + exchange2.enterprise_fees << enterprise_fee + exchange2.variants << variant + exchange1.variants << variant - # -- Selecting an order cycle - visit shop_path - select "turtles", from: "order_cycle_id" - page.should have_content "$1020.99" + # -- Selecting an order cycle + visit shop_path + select "turtles", from: "order_cycle_id" + page.should have_content "$1020.99" - # -- Cart shows correct price - fill_in "variants[#{variant.id}]", with: 1 - show_cart - within("li.cart") { page.should have_content "$1020.99" } + # -- Cart shows correct price + fill_in "variants[#{variant.id}]", with: 1 + show_cart + within("li.cart") { page.should have_content "$1020.99" } - # -- Changing order cycle - select "frogs", from: "order_cycle_id" - page.should have_content "$19.99" + # -- Changing order cycle + select "frogs", from: "order_cycle_id" + page.should have_content "$19.99" - # -- Cart should be cleared - # ng-animate means that the old product row is likely to be present, so we explicitly - # fill in the quantity in the incoming row - page.should_not have_selector "tr.product-cart" - within('product.ng-enter') { fill_in "variants[#{variant.id}]", with: 1 } - within("li.cart") { page.should have_content "$19.99" } + # -- Cart should be cleared + # ng-animate means that the old product row is likely to be present, so we explicitly + # fill in the quantity in the incoming row + page.should_not have_selector "tr.product-cart" + within('product.ng-enter') { fill_in "variants[#{variant.id}]", with: 1 } + within("li.cart") { page.should have_content "$19.99" } + end + + describe "declining to clear the cart" do + before do + exchange2.variants << variant + exchange1.variants << variant + + visit shop_path + select "turtles", from: "order_cycle_id" + fill_in "variants[#{variant.id}]", with: 1 + end + + it "leaves the cart untouched when the user declines" do + handle_js_confirm(false) do + select "frogs", from: "order_cycle_id" + show_cart + page.should have_selector "tr.product-cart" + page.should have_selector 'li.cart', text: '1 item' + + # The order cycle choice should not have changed + page.should have_select 'order_cycle_id', selected: 'turtles' + end + end + end end end end diff --git a/spec/javascripts/unit/darkswarm/controllers/order_cycle_controller_spec.js.coffee b/spec/javascripts/unit/darkswarm/controllers/order_cycle_controller_spec.js.coffee index a9746f2d39..deb7aea8b1 100644 --- a/spec/javascripts/unit/darkswarm/controllers/order_cycle_controller_spec.js.coffee +++ b/spec/javascripts/unit/darkswarm/controllers/order_cycle_controller_spec.js.coffee @@ -1,19 +1,17 @@ describe 'OrderCycleCtrl', -> ctrl = null scope = null - event = null - product_ctrl = null OrderCycle = null beforeEach -> module 'Darkswarm' scope = {} - OrderCycle = - order_cycle: "test" + OrderCycle = + order_cycle: + id: 123 inject ($controller) -> scope = {} ctrl = $controller 'OrderCycleCtrl', {$scope: scope, OrderCycle: OrderCycle} it "puts the order cycle in scope", -> - expect(scope.order_cycle).toEqual "test" - + expect(scope.order_cycle).toEqual {id: 123}