Call VariantUnitManager directly

This commit is contained in:
Rohan Mitchell
2014-07-18 11:02:20 +10:00
parent 8d5f953f78
commit ab1628681e
2 changed files with 9 additions and 14 deletions

View File

@@ -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
''

View File

@@ -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", ->