From d337561ea889aae14ffb6e50243a61b918f38f9d Mon Sep 17 00:00:00 2001 From: Maikel Linke Date: Wed, 1 Jul 2020 11:01:00 +1000 Subject: [PATCH] Move ShopVariantCtrl to own file and spec it --- .../shop_variant_controller.js.coffee | 39 ++++++++ .../directives/shop_variant.js.coffee | 40 +------- .../shop_variant_controller_spec.js.coffee | 91 +++++++++++++++++++ 3 files changed, 131 insertions(+), 39 deletions(-) create mode 100644 app/assets/javascripts/darkswarm/controllers/shop_variant_controller.js.coffee create mode 100644 spec/javascripts/unit/darkswarm/controllers/checkout/shop_variant_controller_spec.js.coffee diff --git a/app/assets/javascripts/darkswarm/controllers/shop_variant_controller.js.coffee b/app/assets/javascripts/darkswarm/controllers/shop_variant_controller.js.coffee new file mode 100644 index 0000000000..d9532ffb27 --- /dev/null +++ b/app/assets/javascripts/darkswarm/controllers/shop_variant_controller.js.coffee @@ -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) diff --git a/app/assets/javascripts/darkswarm/directives/shop_variant.js.coffee b/app/assets/javascripts/darkswarm/directives/shop_variant.js.coffee index 97caf2a8b5..1872b55dfd 100644 --- a/app/assets/javascripts/darkswarm/directives/shop_variant.js.coffee +++ b/app/assets/javascripts/darkswarm/directives/shop_variant.js.coffee @@ -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' diff --git a/spec/javascripts/unit/darkswarm/controllers/checkout/shop_variant_controller_spec.js.coffee b/spec/javascripts/unit/darkswarm/controllers/checkout/shop_variant_controller_spec.js.coffee new file mode 100644 index 0000000000..8a2be1ee42 --- /dev/null +++ b/spec/javascripts/unit/darkswarm/controllers/checkout/shop_variant_controller_spec.js.coffee @@ -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