source: imaps-frontend/node_modules/jsx-ast-utils/__tests__/src/getPropLiteralValue-babelparser-test.js@ d565449

main
Last change on this file since d565449 was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago

Update repo after prototype presentation

  • Property mode set to 100644
File size: 15.0 KB
Line 
1/* eslint-env mocha */
2/* eslint no-template-curly-in-string: 0 */
3import assert from 'assert';
4import {
5 extractProp,
6 describeIfNotBabylon,
7 changePlugins,
8 setParserName,
9 setIsESM,
10} from '../helper';
11import { getLiteralPropValue } from '../../src/getPropValue';
12
13describe('getLiteralPropValue', () => {
14 beforeEach(() => {
15 setParserName('babel');
16 });
17 it('should export a function', () => {
18 const expected = 'function';
19 const actual = typeof getLiteralPropValue;
20
21 assert.equal(actual, expected);
22 });
23
24 it('should return undefined when not provided with a JSXAttribute', () => {
25 const expected = undefined;
26 const actual = getLiteralPropValue(1);
27
28 assert.equal(actual, expected);
29 });
30
31 it('should not throw error when trying to get value from unknown node type', () => {
32 const prop = {
33 type: 'JSXAttribute',
34 value: {
35 type: 'JSXExpressionContainer',
36 },
37 };
38 let counter = 0;
39 // eslint-disable-next-line no-console
40 const errorOrig = console.error;
41 // eslint-disable-next-line no-console
42 console.error = () => {
43 counter += 1;
44 };
45 let value;
46 assert.doesNotThrow(() => {
47 value = getLiteralPropValue(prop);
48 }, Error);
49
50 assert.equal(null, value);
51 assert.equal(counter, 1);
52 // eslint-disable-next-line no-console
53 console.error = errorOrig;
54 });
55
56 describe('Null', () => {
57 it('should return true when no value is given', () => {
58 const prop = extractProp('<div foo />');
59
60 const expected = true;
61 const actual = getLiteralPropValue(prop);
62
63 assert.equal(actual, expected);
64 });
65 });
66
67 describe('Literal', () => {
68 it('should return correct string if value is a string', () => {
69 const prop = extractProp('<div foo="bar" />');
70
71 const expected = 'bar';
72 const actual = getLiteralPropValue(prop);
73
74 assert.equal(actual, expected);
75 });
76
77 it('should return correct string if value is a string expression', () => {
78 const prop = extractProp('<div foo={"bar"} />');
79
80 const expected = 'bar';
81 const actual = getLiteralPropValue(prop);
82
83 assert.equal(actual, expected);
84 });
85
86 it('should return correct integer if value is a integer expression', () => {
87 const prop = extractProp('<div foo={1} />');
88
89 const expected = 1;
90 const actual = getLiteralPropValue(prop);
91
92 assert.equal(actual, expected);
93 });
94
95 it('should convert "true" to boolean type', () => {
96 const prop = extractProp('<div foo="true" />');
97
98 const expected = true;
99 const actual = getLiteralPropValue(prop);
100
101 assert.equal(actual, expected);
102 });
103
104 it('should convert "TrUE" to boolean type', () => {
105 const prop = extractProp('<div foo="TrUE" />');
106
107 const expected = true;
108 const actual = getLiteralPropValue(prop);
109
110 assert.equal(actual, expected);
111 });
112
113 it('should convert "false" to boolean type', () => {
114 const prop = extractProp('<div foo="false" />');
115
116 const expected = false;
117 const actual = getLiteralPropValue(prop);
118
119 assert.equal(actual, expected);
120 });
121
122 it('should convert "FaLsE" to boolean type', () => {
123 const prop = extractProp('<div foo="FaLsE" />');
124
125 const expected = false;
126 const actual = getLiteralPropValue(prop);
127
128 assert.equal(actual, expected);
129 });
130
131 it('should return String null when value is null', () => {
132 const prop = extractProp('<div foo={null} />');
133
134 const expected = 'null';
135 const actual = getLiteralPropValue(prop);
136
137 assert.equal(actual, expected);
138 });
139 });
140
141 describe('JSXElement', () => {
142 it('should return null', () => {
143 const prop = extractProp('<div foo={<bar />} />');
144
145 const expected = null;
146 const actual = getLiteralPropValue(prop);
147
148 assert.equal(actual, expected);
149 });
150 });
151
152 describe('Identifier', () => {
153 it('should return null', () => {
154 const prop = extractProp('<div foo={bar} />');
155
156 const expected = null;
157 const actual = getLiteralPropValue(prop);
158
159 assert.equal(actual, expected);
160 });
161
162 it('should return undefined when identifier is literally `undefined`', () => {
163 const prop = extractProp('<div foo={undefined} />');
164
165 const expected = undefined;
166 const actual = getLiteralPropValue(prop);
167
168 assert.equal(actual, expected);
169 });
170 });
171
172 describeIfNotBabylon('Chain Expression', () => {
173 it('should return null', () => {
174 const prop = extractProp('<div foo={abc?.def} />');
175
176 const expected = null;
177 const actual = getLiteralPropValue(prop);
178
179 assert.equal(actual, expected);
180 });
181 });
182
183 describe('Template literal', () => {
184 it('should return template literal with vars wrapped in curly braces', () => {
185 const prop = extractProp('<div foo={`bar ${baz}`} />');
186
187 const expected = 'bar {baz}';
188 const actual = getLiteralPropValue(prop);
189
190 assert.equal(actual, expected);
191 });
192
193 it('should return string "undefined" for expressions that evaluate to undefined', () => {
194 const prop = extractProp('<div foo={`bar ${undefined}`} />');
195
196 const expected = 'bar undefined';
197 const actual = getLiteralPropValue(prop);
198
199 assert.equal(actual, expected);
200 });
201 });
202
203 describe('Tagged Template literal', () => {
204 it('should return template literal with vars wrapped in curly braces', () => {
205 const prop = extractProp('<div foo={noop`bar ${baz}`} />');
206
207 const expected = 'bar {baz}';
208 const actual = getLiteralPropValue(prop);
209
210 assert.equal(actual, expected);
211 });
212
213 it('should return string "undefined" for expressions that evaluate to undefined', () => {
214 const prop = extractProp('<div foo={noop`bar ${undefined}`} />');
215
216 const expected = 'bar undefined';
217 const actual = getLiteralPropValue(prop);
218
219 assert.equal(actual, expected);
220 });
221 });
222
223 describe('Arrow function expression', () => {
224 it('should return null', () => {
225 const prop = extractProp('<div foo={ () => { return "bar"; }} />');
226
227 const expected = null;
228 const actual = getLiteralPropValue(prop);
229
230 assert.equal(actual, expected);
231 });
232 });
233
234 describe('Function expression', () => {
235 it('should return null', () => {
236 const prop = extractProp('<div foo={ function() { return "bar"; } } />');
237
238 const expected = null;
239 const actual = getLiteralPropValue(prop);
240
241 assert.equal(actual, expected);
242 });
243 });
244
245 describe('Logical expression', () => {
246 it('should return null for && operator', () => {
247 const prop = extractProp('<div foo={bar && baz} />');
248
249 const expected = null;
250 const actual = getLiteralPropValue(prop);
251
252 assert.equal(actual, expected);
253 });
254
255 it('should return null for || operator', () => {
256 const prop = extractProp('<div foo={bar || baz} />');
257
258 const expected = null;
259 const actual = getLiteralPropValue(prop);
260
261 assert.equal(actual, expected);
262 });
263 });
264
265 describe('Member expression', () => {
266 it('should return null', () => {
267 const prop = extractProp('<div foo={bar.baz} />');
268
269 const expected = null;
270 const actual = getLiteralPropValue(prop);
271
272 assert.equal(actual, expected);
273 });
274 });
275
276 describe('Call expression', () => {
277 it('should return null', () => {
278 const prop = extractProp('<div foo={bar()} />');
279
280 const expected = null;
281 const actual = getLiteralPropValue(prop);
282
283 assert.equal(actual, expected);
284 });
285 });
286
287 describe('Unary expression', () => {
288 it('should correctly evaluate an expression that prefixes with -', () => {
289 const prop = extractProp('<div foo={-bar} />');
290
291 // -"bar" => NaN
292 const expected = true;
293 const actual = Number.isNaN(getLiteralPropValue(prop));
294
295 assert.equal(actual, expected);
296 });
297
298 it('should correctly evaluate an expression that prefixes with -', () => {
299 const prop = extractProp('<div foo={-42} />');
300
301 const expected = -42;
302 const actual = getLiteralPropValue(prop);
303
304 assert.equal(actual, expected);
305 });
306
307 it('should correctly evaluate an expression that prefixes with +', () => {
308 const prop = extractProp('<div foo={+bar} />');
309
310 // +"bar" => NaN
311 const expected = true;
312 const actual = Number.isNaN(getLiteralPropValue(prop));
313
314 assert.equal(actual, expected);
315 });
316
317 it('should correctly evaluate an expression that prefixes with +', () => {
318 const prop = extractProp('<div foo={+42} />');
319
320 const expected = 42;
321 const actual = getLiteralPropValue(prop);
322
323 assert.equal(actual, expected);
324 });
325
326 it('should correctly evaluate an expression that prefixes with !', () => {
327 const prop = extractProp('<div foo={!bar} />');
328
329 const expected = false; // !"bar" === false
330 const actual = getLiteralPropValue(prop);
331
332 assert.equal(actual, expected);
333 });
334
335 it('should correctly evaluate an expression that prefixes with ~', () => {
336 const prop = extractProp('<div foo={~bar} />');
337
338 const expected = -1; // ~"bar" === -1
339 const actual = getLiteralPropValue(prop);
340
341 assert.equal(actual, expected);
342 });
343
344 it('should return true when evaluating `delete foo`', () => {
345 const prop = extractProp('<div foo={delete x} />');
346
347 const expected = true;
348 const actual = getLiteralPropValue(prop);
349
350 assert.equal(actual, expected);
351 });
352
353 it('should return undefined when evaluating `void foo`', () => {
354 const prop = extractProp('<div foo={void x} />');
355
356 const expected = undefined;
357 const actual = getLiteralPropValue(prop);
358
359 assert.equal(actual, expected);
360 });
361
362 // TODO: We should fix this to check to see if we can evaluate it.
363 it('should return undefined when evaluating `typeof foo`', () => {
364 const prop = extractProp('<div foo={typeof x} />');
365
366 const expected = undefined;
367 const actual = getLiteralPropValue(prop);
368
369 assert.equal(actual, expected);
370 });
371 });
372
373 describe('Update expression', () => {
374 it('should correctly evaluate an expression that prefixes with ++', () => {
375 const prop = extractProp('<div foo={++bar} />');
376
377 // ++"bar" => NaN
378 const expected = true;
379 const actual = Number.isNaN(getLiteralPropValue(prop));
380
381 assert.equal(actual, expected);
382 });
383
384 it('should correctly evaluate an expression that prefixes with --', () => {
385 const prop = extractProp('<div foo={--bar} />');
386
387 // --"bar" => NaN
388 const expected = true;
389 const actual = Number.isNaN(getLiteralPropValue(prop));
390
391 assert.equal(actual, expected);
392 });
393
394 it('should correctly evaluate an expression that suffixes with ++', () => {
395 const prop = extractProp('<div foo={bar++} />');
396
397 // "bar"++ => NaN
398 const expected = true;
399 const actual = Number.isNaN(getLiteralPropValue(prop));
400
401 assert.equal(actual, expected);
402 });
403
404 it('should correctly evaluate an expression that suffixes with --', () => {
405 const prop = extractProp('<div foo={bar--} />');
406
407 // "bar"-- => NaN
408 const expected = true;
409 const actual = Number.isNaN(getLiteralPropValue(prop));
410
411 assert.equal(actual, expected);
412 });
413 });
414
415 describe('This expression', () => {
416 it('should return null', () => {
417 const prop = extractProp('<div foo={this} />');
418
419 const expected = null;
420 const actual = getLiteralPropValue(prop);
421
422 assert.equal(actual, expected);
423 });
424 });
425
426 describe('Conditional expression', () => {
427 it('should return null', () => {
428 const prop = extractProp('<div foo={bar ? baz : bam} />');
429
430 const expected = null;
431 const actual = getLiteralPropValue(prop);
432
433 assert.equal(actual, expected);
434 });
435 });
436
437 describe('Binary expression', () => {
438 it('should return null', () => {
439 const prop = extractProp('<div foo={1 == "1"} />');
440
441 const expected = null;
442 const actual = getLiteralPropValue(prop);
443
444 assert.equal(actual, expected);
445 });
446 });
447
448 describe('Object expression', () => {
449 it('should return null', () => {
450 const prop = extractProp('<div foo={ { bar: "baz" } } />');
451
452 const expected = null;
453 const actual = getLiteralPropValue(prop);
454
455 assert.deepEqual(actual, expected);
456 });
457 });
458
459 describe('New expression', () => {
460 it('should return null', () => {
461 const prop = extractProp('<div foo={new Bar()} />');
462
463 const expected = null;
464 const actual = getLiteralPropValue(prop);
465
466 assert.deepEqual(actual, expected);
467 });
468 });
469
470 describe('Array expression', () => {
471 it('should evaluate to correct representation of the the array in props', () => {
472 const prop = extractProp('<div foo={["bar", 42, null]} />');
473
474 const expected = ['bar', 42];
475 const actual = getLiteralPropValue(prop);
476
477 assert.deepEqual(actual, expected);
478 });
479 });
480
481 it('should return an empty array provided an empty array in props', () => {
482 const prop = extractProp('<div foo={[]} />');
483
484 const expected = [];
485 const actual = getLiteralPropValue(prop);
486
487 assert.deepEqual(actual, expected);
488 });
489
490 describe('Bind expression', () => {
491 it('should return null', () => {
492 const prop = extractProp('<div foo={::this.handleClick} />');
493
494 const expected = null;
495 const actual = getLiteralPropValue(prop);
496
497 assert.deepEqual(actual, expected);
498 });
499 });
500
501 describeIfNotBabylon('import.meta', () => {
502 beforeEach(() => {
503 setIsESM();
504 });
505
506 it('should return null', () => {
507 const prop = extractProp('<div foo={import.meta.env.whyIsThisNotOnProcess} />');
508
509 const expected = null;
510 const actual = getLiteralPropValue(prop);
511
512 assert.deepEqual(actual, expected);
513 });
514 });
515
516 describeIfNotBabylon('Typescript', () => {
517 beforeEach(() => {
518 changePlugins((pls) => [...pls, 'typescript']);
519 });
520
521 it('should return string representation of variable identifier wrapped in a Typescript non-null assertion', () => {
522 const prop = extractProp('<div foo={bar!} />');
523
524 const expected = null;
525 const actual = getLiteralPropValue(prop);
526
527 assert.equal(actual, expected);
528 });
529
530 it('should return string representation of variable identifier wrapped in a deep Typescript non-null assertion', () => {
531 const prop = extractProp('<div foo={(bar!)!} />');
532
533 const expected = null;
534 const actual = getLiteralPropValue(prop);
535
536 assert.equal(actual, expected);
537 });
538
539 it('should return string representation of variable identifier wrapped in a Typescript type coercion', () => {
540 changePlugins((pls) => [...pls, 'typescript']);
541 const prop = extractProp('<div foo={bar as any} />');
542
543 const expected = null;
544 const actual = getLiteralPropValue(prop);
545
546 assert.equal(actual, expected);
547 });
548
549 it('should work with a this.props value', () => {
550 const prop = extractProp('<a href={this.props.href!}>Download</a>', 'href');
551 const expected = null;
552 const actual = getLiteralPropValue(prop);
553 assert.equal(actual, expected);
554 });
555 });
556});
Note: See TracBrowser for help on using the repository browser.