getPropValue-flowparser-test.js 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963
  1. /* eslint-env mocha */
  2. /* eslint no-template-curly-in-string: 0 */
  3. import assert from 'assert';
  4. import {
  5. extractProp,
  6. changePlugins,
  7. describeIfNotBabylon,
  8. setParserName,
  9. } from '../helper';
  10. import getPropValue from '../../src/getPropValue';
  11. describe('getPropValue', () => {
  12. beforeEach(() => {
  13. setParserName('flow');
  14. });
  15. it('should export a function', () => {
  16. const expected = 'function';
  17. const actual = typeof getPropValue;
  18. assert.equal(actual, expected);
  19. });
  20. it('should return undefined when not provided with a JSXAttribute', () => {
  21. const expected = undefined;
  22. const actual = getPropValue(1);
  23. assert.equal(actual, expected);
  24. });
  25. it('should throw not error when trying to get value from unknown node type', () => {
  26. const prop = {
  27. type: 'JSXAttribute',
  28. value: {
  29. type: 'JSXExpressionContainer',
  30. },
  31. };
  32. let counter = 0;
  33. // eslint-disable-next-line no-console
  34. const errorOrig = console.error;
  35. // eslint-disable-next-line no-console
  36. console.error = () => {
  37. counter += 1;
  38. };
  39. let value;
  40. assert.doesNotThrow(() => {
  41. value = getPropValue(prop);
  42. }, Error);
  43. assert.equal(null, value);
  44. assert.equal(counter, 1);
  45. // eslint-disable-next-line no-console
  46. console.error = errorOrig;
  47. });
  48. describe('Null', () => {
  49. it('should return true when no value is given', () => {
  50. const prop = extractProp('<div foo />');
  51. const expected = true;
  52. const actual = getPropValue(prop);
  53. assert.equal(actual, expected);
  54. });
  55. });
  56. describe('Literal', () => {
  57. it('should return correct string if value is a string', () => {
  58. const prop = extractProp('<div foo="bar" />');
  59. const expected = 'bar';
  60. const actual = getPropValue(prop);
  61. assert.equal(actual, expected);
  62. });
  63. it('should return correct string if value is a string expression', () => {
  64. const prop = extractProp('<div foo={"bar"} />');
  65. const expected = 'bar';
  66. const actual = getPropValue(prop);
  67. assert.equal(actual, expected);
  68. });
  69. it('should return correct integer if value is a integer expression', () => {
  70. const prop = extractProp('<div foo={1} />');
  71. const expected = 1;
  72. const actual = getPropValue(prop);
  73. assert.equal(actual, expected);
  74. });
  75. it('should convert "true" to boolean type', () => {
  76. const prop = extractProp('<div foo="true" />');
  77. const expected = true;
  78. const actual = getPropValue(prop);
  79. assert.equal(actual, expected);
  80. });
  81. it('should convert "false" to boolean type', () => {
  82. const prop = extractProp('<div foo="false" />');
  83. const expected = false;
  84. const actual = getPropValue(prop);
  85. assert.equal(actual, expected);
  86. });
  87. });
  88. describe('JSXElement', () => {
  89. it('should return correct representation of JSX element as a string', () => {
  90. const prop = extractProp('<div foo={<bar />} />');
  91. const expected = '<bar />';
  92. const actual = getPropValue(prop);
  93. assert.equal(actual, expected);
  94. });
  95. });
  96. describe('Identifier', () => {
  97. it('should return string representation of variable identifier', () => {
  98. const prop = extractProp('<div foo={bar} />');
  99. const expected = 'bar';
  100. const actual = getPropValue(prop);
  101. assert.equal(actual, expected);
  102. });
  103. it('should return undefined when identifier is literally `undefined`', () => {
  104. const prop = extractProp('<div foo={undefined} />');
  105. const expected = undefined;
  106. const actual = getPropValue(prop);
  107. assert.equal(actual, expected);
  108. });
  109. it('should return String object when using a reserved JavaScript object', () => {
  110. const prop = extractProp('<div foo={String} />');
  111. const expected = String;
  112. const actual = getPropValue(prop);
  113. assert.equal(actual, expected);
  114. });
  115. it('should return Array object when using a reserved JavaScript object', () => {
  116. const prop = extractProp('<div foo={Array} />');
  117. const expected = Array;
  118. const actual = getPropValue(prop);
  119. assert.equal(actual, expected);
  120. });
  121. it('should return Date object when using a reserved JavaScript object', () => {
  122. const prop = extractProp('<div foo={Date} />');
  123. const expected = Date;
  124. const actual = getPropValue(prop);
  125. assert.equal(actual, expected);
  126. });
  127. it('should return Infinity object when using a reserved JavaScript object', () => {
  128. const prop = extractProp('<div foo={Infinity} />');
  129. const expected = Infinity;
  130. const actual = getPropValue(prop);
  131. assert.equal(actual, expected);
  132. });
  133. it('should return Math object when using a reserved JavaScript object', () => {
  134. const prop = extractProp('<div foo={Math} />');
  135. const expected = Math;
  136. const actual = getPropValue(prop);
  137. assert.equal(actual, expected);
  138. });
  139. it('should return Number object when using a reserved JavaScript object', () => {
  140. const prop = extractProp('<div foo={Number} />');
  141. const expected = Number;
  142. const actual = getPropValue(prop);
  143. assert.equal(actual, expected);
  144. });
  145. it('should return Object object when using a reserved JavaScript object', () => {
  146. const prop = extractProp('<div foo={Object} />');
  147. const expected = Object;
  148. const actual = getPropValue(prop);
  149. assert.equal(actual, expected);
  150. });
  151. });
  152. describe('Template literal', () => {
  153. it('should return template literal with vars wrapped in curly braces', () => {
  154. const prop = extractProp('<div foo={`bar ${baz}`} />');
  155. const expected = 'bar {baz}';
  156. const actual = getPropValue(prop);
  157. assert.equal(actual, expected);
  158. });
  159. it('should return string "undefined" for expressions that evaluate to undefined', () => {
  160. const prop = extractProp('<div foo={`bar ${undefined}`} />');
  161. const expected = 'bar undefined';
  162. const actual = getPropValue(prop);
  163. assert.equal(actual, expected);
  164. });
  165. it('should return template literal with expression type wrapped in curly braces', () => {
  166. const prop = extractProp('<div foo={`bar ${baz()}`} />');
  167. const expected = 'bar {CallExpression}';
  168. const actual = getPropValue(prop);
  169. assert.equal(actual, expected);
  170. });
  171. it('should ignore non-expressions in the template literal', () => {
  172. const prop = extractProp('<div foo={`bar ${<baz />}`} />');
  173. const expected = 'bar ';
  174. const actual = getPropValue(prop);
  175. assert.equal(actual, expected);
  176. });
  177. });
  178. describe('Tagged Template literal', () => {
  179. it('should return template literal with vars wrapped in curly braces', () => {
  180. const prop = extractProp('<div foo={noop`bar ${baz}`} />');
  181. const expected = 'bar {baz}';
  182. const actual = getPropValue(prop);
  183. assert.equal(actual, expected);
  184. });
  185. it('should return string "undefined" for expressions that evaluate to undefined', () => {
  186. const prop = extractProp('<div foo={noop`bar ${undefined}`} />');
  187. const expected = 'bar undefined';
  188. const actual = getPropValue(prop);
  189. assert.equal(actual, expected);
  190. });
  191. it('should return template literal with expression type wrapped in curly braces', () => {
  192. const prop = extractProp('<div foo={noop`bar ${baz()}`} />');
  193. const expected = 'bar {CallExpression}';
  194. const actual = getPropValue(prop);
  195. assert.equal(actual, expected);
  196. });
  197. it('should ignore non-expressions in the template literal', () => {
  198. const prop = extractProp('<div foo={noop`bar ${<baz />}`} />');
  199. const expected = 'bar ';
  200. const actual = getPropValue(prop);
  201. assert.equal(actual, expected);
  202. });
  203. });
  204. describe('Arrow function expression', () => {
  205. it('should return a function', () => {
  206. const prop = extractProp('<div foo={ () => { return "bar"; }} />');
  207. const expected = 'function';
  208. const actual = getPropValue(prop);
  209. assert.equal(expected, typeof actual);
  210. // For code coverage ¯\_(ツ)_/¯
  211. actual();
  212. });
  213. it('should handle ArrowFunctionExpression as conditional consequent', () => {
  214. const prop = extractProp('<div foo={ (true) ? () => null : () => ({})} />');
  215. const expected = 'function';
  216. const actual = getPropValue(prop);
  217. assert.equal(expected, typeof actual);
  218. // For code coverage ¯\_(ツ)_/¯
  219. actual();
  220. });
  221. });
  222. describe('Function expression', () => {
  223. it('should return a function', () => {
  224. const prop = extractProp('<div foo={ function() { return "bar"; } } />');
  225. const expected = 'function';
  226. const actual = getPropValue(prop);
  227. assert.equal(expected, typeof actual);
  228. // For code coverage ¯\_(ツ)_/¯
  229. actual();
  230. });
  231. });
  232. describe('Logical expression', () => {
  233. it('should correctly infer result of && logical expression based on derived values', () => {
  234. const prop = extractProp('<div foo={bar && baz} />');
  235. const expected = 'baz';
  236. const actual = getPropValue(prop);
  237. assert.equal(actual, expected);
  238. });
  239. it('should return undefined when evaluating `undefined && undefined` ', () => {
  240. const prop = extractProp('<div foo={undefined && undefined} />');
  241. const expected = undefined;
  242. const actual = getPropValue(prop);
  243. assert.equal(actual, expected);
  244. });
  245. it('should correctly infer result of || logical expression based on derived values', () => {
  246. const prop = extractProp('<div foo={bar || baz} />');
  247. const expected = 'bar';
  248. const actual = getPropValue(prop);
  249. assert.equal(actual, expected);
  250. });
  251. it('should correctly infer result of || logical expression based on derived values', () => {
  252. const prop = extractProp('<div foo={undefined || baz} />');
  253. const expected = 'baz';
  254. const actual = getPropValue(prop);
  255. assert.equal(actual, expected);
  256. });
  257. it('should return undefined when evaluating `undefined || undefined` ', () => {
  258. const prop = extractProp('<div foo={undefined || undefined} />');
  259. const expected = undefined;
  260. const actual = getPropValue(prop);
  261. assert.equal(actual, expected);
  262. });
  263. });
  264. describe('Member expression', () => {
  265. it('should return string representation of form `object.property`', () => {
  266. const prop = extractProp('<div foo={bar.baz} />');
  267. const expected = 'bar.baz';
  268. const actual = getPropValue(prop);
  269. assert.equal(actual, expected);
  270. });
  271. it('should evaluate to a correct representation of member expression with a nullable member', () => {
  272. const prop = extractProp('<div foo={bar?.baz} />');
  273. const expected = 'bar?.baz';
  274. const actual = getPropValue(prop);
  275. assert.equal(actual, expected);
  276. });
  277. });
  278. describe('Call expression', () => {
  279. it('should return string representation of callee', () => {
  280. const prop = extractProp('<div foo={bar()} />');
  281. const expected = 'bar()';
  282. const actual = getPropValue(prop);
  283. assert.equal(actual, expected);
  284. });
  285. it('should return string representation of callee', () => {
  286. const prop = extractProp('<div foo={bar.call()} />');
  287. const expected = 'bar.call()';
  288. const actual = getPropValue(prop);
  289. assert.equal(actual, expected);
  290. });
  291. });
  292. describe('Unary expression', () => {
  293. it('should correctly evaluate an expression that prefixes with -', () => {
  294. const prop = extractProp('<div foo={-bar} />');
  295. // -"bar" => NaN
  296. const expected = true;
  297. const actual = Number.isNaN(getPropValue(prop));
  298. assert.equal(actual, expected);
  299. });
  300. it('should correctly evaluate an expression that prefixes with -', () => {
  301. const prop = extractProp('<div foo={-42} />');
  302. const expected = -42;
  303. const actual = getPropValue(prop);
  304. assert.equal(actual, expected);
  305. });
  306. it('should correctly evaluate an expression that prefixes with +', () => {
  307. const prop = extractProp('<div foo={+bar} />');
  308. // +"bar" => NaN
  309. const expected = true;
  310. const actual = Number.isNaN(getPropValue(prop));
  311. assert.equal(actual, expected);
  312. });
  313. it('should correctly evaluate an expression that prefixes with +', () => {
  314. const prop = extractProp('<div foo={+42} />');
  315. const expected = 42;
  316. const actual = getPropValue(prop);
  317. assert.equal(actual, expected);
  318. });
  319. it('should correctly evaluate an expression that prefixes with !', () => {
  320. const prop = extractProp('<div foo={!bar} />');
  321. const expected = false; // !"bar" === false
  322. const actual = getPropValue(prop);
  323. assert.equal(actual, expected);
  324. });
  325. it('should correctly evaluate an expression that prefixes with ~', () => {
  326. const prop = extractProp('<div foo={~bar} />');
  327. const expected = -1; // ~"bar" === -1
  328. const actual = getPropValue(prop);
  329. assert.equal(actual, expected);
  330. });
  331. it('should return true when evaluating `delete foo`', () => {
  332. const prop = extractProp('<div foo={delete x} />');
  333. const expected = true;
  334. const actual = getPropValue(prop);
  335. assert.equal(actual, expected);
  336. });
  337. it('should return undefined when evaluating `void foo`', () => {
  338. const prop = extractProp('<div foo={void x} />');
  339. const expected = undefined;
  340. const actual = getPropValue(prop);
  341. assert.equal(actual, expected);
  342. });
  343. // TODO: We should fix this to check to see if we can evaluate it.
  344. it('should return undefined when evaluating `typeof foo`', () => {
  345. const prop = extractProp('<div foo={typeof x} />');
  346. const expected = undefined;
  347. const actual = getPropValue(prop);
  348. assert.equal(actual, expected);
  349. });
  350. });
  351. describe('Update expression', () => {
  352. it('should correctly evaluate an expression that prefixes with ++', () => {
  353. const prop = extractProp('<div foo={++bar} />');
  354. // ++"bar" => NaN
  355. const expected = true;
  356. const actual = Number.isNaN(getPropValue(prop));
  357. assert.equal(actual, expected);
  358. });
  359. it('should correctly evaluate an expression that prefixes with --', () => {
  360. const prop = extractProp('<div foo={--bar} />');
  361. const expected = true;
  362. const actual = Number.isNaN(getPropValue(prop));
  363. assert.equal(actual, expected);
  364. });
  365. it('should correctly evaluate an expression that suffixes with ++', () => {
  366. const prop = extractProp('<div foo={bar++} />');
  367. // "bar"++ => NaN
  368. const expected = true;
  369. const actual = Number.isNaN(getPropValue(prop));
  370. assert.equal(actual, expected);
  371. });
  372. it('should correctly evaluate an expression that suffixes with --', () => {
  373. const prop = extractProp('<div foo={bar--} />');
  374. const expected = true;
  375. const actual = Number.isNaN(getPropValue(prop));
  376. assert.equal(actual, expected);
  377. });
  378. });
  379. describe('This expression', () => {
  380. it('should return string value `this`', () => {
  381. const prop = extractProp('<div foo={this} />');
  382. const expected = 'this';
  383. const actual = getPropValue(prop);
  384. assert.equal(actual, expected);
  385. });
  386. });
  387. describe('Conditional expression', () => {
  388. it('should evaluate the conditional based on the derived values correctly', () => {
  389. const prop = extractProp('<div foo={bar ? baz : bam} />');
  390. const expected = 'baz';
  391. const actual = getPropValue(prop);
  392. assert.equal(actual, expected);
  393. });
  394. it('should evaluate the conditional based on the derived values correctly', () => {
  395. const prop = extractProp('<div foo={undefined ? baz : bam} />');
  396. const expected = 'bam';
  397. const actual = getPropValue(prop);
  398. assert.equal(actual, expected);
  399. });
  400. it('should evaluate the conditional based on the derived values correctly', () => {
  401. const prop = extractProp('<div foo={(1 > 2) ? baz : bam} />');
  402. const expected = 'bam';
  403. const actual = getPropValue(prop);
  404. assert.equal(actual, expected);
  405. });
  406. });
  407. describe('Binary expression', () => {
  408. it('should evaluate the `==` operator correctly', () => {
  409. const trueProp = extractProp('<div foo={1 == "1"} />');
  410. const falseProp = extractProp('<div foo={1 == bar} />');
  411. const trueVal = getPropValue(trueProp);
  412. const falseVal = getPropValue(falseProp);
  413. assert.equal(true, trueVal);
  414. assert.equal(false, falseVal);
  415. });
  416. it('should evaluate the `!=` operator correctly', () => {
  417. const trueProp = extractProp('<div foo={1 != "2"} />');
  418. const falseProp = extractProp('<div foo={1 != "1"} />');
  419. const trueVal = getPropValue(trueProp);
  420. const falseVal = getPropValue(falseProp);
  421. assert.equal(true, trueVal);
  422. assert.equal(false, falseVal);
  423. });
  424. it('should evaluate the `===` operator correctly', () => {
  425. const trueProp = extractProp('<div foo={1 === 1} />');
  426. const falseProp = extractProp('<div foo={1 === "1"} />');
  427. const trueVal = getPropValue(trueProp);
  428. const falseVal = getPropValue(falseProp);
  429. assert.equal(true, trueVal);
  430. assert.equal(false, falseVal);
  431. });
  432. it('should evaluate the `!==` operator correctly', () => {
  433. const trueProp = extractProp('<div foo={1 !== "1"} />');
  434. const falseProp = extractProp('<div foo={1 !== 1} />');
  435. const trueVal = getPropValue(trueProp);
  436. const falseVal = getPropValue(falseProp);
  437. assert.equal(true, trueVal);
  438. assert.equal(false, falseVal);
  439. });
  440. it('should evaluate the `<` operator correctly', () => {
  441. const trueProp = extractProp('<div foo={1 < 2} />');
  442. const falseProp = extractProp('<div foo={1 < 0} />');
  443. const trueVal = getPropValue(trueProp);
  444. const falseVal = getPropValue(falseProp);
  445. assert.equal(true, trueVal);
  446. assert.equal(false, falseVal);
  447. });
  448. it('should evaluate the `>` operator correctly', () => {
  449. const trueProp = extractProp('<div foo={1 > 0} />');
  450. const falseProp = extractProp('<div foo={1 > 2} />');
  451. const trueVal = getPropValue(trueProp);
  452. const falseVal = getPropValue(falseProp);
  453. assert.equal(true, trueVal);
  454. assert.equal(false, falseVal);
  455. });
  456. it('should evaluate the `<=` operator correctly', () => {
  457. const trueProp = extractProp('<div foo={1 <= 1} />');
  458. const falseProp = extractProp('<div foo={1 <= 0} />');
  459. const trueVal = getPropValue(trueProp);
  460. const falseVal = getPropValue(falseProp);
  461. assert.equal(true, trueVal);
  462. assert.equal(false, falseVal);
  463. });
  464. it('should evaluate the `>=` operator correctly', () => {
  465. const trueProp = extractProp('<div foo={1 >= 1} />');
  466. const falseProp = extractProp('<div foo={1 >= 2} />');
  467. const trueVal = getPropValue(trueProp);
  468. const falseVal = getPropValue(falseProp);
  469. assert.equal(true, trueVal);
  470. assert.equal(false, falseVal);
  471. });
  472. it('should evaluate the `<<` operator correctly', () => {
  473. const prop = extractProp('<div foo={1 << 2} />');
  474. const expected = 4;
  475. const actual = getPropValue(prop);
  476. assert.equal(actual, expected);
  477. });
  478. it('should evaluate the `>>` operator correctly', () => {
  479. const prop = extractProp('<div foo={1 >> 2} />');
  480. const expected = 0;
  481. const actual = getPropValue(prop);
  482. assert.equal(actual, expected);
  483. });
  484. it('should evaluate the `>>>` operator correctly', () => {
  485. const prop = extractProp('<div foo={2 >>> 1} />');
  486. const expected = 1;
  487. const actual = getPropValue(prop);
  488. assert.equal(actual, expected);
  489. });
  490. it('should evaluate the `+` operator correctly', () => {
  491. const prop = extractProp('<div foo={1 + 1} />');
  492. const expected = 2;
  493. const actual = getPropValue(prop);
  494. assert.equal(actual, expected);
  495. });
  496. it('should evaluate the `-` operator correctly', () => {
  497. const prop = extractProp('<div foo={1 - 1} />');
  498. const expected = 0;
  499. const actual = getPropValue(prop);
  500. assert.equal(actual, expected);
  501. });
  502. it('should evaluate the `*` operator correctly', () => {
  503. const prop = extractProp('<div foo={10 * 10} />');
  504. const expected = 100;
  505. const actual = getPropValue(prop);
  506. assert.equal(actual, expected);
  507. });
  508. it('should evaluate the `/` operator correctly', () => {
  509. const prop = extractProp('<div foo={10 / 2} />');
  510. const expected = 5;
  511. const actual = getPropValue(prop);
  512. assert.equal(actual, expected);
  513. });
  514. it('should evaluate the `%` operator correctly', () => {
  515. const prop = extractProp('<div foo={10 % 3} />');
  516. const expected = 1;
  517. const actual = getPropValue(prop);
  518. assert.equal(actual, expected);
  519. });
  520. it('should evaluate the `|` operator correctly', () => {
  521. const prop = extractProp('<div foo={10 | 1} />');
  522. const expected = 11;
  523. const actual = getPropValue(prop);
  524. assert.equal(actual, expected);
  525. });
  526. it('should evaluate the `^` operator correctly', () => {
  527. const prop = extractProp('<div foo={10 ^ 1} />');
  528. const expected = 11;
  529. const actual = getPropValue(prop);
  530. assert.equal(actual, expected);
  531. });
  532. it('should evaluate the `&` operator correctly', () => {
  533. const prop = extractProp('<div foo={10 & 1} />');
  534. const expected = 0;
  535. const actual = getPropValue(prop);
  536. assert.equal(actual, expected);
  537. });
  538. it('should evaluate the `in` operator correctly', () => {
  539. const prop = extractProp('<div foo={foo in bar} />');
  540. const expected = false;
  541. const actual = getPropValue(prop);
  542. assert.equal(actual, expected);
  543. });
  544. it('should evaluate the `instanceof` operator correctly', () => {
  545. const prop = extractProp('<div foo={{} instanceof Object} />');
  546. const expected = true;
  547. const actual = getPropValue(prop);
  548. assert.equal(actual, expected);
  549. });
  550. it('should evaluate the `instanceof` operator when right side is not a function', () => {
  551. const prop = extractProp('<div foo={"bar" instanceof Baz} />');
  552. const expected = false;
  553. const actual = getPropValue(prop);
  554. assert.equal(actual, expected);
  555. });
  556. });
  557. describe('Object expression', () => {
  558. it('should evaluate to a correct representation of the object in props', () => {
  559. const prop = extractProp('<div foo={ { bar: "baz" } } />');
  560. const expected = { bar: 'baz' };
  561. const actual = getPropValue(prop);
  562. assert.deepEqual(actual, expected);
  563. });
  564. it('should evaluate to a correct representation of the object, ignore spread properties', () => {
  565. const prop = extractProp('<div foo={{bar: "baz", ...{baz: "bar", foo: {...{bar: "meh"}}}}} />');
  566. const expected = { bar: 'baz', baz: 'bar', foo: { bar: 'meh' } };
  567. const actual = getPropValue(prop);
  568. assert.deepEqual(actual, expected);
  569. });
  570. it('should evaluate to a correct representation of the object, ignore spread properties', () => {
  571. const prop = extractProp('<div foo={{ pathname: manageRoute, state: {...data}}} />');
  572. const expected = { pathname: 'manageRoute', state: {} };
  573. const actual = getPropValue(prop);
  574. assert.deepEqual(actual, expected);
  575. });
  576. });
  577. describe('New expression', () => {
  578. it('should return a new empty object', () => {
  579. const prop = extractProp('<div foo={new Bar()} />');
  580. const expected = {};
  581. const actual = getPropValue(prop);
  582. assert.deepEqual(actual, expected);
  583. });
  584. });
  585. describe('Sequence array expression', () => {
  586. it('should evaluate to correct representation of the the array in props', () => {
  587. const prop = extractProp('<div foo={[{"type":"Literal","start":821,"end":827}]} />');
  588. const expected = [{
  589. type: 'Literal',
  590. start: 821,
  591. end: 827,
  592. }];
  593. const actual = getPropValue(prop);
  594. assert.deepEqual(actual, expected);
  595. });
  596. });
  597. describe('Array expression', () => {
  598. it('should evaluate to correct representation of the the array in props', () => {
  599. const prop = extractProp('<div foo={["bar", 42, null]} />');
  600. const expected = ['bar', 42, null];
  601. const actual = getPropValue(prop);
  602. assert.deepEqual(actual, expected);
  603. });
  604. it('should evaluate to a correct representation of an array with spread elements', () => {
  605. const prop = extractProp('<div foo={[...this.props.params, bar]} />');
  606. const expected = [undefined, 'bar'];
  607. const actual = getPropValue(prop);
  608. assert.deepEqual(actual, expected);
  609. });
  610. });
  611. it('should return an empty array provided an empty array in props', () => {
  612. const prop = extractProp('<div foo={[]} />');
  613. const expected = [];
  614. const actual = getPropValue(prop);
  615. assert.deepEqual(actual, expected);
  616. });
  617. describe('Bind expression', () => {
  618. it('should return string representation of bind function call when object is null', () => {
  619. const prop = extractProp('<div foo={::this.handleClick} />');
  620. const expected = null;
  621. const actual = getPropValue(prop);
  622. assert.deepEqual(actual, expected);
  623. });
  624. it('should return string representation of bind function call when object is not null', () => {
  625. const prop = extractProp('<div foo={foo::bar} />');
  626. const expected = 'foo';
  627. const actual = getPropValue(prop);
  628. assert.deepEqual(actual, expected);
  629. });
  630. it('should return string representation of bind function call when binding to object properties', () => {
  631. const prop = extractProp('<div foo={a.b::c} />');
  632. const otherProp = extractProp('<div foo={::a.b.c} />');
  633. const expected = 'a.b';
  634. const actual = getPropValue(prop);
  635. const otherExpected = null;
  636. const otherActual = getPropValue(otherProp);
  637. assert.deepEqual(actual, expected);
  638. assert.deepEqual(otherActual, otherExpected);
  639. });
  640. });
  641. describe('Type Cast Expression', () => {
  642. it('should return the expression from a type cast', () => {
  643. const prop = extractProp('<div foo={(this.handleClick: (event: MouseEvent) => void))} />');
  644. const expected = 'this.handleClick';
  645. const actual = getPropValue(prop);
  646. assert.deepEqual(actual, expected);
  647. });
  648. });
  649. describeIfNotBabylon('Typescript', () => {
  650. beforeEach(() => {
  651. changePlugins((pls) => [...pls, 'typescript']);
  652. });
  653. it('should return string representation of variable identifier wrapped in a Typescript non-null assertion', () => {
  654. const prop = extractProp('<div foo={bar!} />');
  655. const expected = 'bar';
  656. const actual = getPropValue(prop);
  657. assert.equal(actual, expected);
  658. });
  659. it('should return string representation of variable identifier wrapped in a deep Typescript non-null assertion', () => {
  660. const prop = extractProp('<div foo={(bar!)!} />');
  661. const expected = 'bar';
  662. const actual = getPropValue(prop);
  663. assert.equal(actual, expected);
  664. });
  665. it('should return string representation of variable identifier wrapped in a Typescript type coercion', () => {
  666. changePlugins((pls) => [...pls, 'typescript']);
  667. const prop = extractProp('<div foo={bar as any} />');
  668. const expected = 'bar';
  669. const actual = getPropValue(prop);
  670. assert.equal(actual, expected);
  671. });
  672. });
  673. describe('JSX empty expression', () => {
  674. it('should work with an empty expression', () => {
  675. const prop = extractProp('<div>\n{/* Hello there */}\n</div>', 'children');
  676. const expected = undefined;
  677. const actual = getPropValue(prop);
  678. assert.equal(actual, expected);
  679. });
  680. });
  681. });