ember_computed_test.js 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469
  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: Em.computed.formatRole('prop1')
  877. });
  878. sinon.stub(App.StackServiceComponent, 'find', function () {
  879. return [
  880. Em.Object.create({id: 'NAMENODE', displayName: 'NameNode'}),
  881. Em.Object.create({id: 'SECONDARY_NAMENODE', displayName: 'Secondary NameNode'})
  882. ];
  883. });
  884. sinon.stub(App.StackService, 'find', function () {
  885. return [
  886. Em.Object.create({id: 'MAPREDUCE2', displayName: 'MapReduce2'}),
  887. Em.Object.create({id: 'HIVE', displayName: 'Hive'})
  888. ];
  889. });
  890. });
  891. afterEach(function () {
  892. App.StackService.find.restore();
  893. App.StackServiceComponent.find.restore();
  894. });
  895. it('should format as role', function () {
  896. expect(this.obj.get('prop2')).to.equal('NameNode');
  897. });
  898. it('should format as role (2)', function () {
  899. this.obj.set('prop1', 'HIVE');
  900. expect(this.obj.get('prop2')).to.equal('Hive');
  901. });
  902. });
  903. describe('#sumBy', function () {
  904. beforeEach(function () {
  905. this.obj = Em.Object.create({
  906. prop1: [
  907. {a: 1}, {a: 2}, {a: 3}
  908. ],
  909. prop2: Em.computed.sumBy('prop1', 'a')
  910. });
  911. });
  912. it('should calculate sum', function () {
  913. expect(this.obj.get('prop2')).to.equal(6);
  914. });
  915. it('should calculate sum (2)', function () {
  916. this.obj.get('prop1').pushObject({a: 4});
  917. expect(this.obj.get('prop2')).to.equal(10);
  918. });
  919. it('0 for empty collection', function () {
  920. this.obj.set('prop1', []);
  921. expect(this.obj.get('prop2')).to.equal(0);
  922. });
  923. });
  924. describe('#i18nFormat', function () {
  925. beforeEach(function () {
  926. App.setProperties({
  927. someAnotherKey: 'some value'
  928. });
  929. sinon.stub(Em.I18n, 't', function (key) {
  930. var msgs = {
  931. key1: '{0} {1} {2}'
  932. };
  933. return msgs[key];
  934. });
  935. this.obj = Em.Object.create({
  936. prop1: 'abc',
  937. prop2: 'cba',
  938. prop3: 'aaa',
  939. prop4: Em.computed.i18nFormat('key1', 'prop1', 'prop2', 'prop3'),
  940. prop5: Em.computed.i18nFormat('not_existing_key', 'prop1', 'prop2', 'prop3'),
  941. prop6: Em.computed.i18nFormat('key1', 'App.someRandomTestingKey', 'prop2', 'prop3')
  942. });
  943. });
  944. afterEach(function () {
  945. Em.I18n.t.restore();
  946. });
  947. it('`prop4` check dependent keys', function () {
  948. expect(Em.meta(this.obj).descs.prop4._dependentKeys).to.eql(['prop1', 'prop2', 'prop3']);
  949. });
  950. it('should format message', function () {
  951. expect(this.obj.get('prop4')).to.equal('abc cba aaa');
  952. });
  953. it('should format message (2)', function () {
  954. this.obj.set('prop1', 'aaa');
  955. expect(this.obj.get('prop4')).to.equal('aaa cba aaa');
  956. });
  957. it('empty string for not existing i18-key', function () {
  958. expect(this.obj.get('prop5')).to.equal('');
  959. });
  960. it('`prop6` depends on App.* key', function () {
  961. expect(this.obj.get('prop6')).to.equal('some value cba aaa');
  962. App.set('someAnotherKey', '');
  963. expect(this.obj.get('prop6')).to.equal(' cba aaa');
  964. });
  965. it('prop6 dependent keys are valid', function () {
  966. expect(Em.meta(this.obj).descs.prop6._dependentKeys).to.eql(['App.someRandomTestingKey', 'prop2', 'prop3']);
  967. });
  968. });
  969. describe('#concat', function () {
  970. beforeEach(function () {
  971. App.setProperties({
  972. someAnotherKey: 'some value'
  973. });
  974. this.obj = Em.Object.create({
  975. prop1: 'abc',
  976. prop2: 'cba',
  977. prop3: 'aaa',
  978. prop4: Em.computed.concat(' ', 'prop1', 'prop2', 'prop3'),
  979. prop5: Em.computed.concat(' ', 'App.someRandomTestingKey', 'prop2', 'prop3'),
  980. prop6: Em.computed.concat(' ')
  981. });
  982. });
  983. it('should concat dependent values', function () {
  984. expect(this.obj.get('prop4')).to.equal('abc cba aaa');
  985. });
  986. it('should concat dependent values (2)', function () {
  987. this.obj.set('prop1', 'aaa');
  988. expect(this.obj.get('prop4')).to.equal('aaa cba aaa');
  989. });
  990. it('`prop5` depends on App.* key', function () {
  991. expect(this.obj.get('prop5')).to.equal('some value cba aaa');
  992. App.set('someAnotherKey', '');
  993. expect(this.obj.get('prop5')).to.equal(' cba aaa');
  994. });
  995. it('prop5 dependent keys are valid', function () {
  996. expect(Em.meta(this.obj).descs.prop5._dependentKeys).to.eql(['App.someRandomTestingKey', 'prop2', 'prop3']);
  997. });
  998. it('prop6 without dependent keys', function () {
  999. expect(this.obj.get('prop6')).to.equal('');
  1000. });
  1001. });
  1002. describe('#notExistsIn', function () {
  1003. beforeEach(function () {
  1004. this.obj = Em.Object.create({
  1005. prop1: 'v1',
  1006. prop2: Em.computed.notExistsIn('prop1', ['v1', 'v2'])
  1007. });
  1008. });
  1009. it('`false` if dependent value is in the array', function () {
  1010. expect(this.obj.get('prop2')).to.be.false;
  1011. });
  1012. it('`false` if dependent value is in the array (2)', function () {
  1013. this.obj.set('prop1', 'v2');
  1014. expect(this.obj.get('prop2')).to.be.false;
  1015. });
  1016. it('`true` if dependent value is not in the array', function () {
  1017. this.obj.set('prop1', 'v3');
  1018. expect(this.obj.get('prop2')).to.be.true;
  1019. });
  1020. });
  1021. describe('#firstNotBlank', function () {
  1022. beforeEach(function () {
  1023. App.setProperties({
  1024. someAnotherKey: 'NOT-EMPTY-STRING'
  1025. });
  1026. this.obj = Em.Object.create({
  1027. prop1: '',
  1028. prop2: null,
  1029. prop3: '1234',
  1030. prop4: Em.computed.firstNotBlank('prop1', 'prop2', 'prop3'),
  1031. prop5: Em.computed.firstNotBlank('prop1', 'App.someRandomTestingKey', 'prop3'),
  1032. prop6: Em.computed.firstNotBlank('prop1', 'prop2')
  1033. })
  1034. });
  1035. it('`prop4` check dependent keys', function () {
  1036. expect(Em.meta(this.obj).descs.prop4._dependentKeys).to.eql(['prop1', 'prop2', 'prop3']);
  1037. });
  1038. it('should returns prop3', function () {
  1039. expect(this.obj.get('prop4')).to.equal('1234');
  1040. });
  1041. it('should returns prop2', function () {
  1042. this.obj.set('prop2', 'not empty string');
  1043. expect(this.obj.get('prop4')).to.equal('not empty string');
  1044. });
  1045. it('should returns prop1', function () {
  1046. this.obj.set('prop2', 'not empty string');
  1047. this.obj.set('prop1', 'prop1 is used');
  1048. expect(this.obj.get('prop4')).to.equal('prop1 is used');
  1049. });
  1050. it('`prop5` depends on App.* key', function () {
  1051. expect(this.obj.get('prop5')).to.equal('NOT-EMPTY-STRING');
  1052. App.set('someAnotherKey', '!!!!!!!');
  1053. expect(this.obj.get('prop5')).to.equal('!!!!!!!');
  1054. App.set('someAnotherKey', null);
  1055. expect(this.obj.get('prop5')).to.equal('1234');
  1056. });
  1057. it('prop5 dependent keys are valid', function () {
  1058. expect(Em.meta(this.obj).descs.prop5._dependentKeys).to.eql(['prop1', 'App.someRandomTestingKey', 'prop3']);
  1059. });
  1060. it('prop6 depends on blank values', function () {
  1061. expect(this.obj.get('prop6')).to.be.null;
  1062. });
  1063. });
  1064. describe('#format', function () {
  1065. beforeEach(function () {
  1066. App.setProperties({
  1067. someAnotherKey: 'some value'
  1068. });
  1069. this.obj = Em.Object.create({
  1070. prop1: 'abc',
  1071. prop2: 'cba',
  1072. prop3: 'aaa',
  1073. prop4: Em.computed.format('{0} {1} {2}', 'prop1', 'prop2', 'prop3'),
  1074. prop5: Em.computed.format(null, 'prop1', 'prop2', 'prop3'),
  1075. prop6: Em.computed.format('{0} {1} {2}', 'App.someRandomTestingKey', 'prop2', 'prop3')
  1076. });
  1077. });
  1078. it('`prop4` check dependent keys', function () {
  1079. expect(Em.meta(this.obj).descs.prop4._dependentKeys).to.eql(['prop1', 'prop2', 'prop3']);
  1080. });
  1081. it('should format message', function () {
  1082. expect(this.obj.get('prop4')).to.equal('abc cba aaa');
  1083. });
  1084. it('should format message (2)', function () {
  1085. this.obj.set('prop1', 'aaa');
  1086. expect(this.obj.get('prop4')).to.equal('aaa cba aaa');
  1087. });
  1088. it('empty string for not existing i18-key', function () {
  1089. expect(this.obj.get('prop5')).to.equal('');
  1090. });
  1091. it('`prop6` depends on App.* key', function () {
  1092. expect(this.obj.get('prop6')).to.equal('some value cba aaa');
  1093. App.set('someAnotherKey', '');
  1094. expect(this.obj.get('prop6')).to.equal(' cba aaa');
  1095. });
  1096. it('prop6 dependent keys are valid', function () {
  1097. expect(Em.meta(this.obj).descs.prop6._dependentKeys).to.eql(['App.someRandomTestingKey', 'prop2', 'prop3']);
  1098. });
  1099. });
  1100. describe('#formatUnavailable', function () {
  1101. beforeEach(function () {
  1102. App.setProperties({
  1103. someAnotherKey: 1
  1104. });
  1105. this.obj = Em.Object.create({
  1106. prop1: 1,
  1107. prop2: Em.computed.formatUnavailable('prop1'),
  1108. prop3: Em.computed.formatUnavailable('App.someRandomTestingKey')
  1109. });
  1110. });
  1111. it('`value` is 1', function () {
  1112. expect(this.obj.get('prop2')).to.equal(1);
  1113. expect(this.obj.get('prop3')).to.equal(1);
  1114. });
  1115. it('`value` is 0', function () {
  1116. App.set('someAnotherKey', 0);
  1117. this.obj.set('prop1', 0);
  1118. expect(this.obj.get('prop2')).to.equal(0);
  1119. expect(this.obj.get('prop3')).to.equal(0);
  1120. });
  1121. it('`value` is `0`', function () {
  1122. App.set('someAnotherKey', '0');
  1123. this.obj.set('prop1', '0');
  1124. expect(this.obj.get('prop2')).to.equal('0');
  1125. expect(this.obj.get('prop3')).to.equal('0');
  1126. });
  1127. it('`value` is not numeric', function () {
  1128. App.set('someAnotherKey', null);
  1129. this.obj.set('prop1', null);
  1130. expect(this.obj.get('prop2')).to.equal('n/a');
  1131. expect(this.obj.get('prop3')).to.equal('n/a');
  1132. });
  1133. it('prop3 dependent keys are valid', function () {
  1134. expect(Em.meta(this.obj).descs.prop3._dependentKeys).to.eql(['App.someRandomTestingKey']);
  1135. });
  1136. });
  1137. describe('#countBasedMessage', function () {
  1138. var msg0 = 'msg0';
  1139. var msg1 = 'msg1';
  1140. var msgM = 'msgM';
  1141. beforeEach(function () {
  1142. App.setProperties({
  1143. someAnotherKey: 1
  1144. });
  1145. this.obj = Em.Object.create({
  1146. prop1: 1,
  1147. prop2: Em.computed.countBasedMessage('prop1', msg0, msg1, msgM),
  1148. prop3: Em.computed.countBasedMessage('App.someRandomTestingKey', msg0, msg1, msgM)
  1149. });
  1150. });
  1151. it('`value` is 1', function () {
  1152. expect(this.obj.get('prop2')).to.equal(msg1);
  1153. expect(this.obj.get('prop3')).to.equal(msg1);
  1154. });
  1155. it('`value` is 0', function () {
  1156. App.set('someAnotherKey', 0);
  1157. this.obj.set('prop1', 0);
  1158. expect(this.obj.get('prop2')).to.equal(msg0);
  1159. expect(this.obj.get('prop3')).to.equal(msg0);
  1160. });
  1161. it('`value` is greater than 1', function () {
  1162. App.set('someAnotherKey', 3);
  1163. this.obj.set('prop1', 3);
  1164. expect(this.obj.get('prop2')).to.equal(msgM);
  1165. expect(this.obj.get('prop3')).to.equal(msgM);
  1166. });
  1167. it('prop3 dependent keys are valid', function () {
  1168. expect(Em.meta(this.obj).descs.prop3._dependentKeys).to.eql(['App.someRandomTestingKey']);
  1169. });
  1170. });
  1171. });