init_computed_aliases.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. /**
  19. *
  20. *
  21. * @class App.TestAliases
  22. */
  23. App.TestAliases = {
  24. helpers: {
  25. /**
  26. * Get needed value (basing on <code>key</code>) from <code>self</code> or <code>App</code>
  27. *
  28. * @param {Ember.Object} self
  29. * @param {string} key
  30. * @returns {*}
  31. */
  32. smartGet: function (self, key) {
  33. var isApp = key.startsWith('App.');
  34. var name = isApp ? key.replace('App.', '') : key;
  35. return isApp ? App.get(name) : self.get(name);
  36. },
  37. /**
  38. * Stub <code>get</code> for <code>App</code> or <code>self</code>
  39. *
  40. * @returns {App.TestAliases}
  41. */
  42. smartStubGet: function () {
  43. var args = [].slice.call(arguments);
  44. if (args.length === 3) {
  45. return this._stubOneKey.apply(this, args);
  46. }
  47. return this._stubManyKeys.apply(this, args)
  48. },
  49. /**
  50. * Trigger recalculation of the needed property in the <code>self</code>
  51. * or in the <code>App</code> (depends on <code>propertyName</code>)
  52. *
  53. * @param {Ember.Object} self
  54. * @param {string} propertyName
  55. * @returns {App.TestAliases}
  56. */
  57. propertyDidChange: function (self, propertyName) {
  58. var isApp = propertyName.startsWith('App.');
  59. var name = isApp ? propertyName.replace('App.', '') : propertyName;
  60. var context = isApp ? App : self;
  61. Em.propertyDidChange(context, name);
  62. return this;
  63. },
  64. /**
  65. * Try to restore (@see sinon.restore) <code>get</code> for <code>App</code> and <code>context</code>
  66. *
  67. * @param {Ember.Object} context
  68. * @returns {App.TestAliases}
  69. */
  70. smartRestoreGet: function(context) {
  71. Em.tryInvoke(context.get, 'restore');
  72. Em.tryInvoke(App.get, 'restore');
  73. return this;
  74. },
  75. /**
  76. * Stub <code>get</code>-method for <code>App</code> or <code>self</code> (depends on <code>dependentKey</code>)
  77. * to return <code>value</code> if <code>dependentKey</code> is get
  78. *
  79. * @param {Ember.Object} self
  80. * @param {string} dependentKey
  81. * @param {*} value
  82. * @returns {App.TestAliases}
  83. * @private
  84. */
  85. _stubOneKey: function (self,dependentKey, value) {
  86. var isApp = dependentKey.startsWith('App.');
  87. var name = isApp ? dependentKey.replace('App.', '') : dependentKey;
  88. var context = isApp ? App : self;
  89. sinon.stub(context, 'get', function (k) {
  90. return k === name ? value : Em.get(context, k);
  91. });
  92. return this;
  93. },
  94. /**
  95. * Stub <code>get</code>-method for <code>App</code> or <code>self</code> (depends on </code>hash</code>-keys)
  96. * If some key is starts with 'App.' it will be used in the App-stub,
  97. * otherwise it will be used in thw self-stub
  98. *
  99. * @param {Ember.Object} self
  100. * @param {object} hash
  101. * @returns {App.TestAliases}
  102. * @private
  103. */
  104. _stubManyKeys: function (self, hash) {
  105. var hashForApp = {}; // used in the App-stub
  106. var hashForSelf = {}; // used in the self-stub
  107. Object.keys(hash).forEach(function(key) {
  108. var isApp = key.startsWith('App.');
  109. var name = isApp ? key.replace('App.', '') : key;
  110. if(isApp) {
  111. hashForApp[name] = hash[key];
  112. }
  113. else {
  114. hashForSelf[name] = hash[key];
  115. }
  116. });
  117. sinon.stub(App, 'get', function (k) {
  118. if (hashForApp.hasOwnProperty(k)) {
  119. return hashForApp[k];
  120. }
  121. return Em.get(App, k);
  122. });
  123. sinon.stub(self, 'get', function (k) {
  124. if (hashForSelf.hasOwnProperty(k)) {
  125. return hashForSelf[k];
  126. }
  127. return Em.get(self, k);
  128. });
  129. return this;
  130. },
  131. /**
  132. * Generates array of all possible boolean combinations
  133. * Example:
  134. * <code>
  135. * var keys = ['a', 'b'];
  136. * var result = getBinaryCombos(keys);
  137. * console.log(result); // [{a: true, b: true}, {a: true, b: false}, {a: false, b: true}, {a: false, b: false}]
  138. * </code>
  139. *
  140. * @param {string[]} dependentKeys
  141. * @returns {Array}
  142. */
  143. getBinaryCombos: function (dependentKeys) {
  144. var n = dependentKeys.length;
  145. var result = [];
  146. var allCombos = Math.pow(2, n);
  147. for (var y = 0; y < allCombos; y++) {
  148. var combo = {};
  149. for (var x = 0; x < n; x++) {
  150. combo[dependentKeys[x]] = !!(y >> x & 1);
  151. }
  152. result.push(combo);
  153. }
  154. return result;
  155. }
  156. }
  157. };
  158. require('test/aliases/computed/equal');
  159. require('test/aliases/computed/notEqual');
  160. require('test/aliases/computed/equalProperties');
  161. require('test/aliases/computed/notEqualProperties');
  162. require('test/aliases/computed/ifThenElse');
  163. require('test/aliases/computed/sumProperties');
  164. require('test/aliases/computed/countBasedMessage');
  165. require('test/aliases/computed/firstNotBlank');
  166. require('test/aliases/computed/percents');
  167. require('test/aliases/computed/existsIn');
  168. require('test/aliases/computed/notExistsIn');
  169. require('test/aliases/computed/alias');
  170. require('test/aliases/computed/gte');
  171. require('test/aliases/computed/gt');
  172. require('test/aliases/computed/gteProperties');
  173. require('test/aliases/computed/gtProperties');
  174. require('test/aliases/computed/lte');
  175. require('test/aliases/computed/lt');
  176. require('test/aliases/computed/lteProperties');
  177. require('test/aliases/computed/ltProperties');
  178. require('test/aliases/computed/someBy');
  179. require('test/aliases/computed/everyBy');
  180. require('test/aliases/computed/mapBy');
  181. require('test/aliases/computed/filterBy');
  182. require('test/aliases/computed/findBy');
  183. require('test/aliases/computed/sumBy');
  184. require('test/aliases/computed/and');
  185. require('test/aliases/computed/or');