<NME> units_controller_spec.js.coffee
<BEF> ADDFILE
<MSG> Refactoring unitsCtrl

<DFF> @@ -0,0 +1,55 @@
+describe "unitsCtrl", ->
+  ctrl = null
+  scope = null
+  product = null
+
+  beforeEach ->
+    module('admin.products')
+    inject ($rootScope, $controller, VariantUnitManager) ->
+      scope = $rootScope
+      ctrl = $controller 'unitsCtrl', {$scope: scope, VariantUnitManager: VariantUnitManager}
+
+  describe "interpretting variant_unit_with_scale", ->
+    it "splits string with one underscore and stores the two parts", ->
+      scope.product.variant_unit_with_scale = "weight_1000"
+      scope.processVariantUnitWithScale()
+      expect(scope.product.variant_unit).toEqual "weight"
+      expect(scope.product.variant_unit_scale).toEqual 1000
+
+    it "interprets strings with no underscore as variant_unit", ->
+      scope.product.variant_unit_with_scale = "items"
+      scope.processVariantUnitWithScale()
+      expect(scope.product.variant_unit).toEqual "items"
+      expect(scope.product.variant_unit_scale).toEqual null
+
+    it "sets variant_unit and variant_unit_scale to null", ->
+      scope.product.variant_unit_with_scale = null
+      scope.processVariantUnitWithScale()
+      expect(scope.product.variant_unit).toEqual null
+      expect(scope.product.variant_unit_scale).toEqual null
+
+  describe "interpretting unit_value_with_description", ->
+    beforeEach ->
+      scope.product.master = {}
+
+    describe "when a variant_unit_scale is present", ->
+      beforeEach ->
+        scope.product.variant_unit_scale = 1
+
+      it "splits by whitespace in to unit_value and unit_description", ->
+        scope.product.master.unit_value_with_description = "12 boxes"
+        scope.processUnitValueWithDescription()
+        expect(scope.product.master.unit_value).toEqual 12
+        expect(scope.product.master.unit_description).toEqual "boxes"
+
+      it "uses whole string as unit_value when only numerical characters are present", ->
+        scope.product.master.unit_value_with_description = "12345"
+        scope.processUnitValueWithDescription()
+        expect(scope.product.master.unit_value).toEqual 12345
+        expect(scope.product.master.unit_description).toEqual ''
+
+      it "uses whole string as description when string does not start with a number", ->
+        scope.product.master.unit_value_with_description = "boxes 12"
+        scope.processUnitValueWithDescription()
+        expect(scope.product.master.unit_value).toEqual null
+        expect(scope.product.master.unit_description).toEqual "boxes 12"

{'commit': 'diff --git a/app/assets/javascripts/admin/products/units_controller.js.coffee b/app/assets/javascripts/admin/products/units_controller.js.coffee\nindex db005e490..c9c5807af 100644\n--- a/app/assets/javascripts/admin/products/units_controller.js.coffee\n+++ b/app/assets/javascripts/admin/products/units_controller.js.coffee\n@@ -5,6 +5,13 @@ angular.module("admin.products")\n     $scope.placeholder_text = ""\n \n     $scope.$watchCollection \'[product.variant_unit_with_scale, product.master.unit_value_with_description]\', ->\n+      $scope.processVariantUnitWithScale()\n+      $scope.processUnitValueWithDescription()\n+      $scope.placeholder_text = new OptionValueNamer($scope.product.master).name()\n+\n+    $scope.variant_unit_options = VariantUnitManager.variantUnitOptions()\n+\n+    $scope.processVariantUnitWithScale = ->\n       if $scope.product.variant_unit_with_scale\n         match = $scope.product.variant_unit_with_scale.match(/^([^_]+)_([\\d\\.]+)$/)\n         if match\n@@ -16,6 +23,7 @@ angular.module("admin.products")\n       else\n         $scope.product.variant_unit = $scope.product.variant_unit_scale = null\n \n+    $scope.processUnitValueWithDescription = ->\n       if $scope.product.master.hasOwnProperty("unit_value_with_description")\n         match = $scope.product.master.unit_value_with_description.match(/^([\\d\\.]+(?= |$)|)( |)(.*)$/)\n         if match\n@@ -24,10 +32,6 @@ angular.module("admin.products")\n           $scope.product.master.unit_value *= $scope.product.variant_unit_scale if $scope.product.master.unit_value && $scope.product.variant_unit_scale\n           $scope.product.master.unit_description = match[3]\n \n-      $scope.placeholder_text = new OptionValueNamer($scope.product.master).name()\n-\n-    $scope.variant_unit_options = VariantUnitManager.variantUnitOptions()\n-\n     $scope.hasVariants = (product) ->\n       Object.keys(product.variants).length > 0\n \ndiff --git a/spec/javascripts/unit/admin/products/units_controller_spec.js.coffee b/spec/javascripts/unit/admin/products/units_controller_spec.js.coffee\nnew file mode 100644\nindex 000000000..c5f104f07\n--- /dev/null\n+++ b/spec/javascripts/unit/admin/products/units_controller_spec.js.coffee\n@@ -0,0 +1,55 @@\n+describe "unitsCtrl", ->\n+  ctrl = null\n+  scope = null\n+  product = null\n+\n+  beforeEach ->\n+    module(\'admin.products\')\n+    inject ($rootScope, $controller, VariantUnitManager) ->\n+      scope = $rootScope\n+      ctrl = $controller \'unitsCtrl\', {$scope: scope, VariantUnitManager: VariantUnitManager}\n+\n+  describe "interpretting variant_unit_with_scale", ->\n+    it "splits string with one underscore and stores the two parts", ->\n+      scope.product.variant_unit_with_scale = "weight_1000"\n+      scope.processVariantUnitWithScale()\n+      expect(scope.product.variant_unit).toEqual "weight"\n+      expect(scope.product.variant_unit_scale).toEqual 1000\n+\n+    it "interprets strings with no underscore as variant_unit", ->\n+      scope.product.variant_unit_with_scale = "items"\n+      scope.processVariantUnitWithScale()\n+      expect(scope.product.variant_unit).toEqual "items"\n+      expect(scope.product.variant_unit_scale).toEqual null\n+\n+    it "sets variant_unit and variant_unit_scale to null", ->\n+      scope.product.variant_unit_with_scale = null\n+      scope.processVariantUnitWithScale()\n+      expect(scope.product.variant_unit).toEqual null\n+      expect(scope.product.variant_unit_scale).toEqual null\n+\n+  describe "interpretting unit_value_with_description", ->\n+    beforeEach ->\n+      scope.product.master = {}\n+\n+    describe "when a variant_unit_scale is present", ->\n+      beforeEach ->\n+        scope.product.variant_unit_scale = 1\n+\n+      it "splits by whitespace in to unit_value and unit_description", ->\n+        scope.product.master.unit_value_with_description = "12 boxes"\n+        scope.processUnitValueWithDescription()\n+        expect(scope.product.master.unit_value).toEqual 12\n+        expect(scope.product.master.unit_description).toEqual "boxes"\n+\n+      it "uses whole string as unit_value when only numerical characters are present", ->\n+        scope.product.master.unit_value_with_description = "12345"\n+        scope.processUnitValueWithDescription()\n+        expect(scope.product.master.unit_value).toEqual 12345\n+        expect(scope.product.master.unit_description).toEqual \'\'\n+\n+      it "uses whole string as description when string does not start with a number", ->\n+        scope.product.master.unit_value_with_description = "boxes 12"\n+        scope.processUnitValueWithDescription()\n+        expect(scope.product.master.unit_value).toEqual null\n+        expect(scope.product.master.unit_description).toEqual "boxes 12"\n', 'commit_subject': 'Refactoring unitsCtrl', 'repo_name': 'openfoodfoundation/openfoodnetwork', 'committer': {'name': 'Rob Harrington', 'email': '4c1b7a69c57001e8088d6662955012658921d8fa@gmail.com', 'time_sec': '1429838867', 'tz_offset': '600', 'date': {'seconds': '1429838867'}}, 'commit_tree': 'b0b7bfbd89b4e03c044f74e3e6f23c17a8f95dc0', 'commit_message': 'Refactoring unitsCtrl\n', 'language_name': 'Shell', 'license': 'agpl-3.0', 'hunks': '@@ -0,0 +1,55 @@\n+describe "unitsCtrl", ->\n+  ctrl = null\n+  scope = null\n+  product = null\n+\n+  beforeEach ->\n+    module(\'admin.products\')\n+    inject ($rootScope, $controller, VariantUnitManager) ->\n+      scope = $rootScope\n+      ctrl = $controller \'unitsCtrl\', {$scope: scope, VariantUnitManager: VariantUnitManager}\n+\n+  describe "interpretting variant_unit_with_scale", ->\n+    it "splits string with one underscore and stores the two parts", ->\n+      scope.product.variant_unit_with_scale = "weight_1000"\n+      scope.processVariantUnitWithScale()\n+      expect(scope.product.variant_unit).toEqual "weight"\n+      expect(scope.product.variant_unit_scale).toEqual 1000\n+\n+    it "interprets strings with no underscore as variant_unit", ->\n+      scope.product.variant_unit_with_scale = "items"\n+      scope.processVariantUnitWithScale()\n+      expect(scope.product.variant_unit).toEqual "items"\n+      expect(scope.product.variant_unit_scale).toEqual null\n+\n+    it "sets variant_unit and variant_unit_scale to null", ->\n+      scope.product.variant_unit_with_scale = null\n+      scope.processVariantUnitWithScale()\n+      expect(scope.product.variant_unit).toEqual null\n+      expect(scope.product.variant_unit_scale).toEqual null\n+\n+  describe "interpretting unit_value_with_description", ->\n+    beforeEach ->\n+      scope.product.master = {}\n+\n+    describe "when a variant_unit_scale is present", ->\n+      beforeEach ->\n+        scope.product.variant_unit_scale = 1\n+\n+      it "splits by whitespace in to unit_value and unit_description", ->\n+        scope.product.master.unit_value_with_description = "12 boxes"\n+        scope.processUnitValueWithDescription()\n+        expect(scope.product.master.unit_value).toEqual 12\n+        expect(scope.product.master.unit_description).toEqual "boxes"\n+\n+      it "uses whole string as unit_value when only numerical characters are present", ->\n+        scope.product.master.unit_value_with_description = "12345"\n+        scope.processUnitValueWithDescription()\n+        expect(scope.product.master.unit_value).toEqual 12345\n+        expect(scope.product.master.unit_description).toEqual \'\'\n+\n+      it "uses whole string as description when string does not start with a number", ->\n+        scope.product.master.unit_value_with_description = "boxes 12"\n+        scope.processUnitValueWithDescription()\n+        expect(scope.product.master.unit_value).toEqual null\n+        expect(scope.product.master.unit_description).toEqual "boxes 12"\n', 'addition_count': 55, 'deletion_count': 0, 'src_file': '/dev/null', 'tgt_file': 'b/spec/javascripts/unit/admin/products/units_controller_spec.js.coffee', 'file_extension': '.coffee', 'before_file': 'ADDFILE', 'message': 'Refactoring unitsCtrl\n', 'author_name': 'Rob Harrington', 'lang': 'js'}