ember_computed_test.js 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610
  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. describe('#truncate', function () {
  1226. beforeEach(function () {
  1227. this.obj = Em.Object.create({
  1228. prop1: '123456789',
  1229. prop2: Em.computed.truncate('prop1', 8, 5),
  1230. prop3: Em.computed.truncate('App.someRandomTestingKey', 8, 5),
  1231. prop4: Em.computed.truncate('prop1', 8, 5, '###')
  1232. });
  1233. App.set('someAnotherKey', 'abcdefghi');
  1234. });
  1235. it('prop2 dependent keys are valid', function () {
  1236. expect(Em.meta(this.obj).descs.prop2._dependentKeys).to.be.eql(['prop1']);
  1237. });
  1238. it('prop3 dependent keys are valid', function () {
  1239. expect(Em.meta(this.obj).descs.prop3._dependentKeys).to.be.eql(['App.someRandomTestingKey']);
  1240. });
  1241. it('prop4 dependent keys are valid', function () {
  1242. expect(Em.meta(this.obj).descs.prop4._dependentKeys).to.be.eql(['prop1']);
  1243. });
  1244. it('prop2 value is 12345...', function () {
  1245. expect(this.obj.get('prop2')).to.be.equal('12345...');
  1246. });
  1247. it('prop2 value is 54321...', function () {
  1248. this.obj.set('prop1', '543216789');
  1249. expect(this.obj.get('prop2')).to.be.equal('54321...');
  1250. });
  1251. it('prop2 value is 1234', function () {
  1252. this.obj.set('prop1', '1234');
  1253. expect(this.obj.get('prop2')).to.be.equal('1234');
  1254. });
  1255. it('prop2 value is ""', function () {
  1256. this.obj.set('prop1', null);
  1257. expect(this.obj.get('prop2')).to.be.equal('');
  1258. });
  1259. it('prop3 value is abcde...', function () {
  1260. expect(this.obj.get('prop3')).to.be.equal('abcde...');
  1261. });
  1262. it('prop3 value is edcba...', function () {
  1263. App.set('someAnotherKey', 'edcbafghi');
  1264. expect(this.obj.get('prop3')).to.be.equal('edcba...');
  1265. });
  1266. it('prop3 value is abcd', function () {
  1267. App.set('someAnotherKey', 'abcd');
  1268. expect(this.obj.get('prop3')).to.be.equal('abcd');
  1269. });
  1270. it('prop4 value is 12345###', function () {
  1271. expect(this.obj.get('prop4')).to.be.equal('12345###');
  1272. });
  1273. it('prop4 value is 54321###', function () {
  1274. this.obj.set('prop1', '543216789');
  1275. expect(this.obj.get('prop4')).to.be.equal('54321###');
  1276. });
  1277. it('prop4 value is 12345', function () {
  1278. this.obj.set('prop1', '12345');
  1279. expect(this.obj.get('prop4')).to.be.equal('12345');
  1280. });
  1281. });
  1282. });