Repository: ambari
Updated Branches:
  refs/heads/trunk e79ff36d5 -> 21975c9ca


AMBARI-19401. Add new macros `ifThenElseByKeys` and write UT (onechiporenko)


Project: http://git-wip-us.apache.org/repos/asf/ambari/repo
Commit: http://git-wip-us.apache.org/repos/asf/ambari/commit/21975c9c
Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/21975c9c
Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/21975c9c

Branch: refs/heads/trunk
Commit: 21975c9caac148684cbf96a90753e2946e2a5263
Parents: e79ff36
Author: Oleg Nechiporenko <[email protected]>
Authored: Fri Jan 6 13:55:43 2017 +0200
Committer: Oleg Nechiporenko <[email protected]>
Committed: Fri Jan 6 15:53:29 2017 +0200

----------------------------------------------------------------------
 .../configs/objects/service_config_property.js  |  8 +--
 ambari-web/app/models/configs/theme/tab.js      |  9 ++-
 ambari-web/app/utils/ember_computed.js          | 30 +++++++++
 ambari-web/test/aliases/computed/filterBy.js    |  2 +-
 .../test/aliases/computed/ifThenElseByKeys.js   | 65 ++++++++++++++++++++
 ambari-web/test/init_computed_aliases.js        |  1 +
 .../objects/service_config_property_test.js     | 55 +++--------------
 .../models/configs/theme/sub_section_test.js    |  2 +
 .../test/models/configs/theme/tab_test.js       |  2 +
 ambari-web/test/utils/ember_computed_test.js    | 42 ++++++++++++-
 10 files changed, 159 insertions(+), 57 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ambari/blob/21975c9c/ambari-web/app/models/configs/objects/service_config_property.js
----------------------------------------------------------------------
diff --git a/ambari-web/app/models/configs/objects/service_config_property.js 
b/ambari-web/app/models/configs/objects/service_config_property.js
index 4459946..ed72dc3 100644
--- a/ambari-web/app/models/configs/objects/service_config_property.js
+++ b/ambari-web/app/models/configs/objects/service_config_property.js
@@ -209,9 +209,7 @@ App.ServiceConfigProperty = Em.Object.extend({
    * When <code>true</code> means that property is shown and may affect 
validation process.
    * When <code>false</code> means that property won't affect validation.
    */
-  isActive: function() {
-    return this.get('isVisible') && !this.get('hiddenBySubSection') && 
!this.get('hiddenBySection');
-  }.property('isVisible', 'hiddenBySubSection', 'hiddenBySection'),
+  isActive: Em.computed.and('isVisible', '!hiddenBySubSection', 
'!hiddenBySection'),
 
   /**
    * @type {boolean}
@@ -265,9 +263,7 @@ App.ServiceConfigProperty = Em.Object.extend({
     return overrideable && (editable || !overrides || !overrides.length) && 
(!["componentHost", "password"].contains(dt));
   }.property('isEditable', 'displayType', 'isOverridable', 'overrides.length'),
 
-  isOverridden: function() {
-    return (this.get('overrides') != null && this.get('overrides.length') > 0) 
|| !this.get('isOriginalSCP');
-  }.property('overrides', 'overrides.length', 'isOriginalSCP'),
+  isOverridden: Em.computed.or('overrides.length', '!isOriginalSCP'),
 
   isOverrideChanged: function () {
     if (Em.isNone(this.get('overrides')) && this.get('overrideValues.length') 
=== 0) return false;

http://git-wip-us.apache.org/repos/asf/ambari/blob/21975c9c/ambari-web/app/models/configs/theme/tab.js
----------------------------------------------------------------------
diff --git a/ambari-web/app/models/configs/theme/tab.js 
b/ambari-web/app/models/configs/theme/tab.js
index ad3e6e4..18461b2 100644
--- a/ambari-web/app/models/configs/theme/tab.js
+++ b/ambari-web/app/models/configs/theme/tab.js
@@ -63,13 +63,16 @@ App.Tab = DS.Model.extend({
   tooltipMsg: Em.computed.ifThenElse('isHiddenByFilter', 
Em.I18n.t('services.service.config.nothing.to.display') , ''),
 
   /**
+   * @type {boolean}
+   */
+  allSectionsAreHiddenByFilter: Em.computed.everyBy('sections', 
'isHiddenByFilter', true),
+
+  /**
    * Determines if tab is filtered out (all it's sections should be hidden)
    * If it's an Advanced Tab it can't be hidden
    * @type {boolean}
    */
-  isHiddenByFilter: function () {
-    return this.get('isAdvanced') ? this.get('isAdvancedHidden') : 
this.get('sections').everyProperty('isHiddenByFilter', true);
-  }.property('isAdvanced', '[email protected]', 
'isAdvancedHidden')
+  isHiddenByFilter: Em.computed.ifThenElseByKeys('isAdvanced', 
'isAdvancedHidden', 'allSectionsAreHiddenByFilter')
 
 });
 

http://git-wip-us.apache.org/repos/asf/ambari/blob/21975c9c/ambari-web/app/utils/ember_computed.js
----------------------------------------------------------------------
diff --git a/ambari-web/app/utils/ember_computed.js 
b/ambari-web/app/utils/ember_computed.js
index 08be4e5..e21f4d3 100644
--- a/ambari-web/app/utils/ember_computed.js
+++ b/ambari-web/app/utils/ember_computed.js
@@ -291,6 +291,36 @@ computed.ifThenElse = function (dependentKey, trueValue, 
falseValue) {
 };
 
 /**
+ * A computed property that returns value for trueKey property if dependent 
value is true and falseKey-value otherwise
+ * App.*-keys are supported
+ * <pre>
+ * var o = Em.Object.create({
+ *  p1: true,
+ *  p2: 1,
+ *  p3: 2,
+ *  p4: Em.computed.ifThenElseByKeys('p1', 'p2', 'p3')
+ * });
+ * console.log(o.get('p4')); // 1
+ * o.set('p1', false);
+ * console.log(o.get('p4')); // 2
+ *
+ * o.set('p3', 3);
+ * console.log(o.get('p4')); // 3
+ * </pre>
+ *
+ * @method ifThenElseByKeys
+ * @param {string} dependentKey
+ * @param {string} trueKey
+ * @param {string} falseKey
+ * @returns {Ember.ComputedProperty}
+ */
+computed.ifThenElseByKeys = function (dependentKey, trueKey, falseKey) {
+  return computed(dependentKey, trueKey, falseKey, function () {
+    return smartGet(this, dependentKey) ? smartGet(this, trueKey) : 
smartGet(this, falseKey);
+  });
+};
+
+/**
  * A computed property that is equal to the logical 'and'
  * Takes any number of arguments
  * Returns true if all of them are truly, false - otherwise

http://git-wip-us.apache.org/repos/asf/ambari/blob/21975c9c/ambari-web/test/aliases/computed/filterBy.js
----------------------------------------------------------------------
diff --git a/ambari-web/test/aliases/computed/filterBy.js 
b/ambari-web/test/aliases/computed/filterBy.js
index 154c372..748d12d 100644
--- a/ambari-web/test/aliases/computed/filterBy.js
+++ b/ambari-web/test/aliases/computed/filterBy.js
@@ -52,7 +52,7 @@ App.TestAliases.testAsComputedFilterBy = function (context, 
propertyName, collec
       expect(value).to.eql([]);
     });
 
-    it('should be an array objects from  ' + JSON.stringify(collectionName) + 
' with ' + JSON.stringify(keyName) + ' equal to the ' + 
JSON.stringify(neededValue), function () {
+    it('should be an array of objects from  ' + JSON.stringify(collectionName) 
+ ' with ' + JSON.stringify(keyName) + ' equal to the ' + 
JSON.stringify(neededValue), function () {
       var collection = [{}, {}, {}];
       collection.forEach(function (item) {
         Ember.setFullPath(item, keyName, neededValue);

http://git-wip-us.apache.org/repos/asf/ambari/blob/21975c9c/ambari-web/test/aliases/computed/ifThenElseByKeys.js
----------------------------------------------------------------------
diff --git a/ambari-web/test/aliases/computed/ifThenElseByKeys.js 
b/ambari-web/test/aliases/computed/ifThenElseByKeys.js
new file mode 100644
index 0000000..5da96b9
--- /dev/null
+++ b/ambari-web/test/aliases/computed/ifThenElseByKeys.js
@@ -0,0 +1,65 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+var helpers = App.TestAliases.helpers;
+
+/**
+ *
+ * @param {Em.Object} context
+ * @param {string} propertyName
+ * @param {string} dependentKey
+ * @param {string} trueKey
+ * @param {string} falseKey
+ */
+App.TestAliases.testAsComputedIfThenElseByKeys = function (context, 
propertyName, dependentKey, trueKey, falseKey) {
+
+  describe('#' + propertyName + ' as Em.computed.ifThenElseByKeys', function 
() {
+
+    afterEach(function () {
+      helpers.smartRestoreGet(context);
+    });
+
+    it('has valid dependent keys', function () {
+      
expect(Em.meta(context).descs[propertyName]._dependentKeys).to.eql([dependentKey,
 trueKey, falseKey]);
+    });
+
+    it('should be `trueKey`-value if ' + JSON.stringify(dependentKey) + ' is 
`true`', function () {
+      var hash = {};
+      var neededValue = 'trulyValue';
+      hash[dependentKey] = true;
+      hash[trueKey] = neededValue;
+      helpers.smartStubGet(context, hash)
+        .propertyDidChange(context, propertyName);
+      var value = helpers.smartGet(context, propertyName);
+      expect(value).to.equal(neededValue);
+    });
+
+    it('should be `falseKey`-value if ' + JSON.stringify(dependentKey) + ' is 
`false`', function () {
+      var hash = {};
+      var neededValue = 'falsyValue';
+      hash[dependentKey] = false;
+      hash[falseKey] = neededValue;
+      helpers.smartStubGet(context, hash)
+        .propertyDidChange(context, propertyName);
+      var value = helpers.smartGet(context, propertyName);
+      expect(value).to.equal(neededValue);
+    });
+
+  });
+
+};
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/ambari/blob/21975c9c/ambari-web/test/init_computed_aliases.js
----------------------------------------------------------------------
diff --git a/ambari-web/test/init_computed_aliases.js 
b/ambari-web/test/init_computed_aliases.js
index 81ae5fa..612d831 100644
--- a/ambari-web/test/init_computed_aliases.js
+++ b/ambari-web/test/init_computed_aliases.js
@@ -187,6 +187,7 @@ require('test/aliases/computed/notEqual');
 require('test/aliases/computed/equalProperties');
 require('test/aliases/computed/notEqualProperties');
 require('test/aliases/computed/ifThenElse');
+require('test/aliases/computed/ifThenElseByKeys');
 require('test/aliases/computed/sumProperties');
 require('test/aliases/computed/countBasedMessage');
 require('test/aliases/computed/firstNotBlank');

http://git-wip-us.apache.org/repos/asf/ambari/blob/21975c9c/ambari-web/test/models/configs/objects/service_config_property_test.js
----------------------------------------------------------------------
diff --git 
a/ambari-web/test/models/configs/objects/service_config_property_test.js 
b/ambari-web/test/models/configs/objects/service_config_property_test.js
index 728141b..83e6d74 100644
--- a/ambari-web/test/models/configs/objects/service_config_property_test.js
+++ b/ambari-web/test/models/configs/objects/service_config_property_test.js
@@ -79,24 +79,6 @@ var serviceConfigProperty,
       isOverridable: true
     }
   ],
-  overriddenFalseData = [
-    {
-      overrides: null,
-      isOriginalSCP: true
-    },
-    {
-      overrides: [],
-      isOriginalSCP: true
-    }
-  ],
-  overriddenTrueData = [
-    {
-      overrides: configsData[0].overrides
-    },
-    {
-      isOriginalSCP: false
-    }
-  ],
   removableFalseData = [
     {
       isEditable: false
@@ -172,8 +154,7 @@ var serviceConfigProperty,
     isEditable: true,
     value: 'value',
     savedValue: 'default'
-  },
-  types = ['componentHost', 'componentHosts', 'radio button'];
+  };
 
 
 function getProperty() {
@@ -188,6 +169,8 @@ describe('App.ServiceConfigProperty', function () {
 
   App.TestAliases.testAsComputedAnd(getProperty(), 'hideFinalIcon', 
['!isFinal', 'isNotEditable']);
 
+  App.TestAliases.testAsComputedAnd(getProperty(), 'isActive', ['isVisible', 
'!hiddenBySubSection', '!hiddenBySection']);
+
   describe('#placeholder', function () {
       [
         {
@@ -232,20 +215,7 @@ describe('App.ServiceConfigProperty', function () {
     });
   });
 
-  describe('#isOverridden', function () {
-    overriddenFalseData.forEach(function (item) {
-      it('should be false', function () {
-        serviceConfigProperty.setProperties(item);
-        expect(serviceConfigProperty.get('isOverridden')).to.be.false;
-      });
-    });
-    overriddenTrueData.forEach(function (item) {
-      it('should be true', function () {
-        serviceConfigProperty.setProperties(item);
-        expect(serviceConfigProperty.get('isOverridden')).to.be.true;
-      });
-    });
-  });
+  App.TestAliases.testAsComputedOr(getProperty(), 'isOverridden', 
['overrides.length', '!isOriginalSCP']);
 
   describe('#isRemovable', function () {
     removableFalseData.forEach(function (item) {
@@ -292,18 +262,7 @@ describe('App.ServiceConfigProperty', function () {
     });
   });
 
-  describe('#cantBeUndone', function () {
-    types.forEach(function (item) {
-      it('should be true', function () {
-        serviceConfigProperty.set('displayType', item);
-        expect(serviceConfigProperty.get('cantBeUndone')).to.be.true;
-      });
-    });
-    it('should be false', function () {
-      serviceConfigProperty.set('displayType', 'type');
-      expect(serviceConfigProperty.get('cantBeUndone')).to.be.false;
-    });
-  });
+  App.TestAliases.testAsComputedExistsIn(getProperty(), 'cantBeUndone', 
'displayType', ['componentHost', 'componentHosts', 'radio button']);
 
   describe('#isValid', function () {
     it('should be true', function () {
@@ -347,4 +306,8 @@ describe('App.ServiceConfigProperty', function () {
 
   });
 
+  App.TestAliases.testAsComputedOr(getProperty(), 'hasIssues', ['error', 
'warn', 'overridesWithIssues.length']);
+
+  App.TestAliases.testAsComputedFilterBy(getProperty(), 'overridesWithIssues', 
'overrides', 'hasIssues', true);
+
 });

http://git-wip-us.apache.org/repos/asf/ambari/blob/21975c9c/ambari-web/test/models/configs/theme/sub_section_test.js
----------------------------------------------------------------------
diff --git a/ambari-web/test/models/configs/theme/sub_section_test.js 
b/ambari-web/test/models/configs/theme/sub_section_test.js
index e89bce9..c3ff136 100644
--- a/ambari-web/test/models/configs/theme/sub_section_test.js
+++ b/ambari-web/test/models/configs/theme/sub_section_test.js
@@ -37,6 +37,8 @@ describe('App.SubSection', function () {
 
   App.TestAliases.testAsComputedAnd(getModel(), 'isSectionVisible', 
['!isHiddenByFilter', '!isHiddenByConfig', 'someConfigIsVisible']);
 
+  App.TestAliases.testAsComputedFilterBy(getModel(), 'visibleTabs', 
'subSectionTabs', 'isVisible', true);
+
   describe('#errorsCount', function () {
 
     beforeEach(function () {

http://git-wip-us.apache.org/repos/asf/ambari/blob/21975c9c/ambari-web/test/models/configs/theme/tab_test.js
----------------------------------------------------------------------
diff --git a/ambari-web/test/models/configs/theme/tab_test.js 
b/ambari-web/test/models/configs/theme/tab_test.js
index 3a5f21d..fb940bf 100644
--- a/ambari-web/test/models/configs/theme/tab_test.js
+++ b/ambari-web/test/models/configs/theme/tab_test.js
@@ -28,4 +28,6 @@ describe('App.Tab', function () {
 
   App.TestAliases.testAsComputedIfThenElse(getModel(), 'tooltipMsg', 
'isHiddenByFilter', Em.I18n.t('services.service.config.nothing.to.display') , 
'');
 
+  App.TestAliases.testAsComputedIfThenElseByKeys(getModel(), 
'isHiddenByFilter', 'isAdvanced', 'isAdvancedHidden', 
'allSectionsAreHiddenByFilter');
+
 });

http://git-wip-us.apache.org/repos/asf/ambari/blob/21975c9c/ambari-web/test/utils/ember_computed_test.js
----------------------------------------------------------------------
diff --git a/ambari-web/test/utils/ember_computed_test.js 
b/ambari-web/test/utils/ember_computed_test.js
index 829f2e2..e5754f5 100644
--- a/ambari-web/test/utils/ember_computed_test.js
+++ b/ambari-web/test/utils/ember_computed_test.js
@@ -25,7 +25,9 @@ describe('Ember.computed macros', function () {
       someRandomTestingKey: function () {
         return this.get('someAnotherKey');
       }.property('someAnotherKey'),
-      someAnotherKey: ''
+      someAnotherKey: '',
+      appProp1: 1,
+      appProp2: 2
     });
   });
 
@@ -222,6 +224,44 @@ describe('Ember.computed macros', function () {
 
   });
 
+  describe('#ifThenElseByKeys', function () {
+
+    beforeEach(function () {
+      App.set('someAnotherKey', true);
+      this.obj = Em.Object.create({
+        prop1: true,
+        prop2: Em.computed.ifThenElseByKeys('prop1', 'prop4', 'prop5'),
+        prop3: Em.computed.ifThenElseByKeys('App.someRandomTestingKey', 
'App.appProp1', 'App.appProp2'),
+        prop4: 1,
+        prop5: 2
+      });
+    });
+
+    it('`1` if `prop1` is true', function () {
+      expect(this.obj.get('prop2')).to.equal(1);
+    });
+
+    it('`0` if `prop1` is false', function () {
+      this.obj.set('prop1', false);
+      expect(this.obj.get('prop2')).to.equal(2);
+    });
+
+    it('prop2 dependent keys are valid', function () {
+      expect(Em.meta(this.obj).descs.prop2._dependentKeys).to.eql(['prop1', 
'prop4', 'prop5']);
+    });
+
+    it('prop3 depends on App.* key', function () {
+      expect(this.obj.get('prop3')).to.equal(1);
+      App.set('someAnotherKey', false);
+      expect(this.obj.get('prop3')).to.equal(2);
+    });
+
+    it('prop3 dependent keys are valid', function () {
+      
expect(Em.meta(this.obj).descs.prop3._dependentKeys).to.eql(['App.someRandomTestingKey',
 'App.appProp1', 'App.appProp2']);
+    });
+
+  });
+
   describe('#and', function () {
 
     beforeEach(function () {

Reply via email to