Guard against invalid quantity input

The user can now type anything into the quantity field and some of it
may not be valid. These safe guards ensure that the buttons still work
even if the quantity is undefined or out of range.

Angular guards against the value being out of range but that has other
side-effects. We want to be able to de-activate some of Angular's
behaviour.
This commit is contained in:
Maikel Linke
2021-01-06 11:00:43 +11:00
parent cf2a105b2a
commit 0166400b03
2 changed files with 70 additions and 4 deletions

View File

@@ -27,11 +27,29 @@ describe "ShopVariantCtrl", ->
scope.add 5
expect(scope.variant.line_item.quantity).toEqual 6
it "adds to an invalid quantity", ->
scope.$apply ->
scope.variant.line_item.quantity = -5
scope.add 1
expect(scope.variant.line_item.quantity).toEqual 1
it "adds to an undefined quantity", ->
scope.$apply ->
scope.variant.line_item.quantity = undefined
scope.add 1
expect(scope.variant.line_item.quantity).toEqual 1
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 an undefined max quantity", ->
scope.variant.line_item.quantity = 3
scope.variant.line_item.max_quantity = undefined
scope.addMax 1
expect(scope.variant.line_item.max_quantity).toEqual 4
it "adds to the max quantity to be at least min quantity", ->
scope.$apply ->
scope.variant.line_item.max_quantity = 2
@@ -74,6 +92,42 @@ describe "ShopVariantCtrl", ->
expect(scope.canAdd(2)).toEqual true
expect(scope.canAdd(3)).toEqual false
it "denies adding if quantity is too high", ->
scope.variant.on_demand = false
scope.variant.on_hand = 5
scope.variant.line_item.quantity = 7
scope.variant.line_item.max_quantity = 7
expect(scope.canAdd(1)).toEqual false
expect(scope.canAddMax(1)).toEqual false
it "allows decrease when quantity is too high", ->
scope.variant.on_demand = false
scope.variant.on_hand = 5
scope.variant.line_item.quantity = 7
scope.variant.line_item.max_quantity = 7
expect(scope.canAdd(-1)).toEqual true
expect(scope.canAddMax(-1)).toEqual true
it "allows increase when quantity is negative", ->
scope.variant.on_demand = false
scope.variant.on_hand = 5
scope.variant.line_item.quantity = -3
scope.variant.line_item.max_quantity = -3
expect(scope.canAdd(1)).toEqual true
expect(scope.canAddMax(1)).toEqual false
scope.variant.line_item.quantity = 1
expect(scope.canAddMax(1)).toEqual true
it "denies decrease when quantity is negative", ->
scope.variant.on_demand = false
scope.variant.on_hand = 5
scope.variant.line_item.quantity = -3
scope.variant.line_item.max_quantity = -3
expect(scope.canAdd(-1)).toEqual false
expect(scope.canAddMax(-1)).toEqual false
it "denies declaring max quantity before item is in cart", ->
expect(scope.canAddMax(1)).toEqual false