mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-03-01 02:03:22 +00:00
Call VariantUnitManager directly
This commit is contained in:
@@ -134,17 +134,11 @@ angular.module("ofn.admin").controller "AdminOrderMgmtCtrl", [
|
||||
return false if !lineItem.units_variant.hasOwnProperty('unit_value') || !(lineItem.units_variant.unit_value > 0)
|
||||
true
|
||||
|
||||
$scope.getScale = (value, unitType) ->
|
||||
VariantUnitManager.getScale(value, unitType)
|
||||
|
||||
$scope.getUnitName = (scale, unitType) ->
|
||||
VariantUnitManager.getUnitName(scale, unitType)
|
||||
|
||||
$scope.formattedValueWithUnitName = (value, unitsProduct, unitsVariant) ->
|
||||
# A Units Variant is an API object which holds unit properies of a variant
|
||||
if unitsProduct.hasOwnProperty("variant_unit") && (unitsProduct.variant_unit == "weight" || unitsProduct.variant_unit == "volume") && value > 0
|
||||
scale = $scope.getScale(value, unitsProduct.variant_unit)
|
||||
Math.round(value/scale * 1000)/1000 + " " + $scope.getUnitName(scale, unitsProduct.variant_unit)
|
||||
scale = VariantUnitManager.getScale(value, unitsProduct.variant_unit)
|
||||
Math.round(value/scale * 1000)/1000 + " " + VariantUnitManager.getUnitName(scale, unitsProduct.variant_unit)
|
||||
else
|
||||
''
|
||||
|
||||
|
||||
@@ -1,12 +1,13 @@
|
||||
describe "AdminOrderMgmtCtrl", ->
|
||||
ctrl = scope = httpBackend = null
|
||||
ctrl = scope = httpBackend = VariantUnitManager = null
|
||||
|
||||
beforeEach ->
|
||||
module "ofn.admin"
|
||||
beforeEach inject(($controller, $rootScope, $httpBackend) ->
|
||||
beforeEach inject(($controller, $rootScope, $httpBackend, _VariantUnitManager_) ->
|
||||
scope = $rootScope.$new()
|
||||
ctrl = $controller
|
||||
httpBackend = $httpBackend
|
||||
VariantUnitManager = _VariantUnitManager_
|
||||
spyOn(window, "formatDate").andReturn "SomeDate"
|
||||
|
||||
ctrl "AdminOrderMgmtCtrl", {$scope: scope}
|
||||
@@ -337,14 +338,14 @@ describe "AdminOrderMgmtCtrl", ->
|
||||
|
||||
it "calls Math.round with the quotient of scale and value, multiplied by 1000", ->
|
||||
unitsVariant = { variant_unit: "weight" }
|
||||
spyOn(scope,"getScale").andReturn 5
|
||||
scope.formattedValueWithUnitName(10,unitsVariant)
|
||||
spyOn(VariantUnitManager, "getScale").andReturn 5
|
||||
scope.formattedValueWithUnitName(10, unitsVariant)
|
||||
expect(Math.round).toHaveBeenCalledWith 10/5 * 1000
|
||||
|
||||
it "returns the result of Math.round divided by 1000, followed by the result of getUnitName", ->
|
||||
unitsVariant = { variant_unit: "weight" }
|
||||
spyOn(scope,"getScale").andReturn 1000
|
||||
spyOn(scope,"getUnitName").andReturn "kg"
|
||||
spyOn(VariantUnitManager, "getScale").andReturn 1000
|
||||
spyOn(VariantUnitManager, "getUnitName").andReturn "kg"
|
||||
expect(scope.formattedValueWithUnitName(2000,unitsVariant)).toEqual "2 kg"
|
||||
|
||||
describe "managing pending changes", ->
|
||||
|
||||
Reference in New Issue
Block a user