ember_computed_test.js 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785
  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. require('utils/ember_computed');
  19. describe('Ember.computed macros', function () {
  20. beforeEach(function () {
  21. App.reopen({
  22. someRandomTestingKey: function () {
  23. return this.get('someAnotherKey');
  24. }.property('someAnotherKey'),
  25. someAnotherKey: ''
  26. });
  27. });
  28. afterEach(function () {
  29. delete App.someAnotherKey;
  30. delete App.someRandomTestingKey;
  31. });
  32. describe('#equal', function () {
  33. beforeEach(function () {
  34. App.setProperties({
  35. someAnotherKey: '123'
  36. });
  37. this.obj = Em.Object.create({
  38. prop1: '123',
  39. prop2: Em.computed.equal('prop1', '123'),
  40. prop3: Em.computed.equal('App.someRandomTestingKey', '123')
  41. });
  42. });
  43. it('`true` if values are equal', function () {
  44. expect(this.obj.get('prop2')).to.be.true;
  45. });
  46. it('`false` if values are not equal', function () {
  47. this.obj.set('prop1', '321');
  48. expect(this.obj.get('prop2')).to.be.false;
  49. });
  50. it('`prop3` depends on App.* key', function () {
  51. expect(this.obj.get('prop3')).to.be.true;
  52. App.set('someAnotherKey', '');
  53. expect(this.obj.get('prop3')).to.be.false;
  54. });
  55. it('prop3 dependent keys are valid', function () {
  56. expect(Em.meta(this.obj).descs.prop3._dependentKeys).to.eql(['App.someRandomTestingKey']);
  57. });
  58. });
  59. describe('#notEqual', function () {
  60. beforeEach(function () {
  61. App.setProperties({
  62. someAnotherKey: '123'
  63. });
  64. this.obj = Em.Object.create({
  65. prop1: '123',
  66. prop2: Em.computed.notEqual('prop1', '123'),
  67. prop3: Em.computed.notEqual('App.someRandomTestingKey', '123')
  68. });
  69. });
  70. it('`false` if values are equal', function () {
  71. expect(this.obj.get('prop2')).to.be.false;
  72. });
  73. it('`true` if values are not equal', function () {
  74. this.obj.set('prop1', '321');
  75. expect(this.obj.get('prop2')).to.be.true;
  76. });
  77. it('`prop3` depends on App.* key', function () {
  78. expect(this.obj.get('prop3')).to.be.false;
  79. App.set('someAnotherKey', '');
  80. expect(this.obj.get('prop3')).to.be.true;
  81. });
  82. it('prop3 dependent keys are valid', function () {
  83. expect(Em.meta(this.obj).descs.prop3._dependentKeys).to.eql(['App.someRandomTestingKey']);
  84. });
  85. });
  86. describe('#equalProperties', function () {
  87. beforeEach(function () {
  88. App.set('someAnotherKey', '123');
  89. this.obj = Em.Object.create({
  90. prop1: '123',
  91. prop2: '123',
  92. prop3: Em.computed.equalProperties('prop1', 'prop2'),
  93. prop4: Em.computed.equalProperties('App.someRandomTestingKey', 'prop2'),
  94. prop5: Em.computed.equalProperties('prop1', 'App.someRandomTestingKey')
  95. });
  96. });
  97. it('`true` if values are equal', function () {
  98. expect(this.obj.get('prop3')).to.be.true;
  99. });
  100. it('`false` if values are not equal', function () {
  101. this.obj.set('prop1', '321');
  102. expect(this.obj.get('prop3')).to.be.false;
  103. });
  104. it('prop4 depends on App.* key', function () {
  105. expect(this.obj.get('prop4')).to.be.true;
  106. App.set('someAnotherKey', '');
  107. expect(this.obj.get('prop4')).to.be.false;
  108. });
  109. it('prop5 depends on App.* key', function () {
  110. expect(this.obj.get('prop5')).to.be.true;
  111. App.set('someAnotherKey', '');
  112. expect(this.obj.get('prop5')).to.be.false;
  113. });
  114. it('prop4 dependent keys are valid', function () {
  115. expect(Em.meta(this.obj).descs.prop4._dependentKeys).to.eql(['App.someRandomTestingKey', 'prop2']);
  116. });
  117. it('prop5 dependent keys are valid', function () {
  118. expect(Em.meta(this.obj).descs.prop5._dependentKeys).to.eql(['prop1', 'App.someRandomTestingKey']);
  119. });
  120. });
  121. describe('#notEqualProperties', function () {
  122. beforeEach(function () {
  123. App.set('someAnotherKey', '123');
  124. this.obj = Em.Object.create({
  125. prop1: '123',
  126. prop2: '123',
  127. prop3: Em.computed.notEqualProperties('prop1', 'prop2'),
  128. prop4: Em.computed.notEqualProperties('App.someRandomTestingKey', 'prop2'),
  129. prop5: Em.computed.notEqualProperties('prop1', 'App.someRandomTestingKey')
  130. });
  131. });
  132. it('`false` if values are equal', function () {
  133. expect(this.obj.get('prop3')).to.be.false;
  134. });
  135. it('`true` if values are not equal', function () {
  136. this.obj.set('prop1', '321');
  137. expect(this.obj.get('prop3')).to.be.true;
  138. });
  139. it('prop4 depends on App.* key', function () {
  140. expect(this.obj.get('prop4')).to.be.false;
  141. App.set('someAnotherKey', '');
  142. expect(this.obj.get('prop4')).to.be.true;
  143. });
  144. it('prop5 depends on App.* key', function () {
  145. expect(this.obj.get('prop5')).to.be.false;
  146. App.set('someAnotherKey', '');
  147. expect(this.obj.get('prop5')).to.be.true;
  148. });
  149. it('prop4 dependent keys are valid', function () {
  150. expect(Em.meta(this.obj).descs.prop4._dependentKeys).to.eql(['App.someRandomTestingKey', 'prop2']);
  151. });
  152. it('prop5 dependent keys are valid', function () {
  153. expect(Em.meta(this.obj).descs.prop5._dependentKeys).to.eql(['prop1', 'App.someRandomTestingKey']);
  154. });
  155. });
  156. describe('#ifThenElse', function () {
  157. beforeEach(function () {
  158. App.set('someAnotherKey', true);
  159. this.obj = Em.Object.create({
  160. prop1: true,
  161. prop2: Em.computed.ifThenElse('prop1', '1', '0'),
  162. prop3: Em.computed.ifThenElse('App.someRandomTestingKey', '1', '0')
  163. });
  164. });
  165. it('`1` if `prop1` is true', function () {
  166. expect(this.obj.get('prop2')).to.equal('1');
  167. });
  168. it('`0` if `prop1` is false', function () {
  169. this.obj.set('prop1', false);
  170. expect(this.obj.get('prop2')).to.equal('0');
  171. });
  172. it('prop3 depends on App.* key', function () {
  173. expect(this.obj.get('prop3')).to.equal('1');
  174. App.set('someAnotherKey', false);
  175. expect(this.obj.get('prop3')).to.equal('0');
  176. });
  177. it('prop3 dependent keys are valid', function () {
  178. expect(Em.meta(this.obj).descs.prop3._dependentKeys).to.eql(['App.someRandomTestingKey']);
  179. });
  180. });
  181. describe('#and', function () {
  182. beforeEach(function () {
  183. App.setProperties({
  184. someAnotherKey: true
  185. });
  186. this.obj = Em.Object.create({
  187. prop1: true,
  188. prop2: true,
  189. prop3: true,
  190. prop4: Em.computed.and('prop1', 'prop2', 'prop3'),
  191. prop5: Em.computed.and('prop1', '!prop2', '!prop3'),
  192. prop6: Em.computed.and('App.someRandomTestingKey', 'prop1'),
  193. prop7: Em.computed.and('!App.someRandomTestingKey', 'prop1')
  194. });
  195. });
  196. it('prop4 `true` if all dependent properties are true', function () {
  197. expect(this.obj.get('prop4')).to.be.true;
  198. });
  199. it('prop4 `false` if at elast one dependent property is false', function () {
  200. this.obj.set('prop2', false);
  201. expect(this.obj.get('prop4')).to.be.false;
  202. });
  203. it('prop5 dependent keys are valid', function () {
  204. expect(Em.meta(this.obj).descs.prop5._dependentKeys).to.eql(['prop1', 'prop2', 'prop3']);
  205. });
  206. it('prop5 `false` if some inverted dependent properties is true', function () {
  207. expect(this.obj.get('prop5')).to.be.false;
  208. });
  209. it('prop5 `false` if some inverted dependent properties is true (2)', function () {
  210. this.obj.set('prop1', true);
  211. expect(this.obj.get('prop5')).to.be.false;
  212. });
  213. it('prop5 `true` ', function () {
  214. this.obj.set('prop2', false);
  215. this.obj.set('prop3', false);
  216. expect(this.obj.get('prop5')).to.be.true;
  217. });
  218. it('`prop6` depends on App.* key', function () {
  219. expect(this.obj.get('prop6')).to.be.true;
  220. App.set('someAnotherKey', false);
  221. expect(this.obj.get('prop6')).to.be.false;
  222. App.set('someAnotherKey', true);
  223. expect(this.obj.get('prop6')).to.be.true;
  224. });
  225. it('prop6 dependent keys are valid', function () {
  226. expect(Em.meta(this.obj).descs.prop6._dependentKeys).to.eql(['App.someRandomTestingKey', 'prop1']);
  227. });
  228. it('`prop7` depends on inverted App.* key', function () {
  229. expect(this.obj.get('prop7')).to.be.false;
  230. App.set('someAnotherKey', false);
  231. expect(this.obj.get('prop7')).to.be.true;
  232. App.set('someAnotherKey', true);
  233. expect(this.obj.get('prop7')).to.be.false;
  234. });
  235. it('prop7 dependent keys are valid', function () {
  236. expect(Em.meta(this.obj).descs.prop7._dependentKeys).to.eql(['App.someRandomTestingKey', 'prop1']);
  237. });
  238. });
  239. describe('#or', function () {
  240. beforeEach(function () {
  241. App.setProperties({
  242. someAnotherKey: true
  243. });
  244. this.obj = Em.Object.create({
  245. prop1: false,
  246. prop2: false,
  247. prop3: false,
  248. prop4: Em.computed.or('prop1', 'prop2', 'prop3'),
  249. prop5: Em.computed.or('!prop1', '!prop2', '!prop3'),
  250. prop6: Em.computed.or('App.someRandomTestingKey', 'prop1'),
  251. prop7: Em.computed.or('!App.someRandomTestingKey', 'prop1')
  252. });
  253. });
  254. it('`false` if all dependent properties are false', function () {
  255. expect(this.obj.get('prop4')).to.be.false;
  256. });
  257. it('`true` if at elast one dependent property is true', function () {
  258. this.obj.set('prop2', true);
  259. expect(this.obj.get('prop4')).to.be.true;
  260. });
  261. it('prop5 dependent keys are valid', function () {
  262. expect(Em.meta(this.obj).descs.prop5._dependentKeys).to.eql(['prop1', 'prop2', 'prop3']);
  263. });
  264. it('prop5 `true` if some inverted dependent properties is true', function () {
  265. expect(this.obj.get('prop5')).to.be.true;
  266. });
  267. it('prop5 `true` if some inverted dependent properties is true (2)', function () {
  268. this.obj.set('prop1', true);
  269. expect(this.obj.get('prop5')).to.be.true;
  270. });
  271. it('prop5 `false` ', function () {
  272. this.obj.set('prop1', true);
  273. this.obj.set('prop2', true);
  274. this.obj.set('prop3', true);
  275. expect(this.obj.get('prop5')).to.be.false;
  276. });
  277. it('`prop6` depends on App.* key', function () {
  278. expect(this.obj.get('prop6')).to.be.true;
  279. App.set('someAnotherKey', false);
  280. expect(this.obj.get('prop6')).to.be.false;
  281. App.set('someAnotherKey', true);
  282. expect(this.obj.get('prop6')).to.be.true;
  283. });
  284. it('prop6 dependent keys are valid', function () {
  285. expect(Em.meta(this.obj).descs.prop6._dependentKeys).to.eql(['App.someRandomTestingKey', 'prop1']);
  286. });
  287. it('`prop7` depends on inverted App.* key', function () {
  288. expect(this.obj.get('prop7')).to.be.false;
  289. App.set('someAnotherKey', false);
  290. expect(this.obj.get('prop7')).to.be.true;
  291. App.set('someAnotherKey', true);
  292. expect(this.obj.get('prop7')).to.be.false;
  293. });
  294. it('prop7 dependent keys are valid', function () {
  295. expect(Em.meta(this.obj).descs.prop7._dependentKeys).to.eql(['App.someRandomTestingKey', 'prop1']);
  296. });
  297. });
  298. describe('#sumProperties', function () {
  299. beforeEach(function () {
  300. App.setProperties({
  301. someAnotherKey: 5
  302. });
  303. this.obj = Em.Object.create({
  304. prop1: 1,
  305. prop2: 2,
  306. prop3: 3,
  307. prop4: Em.computed.sumProperties('prop1', 'prop2', 'prop3'),
  308. prop5: Em.computed.sumProperties('prop1', 'prop2', 'App.someRandomTestingKey')
  309. });
  310. });
  311. it('should be sum of dependent values', function () {
  312. expect(this.obj.get('prop4')).to.equal(6);
  313. });
  314. it('should be updated if some dependent value is changed', function () {
  315. this.obj.set('prop1', 4);
  316. expect(this.obj.get('prop4')).to.equal(9);
  317. });
  318. it('should be updated if some dependent value is string', function () {
  319. this.obj.set('prop1', '4');
  320. expect(this.obj.get('prop4')).to.equal(9);
  321. });
  322. it('should be updated if some dependent value is string (2)', function () {
  323. this.obj.set('prop1', '4.5');
  324. expect(this.obj.get('prop4')).to.equal(9.5);
  325. });
  326. it('should be updated if some dependent value is null', function () {
  327. this.obj.set('prop1', null);
  328. expect(this.obj.get('prop4')).to.equal(5);
  329. });
  330. it('`prop5` depends on App.* key', function () {
  331. expect(this.obj.get('prop5')).to.equal(8);
  332. App.set('someAnotherKey', 6);
  333. expect(this.obj.get('prop5')).to.equal(9);
  334. });
  335. it('prop5 dependent keys are valid', function () {
  336. expect(Em.meta(this.obj).descs.prop5._dependentKeys).to.eql(['prop1', 'prop2', 'App.someRandomTestingKey']);
  337. });
  338. });
  339. describe('#gte', function () {
  340. beforeEach(function () {
  341. App.set('someAnotherKey', 4);
  342. this.obj = Em.Object.create({
  343. prop1: 2,
  344. prop2: Em.computed.gte('prop1', 3),
  345. prop3: Em.computed.gte('App.someRandomTestingKey', 3)
  346. });
  347. });
  348. it('`false` if value is less than needed', function () {
  349. expect(this.obj.get('prop2')).to.be.false;
  350. });
  351. it('`true` if value is equal to the needed', function () {
  352. this.obj.set('prop1', 3);
  353. expect(this.obj.get('prop2')).to.be.true;
  354. });
  355. it('`true` if value is greater than needed', function () {
  356. this.obj.set('prop1', 4);
  357. expect(this.obj.get('prop2')).to.be.true;
  358. });
  359. it('prop3 depends on App.* key', function () {
  360. expect(this.obj.get('prop3')).to.be.true;
  361. App.set('someAnotherKey', 3);
  362. expect(this.obj.get('prop3')).to.be.true;
  363. App.set('someAnotherKey', 2);
  364. expect(this.obj.get('prop3')).to.be.false;
  365. });
  366. it('prop3 dependent keys are valid', function () {
  367. expect(Em.meta(this.obj).descs.prop3._dependentKeys).to.eql(['App.someRandomTestingKey']);
  368. });
  369. });
  370. describe('#gteProperties', function () {
  371. beforeEach(function () {
  372. App.set('someAnotherKey', 4);
  373. this.obj = Em.Object.create({
  374. prop1: 2,
  375. prop2: 3,
  376. prop3: Em.computed.gteProperties('prop1', 'prop2'),
  377. prop4: Em.computed.gteProperties('App.someRandomTestingKey', 'prop2'),
  378. prop5: Em.computed.gteProperties('prop1', 'App.someRandomTestingKey')
  379. });
  380. });
  381. it('`false` if value is less than needed', function () {
  382. expect(this.obj.get('prop3')).to.be.false;
  383. });
  384. it('`true` if value is equal to the needed', function () {
  385. this.obj.set('prop1', 3);
  386. expect(this.obj.get('prop3')).to.be.true;
  387. });
  388. it('`true` if value is greater than needed', function () {
  389. this.obj.set('prop1', 4);
  390. expect(this.obj.get('prop3')).to.be.true;
  391. });
  392. it('prop4 depends on App.* key', function () {
  393. expect(this.obj.get('prop4')).to.be.true;
  394. App.set('someAnotherKey', 3);
  395. expect(this.obj.get('prop4')).to.be.true;
  396. App.set('someAnotherKey', 2);
  397. expect(this.obj.get('prop4')).to.be.false;
  398. });
  399. it('prop5 depends on App.* key', function () {
  400. expect(this.obj.get('prop5')).to.be.false;
  401. App.set('someAnotherKey', 2);
  402. expect(this.obj.get('prop5')).to.be.true;
  403. App.set('someAnotherKey', 1);
  404. expect(this.obj.get('prop5')).to.be.true;
  405. });
  406. it('prop4 dependent keys are valid', function () {
  407. expect(Em.meta(this.obj).descs.prop4._dependentKeys).to.eql(['App.someRandomTestingKey', 'prop2']);
  408. });
  409. it('prop5 dependent keys are valid', function () {
  410. expect(Em.meta(this.obj).descs.prop5._dependentKeys).to.eql(['prop1', 'App.someRandomTestingKey']);
  411. });
  412. });
  413. describe('#lte', function () {
  414. beforeEach(function () {
  415. App.set('someAnotherKey', 0);
  416. this.obj = Em.Object.create({
  417. prop1: 2,
  418. prop2: Em.computed.lte('prop1', 1),
  419. prop3: Em.computed.lte('App.someRandomTestingKey', 1)
  420. });
  421. });
  422. it('`false` if value is greater than needed', function () {
  423. expect(this.obj.get('prop2')).to.be.false;
  424. });
  425. it('`true` if value is equal to the needed', function () {
  426. this.obj.set('prop1', 1);
  427. expect(this.obj.get('prop2')).to.be.true;
  428. });
  429. it('`true` if value is less than needed', function () {
  430. this.obj.set('prop1', 0);
  431. expect(this.obj.get('prop2')).to.be.true;
  432. });
  433. it('prop3 depends on App.* key', function () {
  434. expect(this.obj.get('prop3')).to.be.true;
  435. App.set('someAnotherKey', 1);
  436. expect(this.obj.get('prop3')).to.be.true;
  437. App.set('someAnotherKey', 2);
  438. expect(this.obj.get('prop3')).to.be.false;
  439. });
  440. it('prop3 dependent keys are valid', function () {
  441. expect(Em.meta(this.obj).descs.prop3._dependentKeys).to.eql(['App.someRandomTestingKey']);
  442. });
  443. });
  444. describe('#lteProperties', function () {
  445. beforeEach(function () {
  446. App.set('someAnotherKey', 1);
  447. this.obj = Em.Object.create({
  448. prop1: 2,
  449. prop2: 1,
  450. prop3: Em.computed.lteProperties('prop1', 'prop2'),
  451. prop4: Em.computed.lteProperties('App.someRandomTestingKey', 'prop2'),
  452. prop5: Em.computed.lteProperties('prop1', 'App.someRandomTestingKey')
  453. });
  454. });
  455. it('`false` if d1 is greater than d2', function () {
  456. expect(this.obj.get('prop3')).to.be.false;
  457. });
  458. it('`true` if d1 is equal to the d2', function () {
  459. this.obj.set('prop1', 1);
  460. expect(this.obj.get('prop3')).to.be.true;
  461. });
  462. it('`true` if d1 is less than d2', function () {
  463. this.obj.set('prop1', 0);
  464. expect(this.obj.get('prop3')).to.be.true;
  465. });
  466. it('prop4 depends on App.* key', function () {
  467. expect(this.obj.get('prop4')).to.be.true;
  468. App.set('someAnotherKey', 0);
  469. expect(this.obj.get('prop4')).to.be.true;
  470. App.set('someAnotherKey', 2);
  471. expect(this.obj.get('prop4')).to.be.false;
  472. });
  473. it('prop5 depends on App.* key', function () {
  474. expect(this.obj.get('prop5')).to.be.false;
  475. App.set('someAnotherKey', 2);
  476. expect(this.obj.get('prop5')).to.be.true;
  477. App.set('someAnotherKey', 3);
  478. expect(this.obj.get('prop5')).to.be.true;
  479. });
  480. it('prop4 dependent keys are valid', function () {
  481. expect(Em.meta(this.obj).descs.prop4._dependentKeys).to.eql(['App.someRandomTestingKey', 'prop2']);
  482. });
  483. it('prop5 dependent keys are valid', function () {
  484. expect(Em.meta(this.obj).descs.prop5._dependentKeys).to.eql(['prop1', 'App.someRandomTestingKey']);
  485. });
  486. });
  487. describe('#gt', function () {
  488. beforeEach(function () {
  489. App.set('someAnotherKey', 4);
  490. this.obj = Em.Object.create({
  491. prop1: 2,
  492. prop2: Em.computed.gt('prop1', 3),
  493. prop3: Em.computed.gt('App.someRandomTestingKey', 3)
  494. });
  495. });
  496. it('`false` if value is less than needed', function () {
  497. expect(this.obj.get('prop2')).to.be.false;
  498. });
  499. it('`false` if value is equal to the needed', function () {
  500. this.obj.set('prop1', 3);
  501. expect(this.obj.get('prop2')).to.be.false;
  502. });
  503. it('`true` if value is greater than needed', function () {
  504. this.obj.set('prop1', 4);
  505. expect(this.obj.get('prop2')).to.be.true;
  506. });
  507. it('prop3 depends on App.* key', function () {
  508. expect(this.obj.get('prop3')).to.be.true;
  509. App.set('someAnotherKey', 3);
  510. expect(this.obj.get('prop3')).to.be.false;
  511. App.set('someAnotherKey', 2);
  512. expect(this.obj.get('prop3')).to.be.false;
  513. });
  514. it('prop3 dependent keys are valid', function () {
  515. expect(Em.meta(this.obj).descs.prop3._dependentKeys).to.eql(['App.someRandomTestingKey']);
  516. });
  517. });
  518. describe('#gtProperties', function () {
  519. beforeEach(function () {
  520. App.set('someAnotherKey', 4);
  521. this.obj = Em.Object.create({
  522. prop1: 2,
  523. prop2: 3,
  524. prop3: Em.computed.gtProperties('prop1', 'prop2'),
  525. prop4: Em.computed.gtProperties('App.someRandomTestingKey', 'prop2'),
  526. prop5: Em.computed.gtProperties('prop1', 'App.someRandomTestingKey')
  527. });
  528. });
  529. it('`false` if value is less than needed', function () {
  530. expect(this.obj.get('prop3')).to.be.false;
  531. });
  532. it('`false` if value is equal to the needed', function () {
  533. this.obj.set('prop1', 3);
  534. expect(this.obj.get('prop3')).to.be.false;
  535. });
  536. it('`true` if value is greater than needed', function () {
  537. this.obj.set('prop1', 4);
  538. expect(this.obj.get('prop3')).to.be.true;
  539. });
  540. it('prop4 depends on App.* key', function () {
  541. expect(this.obj.get('prop4')).to.be.true;
  542. App.set('someAnotherKey', 3);
  543. expect(this.obj.get('prop4')).to.be.false;
  544. App.set('someAnotherKey', 2);
  545. expect(this.obj.get('prop4')).to.be.false;
  546. });
  547. it('prop5 depends on App.* key', function () {
  548. expect(this.obj.get('prop5')).to.be.false;
  549. App.set('someAnotherKey', 2);
  550. expect(this.obj.get('prop5')).to.be.false;
  551. App.set('someAnotherKey', 1);
  552. expect(this.obj.get('prop5')).to.be.true;
  553. });
  554. it('prop4 dependent keys are valid', function () {
  555. expect(Em.meta(this.obj).descs.prop4._dependentKeys).to.eql(['App.someRandomTestingKey', 'prop2']);
  556. });
  557. it('prop5 dependent keys are valid', function () {
  558. expect(Em.meta(this.obj).descs.prop5._dependentKeys).to.eql(['prop1', 'App.someRandomTestingKey']);
  559. });
  560. });
  561. describe('#lt', function () {
  562. beforeEach(function () {
  563. App.set('someAnotherKey', 0);
  564. this.obj = Em.Object.create({
  565. prop1: 2,
  566. prop2: Em.computed.lt('prop1', 1),
  567. prop3: Em.computed.lt('App.someRandomTestingKey', 1)
  568. });
  569. });
  570. it('`false` if value is greater than needed', function () {
  571. expect(this.obj.get('prop2')).to.be.false;
  572. });
  573. it('`false` if value is equal to the needed', function () {
  574. this.obj.set('prop1', 1);
  575. expect(this.obj.get('prop2')).to.be.false;
  576. });
  577. it('`true` if value is less than needed', function () {
  578. this.obj.set('prop1', 0);
  579. expect(this.obj.get('prop2')).to.be.true;
  580. });
  581. it('prop3 depends on App.* key', function () {
  582. expect(this.obj.get('prop3')).to.be.true;
  583. App.set('someAnotherKey', 1);
  584. expect(this.obj.get('prop3')).to.be.false;
  585. App.set('someAnotherKey', 2);
  586. expect(this.obj.get('prop3')).to.be.false;
  587. });
  588. it('prop3 dependent keys are valid', function () {
  589. expect(Em.meta(this.obj).descs.prop3._dependentKeys).to.eql(['App.someRandomTestingKey']);
  590. });
  591. });
  592. describe('#ltProperties', function () {
  593. beforeEach(function () {
  594. App.set('someAnotherKey', 1);
  595. this.obj = Em.Object.create({
  596. prop1: 2,
  597. prop2: 1,
  598. prop3: Em.computed.ltProperties('prop1', 'prop2'),
  599. prop4: Em.computed.ltProperties('App.someRandomTestingKey', 'prop2'),
  600. prop5: Em.computed.ltProperties('prop1', 'App.someRandomTestingKey')
  601. });
  602. });
  603. it('`false` if d1 is greater than d2', function () {
  604. expect(this.obj.get('prop3')).to.be.false;
  605. });
  606. it('`false` if d1 is equal to the d2', function () {
  607. this.obj.set('prop1', 1);
  608. expect(this.obj.get('prop3')).to.be.false;
  609. });
  610. it('`true` if d1 is less than d2', function () {
  611. this.obj.set('prop1', 0);
  612. expect(this.obj.get('prop3')).to.be.true;
  613. });
  614. it('prop4 depends on App.* key', function () {
  615. expect(this.obj.get('prop4')).to.be.false;
  616. App.set('someAnotherKey', 0);
  617. expect(this.obj.get('prop4')).to.be.true;
  618. App.set('someAnotherKey', 2);
  619. expect(this.obj.get('prop4')).to.be.false;
  620. });
  621. it('prop5 depends on App.* key', function () {
  622. expect(this.obj.get('prop5')).to.be.false;
  623. App.set('someAnotherKey', 2);
  624. expect(this.obj.get('prop5')).to.be.false;
  625. App.set('someAnotherKey', 3);
  626. expect(this.obj.get('prop5')).to.be.true;
  627. });
  628. it('prop4 dependent keys are valid', function () {
  629. expect(Em.meta(this.obj).descs.prop4._dependentKeys).to.eql(['App.someRandomTestingKey', 'prop2']);
  630. });
  631. it('prop5 dependent keys are valid', function () {
  632. expect(Em.meta(this.obj).descs.prop5._dependentKeys).to.eql(['prop1', 'App.someRandomTestingKey']);
  633. });
  634. });
  635. describe('#match', function () {
  636. beforeEach(function () {
  637. this.obj = Em.Object.create({
  638. prop1: 'abc',
  639. prop2: Em.computed.match('prop1', /^ab/)
  640. })
  641. });
  642. it('`true` if value match regexp', function () {
  643. expect(this.obj.get('prop2')).to.be.true;
  644. });
  645. it('`true` if value match regexp (2)', function () {
  646. this.obj.set('prop1', 'abaaa');
  647. expect(this.obj.get('prop2')).to.be.true;
  648. });
  649. it('`false` if value doesn\'t match regexp', function () {
  650. this.obj.set('prop1', '!!!!');
  651. expect(this.obj.get('prop2')).to.be.false;
  652. });
  653. it('`prop2` has valid dependent keys', function () {
  654. expect(Em.meta(this.obj).descs.prop2._dependentKeys).to.eql(['prop1']);
  655. });
  656. });
  657. describe('#someBy', function () {
  658. beforeEach(function () {
  659. this.obj = Em.Object.create({
  660. prop1: [{a: 1}, {a: 2}, {a: 3}],
  661. prop2: Em.computed.someBy('prop1', 'a', 2)
  662. });
  663. });
  664. it('`true` if some collection item has needed property value', function () {
  665. expect(this.obj.get('prop2')).to.be.true;
  666. });
  667. it('`false` if on one collection item doesn\'t have needed property value', function () {
  668. this.obj.set('prop1.1.a', 3);
  669. expect(this.obj.get('prop2')).to.be.false;
  670. });
  671. it('`false` for null/undefined collection', function () {
  672. this.obj.set('prop1', null);
  673. expect(this.obj.get('prop2')).to.be.false;
  674. });
  675. it('`prop2` has valid dependent keys', function () {
  676. expect(Em.meta(this.obj).descs.prop2._dependentKeys).to.eql(['prop1.@each.a']);
  677. });
  678. });
  679. describe('#someByKey', function () {
  680. beforeEach(function () {
  681. App.setProperties({
  682. someAnotherKey: 2
  683. });
  684. this.obj = Em.Object.create({
  685. prop1: [{a: 1}, {a: 2}, {a: 3}],
  686. prop2: Em.computed.someByKey('prop1', 'a', 'value1'),
  687. prop3: Em.computed.someByKey('prop1', 'a', 'App.someRandomTestingKey'),
  688. value1: 2
  689. });
  690. });
  691. it('`true` if some collection item has needed property value', function () {
  692. expect(this.obj.get('prop2')).to.be.true;
  693. });
  694. it('`false` if on one collection item doesn\'t have needed property value', function () {
  695. this.obj.set('prop1.1.a', 3);
  696. expect(this.obj.get('prop2')).to.be.false;
  697. });
  698. it('`false` for null/undefined collection', function () {
  699. this.obj.set('prop1', null);
  700. expect(this.obj.get('prop2')).to.be.false;
  701. });
  702. it('`prop2` has valid dependent keys', function () {
  703. expect(Em.meta(this.obj).descs.prop2._dependentKeys).to.eql(['prop1.@each.a', 'value1']);
  704. });
  705. it('`prop3` has valid dependent keys', function () {
  706. expect(Em.meta(this.obj).descs.prop3._dependentKeys).to.eql(['prop1.@each.a', 'App.someRandomTestingKey']);
  707. });
  708. it('`prop3` depends on App.* key', function () {
  709. expect(this.obj.get('prop3')).to.be.true;
  710. this.obj.set('prop1.1.a', 3);
  711. expect(this.obj.get('prop3')).to.be.false;
  712. });
  713. });
  714. describe('#everyBy', function () {
  715. beforeEach(function () {
  716. this.obj = Em.Object.create({
  717. prop1: [{a: 2}, {a: 2}, {a: 2}],
  718. prop2: Em.computed.everyBy('prop1', 'a', 2)
  719. });
  720. });
  721. it('`true` if all collection items have needed property value', function () {
  722. expect(this.obj.get('prop2')).to.be.true;
  723. });
  724. it('`false` if at least one collection item doesn\'t have needed property value', function () {
  725. this.obj.set('prop1.1.a', 3);
  726. expect(this.obj.get('prop2')).to.be.false;
  727. });
  728. it('`false` for null/undefined collection', function () {
  729. this.obj.set('prop1', null);
  730. expect(this.obj.get('prop2')).to.be.false;
  731. });
  732. it('`prop2` has valid dependent keys', function () {
  733. expect(Em.meta(this.obj).descs.prop2._dependentKeys).to.eql(['prop1.@each.a']);
  734. });
  735. });
  736. describe('#everyByKey', function () {
  737. beforeEach(function () {
  738. App.setProperties({
  739. someAnotherKey: 2
  740. });
  741. this.obj = Em.Object.create({
  742. prop1: [{a: 2}, {a: 2}, {a: 2}],
  743. prop2: Em.computed.everyByKey('prop1', 'a', 'value1'),
  744. prop3: Em.computed.everyByKey('prop1', 'a', 'App.someRandomTestingKey'),
  745. value1: 2
  746. });
  747. });
  748. it('`true` if all collection items have needed property value', function () {
  749. expect(this.obj.get('prop2')).to.be.true;
  750. });
  751. it('`false` if at least one collection item doesn\'t have needed property value', function () {
  752. this.obj.set('prop1.1.a', 3);
  753. expect(this.obj.get('prop2')).to.be.false;
  754. });
  755. it('`false` for null/undefined collection', function () {
  756. this.obj.set('prop1', null);
  757. expect(this.obj.get('prop2')).to.be.false;
  758. });
  759. it('`prop2` has valid dependent keys', function () {
  760. expect(Em.meta(this.obj).descs.prop2._dependentKeys).to.eql(['prop1.@each.a', 'value1']);
  761. });
  762. it('`prop3` has valid dependent keys', function () {
  763. expect(Em.meta(this.obj).descs.prop3._dependentKeys).to.eql(['prop1.@each.a', 'App.someRandomTestingKey']);
  764. });
  765. it('`prop3` depends on App.* key', function () {
  766. expect(this.obj.get('prop3')).to.be.true;
  767. this.obj.set('prop1.1.a', 3);
  768. expect(this.obj.get('prop3')).to.be.false;
  769. });
  770. });
  771. describe('#mapBy', function () {
  772. beforeEach(function () {
  773. this.obj = Em.Object.create({
  774. prop1: [{a: 1}, {a: 2}, {a: 3}],
  775. prop2: Em.computed.mapBy('prop1', 'a')
  776. });
  777. });
  778. it('should map dependent property', function () {
  779. expect(this.obj.get('prop2')).to.eql([1, 2, 3]);
  780. });
  781. it('should map dependent property (2)', function () {
  782. this.obj.get('prop1').push({a: 4});
  783. expect(this.obj.get('prop2')).to.eql([1, 2, 3, 4]);
  784. });
  785. it('`[]` for null/undefined collection', function () {
  786. this.obj.set('prop1', null);
  787. expect(this.obj.get('prop2')).to.eql([]);
  788. });
  789. it('`prop2` has valid dependent keys', function () {
  790. expect(Em.meta(this.obj).descs.prop2._dependentKeys).to.eql(['prop1.@each.a']);
  791. });
  792. });
  793. describe('#filterBy', function () {
  794. beforeEach(function () {
  795. this.obj = Em.Object.create({
  796. prop1: [{a: 2}, {a: 2}, {a: 3}],
  797. prop2: Em.computed.filterBy('prop1', 'a', 2)
  798. });
  799. });
  800. it('should filter dependent property', function () {
  801. expect(this.obj.get('prop2')).to.eql([{a: 2}, {a: 2}]);
  802. });
  803. it('should filter dependent property (2)', function () {
  804. this.obj.get('prop1').pushObject({a: 2});
  805. expect(this.obj.get('prop2')).to.eql([{a: 2}, {a: 2}, {a: 2}]);
  806. });
  807. it('`[]` for null/undefined collection', function () {
  808. this.obj.set('prop1', null);
  809. expect(this.obj.get('prop2')).to.eql([]);
  810. });
  811. it('`prop2` has valid dependent keys', function () {
  812. expect(Em.meta(this.obj).descs.prop2._dependentKeys).to.eql(['prop1.@each.a']);
  813. });
  814. });
  815. describe('#filterByKey', function () {
  816. beforeEach(function () {
  817. App.setProperties({
  818. someAnotherKey: 2
  819. });
  820. this.obj = Em.Object.create({
  821. prop1: [{a: 2}, {a: 2}, {a: 3}],
  822. prop2: Em.computed.filterByKey('prop1', 'a', 'value1'),
  823. prop3: Em.computed.filterByKey('prop1', 'a', 'App.someRandomTestingKey'),
  824. value1: 2
  825. });
  826. });
  827. it('should filter dependent property', function () {
  828. expect(this.obj.get('prop2')).to.eql([{a: 2}, {a: 2}]);
  829. });
  830. it('should filter dependent property (2)', function () {
  831. this.obj.get('prop1').pushObject({a: 2});
  832. expect(this.obj.get('prop2')).to.eql([{a: 2}, {a: 2}, {a: 2}]);
  833. });
  834. it('`[]` for null/undefined collection', function () {
  835. this.obj.set('prop1', null);
  836. expect(this.obj.get('prop2')).to.eql([]);
  837. });
  838. it('`prop2` has valid dependent keys', function () {
  839. expect(Em.meta(this.obj).descs.prop2._dependentKeys).to.eql(['prop1.@each.a', 'value1']);
  840. });
  841. it('`prop3` has valid dependent keys', function () {
  842. expect(Em.meta(this.obj).descs.prop3._dependentKeys).to.eql(['prop1.@each.a', 'App.someRandomTestingKey']);
  843. });
  844. it('`prop3` depends on App.* key', function () {
  845. expect(this.obj.get('prop3')).to.eql([{a: 2}, {a: 2}]);
  846. this.obj.set('prop1.1.a', 3);
  847. expect(this.obj.get('prop3')).to.eql([{a: 2}]);
  848. });
  849. });
  850. describe('#findBy', function () {
  851. beforeEach(function () {
  852. this.obj = Em.Object.create({
  853. prop1: [{b: 1, a: 2}, {b: 2, a: 2}, {a: 3}],
  854. prop2: Em.computed.findBy('prop1', 'a', 2)
  855. });
  856. });
  857. it('should filter dependent property', function () {
  858. expect(this.obj.get('prop2')).to.eql({b:1, a: 2});
  859. });
  860. it('should filter dependent property (2)', function () {
  861. this.obj.get('prop1').pushObject({b: 3, a: 2});
  862. expect(this.obj.get('prop2')).to.eql({b: 1, a: 2});
  863. });
  864. it('`null` for null/undefined collection', function () {
  865. this.obj.set('prop1', null);
  866. expect(this.obj.get('prop2')).to.be.null;
  867. });
  868. it('`prop2` has valid dependent keys', function () {
  869. expect(Em.meta(this.obj).descs.prop2._dependentKeys).to.eql(['prop1.@each.a']);
  870. });
  871. });
  872. describe('#findByKey', function () {
  873. beforeEach(function () {
  874. App.setProperties({
  875. someAnotherKey: 2
  876. });
  877. this.obj = Em.Object.create({
  878. prop1: [{b: 1, a: 2}, {b: 2, a: 2}, {a: 3}],
  879. prop2: Em.computed.findByKey('prop1', 'a', 'value1'),
  880. prop3: Em.computed.findByKey('prop1', 'a', 'App.someRandomTestingKey'),
  881. value1: 2
  882. });
  883. });
  884. it('should filter dependent property', function () {
  885. expect(this.obj.get('prop2')).to.eql({b:1, a: 2});
  886. });
  887. it('should filter dependent property (2)', function () {
  888. this.obj.get('prop1').pushObject({b: 3, a: 2});
  889. expect(this.obj.get('prop2')).to.eql({b: 1, a: 2});
  890. });
  891. it('`null` for null/undefined collection', function () {
  892. this.obj.set('prop1', null);
  893. expect(this.obj.get('prop2')).to.be.null;
  894. });
  895. it('`prop2` has valid dependent keys', function () {
  896. expect(Em.meta(this.obj).descs.prop2._dependentKeys).to.eql(['prop1.@each.a', 'value1']);
  897. });
  898. it('`prop3` has valid dependent keys', function () {
  899. expect(Em.meta(this.obj).descs.prop3._dependentKeys).to.eql(['prop1.@each.a', 'App.someRandomTestingKey']);
  900. });
  901. it('`prop3` depends on App.* key', function () {
  902. expect(this.obj.get('prop3')).to.eql({b: 1, a: 2});
  903. this.obj.get('prop1').pushObject({b: 3, a: 2});
  904. expect(this.obj.get('prop3')).to.eql({b: 1, a: 2});
  905. });
  906. });
  907. describe('#alias', function() {
  908. beforeEach(function () {
  909. App.set('someAnotherKey', {a: {b: 1}});
  910. this.obj = Em.Object.create({
  911. prop1: {
  912. a: {
  913. b: {
  914. c: 1
  915. }
  916. }
  917. },
  918. prop2: Em.computed.alias('prop1.a.b.c'),
  919. prop3: Em.computed.alias('App.someAnotherKey.a.b')
  920. })
  921. });
  922. it('should be equal to dependent property', function () {
  923. expect(this.obj.get('prop2')).to.equal(1);
  924. });
  925. it('should be equal to dependent property (2)', function () {
  926. this.obj.set('prop1.a.b.c', 2);
  927. expect(this.obj.get('prop2')).to.equal(2);
  928. });
  929. it('prop3 depends on App.* key', function () {
  930. expect(this.obj.get('prop3')).to.equal(1);
  931. App.set('someAnotherKey.a.b', 4);
  932. expect(this.obj.get('prop3')).to.equal(4);
  933. });
  934. it('prop3 dependent keys are valid', function () {
  935. expect(Em.meta(this.obj).descs.prop3._dependentKeys).to.eql(['App.someAnotherKey.a.b']);
  936. });
  937. });
  938. describe('#existsIn', function () {
  939. beforeEach(function () {
  940. this.obj = Em.Object.create({
  941. prop1: 'v1',
  942. prop2: Em.computed.existsIn('prop1', ['v1', 'v2'])
  943. });
  944. });
  945. it('`true` if dependent value is in the array', function () {
  946. expect(this.obj.get('prop2')).to.be.true;
  947. });
  948. it('`true` if dependent value is in the array (2)', function () {
  949. this.obj.set('prop1', 'v2');
  950. expect(this.obj.get('prop2')).to.be.true;
  951. });
  952. it('`false` if dependent value is not in the array', function () {
  953. this.obj.set('prop1', 'v3');
  954. expect(this.obj.get('prop2')).to.be.false;
  955. });
  956. });
  957. describe('#percents', function () {
  958. beforeEach(function () {
  959. App.setProperties({
  960. p1: 25,
  961. p2: 100
  962. });
  963. this.obj = Em.Object.create({
  964. prop1: 10,
  965. prop2: 25,
  966. prop3: Em.computed.percents('prop1', 'prop2'),
  967. prop4: Em.computed.percents('prop1', 'prop2', 2),
  968. prop5: Em.computed.percents('App.p1', 'App.p2', 1)
  969. });
  970. });
  971. afterEach(function () {
  972. delete App.p1;
  973. delete App.p2;
  974. });
  975. it('should calculate percents', function () {
  976. expect(this.obj.get('prop3')).to.equal(40);
  977. expect(this.obj.get('prop4')).to.equal(40.00);
  978. });
  979. it('should calculate percents (2)', function () {
  980. this.obj.set('prop2', 35);
  981. expect(this.obj.get('prop3')).to.equal(29);
  982. expect(this.obj.get('prop4')).to.equal(28.57);
  983. });
  984. it('should calculate percents (3)', function () {
  985. this.obj.set('prop2', '35');
  986. expect(this.obj.get('prop3')).to.equal(29);
  987. expect(this.obj.get('prop4')).to.equal(28.57);
  988. });
  989. it('should calculate percents (4)', function () {
  990. this.obj.set('prop1', 10.6);
  991. this.obj.set('prop2', 100);
  992. expect(this.obj.get('prop3')).to.equal(11);
  993. expect(this.obj.get('prop4')).to.equal(10.60);
  994. });
  995. it('should calculate percents (5)', function () {
  996. this.obj.set('prop1', '10.6');
  997. this.obj.set('prop2', 100);
  998. expect(this.obj.get('prop3')).to.equal(11);
  999. expect(this.obj.get('prop4')).to.equal(10.60);
  1000. });
  1001. it('prop5 depends on App.* keys', function () {
  1002. expect(this.obj.get('prop5')).to.equal(25.0);
  1003. App.set('p2', 50);
  1004. expect(this.obj.get('prop5')).to.equal(50.0);
  1005. App.set('p1', 10);
  1006. expect(this.obj.get('prop5')).to.equal(20.0);
  1007. });
  1008. it('prop4 dependent keys are valid', function () {
  1009. expect(Em.meta(this.obj).descs.prop5._dependentKeys).to.eql(['App.p1', 'App.p2']);
  1010. });
  1011. });
  1012. describe('#formatRole', function () {
  1013. beforeEach(function () {
  1014. this.obj = Em.Object.create({
  1015. prop1: 'NAMENODE',
  1016. prop2: false,
  1017. prop3: Em.computed.formatRole('prop1', 'prop2')
  1018. });
  1019. sinon.stub(App.StackServiceComponent, 'find', function () {
  1020. return [
  1021. Em.Object.create({id: 'NAMENODE', displayName: 'NameNode'}),
  1022. Em.Object.create({id: 'SECONDARY_NAMENODE', displayName: 'Secondary NameNode'})
  1023. ];
  1024. });
  1025. sinon.stub(App.StackService, 'find', function () {
  1026. return [
  1027. Em.Object.create({id: 'MAPREDUCE2', displayName: 'MapReduce2'}),
  1028. Em.Object.create({id: 'HIVE', displayName: 'Hive'})
  1029. ];
  1030. });
  1031. });
  1032. afterEach(function () {
  1033. App.StackService.find.restore();
  1034. App.StackServiceComponent.find.restore();
  1035. });
  1036. it('should format as role', function () {
  1037. expect(this.obj.get('prop3')).to.equal('NameNode');
  1038. });
  1039. it('should format as role (2)', function () {
  1040. this.obj.set('prop1', 'HIVE');
  1041. this.obj.set('prop2', true);
  1042. expect(this.obj.get('prop3')).to.equal('Hive');
  1043. });
  1044. });
  1045. describe('#sumBy', function () {
  1046. beforeEach(function () {
  1047. this.obj = Em.Object.create({
  1048. prop1: [
  1049. {a: 1}, {a: 2}, {a: 3}
  1050. ],
  1051. prop2: Em.computed.sumBy('prop1', 'a')
  1052. });
  1053. });
  1054. it('should calculate sum', function () {
  1055. expect(this.obj.get('prop2')).to.equal(6);
  1056. });
  1057. it('should calculate sum (2)', function () {
  1058. this.obj.get('prop1').pushObject({a: 4});
  1059. expect(this.obj.get('prop2')).to.equal(10);
  1060. });
  1061. it('0 for empty collection', function () {
  1062. this.obj.set('prop1', []);
  1063. expect(this.obj.get('prop2')).to.equal(0);
  1064. });
  1065. });
  1066. describe('#i18nFormat', function () {
  1067. beforeEach(function () {
  1068. App.setProperties({
  1069. someAnotherKey: 'some value'
  1070. });
  1071. sinon.stub(Em.I18n, 't', function (key) {
  1072. var msgs = {
  1073. key1: '{0} {1} {2}'
  1074. };
  1075. return msgs[key];
  1076. });
  1077. this.obj = Em.Object.create({
  1078. prop1: 'abc',
  1079. prop2: 'cba',
  1080. prop3: 'aaa',
  1081. prop4: Em.computed.i18nFormat('key1', 'prop1', 'prop2', 'prop3'),
  1082. prop5: Em.computed.i18nFormat('not_existing_key', 'prop1', 'prop2', 'prop3'),
  1083. prop6: Em.computed.i18nFormat('key1', 'App.someRandomTestingKey', 'prop2', 'prop3')
  1084. });
  1085. });
  1086. afterEach(function () {
  1087. Em.I18n.t.restore();
  1088. });
  1089. it('`prop4` check dependent keys', function () {
  1090. expect(Em.meta(this.obj).descs.prop4._dependentKeys).to.eql(['prop1', 'prop2', 'prop3']);
  1091. });
  1092. it('should format message', function () {
  1093. expect(this.obj.get('prop4')).to.equal('abc cba aaa');
  1094. });
  1095. it('should format message (2)', function () {
  1096. this.obj.set('prop1', 'aaa');
  1097. expect(this.obj.get('prop4')).to.equal('aaa cba aaa');
  1098. });
  1099. it('empty string for not existing i18-key', function () {
  1100. expect(this.obj.get('prop5')).to.equal('');
  1101. });
  1102. it('`prop6` depends on App.* key', function () {
  1103. expect(this.obj.get('prop6')).to.equal('some value cba aaa');
  1104. App.set('someAnotherKey', '');
  1105. expect(this.obj.get('prop6')).to.equal(' cba aaa');
  1106. });
  1107. it('prop6 dependent keys are valid', function () {
  1108. expect(Em.meta(this.obj).descs.prop6._dependentKeys).to.eql(['App.someRandomTestingKey', 'prop2', 'prop3']);
  1109. });
  1110. });
  1111. describe('#concat', function () {
  1112. beforeEach(function () {
  1113. App.setProperties({
  1114. someAnotherKey: 'some value'
  1115. });
  1116. this.obj = Em.Object.create({
  1117. prop1: 'abc',
  1118. prop2: 'cba',
  1119. prop3: 'aaa',
  1120. prop4: Em.computed.concat(' ', 'prop1', 'prop2', 'prop3'),
  1121. prop5: Em.computed.concat(' ', 'App.someRandomTestingKey', 'prop2', 'prop3'),
  1122. prop6: Em.computed.concat(' ')
  1123. });
  1124. });
  1125. it('should concat dependent values', function () {
  1126. expect(this.obj.get('prop4')).to.equal('abc cba aaa');
  1127. });
  1128. it('should concat dependent values (2)', function () {
  1129. this.obj.set('prop1', 'aaa');
  1130. expect(this.obj.get('prop4')).to.equal('aaa cba aaa');
  1131. });
  1132. it('`prop5` depends on App.* key', function () {
  1133. expect(this.obj.get('prop5')).to.equal('some value cba aaa');
  1134. App.set('someAnotherKey', '');
  1135. expect(this.obj.get('prop5')).to.equal(' cba aaa');
  1136. });
  1137. it('prop5 dependent keys are valid', function () {
  1138. expect(Em.meta(this.obj).descs.prop5._dependentKeys).to.eql(['App.someRandomTestingKey', 'prop2', 'prop3']);
  1139. });
  1140. it('prop6 without dependent keys', function () {
  1141. expect(this.obj.get('prop6')).to.equal('');
  1142. });
  1143. });
  1144. describe('#notExistsIn', function () {
  1145. beforeEach(function () {
  1146. this.obj = Em.Object.create({
  1147. prop1: 'v1',
  1148. prop2: Em.computed.notExistsIn('prop1', ['v1', 'v2'])
  1149. });
  1150. });
  1151. it('`false` if dependent value is in the array', function () {
  1152. expect(this.obj.get('prop2')).to.be.false;
  1153. });
  1154. it('`false` if dependent value is in the array (2)', function () {
  1155. this.obj.set('prop1', 'v2');
  1156. expect(this.obj.get('prop2')).to.be.false;
  1157. });
  1158. it('`true` if dependent value is not in the array', function () {
  1159. this.obj.set('prop1', 'v3');
  1160. expect(this.obj.get('prop2')).to.be.true;
  1161. });
  1162. });
  1163. describe('#firstNotBlank', function () {
  1164. beforeEach(function () {
  1165. App.setProperties({
  1166. someAnotherKey: 'NOT-EMPTY-STRING'
  1167. });
  1168. this.obj = Em.Object.create({
  1169. prop1: '',
  1170. prop2: null,
  1171. prop3: '1234',
  1172. prop4: Em.computed.firstNotBlank('prop1', 'prop2', 'prop3'),
  1173. prop5: Em.computed.firstNotBlank('prop1', 'App.someRandomTestingKey', 'prop3'),
  1174. prop6: Em.computed.firstNotBlank('prop1', 'prop2')
  1175. })
  1176. });
  1177. it('`prop4` check dependent keys', function () {
  1178. expect(Em.meta(this.obj).descs.prop4._dependentKeys).to.eql(['prop1', 'prop2', 'prop3']);
  1179. });
  1180. it('should returns prop3', function () {
  1181. expect(this.obj.get('prop4')).to.equal('1234');
  1182. });
  1183. it('should returns prop2', function () {
  1184. this.obj.set('prop2', 'not empty string');
  1185. expect(this.obj.get('prop4')).to.equal('not empty string');
  1186. });
  1187. it('should returns prop1', function () {
  1188. this.obj.set('prop2', 'not empty string');
  1189. this.obj.set('prop1', 'prop1 is used');
  1190. expect(this.obj.get('prop4')).to.equal('prop1 is used');
  1191. });
  1192. it('`prop5` depends on App.* key', function () {
  1193. expect(this.obj.get('prop5')).to.equal('NOT-EMPTY-STRING');
  1194. App.set('someAnotherKey', '!!!!!!!');
  1195. expect(this.obj.get('prop5')).to.equal('!!!!!!!');
  1196. App.set('someAnotherKey', null);
  1197. expect(this.obj.get('prop5')).to.equal('1234');
  1198. });
  1199. it('prop5 dependent keys are valid', function () {
  1200. expect(Em.meta(this.obj).descs.prop5._dependentKeys).to.eql(['prop1', 'App.someRandomTestingKey', 'prop3']);
  1201. });
  1202. it('prop6 depends on blank values', function () {
  1203. expect(this.obj.get('prop6')).to.be.null;
  1204. });
  1205. });
  1206. describe('#format', function () {
  1207. beforeEach(function () {
  1208. App.setProperties({
  1209. someAnotherKey: 'some value'
  1210. });
  1211. this.obj = Em.Object.create({
  1212. prop1: 'abc',
  1213. prop2: 'cba',
  1214. prop3: 'aaa',
  1215. prop4: Em.computed.format('{0} {1} {2}', 'prop1', 'prop2', 'prop3'),
  1216. prop5: Em.computed.format(null, 'prop1', 'prop2', 'prop3'),
  1217. prop6: Em.computed.format('{0} {1} {2}', 'App.someRandomTestingKey', 'prop2', 'prop3')
  1218. });
  1219. });
  1220. it('`prop4` check dependent keys', function () {
  1221. expect(Em.meta(this.obj).descs.prop4._dependentKeys).to.eql(['prop1', 'prop2', 'prop3']);
  1222. });
  1223. it('should format message', function () {
  1224. expect(this.obj.get('prop4')).to.equal('abc cba aaa');
  1225. });
  1226. it('should format message (2)', function () {
  1227. this.obj.set('prop1', 'aaa');
  1228. expect(this.obj.get('prop4')).to.equal('aaa cba aaa');
  1229. });
  1230. it('empty string for not existing i18-key', function () {
  1231. expect(this.obj.get('prop5')).to.equal('');
  1232. });
  1233. it('`prop6` depends on App.* key', function () {
  1234. expect(this.obj.get('prop6')).to.equal('some value cba aaa');
  1235. App.set('someAnotherKey', '');
  1236. expect(this.obj.get('prop6')).to.equal(' cba aaa');
  1237. });
  1238. it('prop6 dependent keys are valid', function () {
  1239. expect(Em.meta(this.obj).descs.prop6._dependentKeys).to.eql(['App.someRandomTestingKey', 'prop2', 'prop3']);
  1240. });
  1241. });
  1242. describe('#formatUnavailable', function () {
  1243. beforeEach(function () {
  1244. App.setProperties({
  1245. someAnotherKey: 1
  1246. });
  1247. this.obj = Em.Object.create({
  1248. prop1: 1,
  1249. prop2: Em.computed.formatUnavailable('prop1'),
  1250. prop3: Em.computed.formatUnavailable('App.someRandomTestingKey')
  1251. });
  1252. });
  1253. it('`value` is 1', function () {
  1254. expect(this.obj.get('prop2')).to.equal(1);
  1255. expect(this.obj.get('prop3')).to.equal(1);
  1256. });
  1257. it('`value` is 0', function () {
  1258. App.set('someAnotherKey', 0);
  1259. this.obj.set('prop1', 0);
  1260. expect(this.obj.get('prop2')).to.equal(0);
  1261. expect(this.obj.get('prop3')).to.equal(0);
  1262. });
  1263. it('`value` is `0`', function () {
  1264. App.set('someAnotherKey', '0');
  1265. this.obj.set('prop1', '0');
  1266. expect(this.obj.get('prop2')).to.equal('0');
  1267. expect(this.obj.get('prop3')).to.equal('0');
  1268. });
  1269. it('`value` is not numeric', function () {
  1270. App.set('someAnotherKey', null);
  1271. this.obj.set('prop1', null);
  1272. expect(this.obj.get('prop2')).to.equal('n/a');
  1273. expect(this.obj.get('prop3')).to.equal('n/a');
  1274. });
  1275. it('prop3 dependent keys are valid', function () {
  1276. expect(Em.meta(this.obj).descs.prop3._dependentKeys).to.eql(['App.someRandomTestingKey']);
  1277. });
  1278. });
  1279. describe('#countBasedMessage', function () {
  1280. var msg0 = 'msg0';
  1281. var msg1 = 'msg1';
  1282. var msgM = 'msgM';
  1283. beforeEach(function () {
  1284. App.setProperties({
  1285. someAnotherKey: 1
  1286. });
  1287. this.obj = Em.Object.create({
  1288. prop1: 1,
  1289. prop2: Em.computed.countBasedMessage('prop1', msg0, msg1, msgM),
  1290. prop3: Em.computed.countBasedMessage('App.someRandomTestingKey', msg0, msg1, msgM)
  1291. });
  1292. });
  1293. it('`value` is 1', function () {
  1294. expect(this.obj.get('prop2')).to.equal(msg1);
  1295. expect(this.obj.get('prop3')).to.equal(msg1);
  1296. });
  1297. it('`value` is 0', function () {
  1298. App.set('someAnotherKey', 0);
  1299. this.obj.set('prop1', 0);
  1300. expect(this.obj.get('prop2')).to.equal(msg0);
  1301. expect(this.obj.get('prop3')).to.equal(msg0);
  1302. });
  1303. it('`value` is greater than 1', function () {
  1304. App.set('someAnotherKey', 3);
  1305. this.obj.set('prop1', 3);
  1306. expect(this.obj.get('prop2')).to.equal(msgM);
  1307. expect(this.obj.get('prop3')).to.equal(msgM);
  1308. });
  1309. it('prop3 dependent keys are valid', function () {
  1310. expect(Em.meta(this.obj).descs.prop3._dependentKeys).to.eql(['App.someRandomTestingKey']);
  1311. });
  1312. });
  1313. describe('#getByKey', function () {
  1314. beforeEach(function () {
  1315. this.obj = Em.Object.create({
  1316. prop1: {a: 1, b: 2, c: 3},
  1317. prop2: 'a',
  1318. prop3: Em.computed.getByKey('prop1', 'prop2'),
  1319. prop4: Em.computed.getByKey('prop1', 'App.someRandomTestingKey'),
  1320. prop5: Em.computed.getByKey('prop1', 'prop2', 100500) // with default value
  1321. });
  1322. App.set('someAnotherKey', 'a');
  1323. });
  1324. it('prop3 dependent keys are valid', function () {
  1325. expect(Em.meta(this.obj).descs.prop3._dependentKeys).to.eql(['prop1', 'prop2']);
  1326. });
  1327. it('prop4 dependent keys are valid', function () {
  1328. expect(Em.meta(this.obj).descs.prop4._dependentKeys).to.eql(['prop1', 'App.someRandomTestingKey']);
  1329. });
  1330. it('prop5 dependent keys are valid', function () {
  1331. expect(Em.meta(this.obj).descs.prop5._dependentKeys).to.eql(['prop1', 'prop2']);
  1332. });
  1333. it('prop3 value is 1', function () {
  1334. expect(this.obj.get('prop3')).to.be.equal(1);
  1335. });
  1336. it('prop3 value is 2', function () {
  1337. this.obj.set('prop2', 'b');
  1338. expect(this.obj.get('prop3')).to.be.equal(2);
  1339. });
  1340. it('prop3 value is 3', function () {
  1341. this.obj.set('prop2', 'c');
  1342. expect(this.obj.get('prop3')).to.be.equal(3);
  1343. });
  1344. it('prop3 value is 4', function () {
  1345. this.obj.set('prop1.c', 4);
  1346. this.obj.set('prop2', 'c');
  1347. expect(this.obj.get('prop3')).to.be.equal(4);
  1348. });
  1349. it('prop4 values is 1', function () {
  1350. expect(this.obj.get('prop4')).to.be.equal(1);
  1351. });
  1352. it('prop4 values is 2', function () {
  1353. App.set('someAnotherKey', 'b');
  1354. expect(this.obj.get('prop4')).to.be.equal(2);
  1355. });
  1356. it('prop4 values is 3', function () {
  1357. App.set('someAnotherKey', 'c');
  1358. expect(this.obj.get('prop4')).to.be.equal(3);
  1359. });
  1360. it('prop5 value is set to the default value', function () {
  1361. this.obj.set('prop2', 'd');
  1362. expect(this.obj.get('prop5')).to.be.equal(100500);
  1363. });
  1364. });
  1365. describe('#truncate', function () {
  1366. beforeEach(function () {
  1367. this.obj = Em.Object.create({
  1368. prop1: '123456789',
  1369. prop2: Em.computed.truncate('prop1', 8, 5),
  1370. prop3: Em.computed.truncate('App.someRandomTestingKey', 8, 5),
  1371. prop4: Em.computed.truncate('prop1', 8, 5, '###')
  1372. });
  1373. App.set('someAnotherKey', 'abcdefghi');
  1374. });
  1375. it('prop2 dependent keys are valid', function () {
  1376. expect(Em.meta(this.obj).descs.prop2._dependentKeys).to.be.eql(['prop1']);
  1377. });
  1378. it('prop3 dependent keys are valid', function () {
  1379. expect(Em.meta(this.obj).descs.prop3._dependentKeys).to.be.eql(['App.someRandomTestingKey']);
  1380. });
  1381. it('prop4 dependent keys are valid', function () {
  1382. expect(Em.meta(this.obj).descs.prop4._dependentKeys).to.be.eql(['prop1']);
  1383. });
  1384. it('prop2 value is 12345...', function () {
  1385. expect(this.obj.get('prop2')).to.be.equal('12345...');
  1386. });
  1387. it('prop2 value is 54321...', function () {
  1388. this.obj.set('prop1', '543216789');
  1389. expect(this.obj.get('prop2')).to.be.equal('54321...');
  1390. });
  1391. it('prop2 value is 1234', function () {
  1392. this.obj.set('prop1', '1234');
  1393. expect(this.obj.get('prop2')).to.be.equal('1234');
  1394. });
  1395. it('prop2 value is ""', function () {
  1396. this.obj.set('prop1', null);
  1397. expect(this.obj.get('prop2')).to.be.equal('');
  1398. });
  1399. it('prop3 value is abcde...', function () {
  1400. expect(this.obj.get('prop3')).to.be.equal('abcde...');
  1401. });
  1402. it('prop3 value is edcba...', function () {
  1403. App.set('someAnotherKey', 'edcbafghi');
  1404. expect(this.obj.get('prop3')).to.be.equal('edcba...');
  1405. });
  1406. it('prop3 value is abcd', function () {
  1407. App.set('someAnotherKey', 'abcd');
  1408. expect(this.obj.get('prop3')).to.be.equal('abcd');
  1409. });
  1410. it('prop4 value is 12345###', function () {
  1411. expect(this.obj.get('prop4')).to.be.equal('12345###');
  1412. });
  1413. it('prop4 value is 54321###', function () {
  1414. this.obj.set('prop1', '543216789');
  1415. expect(this.obj.get('prop4')).to.be.equal('54321###');
  1416. });
  1417. it('prop4 value is 12345', function () {
  1418. this.obj.set('prop1', '12345');
  1419. expect(this.obj.get('prop4')).to.be.equal('12345');
  1420. });
  1421. });
  1422. });