ember_computed_test.js 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537
  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('#everyBy', function () {
  680. beforeEach(function () {
  681. this.obj = Em.Object.create({
  682. prop1: [{a: 2}, {a: 2}, {a: 2}],
  683. prop2: Em.computed.everyBy('prop1', 'a', 2)
  684. });
  685. });
  686. it('`true` if all collection items have needed property value', function () {
  687. expect(this.obj.get('prop2')).to.be.true;
  688. });
  689. it('`false` if at least one collection item doesn\'t have needed property value', function () {
  690. this.obj.set('prop1.1.a', 3);
  691. expect(this.obj.get('prop2')).to.be.false;
  692. });
  693. it('`false` for null/undefined collection', function () {
  694. this.obj.set('prop1', null);
  695. expect(this.obj.get('prop2')).to.be.false;
  696. });
  697. it('`prop2` has valid dependent keys', function () {
  698. expect(Em.meta(this.obj).descs.prop2._dependentKeys).to.eql(['prop1.@each.a']);
  699. });
  700. });
  701. describe('#mapBy', function () {
  702. beforeEach(function () {
  703. this.obj = Em.Object.create({
  704. prop1: [{a: 1}, {a: 2}, {a: 3}],
  705. prop2: Em.computed.mapBy('prop1', 'a')
  706. });
  707. });
  708. it('should map dependent property', function () {
  709. expect(this.obj.get('prop2')).to.eql([1, 2, 3]);
  710. });
  711. it('should map dependent property (2)', function () {
  712. this.obj.get('prop1').push({a: 4});
  713. expect(this.obj.get('prop2')).to.eql([1, 2, 3, 4]);
  714. });
  715. it('`[]` for null/undefined collection', function () {
  716. this.obj.set('prop1', null);
  717. expect(this.obj.get('prop2')).to.eql([]);
  718. });
  719. it('`prop2` has valid dependent keys', function () {
  720. expect(Em.meta(this.obj).descs.prop2._dependentKeys).to.eql(['prop1.@each.a']);
  721. });
  722. });
  723. describe('#filterBy', function () {
  724. beforeEach(function () {
  725. this.obj = Em.Object.create({
  726. prop1: [{a: 2}, {a: 2}, {a: 3}],
  727. prop2: Em.computed.filterBy('prop1', 'a', 2)
  728. });
  729. });
  730. it('should filter dependent property', function () {
  731. expect(this.obj.get('prop2')).to.eql([{a: 2}, {a: 2}]);
  732. });
  733. it('should filter dependent property (2)', function () {
  734. this.obj.get('prop1').pushObject({a: 2});
  735. expect(this.obj.get('prop2')).to.eql([{a: 2}, {a: 2}, {a: 2}]);
  736. });
  737. it('`[]` for null/undefined collection', function () {
  738. this.obj.set('prop1', null);
  739. expect(this.obj.get('prop2')).to.eql([]);
  740. });
  741. it('`prop2` has valid dependent keys', function () {
  742. expect(Em.meta(this.obj).descs.prop2._dependentKeys).to.eql(['prop1.@each.a']);
  743. });
  744. });
  745. describe('#findBy', function () {
  746. beforeEach(function () {
  747. this.obj = Em.Object.create({
  748. prop1: [{b: 1, a: 2}, {b: 2, a: 2}, {a: 3}],
  749. prop2: Em.computed.findBy('prop1', 'a', 2)
  750. });
  751. });
  752. it('should filter dependent property', function () {
  753. expect(this.obj.get('prop2')).to.eql({b:1, a: 2});
  754. });
  755. it('should filter dependent property (2)', function () {
  756. this.obj.get('prop1').pushObject({b: 3, a: 2});
  757. expect(this.obj.get('prop2')).to.eql({b: 1, a: 2});
  758. });
  759. it('`null` for null/undefined collection', function () {
  760. this.obj.set('prop1', null);
  761. expect(this.obj.get('prop2')).to.be.null;
  762. });
  763. it('`prop2` has valid dependent keys', function () {
  764. expect(Em.meta(this.obj).descs.prop2._dependentKeys).to.eql(['prop1.@each.a']);
  765. });
  766. });
  767. describe('#alias', function() {
  768. beforeEach(function () {
  769. App.set('someAnotherKey', {a: {b: 1}});
  770. this.obj = Em.Object.create({
  771. prop1: {
  772. a: {
  773. b: {
  774. c: 1
  775. }
  776. }
  777. },
  778. prop2: Em.computed.alias('prop1.a.b.c'),
  779. prop3: Em.computed.alias('App.someAnotherKey.a.b')
  780. })
  781. });
  782. it('should be equal to dependent property', function () {
  783. expect(this.obj.get('prop2')).to.equal(1);
  784. });
  785. it('should be equal to dependent property (2)', function () {
  786. this.obj.set('prop1.a.b.c', 2);
  787. expect(this.obj.get('prop2')).to.equal(2);
  788. });
  789. it('prop3 depends on App.* key', function () {
  790. expect(this.obj.get('prop3')).to.equal(1);
  791. App.set('someAnotherKey.a.b', 4);
  792. expect(this.obj.get('prop3')).to.equal(4);
  793. });
  794. it('prop3 dependent keys are valid', function () {
  795. expect(Em.meta(this.obj).descs.prop3._dependentKeys).to.eql(['App.someAnotherKey.a.b']);
  796. });
  797. });
  798. describe('#existsIn', function () {
  799. beforeEach(function () {
  800. this.obj = Em.Object.create({
  801. prop1: 'v1',
  802. prop2: Em.computed.existsIn('prop1', ['v1', 'v2'])
  803. });
  804. });
  805. it('`true` if dependent value is in the array', function () {
  806. expect(this.obj.get('prop2')).to.be.true;
  807. });
  808. it('`true` if dependent value is in the array (2)', function () {
  809. this.obj.set('prop1', 'v2');
  810. expect(this.obj.get('prop2')).to.be.true;
  811. });
  812. it('`false` if dependent value is not in the array', function () {
  813. this.obj.set('prop1', 'v3');
  814. expect(this.obj.get('prop2')).to.be.false;
  815. });
  816. });
  817. describe('#percents', function () {
  818. beforeEach(function () {
  819. App.setProperties({
  820. p1: 25,
  821. p2: 100
  822. });
  823. this.obj = Em.Object.create({
  824. prop1: 10,
  825. prop2: 25,
  826. prop3: Em.computed.percents('prop1', 'prop2'),
  827. prop4: Em.computed.percents('prop1', 'prop2', 2),
  828. prop5: Em.computed.percents('App.p1', 'App.p2', 1)
  829. });
  830. });
  831. afterEach(function () {
  832. delete App.p1;
  833. delete App.p2;
  834. });
  835. it('should calculate percents', function () {
  836. expect(this.obj.get('prop3')).to.equal(40);
  837. expect(this.obj.get('prop4')).to.equal(40.00);
  838. });
  839. it('should calculate percents (2)', function () {
  840. this.obj.set('prop2', 35);
  841. expect(this.obj.get('prop3')).to.equal(29);
  842. expect(this.obj.get('prop4')).to.equal(28.57);
  843. });
  844. it('should calculate percents (3)', function () {
  845. this.obj.set('prop2', '35');
  846. expect(this.obj.get('prop3')).to.equal(29);
  847. expect(this.obj.get('prop4')).to.equal(28.57);
  848. });
  849. it('should calculate percents (4)', function () {
  850. this.obj.set('prop1', 10.6);
  851. this.obj.set('prop2', 100);
  852. expect(this.obj.get('prop3')).to.equal(11);
  853. expect(this.obj.get('prop4')).to.equal(10.60);
  854. });
  855. it('should calculate percents (5)', function () {
  856. this.obj.set('prop1', '10.6');
  857. this.obj.set('prop2', 100);
  858. expect(this.obj.get('prop3')).to.equal(11);
  859. expect(this.obj.get('prop4')).to.equal(10.60);
  860. });
  861. it('prop5 depends on App.* keys', function () {
  862. expect(this.obj.get('prop5')).to.equal(25.0);
  863. App.set('p2', 50);
  864. expect(this.obj.get('prop5')).to.equal(50.0);
  865. App.set('p1', 10);
  866. expect(this.obj.get('prop5')).to.equal(20.0);
  867. });
  868. it('prop4 dependent keys are valid', function () {
  869. expect(Em.meta(this.obj).descs.prop5._dependentKeys).to.eql(['App.p1', 'App.p2']);
  870. });
  871. });
  872. describe('#formatRole', function () {
  873. beforeEach(function () {
  874. this.obj = Em.Object.create({
  875. prop1: 'NAMENODE',
  876. prop2: false,
  877. prop3: Em.computed.formatRole('prop1', 'prop2')
  878. });
  879. sinon.stub(App.StackServiceComponent, 'find', function () {
  880. return [
  881. Em.Object.create({id: 'NAMENODE', displayName: 'NameNode'}),
  882. Em.Object.create({id: 'SECONDARY_NAMENODE', displayName: 'Secondary NameNode'})
  883. ];
  884. });
  885. sinon.stub(App.StackService, 'find', function () {
  886. return [
  887. Em.Object.create({id: 'MAPREDUCE2', displayName: 'MapReduce2'}),
  888. Em.Object.create({id: 'HIVE', displayName: 'Hive'})
  889. ];
  890. });
  891. });
  892. afterEach(function () {
  893. App.StackService.find.restore();
  894. App.StackServiceComponent.find.restore();
  895. });
  896. it('should format as role', function () {
  897. expect(this.obj.get('prop3')).to.equal('NameNode');
  898. });
  899. it('should format as role (2)', function () {
  900. this.obj.set('prop1', 'HIVE');
  901. this.obj.set('prop2', true);
  902. expect(this.obj.get('prop3')).to.equal('Hive');
  903. });
  904. });
  905. describe('#sumBy', function () {
  906. beforeEach(function () {
  907. this.obj = Em.Object.create({
  908. prop1: [
  909. {a: 1}, {a: 2}, {a: 3}
  910. ],
  911. prop2: Em.computed.sumBy('prop1', 'a')
  912. });
  913. });
  914. it('should calculate sum', function () {
  915. expect(this.obj.get('prop2')).to.equal(6);
  916. });
  917. it('should calculate sum (2)', function () {
  918. this.obj.get('prop1').pushObject({a: 4});
  919. expect(this.obj.get('prop2')).to.equal(10);
  920. });
  921. it('0 for empty collection', function () {
  922. this.obj.set('prop1', []);
  923. expect(this.obj.get('prop2')).to.equal(0);
  924. });
  925. });
  926. describe('#i18nFormat', function () {
  927. beforeEach(function () {
  928. App.setProperties({
  929. someAnotherKey: 'some value'
  930. });
  931. sinon.stub(Em.I18n, 't', function (key) {
  932. var msgs = {
  933. key1: '{0} {1} {2}'
  934. };
  935. return msgs[key];
  936. });
  937. this.obj = Em.Object.create({
  938. prop1: 'abc',
  939. prop2: 'cba',
  940. prop3: 'aaa',
  941. prop4: Em.computed.i18nFormat('key1', 'prop1', 'prop2', 'prop3'),
  942. prop5: Em.computed.i18nFormat('not_existing_key', 'prop1', 'prop2', 'prop3'),
  943. prop6: Em.computed.i18nFormat('key1', 'App.someRandomTestingKey', 'prop2', 'prop3')
  944. });
  945. });
  946. afterEach(function () {
  947. Em.I18n.t.restore();
  948. });
  949. it('`prop4` check dependent keys', function () {
  950. expect(Em.meta(this.obj).descs.prop4._dependentKeys).to.eql(['prop1', 'prop2', 'prop3']);
  951. });
  952. it('should format message', function () {
  953. expect(this.obj.get('prop4')).to.equal('abc cba aaa');
  954. });
  955. it('should format message (2)', function () {
  956. this.obj.set('prop1', 'aaa');
  957. expect(this.obj.get('prop4')).to.equal('aaa cba aaa');
  958. });
  959. it('empty string for not existing i18-key', function () {
  960. expect(this.obj.get('prop5')).to.equal('');
  961. });
  962. it('`prop6` depends on App.* key', function () {
  963. expect(this.obj.get('prop6')).to.equal('some value cba aaa');
  964. App.set('someAnotherKey', '');
  965. expect(this.obj.get('prop6')).to.equal(' cba aaa');
  966. });
  967. it('prop6 dependent keys are valid', function () {
  968. expect(Em.meta(this.obj).descs.prop6._dependentKeys).to.eql(['App.someRandomTestingKey', 'prop2', 'prop3']);
  969. });
  970. });
  971. describe('#concat', function () {
  972. beforeEach(function () {
  973. App.setProperties({
  974. someAnotherKey: 'some value'
  975. });
  976. this.obj = Em.Object.create({
  977. prop1: 'abc',
  978. prop2: 'cba',
  979. prop3: 'aaa',
  980. prop4: Em.computed.concat(' ', 'prop1', 'prop2', 'prop3'),
  981. prop5: Em.computed.concat(' ', 'App.someRandomTestingKey', 'prop2', 'prop3'),
  982. prop6: Em.computed.concat(' ')
  983. });
  984. });
  985. it('should concat dependent values', function () {
  986. expect(this.obj.get('prop4')).to.equal('abc cba aaa');
  987. });
  988. it('should concat dependent values (2)', function () {
  989. this.obj.set('prop1', 'aaa');
  990. expect(this.obj.get('prop4')).to.equal('aaa cba aaa');
  991. });
  992. it('`prop5` depends on App.* key', function () {
  993. expect(this.obj.get('prop5')).to.equal('some value cba aaa');
  994. App.set('someAnotherKey', '');
  995. expect(this.obj.get('prop5')).to.equal(' cba aaa');
  996. });
  997. it('prop5 dependent keys are valid', function () {
  998. expect(Em.meta(this.obj).descs.prop5._dependentKeys).to.eql(['App.someRandomTestingKey', 'prop2', 'prop3']);
  999. });
  1000. it('prop6 without dependent keys', function () {
  1001. expect(this.obj.get('prop6')).to.equal('');
  1002. });
  1003. });
  1004. describe('#notExistsIn', function () {
  1005. beforeEach(function () {
  1006. this.obj = Em.Object.create({
  1007. prop1: 'v1',
  1008. prop2: Em.computed.notExistsIn('prop1', ['v1', 'v2'])
  1009. });
  1010. });
  1011. it('`false` if dependent value is in the array', function () {
  1012. expect(this.obj.get('prop2')).to.be.false;
  1013. });
  1014. it('`false` if dependent value is in the array (2)', function () {
  1015. this.obj.set('prop1', 'v2');
  1016. expect(this.obj.get('prop2')).to.be.false;
  1017. });
  1018. it('`true` if dependent value is not in the array', function () {
  1019. this.obj.set('prop1', 'v3');
  1020. expect(this.obj.get('prop2')).to.be.true;
  1021. });
  1022. });
  1023. describe('#firstNotBlank', function () {
  1024. beforeEach(function () {
  1025. App.setProperties({
  1026. someAnotherKey: 'NOT-EMPTY-STRING'
  1027. });
  1028. this.obj = Em.Object.create({
  1029. prop1: '',
  1030. prop2: null,
  1031. prop3: '1234',
  1032. prop4: Em.computed.firstNotBlank('prop1', 'prop2', 'prop3'),
  1033. prop5: Em.computed.firstNotBlank('prop1', 'App.someRandomTestingKey', 'prop3'),
  1034. prop6: Em.computed.firstNotBlank('prop1', 'prop2')
  1035. })
  1036. });
  1037. it('`prop4` check dependent keys', function () {
  1038. expect(Em.meta(this.obj).descs.prop4._dependentKeys).to.eql(['prop1', 'prop2', 'prop3']);
  1039. });
  1040. it('should returns prop3', function () {
  1041. expect(this.obj.get('prop4')).to.equal('1234');
  1042. });
  1043. it('should returns prop2', function () {
  1044. this.obj.set('prop2', 'not empty string');
  1045. expect(this.obj.get('prop4')).to.equal('not empty string');
  1046. });
  1047. it('should returns prop1', function () {
  1048. this.obj.set('prop2', 'not empty string');
  1049. this.obj.set('prop1', 'prop1 is used');
  1050. expect(this.obj.get('prop4')).to.equal('prop1 is used');
  1051. });
  1052. it('`prop5` depends on App.* key', function () {
  1053. expect(this.obj.get('prop5')).to.equal('NOT-EMPTY-STRING');
  1054. App.set('someAnotherKey', '!!!!!!!');
  1055. expect(this.obj.get('prop5')).to.equal('!!!!!!!');
  1056. App.set('someAnotherKey', null);
  1057. expect(this.obj.get('prop5')).to.equal('1234');
  1058. });
  1059. it('prop5 dependent keys are valid', function () {
  1060. expect(Em.meta(this.obj).descs.prop5._dependentKeys).to.eql(['prop1', 'App.someRandomTestingKey', 'prop3']);
  1061. });
  1062. it('prop6 depends on blank values', function () {
  1063. expect(this.obj.get('prop6')).to.be.null;
  1064. });
  1065. });
  1066. describe('#format', function () {
  1067. beforeEach(function () {
  1068. App.setProperties({
  1069. someAnotherKey: 'some value'
  1070. });
  1071. this.obj = Em.Object.create({
  1072. prop1: 'abc',
  1073. prop2: 'cba',
  1074. prop3: 'aaa',
  1075. prop4: Em.computed.format('{0} {1} {2}', 'prop1', 'prop2', 'prop3'),
  1076. prop5: Em.computed.format(null, 'prop1', 'prop2', 'prop3'),
  1077. prop6: Em.computed.format('{0} {1} {2}', 'App.someRandomTestingKey', 'prop2', 'prop3')
  1078. });
  1079. });
  1080. it('`prop4` check dependent keys', function () {
  1081. expect(Em.meta(this.obj).descs.prop4._dependentKeys).to.eql(['prop1', 'prop2', 'prop3']);
  1082. });
  1083. it('should format message', function () {
  1084. expect(this.obj.get('prop4')).to.equal('abc cba aaa');
  1085. });
  1086. it('should format message (2)', function () {
  1087. this.obj.set('prop1', 'aaa');
  1088. expect(this.obj.get('prop4')).to.equal('aaa cba aaa');
  1089. });
  1090. it('empty string for not existing i18-key', function () {
  1091. expect(this.obj.get('prop5')).to.equal('');
  1092. });
  1093. it('`prop6` depends on App.* key', function () {
  1094. expect(this.obj.get('prop6')).to.equal('some value cba aaa');
  1095. App.set('someAnotherKey', '');
  1096. expect(this.obj.get('prop6')).to.equal(' cba aaa');
  1097. });
  1098. it('prop6 dependent keys are valid', function () {
  1099. expect(Em.meta(this.obj).descs.prop6._dependentKeys).to.eql(['App.someRandomTestingKey', 'prop2', 'prop3']);
  1100. });
  1101. });
  1102. describe('#formatUnavailable', function () {
  1103. beforeEach(function () {
  1104. App.setProperties({
  1105. someAnotherKey: 1
  1106. });
  1107. this.obj = Em.Object.create({
  1108. prop1: 1,
  1109. prop2: Em.computed.formatUnavailable('prop1'),
  1110. prop3: Em.computed.formatUnavailable('App.someRandomTestingKey')
  1111. });
  1112. });
  1113. it('`value` is 1', function () {
  1114. expect(this.obj.get('prop2')).to.equal(1);
  1115. expect(this.obj.get('prop3')).to.equal(1);
  1116. });
  1117. it('`value` is 0', function () {
  1118. App.set('someAnotherKey', 0);
  1119. this.obj.set('prop1', 0);
  1120. expect(this.obj.get('prop2')).to.equal(0);
  1121. expect(this.obj.get('prop3')).to.equal(0);
  1122. });
  1123. it('`value` is `0`', function () {
  1124. App.set('someAnotherKey', '0');
  1125. this.obj.set('prop1', '0');
  1126. expect(this.obj.get('prop2')).to.equal('0');
  1127. expect(this.obj.get('prop3')).to.equal('0');
  1128. });
  1129. it('`value` is not numeric', function () {
  1130. App.set('someAnotherKey', null);
  1131. this.obj.set('prop1', null);
  1132. expect(this.obj.get('prop2')).to.equal('n/a');
  1133. expect(this.obj.get('prop3')).to.equal('n/a');
  1134. });
  1135. it('prop3 dependent keys are valid', function () {
  1136. expect(Em.meta(this.obj).descs.prop3._dependentKeys).to.eql(['App.someRandomTestingKey']);
  1137. });
  1138. });
  1139. describe('#countBasedMessage', function () {
  1140. var msg0 = 'msg0';
  1141. var msg1 = 'msg1';
  1142. var msgM = 'msgM';
  1143. beforeEach(function () {
  1144. App.setProperties({
  1145. someAnotherKey: 1
  1146. });
  1147. this.obj = Em.Object.create({
  1148. prop1: 1,
  1149. prop2: Em.computed.countBasedMessage('prop1', msg0, msg1, msgM),
  1150. prop3: Em.computed.countBasedMessage('App.someRandomTestingKey', msg0, msg1, msgM)
  1151. });
  1152. });
  1153. it('`value` is 1', function () {
  1154. expect(this.obj.get('prop2')).to.equal(msg1);
  1155. expect(this.obj.get('prop3')).to.equal(msg1);
  1156. });
  1157. it('`value` is 0', function () {
  1158. App.set('someAnotherKey', 0);
  1159. this.obj.set('prop1', 0);
  1160. expect(this.obj.get('prop2')).to.equal(msg0);
  1161. expect(this.obj.get('prop3')).to.equal(msg0);
  1162. });
  1163. it('`value` is greater than 1', function () {
  1164. App.set('someAnotherKey', 3);
  1165. this.obj.set('prop1', 3);
  1166. expect(this.obj.get('prop2')).to.equal(msgM);
  1167. expect(this.obj.get('prop3')).to.equal(msgM);
  1168. });
  1169. it('prop3 dependent keys are valid', function () {
  1170. expect(Em.meta(this.obj).descs.prop3._dependentKeys).to.eql(['App.someRandomTestingKey']);
  1171. });
  1172. });
  1173. describe('#getByKey', function () {
  1174. beforeEach(function () {
  1175. this.obj = Em.Object.create({
  1176. prop1: {a: 1, b: 2, c: 3},
  1177. prop2: 'a',
  1178. prop3: Em.computed.getByKey('prop1', 'prop2'),
  1179. prop4: Em.computed.getByKey('prop1', 'App.someRandomTestingKey'),
  1180. prop5: Em.computed.getByKey('prop1', 'prop2', 100500) // with default value
  1181. });
  1182. App.set('someAnotherKey', 'a');
  1183. });
  1184. it('prop3 dependent keys are valid', function () {
  1185. expect(Em.meta(this.obj).descs.prop3._dependentKeys).to.eql(['prop1', 'prop2']);
  1186. });
  1187. it('prop4 dependent keys are valid', function () {
  1188. expect(Em.meta(this.obj).descs.prop4._dependentKeys).to.eql(['prop1', 'App.someRandomTestingKey']);
  1189. });
  1190. it('prop5 dependent keys are valid', function () {
  1191. expect(Em.meta(this.obj).descs.prop5._dependentKeys).to.eql(['prop1', 'prop2']);
  1192. });
  1193. it('prop3 value is 1', function () {
  1194. expect(this.obj.get('prop3')).to.be.equal(1);
  1195. });
  1196. it('prop3 value is 2', function () {
  1197. this.obj.set('prop2', 'b');
  1198. expect(this.obj.get('prop3')).to.be.equal(2);
  1199. });
  1200. it('prop3 value is 3', function () {
  1201. this.obj.set('prop2', 'c');
  1202. expect(this.obj.get('prop3')).to.be.equal(3);
  1203. });
  1204. it('prop3 value is 4', function () {
  1205. this.obj.set('prop1.c', 4);
  1206. this.obj.set('prop2', 'c');
  1207. expect(this.obj.get('prop3')).to.be.equal(4);
  1208. });
  1209. it('prop4 values is 1', function () {
  1210. expect(this.obj.get('prop4')).to.be.equal(1);
  1211. });
  1212. it('prop4 values is 2', function () {
  1213. App.set('someAnotherKey', 'b');
  1214. expect(this.obj.get('prop4')).to.be.equal(2);
  1215. });
  1216. it('prop4 values is 3', function () {
  1217. App.set('someAnotherKey', 'c');
  1218. expect(this.obj.get('prop4')).to.be.equal(3);
  1219. });
  1220. it('prop5 value is set to the default value', function () {
  1221. this.obj.set('prop2', 'd');
  1222. expect(this.obj.get('prop5')).to.be.equal(100500);
  1223. });
  1224. });
  1225. });