Add shipping method js infrastructure for angularising admin enterprises page

This commit is contained in:
Rob H
2014-07-23 17:02:45 +10:00
parent 6dbe2a3098
commit 37c16fb20c
6 changed files with 108 additions and 10 deletions

View File

@@ -1,19 +1,35 @@
angular.module("admin.enterprises")
.controller "enterpriseCtrl", ($scope, Enterprise, PaymentMethods) ->
.controller "enterpriseCtrl", ($scope, Enterprise, PaymentMethods, ShippingMethods) ->
$scope.Enterprise = Enterprise.enterprise
$scope.PaymentMethods = PaymentMethods.paymentMethods
$scope.ShippingMethods = ShippingMethods.shippingMethods
for PaymentMethod in $scope.PaymentMethods
PaymentMethod.selected = if PaymentMethod.id in $scope.Enterprise.payment_method_ids then true else false
$scope.paymentMethodsColor = ->
if $scope.PaymentMethods.length > 0
if $scope.selectedPaymentMethodsCount() > 0 then "blue" else "red"
else
"red"
$scope.selectedPaymentMethodsCount = ->
$scope.PaymentMethods.reduce (count, PaymentMethod) ->
count++ if PaymentMethod.selected
count
, 0
$scope.paymentMethodsColor = ->
if $scope.PaymentMethods.length > 0
if $scope.selectedPaymentMethodsCount() > 0 then "blue" else "red"
for ShippingMethod in $scope.ShippingMethods
ShippingMethod.selected = if ShippingMethod.id in $scope.Enterprise.shipping_method_ids then true else false
$scope.shippingMethodsColor = ->
if $scope.ShippingMethods.length > 0
if $scope.selectedShippingMethodsCount() > 0 then "blue" else "red"
else
"red"
"red"
$scope.selectedShippingMethodsCount = ->
$scope.ShippingMethods.reduce (count, ShippingMethod) ->
count++ if ShippingMethod.selected
count
, 0

View File

@@ -1 +1 @@
angular.module("admin.enterprises", ["admin.payment_methods"])
angular.module("admin.enterprises", ["admin.payment_methods", "admin.shipping_methods"])

View File

@@ -0,0 +1,4 @@
angular.module("admin.shipping_methods")
.controller "shippingMethodCtrl", ($scope, ShippingMethods) ->
$scope.findShippingMethodByID = (id) ->
$scope.ShippingMethod = ShippingMethods.findByID(id)

View File

@@ -0,0 +1,8 @@
angular.module("admin.shipping_methods")
.factory "ShippingMethods", (shippingMethods) ->
new class ShippingMethods
shippingMethods: shippingMethods
findByID: (id) ->
for shippingMethod in @shippingMethods
return shippingMethod if shippingMethod.id is id

View File

@@ -0,0 +1 @@
angular.module("admin.shipping_methods", [])

View File

@@ -2,15 +2,84 @@ describe "enterpriseCtrl", ->
ctrl = null
scope = null
Enterprise = null
PaymentMethods = null
ShippingMethods = null
beforeEach ->
module('admin.enterprises')
Enterprise =
enterprise: "test enterprise"
enterprise:
payment_method_ids: [ 1, 3 ]
shipping_method_ids: [ 2, 4 ]
PaymentMethods =
paymentMethods: [ { id: 1 }, { id: 2 }, { id: 3 }, { id: 4 } ]
ShippingMethods =
shippingMethods: [ { id: 1 }, { id: 2 }, { id: 3 }, { id: 4 } ]
inject ($controller) ->
scope = {}
ctrl = $controller 'enterpriseCtrl', {$scope: scope, Enterprise: Enterprise}
ctrl = $controller 'enterpriseCtrl', {$scope: scope, Enterprise: Enterprise, PaymentMethods: PaymentMethods, ShippingMethods: ShippingMethods}
it "stores enterprise", ->
expect(scope.enterprise).toBe Enterprise.enterprise
describe "initialisation", ->
it "stores enterprise", ->
expect(scope.Enterprise).toEqual Enterprise.enterprise
it "stores payment methods", ->
expect(scope.PaymentMethods).toBe PaymentMethods.paymentMethods
it "stores shipping methods", ->
expect(scope.ShippingMethods).toBe ShippingMethods.shippingMethods
it "sets the selected property of each payment method", ->
expect(PaymentMethods.paymentMethods[0].selected).toBe true
expect(PaymentMethods.paymentMethods[1].selected).toBe false
expect(PaymentMethods.paymentMethods[2].selected).toBe true
expect(PaymentMethods.paymentMethods[3].selected).toBe false
it "sets the selected property of each shipping method", ->
expect(ShippingMethods.shippingMethods[0].selected).toBe false
expect(ShippingMethods.shippingMethods[1].selected).toBe true
expect(ShippingMethods.shippingMethods[2].selected).toBe false
expect(ShippingMethods.shippingMethods[3].selected).toBe true
describe "determining payment method colour", ->
it "returns 'blue' when at least one payment method is selected", ->
scope.PaymentMethods = [ { id: 1 } ]
spyOn(scope, "selectedPaymentMethodsCount").andReturn 1
expect(scope.paymentMethodsColor()).toBe "blue"
it "returns 'red' when no payment methods are selected", ->
scope.PaymentMethods = [ { id: 1 } ]
spyOn(scope, "selectedPaymentMethodsCount").andReturn 0
expect(scope.paymentMethodsColor()).toBe "red"
it "returns 'red' when no payment methods exist", ->
scope.PaymentMethods = [ ]
spyOn(scope, "selectedPaymentMethodsCount").andReturn 1
expect(scope.paymentMethodsColor()).toBe "red"
describe "counting selected payment methods", ->
it "counts only payment methods with selected: true", ->
scopePaymentMethods = [ { selected: true }, { selected: false }, { selected: false }, { selected: true } ]
expect(scope.selectedPaymentMethodsCount()).toBe 2
describe "determining shipping method colour", ->
it "returns 'blue' when at least one shipping method is selected", ->
scope.ShippingMethods = [ { id: 1 } ]
spyOn(scope, "selectedShippingMethodsCount").andReturn 1
expect(scope.shippingMethodsColor()).toBe "blue"
it "returns 'red' when no shipping methods are selected", ->
scope.ShippingMethods = [ { id: 1 } ]
spyOn(scope, "selectedShippingMethodsCount").andReturn 0
expect(scope.shippingMethodsColor()).toBe "red"
it "returns 'red' when no shipping method exist", ->
scope.ShippingMethods = [ ]
spyOn(scope, "selectedShippingMethodsCount").andReturn 1
expect(scope.shippingMethodsColor()).toBe "red"
describe "counting selected shipping methods", ->
it "counts only shipping methods with selected: true", ->
scope.ShippingMethods = [ { selected: true }, { selected: true }, { selected: false }, { selected: true } ]
expect(scope.selectedShippingMethodsCount()).toBe 3