mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-24 20:36:49 +00:00
Extract getScale from BOM controller to VariantUnitManager, extract specs as well
This commit is contained in:
@@ -135,15 +135,7 @@ angular.module("ofn.admin").controller "AdminOrderMgmtCtrl", [
|
||||
true
|
||||
|
||||
$scope.getScale = (value, unitType) ->
|
||||
scaledValue = null
|
||||
validScales = []
|
||||
unitScales = VariantUnitManager.unitScales(unitType)
|
||||
|
||||
validScales.unshift scale for scale in unitScales when value/scale >= 1
|
||||
if validScales.length > 0
|
||||
validScales[0]
|
||||
else
|
||||
unitScales[0]
|
||||
VariantUnitManager.getScale(value, unitType)
|
||||
|
||||
$scope.getUnitName = (scale, unitType) ->
|
||||
VariantUnitManager.getUnitName(scale, unitType)
|
||||
|
||||
@@ -1,5 +1,16 @@
|
||||
angular.module("admin.products").factory "VariantUnitManager", ->
|
||||
class VariantUnitManager
|
||||
@getScale = (value, unitType) ->
|
||||
scaledValue = null
|
||||
validScales = []
|
||||
unitScales = VariantUnitManager.unitScales(unitType)
|
||||
|
||||
validScales.unshift scale for scale in unitScales when value/scale >= 1
|
||||
if validScales.length > 0
|
||||
validScales[0]
|
||||
else
|
||||
unitScales[0]
|
||||
|
||||
@getUnitName: (scale, unitType) ->
|
||||
unitNames =
|
||||
'weight': {1.0: 'g', 1000.0: 'kg', 1000000.0: 'T'}
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
describe "VariantUnitManager", ->
|
||||
VariantUnitManager = null
|
||||
|
||||
beforeEach ->
|
||||
module "admin.products"
|
||||
|
||||
beforeEach inject (_VariantUnitManager_) ->
|
||||
VariantUnitManager = _VariantUnitManager_
|
||||
|
||||
describe "getScale", ->
|
||||
it "returns the largest scale for which value/scale is greater than 1", ->
|
||||
expect(VariantUnitManager.getScale(1.2,"weight")).toEqual 1.0
|
||||
expect(VariantUnitManager.getScale(1000,"weight")).toEqual 1000.0
|
||||
expect(VariantUnitManager.getScale(0.0012,"volume")).toEqual 0.001
|
||||
expect(VariantUnitManager.getScale(1001,"volume")).toEqual 1000.0
|
||||
|
||||
it "returns the smallest unit available when value is smaller", ->
|
||||
expect(VariantUnitManager.getScale(0.4,"weight")).toEqual 1
|
||||
expect(VariantUnitManager.getScale(0.0004,"volume")).toEqual 0.001
|
||||
|
||||
describe "getUnitName", ->
|
||||
it "returns the unit name based on the scale and unit type (weight/volume) provided", ->
|
||||
expect(VariantUnitManager.getUnitName(1, "weight")).toEqual "g"
|
||||
expect(VariantUnitManager.getUnitName(1000, "weight")).toEqual "kg"
|
||||
expect(VariantUnitManager.getUnitName(1000000, "weight")).toEqual "T"
|
||||
expect(VariantUnitManager.getUnitName(0.001, "volume")).toEqual "mL"
|
||||
expect(VariantUnitManager.getUnitName(1, "volume")).toEqual "L"
|
||||
expect(VariantUnitManager.getUnitName(1000, "volume")).toEqual "kL"
|
||||
@@ -347,26 +347,6 @@ describe "AdminOrderMgmtCtrl", ->
|
||||
spyOn(scope,"getUnitName").andReturn "kg"
|
||||
expect(scope.formattedValueWithUnitName(2000,unitsVariant)).toEqual "2 kg"
|
||||
|
||||
describe "getScale", ->
|
||||
it "returns the largest scale for which value/scale is greater than 1", ->
|
||||
expect(scope.getScale(1.2,"weight")).toEqual 1.0
|
||||
expect(scope.getScale(1000,"weight")).toEqual 1000.0
|
||||
expect(scope.getScale(0.0012,"volume")).toEqual 0.001
|
||||
expect(scope.getScale(1001,"volume")).toEqual 1000.0
|
||||
|
||||
it "returns the smallest unit available when value is smaller", ->
|
||||
expect(scope.getScale(0.4,"weight")).toEqual 1
|
||||
expect(scope.getScale(0.0004,"volume")).toEqual 0.001
|
||||
|
||||
describe "getUnitName", ->
|
||||
it "returns the unit name based on the scale and unit type (weight/volume) provided", ->
|
||||
expect(scope.getUnitName(1,"weight")).toEqual "g"
|
||||
expect(scope.getUnitName(1000,"weight")).toEqual "kg"
|
||||
expect(scope.getUnitName(1000000,"weight")).toEqual "T"
|
||||
expect(scope.getUnitName(0.001,"volume")).toEqual "mL"
|
||||
expect(scope.getUnitName(1,"volume")).toEqual "L"
|
||||
expect(scope.getUnitName(1000,"volume")).toEqual "kL"
|
||||
|
||||
describe "managing pending changes", ->
|
||||
dataSubmitter = pendingChangesService = null
|
||||
|
||||
|
||||
Reference in New Issue
Block a user