From fc4f108724bb41449f4d996d3718385f6dfb562e Mon Sep 17 00:00:00 2001 From: Will Marshall Date: Fri, 14 Mar 2014 12:59:37 +1100 Subject: [PATCH] Adding tests and views to handle changing shipping prices --- .../controllers/checkout_controller.js.coffee | 22 ++++++++----- app/views/shop/checkout/_form.html.haml | 3 ++ app/views/shop/checkout/_order.rabl | 7 +++- app/views/shop/checkout/_summary.html.haml | 3 ++ .../unit/darkswarm/controllers_spec.js.coffee | 33 +++++++++++++++++++ 5 files changed, 59 insertions(+), 9 deletions(-) diff --git a/app/assets/javascripts/darkswarm/controllers/checkout_controller.js.coffee b/app/assets/javascripts/darkswarm/controllers/checkout_controller.js.coffee index 60b0715d25..19f46626d9 100644 --- a/app/assets/javascripts/darkswarm/controllers/checkout_controller.js.coffee +++ b/app/assets/javascripts/darkswarm/controllers/checkout_controller.js.coffee @@ -2,19 +2,25 @@ angular.module("Checkout").controller "CheckoutCtrl", ($scope, $rootScope, order $scope.require_ship_address = false $scope.order = order + $scope.initialize = -> + # Our shipping_methods comes through as a hash like so: {id: requires_shipping_address} + # Here we default to the first shipping method if none is selected + $scope.order.shipping_method_id ||= Object.keys(order.shipping_methods)[0] + $scope.order.ship_address_same_as_billing = true if $scope.order.ship_address_same_as_billing == null + $scope.shippingMethodChanged() - # Our shipping_methods comes through as a hash like so: {id: requires_shipping_address} - # Here we default to the first shipping method if none is selected - $scope.order.shipping_method_id ||= Object.keys(order.shipping_methods)[0] - $scope.order.ship_address_same_as_billing = true if $scope.order.ship_address_same_as_billing == null + $scope.shippingPrice = -> + $scope.shippingMethod().price + + $scope.shippingMethod = -> + $scope.order.shipping_methods[$scope.order.shipping_method_id] - $scope.require_ship_address = $scope.order.shipping_methods[$scope.order.shipping_method_id] - $scope.shippingMethodChanged = -> - $scope.require_ship_address = $scope.order.shipping_methods[$scope.order.shipping_method_id] - + $scope.require_ship_address = $scope.shippingMethod().require_ship_address $scope.purchase = (event)-> event.preventDefault() checkout.submit() + $scope.initialize() + diff --git a/app/views/shop/checkout/_form.html.haml b/app/views/shop/checkout/_form.html.haml index fb05733c86..1a5fc1cfe0 100644 --- a/app/views/shop/checkout/_form.html.haml +++ b/app/views/shop/checkout/_form.html.haml @@ -5,6 +5,9 @@ :javascript angular.module('Checkout').value('order', #{render "shop/checkout/order"}) + -#%pre + -#{{ order | json }} + .large-12.columns %fieldset#details %legend Customer Details diff --git a/app/views/shop/checkout/_order.rabl b/app/views/shop/checkout/_order.rabl index cf8c1e21bd..affa2416f0 100644 --- a/app/views/shop/checkout/_order.rabl +++ b/app/views/shop/checkout/_order.rabl @@ -15,5 +15,10 @@ end # Format here is {id: require_ship_address} node :shipping_methods do - Hash[current_order.distributor.shipping_methods.collect { |method| [method.id, method.require_ship_address] }] + Hash[current_order.distributor.shipping_methods.collect { + |method| [method.id, { + require_ship_address: method.require_ship_address, + price: method.compute_amount(current_order) + }] + }] end diff --git a/app/views/shop/checkout/_summary.html.haml b/app/views/shop/checkout/_summary.html.haml index 23289b696a..05218675a6 100644 --- a/app/views/shop/checkout/_summary.html.haml +++ b/app/views/shop/checkout/_summary.html.haml @@ -11,6 +11,9 @@ %tr %th= adjustment.label %td= adjustment.display_amount.to_html + %tr + %th Shipping cost + %td {{ shippingPrice() | currency }} %tr %th Cart total %td= current_order.display_total.to_html diff --git a/spec/javascripts/unit/darkswarm/controllers_spec.js.coffee b/spec/javascripts/unit/darkswarm/controllers_spec.js.coffee index f7717b6737..5b6b364971 100644 --- a/spec/javascripts/unit/darkswarm/controllers_spec.js.coffee +++ b/spec/javascripts/unit/darkswarm/controllers_spec.js.coffee @@ -46,3 +46,36 @@ describe 'All controllers', -> scope = {} ctrl = $controller 'OrderCycleCtrl', {$scope: scope} + describe "CheckoutCtrl", -> + ctrl = null + scope = null + order = null + + beforeEach -> + module("Checkout") + order = + id: 3102 + shipping_method_id: "7" + ship_address_same_as_billing: true + payment_method_id: null + shipping_methods: + 7: + require_ship_address: true + price: 0.0 + + 25: + require_ship_address: false + price: 13 + inject ($controller) -> + scope = {} + ctrl = $controller 'CheckoutCtrl', {$scope: scope, order: order} + + + it 'Gets the ship address automatically', -> + expect(scope.require_ship_address).toEqual true + + it 'Gets the current shipping price', -> + expect(scope.shippingPrice()).toEqual 0.0 + scope.order.shipping_method_id = 25 + expect(scope.shippingPrice()).toEqual 13 +