mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-28 01:53:25 +00:00
Move ShopVariantCtrl to own file and spec it
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
Darkswarm.controller "ShopVariantCtrl", ($scope, $modal, Cart) ->
|
||||
$scope.$watchGroup [
|
||||
'variant.line_item.quantity',
|
||||
'variant.line_item.max_quantity'
|
||||
], (new_value, old_value) ->
|
||||
return if old_value[0] == null && new_value[0] == null
|
||||
Cart.adjust($scope.variant.line_item)
|
||||
|
||||
$scope.add = (quantity) ->
|
||||
item = $scope.variant.line_item
|
||||
item.quantity += quantity
|
||||
if $scope.variant.product.group_buy
|
||||
if item.quantity < 1 || item.max_quantity < item.quantity
|
||||
item.max_quantity = item.quantity
|
||||
|
||||
$scope.addMax = (quantity) ->
|
||||
item = $scope.variant.line_item
|
||||
item.max_quantity += quantity
|
||||
if item.max_quantity < item.quantity
|
||||
item.quantity = item.max_quantity
|
||||
|
||||
$scope.canAdd = (quantity) ->
|
||||
wantedQuantity = $scope.variant.line_item.quantity + quantity
|
||||
$scope.quantityValid(wantedQuantity)
|
||||
|
||||
$scope.canAddMax = (quantity) ->
|
||||
variant = $scope.variant
|
||||
wantedQuantity = variant.line_item.max_quantity + quantity
|
||||
$scope.quantityValid(wantedQuantity) && variant.line_item.quantity > 0
|
||||
|
||||
$scope.quantityValid = (quantity) ->
|
||||
variant = $scope.variant
|
||||
minimum = 0
|
||||
maximum = variant.on_demand && Infinity || variant.on_hand
|
||||
quantity >= minimum && quantity <= maximum
|
||||
|
||||
$scope.addBulk = (quantity) ->
|
||||
$scope.add(quantity)
|
||||
$modal.open(templateUrl: "bulk_buy_modal.html", scope: $scope)
|
||||
@@ -4,42 +4,4 @@ Darkswarm.directive "shopVariant", ->
|
||||
templateUrl: 'shop_variant.html'
|
||||
scope:
|
||||
variant: '='
|
||||
controller: ($scope, $modal, Cart) ->
|
||||
$scope.$watchGroup [
|
||||
'variant.line_item.quantity',
|
||||
'variant.line_item.max_quantity'
|
||||
], (new_value, old_value) ->
|
||||
return if old_value[0] == null && new_value[0] == null
|
||||
Cart.adjust($scope.variant.line_item)
|
||||
|
||||
$scope.add = (quantity) ->
|
||||
item = $scope.variant.line_item
|
||||
item.quantity += quantity
|
||||
if $scope.variant.product.group_buy
|
||||
if item.quantity < 1 || item.max_quantity < item.quantity
|
||||
item.max_quantity = item.quantity
|
||||
|
||||
$scope.addMax = (quantity) ->
|
||||
item = $scope.variant.line_item
|
||||
item.max_quantity += quantity
|
||||
if item.max_quantity < item.quantity
|
||||
item.quantity = item.max_quantity
|
||||
|
||||
$scope.canAdd = (quantity) ->
|
||||
wantedQuantity = $scope.variant.line_item.quantity + quantity
|
||||
$scope.quantityValid(wantedQuantity)
|
||||
|
||||
$scope.canAddMax = (quantity) ->
|
||||
variant = $scope.variant
|
||||
wantedQuantity = variant.line_item.max_quantity + quantity
|
||||
$scope.quantityValid(wantedQuantity) && variant.line_item.quantity > 0
|
||||
|
||||
$scope.quantityValid = (quantity) ->
|
||||
variant = $scope.variant
|
||||
minimum = 0
|
||||
maximum = variant.on_demand && Infinity || variant.on_hand
|
||||
quantity >= minimum && quantity <= maximum
|
||||
|
||||
$scope.addBulk = (quantity) ->
|
||||
$scope.add(quantity)
|
||||
$modal.open(templateUrl: "bulk_buy_modal.html", scope: $scope)
|
||||
controller: 'ShopVariantCtrl'
|
||||
|
||||
@@ -0,0 +1,91 @@
|
||||
describe "ShopVariantCtrl", ->
|
||||
ctrl = null
|
||||
scope = null
|
||||
|
||||
beforeEach ->
|
||||
module 'Darkswarm'
|
||||
scope =
|
||||
$watchGroup: ->
|
||||
variant: {
|
||||
on_demand: true
|
||||
product: {group_buy: false}
|
||||
line_item: {
|
||||
quantity: 0
|
||||
max_quantity: 0
|
||||
}
|
||||
}
|
||||
|
||||
inject ($controller, $modal)->
|
||||
ctrl = $controller 'ShopVariantCtrl', {$scope: scope, $modal: $modal, Cart: null}
|
||||
|
||||
it "adds an item to the cart", ->
|
||||
scope.add 1
|
||||
expect(scope.variant.line_item.quantity).toEqual 1
|
||||
|
||||
it "adds to the existing quantity", ->
|
||||
scope.add 1
|
||||
scope.add 5
|
||||
expect(scope.variant.line_item.quantity).toEqual 6
|
||||
|
||||
it "adds to the max quantity", ->
|
||||
scope.addMax 5
|
||||
expect(scope.variant.line_item.quantity).toEqual 0
|
||||
expect(scope.variant.line_item.max_quantity).toEqual 5
|
||||
|
||||
it "adds to the max quantity to be at least min quantity", ->
|
||||
scope.variant.product.group_buy = true
|
||||
scope.variant.line_item.max_quantity = 2
|
||||
|
||||
scope.add 3
|
||||
|
||||
expect(scope.variant.line_item.quantity).toEqual 3
|
||||
expect(scope.variant.line_item.max_quantity).toEqual 3
|
||||
|
||||
it "decreases the min quantity to not exceed max quantity", ->
|
||||
scope.variant.product.group_buy = true
|
||||
scope.variant.line_item.quantity = 3
|
||||
scope.variant.line_item.max_quantity = 5
|
||||
|
||||
scope.addMax -3
|
||||
|
||||
expect(scope.variant.line_item.quantity).toEqual 2
|
||||
expect(scope.variant.line_item.max_quantity).toEqual 2
|
||||
|
||||
it "allows adding when variant is on demand", ->
|
||||
expect(scope.canAdd(5000)).toEqual true
|
||||
|
||||
it "denies adding if variant is out of stock", ->
|
||||
scope.variant.on_demand = false
|
||||
scope.variant.on_hand = 0
|
||||
|
||||
expect(scope.canAdd(1)).toEqual false
|
||||
|
||||
it "denies adding if stock is limitted", ->
|
||||
scope.variant.on_demand = false
|
||||
scope.variant.on_hand = 5
|
||||
|
||||
expect(scope.canAdd(4)).toEqual true
|
||||
expect(scope.canAdd(5)).toEqual true
|
||||
expect(scope.canAdd(6)).toEqual false
|
||||
|
||||
scope.add 3
|
||||
expect(scope.canAdd(2)).toEqual true
|
||||
expect(scope.canAdd(3)).toEqual false
|
||||
|
||||
it "denies declaring max quantity before item is in cart", ->
|
||||
expect(scope.canAddMax(1)).toEqual false
|
||||
|
||||
it "allows declaring max quantity when item is in cart", ->
|
||||
scope.add 1
|
||||
expect(scope.canAddMax(1)).toEqual true
|
||||
|
||||
it "denies adding if stock is limitted", ->
|
||||
scope.variant.on_demand = false
|
||||
scope.variant.on_hand = 5
|
||||
scope.variant.line_item.quantity = 1
|
||||
scope.variant.line_item.max_quantity = 1
|
||||
|
||||
expect(scope.canAddMax(3)).toEqual true
|
||||
expect(scope.canAddMax(4)).toEqual true
|
||||
expect(scope.canAddMax(5)).toEqual false
|
||||
expect(scope.canAddMax(6)).toEqual false
|
||||
Reference in New Issue
Block a user