@handlewithcare/react-prosemirror 2.8.0-tiptap.10 → 2.8.0-tiptap.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (60) hide show
  1. package/dist/cjs/tiptap/tiptapNodeView.js +26 -13
  2. package/dist/esm/tiptap/tiptapNodeView.js +27 -14
  3. package/dist/tsconfig.tsbuildinfo +1 -1
  4. package/dist/types/constants.d.ts +1 -1
  5. package/dist/types/props.d.ts +26 -26
  6. package/package.json +1 -1
  7. package/dist/esm/@jest/pattern/src/__tests__/TestPathPatterns.test.js +0 -241
  8. package/dist/esm/@swc/cli/lib/swc/__tests__/compile.test.js +0 -19
  9. package/dist/esm/@swc/cli/lib/swc/__tests__/options.test.js +0 -315
  10. package/dist/esm/@swc/cli/lib/swc/__tests__/sources.test.js +0 -161
  11. package/dist/esm/@uiw/react-codemirror/src/__tests__/index.test.js +0 -111
  12. package/dist/esm/cheerio/src/__tests__/deprecated.spec.js +0 -249
  13. package/dist/esm/cheerio/src/__tests__/xml.spec.js +0 -55
  14. package/dist/esm/components/__tests__/ProseMirror.composition.test.js +0 -395
  15. package/dist/esm/components/__tests__/ProseMirror.domchange.test.js +0 -266
  16. package/dist/esm/components/__tests__/ProseMirror.draw-decoration.test.js +0 -968
  17. package/dist/esm/components/__tests__/ProseMirror.draw.test.js +0 -335
  18. package/dist/esm/components/__tests__/ProseMirror.mobile.test.js +0 -54
  19. package/dist/esm/components/__tests__/ProseMirror.node-view.test.js +0 -553
  20. package/dist/esm/components/__tests__/ProseMirror.selection.test.js +0 -440
  21. package/dist/esm/components/__tests__/ProseMirror.test.js +0 -324
  22. package/dist/esm/contexts/__tests__/DeferredLayoutEffects.test.js +0 -98
  23. package/dist/esm/hooks/__tests__/useEditorEffect.test.js +0 -134
  24. package/dist/esm/jsx-ast-utils/__tests__/helper.js +0 -83
  25. package/dist/esm/jsx-ast-utils/__tests__/src/elementType-test.js +0 -76
  26. package/dist/esm/jsx-ast-utils/__tests__/src/eventHandlers-test.js +0 -98
  27. package/dist/esm/jsx-ast-utils/__tests__/src/getProp-parser-test.js +0 -188
  28. package/dist/esm/jsx-ast-utils/__tests__/src/getProp-test.js +0 -119
  29. package/dist/esm/jsx-ast-utils/__tests__/src/getPropLiteralValue-babelparser-test.js +0 -393
  30. package/dist/esm/jsx-ast-utils/__tests__/src/getPropLiteralValue-flowparser-test.js +0 -379
  31. package/dist/esm/jsx-ast-utils/__tests__/src/getPropValue-babelparser-test.js +0 -925
  32. package/dist/esm/jsx-ast-utils/__tests__/src/getPropValue-flowparser-test.js +0 -700
  33. package/dist/esm/jsx-ast-utils/__tests__/src/hasProp-test.js +0 -350
  34. package/dist/esm/jsx-ast-utils/__tests__/src/index-test.js +0 -24
  35. package/dist/esm/jsx-ast-utils/__tests__/src/propName-test.js +0 -37
  36. package/dist/esm/plugins/__tests__/reactKeys.test.js +0 -77
  37. package/dist/esm/tsconfig-paths/lib/__tests__/config-loader.test.d.js +0 -1
  38. package/dist/esm/tsconfig-paths/lib/__tests__/config-loader.test.js +0 -97
  39. package/dist/esm/tsconfig-paths/lib/__tests__/data/match-path-data.d.js +0 -1
  40. package/dist/esm/tsconfig-paths/lib/__tests__/data/match-path-data.js +0 -319
  41. package/dist/esm/tsconfig-paths/lib/__tests__/filesystem.test.d.js +0 -1
  42. package/dist/esm/tsconfig-paths/lib/__tests__/filesystem.test.js +0 -62
  43. package/dist/esm/tsconfig-paths/lib/__tests__/mapping-entry.test.d.js +0 -1
  44. package/dist/esm/tsconfig-paths/lib/__tests__/mapping-entry.test.js +0 -80
  45. package/dist/esm/tsconfig-paths/lib/__tests__/match-path-async.test.d.js +0 -1
  46. package/dist/esm/tsconfig-paths/lib/__tests__/match-path-async.test.js +0 -22
  47. package/dist/esm/tsconfig-paths/lib/__tests__/match-path-sync.test.d.js +0 -1
  48. package/dist/esm/tsconfig-paths/lib/__tests__/match-path-sync.test.js +0 -20
  49. package/dist/esm/tsconfig-paths/lib/__tests__/try-path.test.d.js +0 -1
  50. package/dist/esm/tsconfig-paths/lib/__tests__/try-path.test.js +0 -219
  51. package/dist/esm/tsconfig-paths/lib/__tests__/tsconfig-loader.test.d.js +0 -1
  52. package/dist/esm/tsconfig-paths/lib/__tests__/tsconfig-loader.test.js +0 -323
  53. package/dist/esm/tsconfig-paths/src/__tests__/config-loader.test.js +0 -89
  54. package/dist/esm/tsconfig-paths/src/__tests__/data/match-path-data.js +0 -314
  55. package/dist/esm/tsconfig-paths/src/__tests__/filesystem.test.js +0 -58
  56. package/dist/esm/tsconfig-paths/src/__tests__/mapping-entry.test.js +0 -76
  57. package/dist/esm/tsconfig-paths/src/__tests__/match-path-async.test.js +0 -12
  58. package/dist/esm/tsconfig-paths/src/__tests__/match-path-sync.test.js +0 -10
  59. package/dist/esm/tsconfig-paths/src/__tests__/try-path.test.js +0 -214
  60. package/dist/esm/tsconfig-paths/src/__tests__/tsconfig-loader.test.js +0 -296
@@ -1,925 +0,0 @@
1
- /* eslint-env mocha */ /* eslint no-template-curly-in-string: 0 */ import assert from 'assert';
2
- import { extractProp, changePlugins, fallbackToBabylon, describeIfNotBabylon, setParserName } from '../helper';
3
- import getPropValue from '../../src/getPropValue';
4
- describe('getPropValue', ()=>{
5
- beforeEach(()=>{
6
- setParserName('babel');
7
- });
8
- it('should export a function', ()=>{
9
- const expected = 'function';
10
- const actual = typeof getPropValue;
11
- assert.equal(actual, expected);
12
- });
13
- it('should return undefined when not provided with a JSXAttribute', ()=>{
14
- const expected = undefined;
15
- const actual = getPropValue(1);
16
- assert.equal(actual, expected);
17
- });
18
- it('should not throw error when trying to get value from unknown node type', ()=>{
19
- const prop = {
20
- type: 'JSXAttribute',
21
- value: {
22
- type: 'JSXExpressionContainer'
23
- }
24
- };
25
- let counter = 0;
26
- // eslint-disable-next-line no-console
27
- const errorOrig = console.error;
28
- // eslint-disable-next-line no-console
29
- console.error = ()=>{
30
- counter += 1;
31
- };
32
- let value;
33
- assert.doesNotThrow(()=>{
34
- value = getPropValue(prop);
35
- }, Error);
36
- assert.equal(null, value);
37
- assert.equal(counter, 1);
38
- // eslint-disable-next-line no-console
39
- console.error = errorOrig;
40
- });
41
- describe('Null', ()=>{
42
- it('should return true when no value is given', ()=>{
43
- const prop = extractProp('<div foo />');
44
- const expected = true;
45
- const actual = getPropValue(prop);
46
- assert.equal(actual, expected);
47
- });
48
- });
49
- describe('Literal', ()=>{
50
- it('should return correct string if value is a string', ()=>{
51
- const prop = extractProp('<div foo="bar" />');
52
- const expected = 'bar';
53
- const actual = getPropValue(prop);
54
- assert.equal(actual, expected);
55
- });
56
- it('should return correct string if value is a string expression', ()=>{
57
- const prop = extractProp('<div foo={"bar"} />');
58
- const expected = 'bar';
59
- const actual = getPropValue(prop);
60
- assert.equal(actual, expected);
61
- });
62
- it('should return correct integer if value is a integer expression', ()=>{
63
- const prop = extractProp('<div foo={1} />');
64
- const expected = 1;
65
- const actual = getPropValue(prop);
66
- assert.equal(actual, expected);
67
- });
68
- it('should convert "true" to boolean type', ()=>{
69
- const prop = extractProp('<div foo="true" />');
70
- const expected = true;
71
- const actual = getPropValue(prop);
72
- assert.equal(actual, expected);
73
- });
74
- it('should convert "false" to boolean type', ()=>{
75
- const prop = extractProp('<div foo="false" />');
76
- const expected = false;
77
- const actual = getPropValue(prop);
78
- assert.equal(actual, expected);
79
- });
80
- });
81
- describe('JSXElement', ()=>{
82
- it('should return correct representation of JSX element as a string', ()=>{
83
- const prop = extractProp('<div foo={<bar />} />');
84
- const expected = '<bar />';
85
- const actual = getPropValue(prop);
86
- assert.equal(actual, expected);
87
- });
88
- it('should return correct representation of JSX element with children as a string', ()=>{
89
- const prop = extractProp('<div foo={<bar><baz />foo</bar>} />');
90
- const expected = '<bar><baz />foo</bar>';
91
- const actual = getPropValue(prop);
92
- assert.equal(actual, expected);
93
- });
94
- });
95
- (fallbackToBabylon ? describe.skip : describe)('JSXFragment', ()=>{
96
- it('should return correct representation of JSX fragment as a string', ()=>{
97
- const prop = extractProp('<div foo={<></>} />');
98
- const expected = '<></>';
99
- const actual = getPropValue(prop);
100
- assert.equal(actual, expected);
101
- });
102
- it('should return correct representation of JSX fragment with children as a string', ()=>{
103
- const prop = extractProp('<div foo={<><div />abc</>} />');
104
- const expected = '<><div />abc</>';
105
- const actual = getPropValue(prop);
106
- assert.equal(actual, expected);
107
- });
108
- });
109
- describe('Identifier', ()=>{
110
- it('should return string representation of variable identifier', ()=>{
111
- const prop = extractProp('<div foo={bar} />');
112
- const expected = 'bar';
113
- const actual = getPropValue(prop);
114
- assert.equal(actual, expected);
115
- });
116
- it('should return undefined when identifier is literally `undefined`', ()=>{
117
- const prop = extractProp('<div foo={undefined} />');
118
- const expected = undefined;
119
- const actual = getPropValue(prop);
120
- assert.equal(actual, expected);
121
- });
122
- it('should return String object when using a reserved JavaScript object', ()=>{
123
- const prop = extractProp('<div foo={String} />');
124
- const expected = String;
125
- const actual = getPropValue(prop);
126
- assert.equal(actual, expected);
127
- });
128
- it('should return Array object when using a reserved JavaScript object', ()=>{
129
- const prop = extractProp('<div foo={Array} />');
130
- const expected = Array;
131
- const actual = getPropValue(prop);
132
- assert.equal(actual, expected);
133
- });
134
- it('should return Date object when using a reserved JavaScript object', ()=>{
135
- const prop = extractProp('<div foo={Date} />');
136
- const expected = Date;
137
- const actual = getPropValue(prop);
138
- assert.equal(actual, expected);
139
- });
140
- it('should return Infinity object when using a reserved JavaScript object', ()=>{
141
- const prop = extractProp('<div foo={Infinity} />');
142
- const expected = Infinity;
143
- const actual = getPropValue(prop);
144
- assert.equal(actual, expected);
145
- });
146
- it('should return Math object when using a reserved JavaScript object', ()=>{
147
- const prop = extractProp('<div foo={Math} />');
148
- const expected = Math;
149
- const actual = getPropValue(prop);
150
- assert.equal(actual, expected);
151
- });
152
- it('should return Number object when using a reserved JavaScript object', ()=>{
153
- const prop = extractProp('<div foo={Number} />');
154
- const expected = Number;
155
- const actual = getPropValue(prop);
156
- assert.equal(actual, expected);
157
- });
158
- it('should return Object object when using a reserved JavaScript object', ()=>{
159
- const prop = extractProp('<div foo={Object} />');
160
- const expected = Object;
161
- const actual = getPropValue(prop);
162
- assert.equal(actual, expected);
163
- });
164
- });
165
- describe('Template literal', ()=>{
166
- it('should return template literal with vars wrapped in curly braces', ()=>{
167
- const prop = extractProp('<div foo={`bar ${baz}`} />');
168
- const expected = 'bar {baz}';
169
- const actual = getPropValue(prop);
170
- assert.equal(actual, expected);
171
- });
172
- it('should return string "undefined" for expressions that evaluate to undefined', ()=>{
173
- const prop = extractProp('<div foo={`bar ${undefined}`} />');
174
- const expected = 'bar undefined';
175
- const actual = getPropValue(prop);
176
- assert.equal(actual, expected);
177
- });
178
- it('should return template literal with expression type wrapped in curly braces', ()=>{
179
- const prop = extractProp('<div foo={`bar ${baz()}`} />');
180
- const expected = 'bar {CallExpression}';
181
- const actual = getPropValue(prop);
182
- assert.equal(actual, expected);
183
- });
184
- it('should ignore non-expressions in the template literal', ()=>{
185
- const prop = extractProp('<div foo={`bar ${<baz />}`} />');
186
- const expected = 'bar ';
187
- const actual = getPropValue(prop);
188
- assert.equal(actual, expected);
189
- });
190
- });
191
- describe('Tagged Template literal', ()=>{
192
- it('should return template literal with vars wrapped in curly braces', ()=>{
193
- const prop = extractProp('<div foo={noop`bar ${baz}`} />');
194
- const expected = 'bar {baz}';
195
- const actual = getPropValue(prop);
196
- assert.equal(actual, expected);
197
- });
198
- it('should return string "undefined" for expressions that evaluate to undefined', ()=>{
199
- const prop = extractProp('<div foo={noop`bar ${undefined}`} />');
200
- const expected = 'bar undefined';
201
- const actual = getPropValue(prop);
202
- assert.equal(actual, expected);
203
- });
204
- it('should return template literal with expression type wrapped in curly braces', ()=>{
205
- const prop = extractProp('<div foo={noop`bar ${baz()}`} />');
206
- const expected = 'bar {CallExpression}';
207
- const actual = getPropValue(prop);
208
- assert.equal(actual, expected);
209
- });
210
- it('should ignore non-expressions in the template literal', ()=>{
211
- const prop = extractProp('<div foo={noop`bar ${<baz />}`} />');
212
- const expected = 'bar ';
213
- const actual = getPropValue(prop);
214
- assert.equal(actual, expected);
215
- });
216
- });
217
- describe('Arrow function expression', ()=>{
218
- it('should return a function', ()=>{
219
- const prop = extractProp('<div foo={ () => { return "bar"; }} />');
220
- const expected = 'function';
221
- const actual = getPropValue(prop);
222
- assert.equal(expected, typeof actual);
223
- // For code coverage ¯\_(ツ)_/¯
224
- actual();
225
- });
226
- it('should handle ArrowFunctionExpression as conditional consequent', ()=>{
227
- const prop = extractProp('<div foo={ (true) ? () => null : () => ({})} />');
228
- const expected = 'function';
229
- const actual = getPropValue(prop);
230
- assert.equal(expected, typeof actual);
231
- // For code coverage ¯\_(ツ)_/¯
232
- actual();
233
- });
234
- });
235
- describe('Function expression', ()=>{
236
- it('should return a function', ()=>{
237
- const prop = extractProp('<div foo={ function() { return "bar"; } } />');
238
- const expected = 'function';
239
- const actual = getPropValue(prop);
240
- assert.equal(expected, typeof actual);
241
- // For code coverage ¯\_(ツ)_/¯
242
- actual();
243
- });
244
- });
245
- describe('Logical expression', ()=>{
246
- it('should correctly infer result of && logical expression based on derived values', ()=>{
247
- const prop = extractProp('<div foo={bar && baz} />');
248
- const expected = 'baz';
249
- const actual = getPropValue(prop);
250
- assert.equal(actual, expected);
251
- });
252
- it('should return undefined when evaluating `undefined && undefined` ', ()=>{
253
- const prop = extractProp('<div foo={undefined && undefined} />');
254
- const expected = undefined;
255
- const actual = getPropValue(prop);
256
- assert.equal(actual, expected);
257
- });
258
- it('should correctly infer result of || logical expression based on derived values', ()=>{
259
- const prop = extractProp('<div foo={bar || baz} />');
260
- const expected = 'bar';
261
- const actual = getPropValue(prop);
262
- assert.equal(actual, expected);
263
- });
264
- it('should correctly infer result of || logical expression based on derived values', ()=>{
265
- const prop = extractProp('<div foo={undefined || baz} />');
266
- const expected = 'baz';
267
- const actual = getPropValue(prop);
268
- assert.equal(actual, expected);
269
- });
270
- it('should return undefined when evaluating `undefined || undefined` ', ()=>{
271
- const prop = extractProp('<div foo={undefined || undefined} />');
272
- const expected = undefined;
273
- const actual = getPropValue(prop);
274
- assert.equal(actual, expected);
275
- });
276
- it('should correctly infer result of ?? logical expression based on derived values', ()=>{
277
- const runTest = ()=>{
278
- const prop = extractProp('<div foo={bar ?? baz} />');
279
- const expected = 'bar';
280
- const actual = getPropValue(prop);
281
- assert.equal(actual, expected);
282
- };
283
- if (fallbackToBabylon) {
284
- // eslint-disable-next-line no-undef
285
- expect(runTest).toThrow();
286
- } else {
287
- runTest();
288
- }
289
- });
290
- it('should correctly infer result of ?? logical expression based on derived values', ()=>{
291
- const runTest = ()=>{
292
- const prop = extractProp('<div foo={undefined ?? baz} />');
293
- const expected = 'baz';
294
- const actual = getPropValue(prop);
295
- assert.equal(actual, expected);
296
- };
297
- if (fallbackToBabylon) {
298
- // eslint-disable-next-line no-undef
299
- expect(runTest).toThrow();
300
- } else {
301
- runTest();
302
- }
303
- });
304
- it('should return undefined when evaluating `undefined ?? undefined` ', ()=>{
305
- const runTest = ()=>{
306
- const prop = extractProp('<div foo={undefined ?? undefined} />');
307
- const expected = undefined;
308
- const actual = getPropValue(prop);
309
- assert.equal(actual, expected);
310
- };
311
- if (fallbackToBabylon) {
312
- // eslint-disable-next-line no-undef
313
- expect(runTest).toThrow();
314
- } else {
315
- runTest();
316
- }
317
- });
318
- it('should return undefined when evaluating `null ?? undefined` ', ()=>{
319
- const runTest = ()=>{
320
- const prop = extractProp('<div foo={null ?? undefined} />');
321
- const expected = undefined;
322
- const actual = getPropValue(prop);
323
- assert.equal(actual, expected);
324
- };
325
- if (fallbackToBabylon) {
326
- // eslint-disable-next-line no-undef
327
- expect(runTest).toThrow();
328
- } else {
329
- runTest();
330
- }
331
- });
332
- it('should return undefined when evaluating `undefined ?? null` ', ()=>{
333
- const runTest = ()=>{
334
- const prop = extractProp('<div foo={undefined ?? null} />');
335
- const expected = null;
336
- const actual = getPropValue(prop);
337
- assert.equal(actual, expected);
338
- };
339
- if (fallbackToBabylon) {
340
- // eslint-disable-next-line no-undef
341
- expect(runTest).toThrow();
342
- } else {
343
- runTest();
344
- }
345
- });
346
- it('should return null when evaluating `null ?? null` ', ()=>{
347
- const runTest = ()=>{
348
- const prop = extractProp('<div foo={null ?? null} />');
349
- const expected = null;
350
- const actual = getPropValue(prop);
351
- assert.equal(actual, expected);
352
- };
353
- if (fallbackToBabylon) {
354
- // eslint-disable-next-line no-undef
355
- expect(runTest).toThrow();
356
- } else {
357
- runTest();
358
- }
359
- });
360
- });
361
- describe('Member expression', ()=>{
362
- it('should return string representation of form `object.property`', ()=>{
363
- const prop = extractProp('<div foo={bar.baz} />');
364
- const expected = 'bar.baz';
365
- const actual = getPropValue(prop);
366
- assert.equal(actual, expected);
367
- });
368
- it('should evaluate to a correct representation of member expression with a nullable member', ()=>{
369
- const runTest = ()=>{
370
- const prop = extractProp('<div foo={bar?.baz} />');
371
- const expected = 'bar?.baz';
372
- const actual = getPropValue(prop);
373
- assert.equal(actual, expected);
374
- };
375
- if (fallbackToBabylon) {
376
- // eslint-disable-next-line no-undef
377
- expect(runTest).toThrow();
378
- } else {
379
- runTest();
380
- }
381
- });
382
- it('should evaluate to a correct representation of optional call expression', ()=>{
383
- const runTest = ()=>{
384
- const prop = extractProp('<div foo={bar.baz?.(quux)} />');
385
- const expected = 'bar.baz?.(quux)';
386
- const actual = getPropValue(prop);
387
- assert.equal(actual, expected);
388
- };
389
- if (fallbackToBabylon) {
390
- // eslint-disable-next-line no-undef
391
- expect(runTest).toThrow();
392
- } else {
393
- runTest();
394
- }
395
- });
396
- });
397
- describe('Call expression', ()=>{
398
- it('should return string representation of callee', ()=>{
399
- const prop = extractProp('<div foo={bar()} />');
400
- const expected = 'bar()';
401
- const actual = getPropValue(prop);
402
- assert.equal(actual, expected);
403
- });
404
- it('should return string representation of callee', ()=>{
405
- const prop = extractProp('<div foo={bar.call()} />');
406
- const expected = 'bar.call()';
407
- const actual = getPropValue(prop);
408
- assert.equal(actual, expected);
409
- });
410
- });
411
- describe('Unary expression', ()=>{
412
- it('should correctly evaluate an expression that prefixes with -', ()=>{
413
- const prop = extractProp('<div foo={-bar} />');
414
- // -"bar" => NaN
415
- const expected = true;
416
- const actual = Number.isNaN(getPropValue(prop));
417
- assert.equal(actual, expected);
418
- });
419
- it('should correctly evaluate an expression that prefixes with -', ()=>{
420
- const prop = extractProp('<div foo={-42} />');
421
- const expected = -42;
422
- const actual = getPropValue(prop);
423
- assert.equal(actual, expected);
424
- });
425
- it('should correctly evaluate an expression that prefixes with +', ()=>{
426
- const prop = extractProp('<div foo={+bar} />');
427
- // +"bar" => NaN
428
- const expected = true;
429
- const actual = Number.isNaN(getPropValue(prop));
430
- assert.equal(actual, expected);
431
- });
432
- it('should correctly evaluate an expression that prefixes with +', ()=>{
433
- const prop = extractProp('<div foo={+42} />');
434
- const expected = 42;
435
- const actual = getPropValue(prop);
436
- assert.equal(actual, expected);
437
- });
438
- it('should correctly evaluate an expression that prefixes with !', ()=>{
439
- const prop = extractProp('<div foo={!bar} />');
440
- const expected = false; // !"bar" === false
441
- const actual = getPropValue(prop);
442
- assert.equal(actual, expected);
443
- });
444
- it('should correctly evaluate an expression that prefixes with ~', ()=>{
445
- const prop = extractProp('<div foo={~bar} />');
446
- const expected = -1; // ~"bar" === -1
447
- const actual = getPropValue(prop);
448
- assert.equal(actual, expected);
449
- });
450
- it('should return true when evaluating `delete foo`', ()=>{
451
- const prop = extractProp('<div foo={delete x} />');
452
- const expected = true;
453
- const actual = getPropValue(prop);
454
- assert.equal(actual, expected);
455
- });
456
- it('should return undefined when evaluating `void foo`', ()=>{
457
- const prop = extractProp('<div foo={void x} />');
458
- const expected = undefined;
459
- const actual = getPropValue(prop);
460
- assert.equal(actual, expected);
461
- });
462
- // TODO: We should fix this to check to see if we can evaluate it.
463
- it('should return undefined when evaluating `typeof foo`', ()=>{
464
- const prop = extractProp('<div foo={typeof x} />');
465
- const expected = undefined;
466
- const actual = getPropValue(prop);
467
- assert.equal(actual, expected);
468
- });
469
- });
470
- describe('Update expression', ()=>{
471
- it('should correctly evaluate an expression that prefixes with ++', ()=>{
472
- const prop = extractProp('<div foo={++bar} />');
473
- // ++"bar" => NaN
474
- const expected = true;
475
- const actual = Number.isNaN(getPropValue(prop));
476
- assert.equal(actual, expected);
477
- });
478
- it('should correctly evaluate an expression that prefixes with --', ()=>{
479
- const prop = extractProp('<div foo={--bar} />');
480
- const expected = true;
481
- const actual = Number.isNaN(getPropValue(prop));
482
- assert.equal(actual, expected);
483
- });
484
- it('should correctly evaluate an expression that suffixes with ++', ()=>{
485
- const prop = extractProp('<div foo={bar++} />');
486
- // "bar"++ => NaN
487
- const expected = true;
488
- const actual = Number.isNaN(getPropValue(prop));
489
- assert.equal(actual, expected);
490
- });
491
- it('should correctly evaluate an expression that suffixes with --', ()=>{
492
- const prop = extractProp('<div foo={bar--} />');
493
- const expected = true;
494
- const actual = Number.isNaN(getPropValue(prop));
495
- assert.equal(actual, expected);
496
- });
497
- });
498
- describe('This expression', ()=>{
499
- it('should return string value `this`', ()=>{
500
- const prop = extractProp('<div foo={this} />');
501
- const expected = 'this';
502
- const actual = getPropValue(prop);
503
- assert.equal(actual, expected);
504
- });
505
- });
506
- describe('Conditional expression', ()=>{
507
- it('should evaluate the conditional based on the derived values correctly', ()=>{
508
- const prop = extractProp('<div foo={bar ? baz : bam} />');
509
- const expected = 'baz';
510
- const actual = getPropValue(prop);
511
- assert.equal(actual, expected);
512
- });
513
- it('should evaluate the conditional based on the derived values correctly', ()=>{
514
- const prop = extractProp('<div foo={undefined ? baz : bam} />');
515
- const expected = 'bam';
516
- const actual = getPropValue(prop);
517
- assert.equal(actual, expected);
518
- });
519
- it('should evaluate the conditional based on the derived values correctly', ()=>{
520
- const prop = extractProp('<div foo={(1 > 2) ? baz : bam} />');
521
- const expected = 'bam';
522
- const actual = getPropValue(prop);
523
- assert.equal(actual, expected);
524
- });
525
- });
526
- describe('Binary expression', ()=>{
527
- it('should evaluate the `==` operator correctly', ()=>{
528
- const trueProp = extractProp('<div foo={1 == "1"} />');
529
- const falseProp = extractProp('<div foo={1 == bar} />');
530
- const trueVal = getPropValue(trueProp);
531
- const falseVal = getPropValue(falseProp);
532
- assert.equal(true, trueVal);
533
- assert.equal(false, falseVal);
534
- });
535
- it('should evaluate the `!=` operator correctly', ()=>{
536
- const trueProp = extractProp('<div foo={1 != "2"} />');
537
- const falseProp = extractProp('<div foo={1 != "1"} />');
538
- const trueVal = getPropValue(trueProp);
539
- const falseVal = getPropValue(falseProp);
540
- assert.equal(true, trueVal);
541
- assert.equal(false, falseVal);
542
- });
543
- it('should evaluate the `===` operator correctly', ()=>{
544
- const trueProp = extractProp('<div foo={1 === 1} />');
545
- const falseProp = extractProp('<div foo={1 === "1"} />');
546
- const trueVal = getPropValue(trueProp);
547
- const falseVal = getPropValue(falseProp);
548
- assert.equal(true, trueVal);
549
- assert.equal(false, falseVal);
550
- });
551
- it('should evaluate the `!==` operator correctly', ()=>{
552
- const trueProp = extractProp('<div foo={1 !== "1"} />');
553
- const falseProp = extractProp('<div foo={1 !== 1} />');
554
- const trueVal = getPropValue(trueProp);
555
- const falseVal = getPropValue(falseProp);
556
- assert.equal(true, trueVal);
557
- assert.equal(false, falseVal);
558
- });
559
- it('should evaluate the `<` operator correctly', ()=>{
560
- const trueProp = extractProp('<div foo={1 < 2} />');
561
- const falseProp = extractProp('<div foo={1 < 0} />');
562
- const trueVal = getPropValue(trueProp);
563
- const falseVal = getPropValue(falseProp);
564
- assert.equal(true, trueVal);
565
- assert.equal(false, falseVal);
566
- });
567
- it('should evaluate the `>` operator correctly', ()=>{
568
- const trueProp = extractProp('<div foo={1 > 0} />');
569
- const falseProp = extractProp('<div foo={1 > 2} />');
570
- const trueVal = getPropValue(trueProp);
571
- const falseVal = getPropValue(falseProp);
572
- assert.equal(true, trueVal);
573
- assert.equal(false, falseVal);
574
- });
575
- it('should evaluate the `<=` operator correctly', ()=>{
576
- const trueProp = extractProp('<div foo={1 <= 1} />');
577
- const falseProp = extractProp('<div foo={1 <= 0} />');
578
- const trueVal = getPropValue(trueProp);
579
- const falseVal = getPropValue(falseProp);
580
- assert.equal(true, trueVal);
581
- assert.equal(false, falseVal);
582
- });
583
- it('should evaluate the `>=` operator correctly', ()=>{
584
- const trueProp = extractProp('<div foo={1 >= 1} />');
585
- const falseProp = extractProp('<div foo={1 >= 2} />');
586
- const trueVal = getPropValue(trueProp);
587
- const falseVal = getPropValue(falseProp);
588
- assert.equal(true, trueVal);
589
- assert.equal(false, falseVal);
590
- });
591
- it('should evaluate the `<<` operator correctly', ()=>{
592
- const prop = extractProp('<div foo={1 << 2} />');
593
- const expected = 4;
594
- const actual = getPropValue(prop);
595
- assert.equal(actual, expected);
596
- });
597
- it('should evaluate the `>>` operator correctly', ()=>{
598
- const prop = extractProp('<div foo={1 >> 2} />');
599
- const expected = 0;
600
- const actual = getPropValue(prop);
601
- assert.equal(actual, expected);
602
- });
603
- it('should evaluate the `>>>` operator correctly', ()=>{
604
- const prop = extractProp('<div foo={2 >>> 1} />');
605
- const expected = 1;
606
- const actual = getPropValue(prop);
607
- assert.equal(actual, expected);
608
- });
609
- it('should evaluate the `+` operator correctly', ()=>{
610
- const prop = extractProp('<div foo={1 + 1} />');
611
- const expected = 2;
612
- const actual = getPropValue(prop);
613
- assert.equal(actual, expected);
614
- });
615
- it('should evaluate the `-` operator correctly', ()=>{
616
- const prop = extractProp('<div foo={1 - 1} />');
617
- const expected = 0;
618
- const actual = getPropValue(prop);
619
- assert.equal(actual, expected);
620
- });
621
- it('should evaluate the `*` operator correctly', ()=>{
622
- const prop = extractProp('<div foo={10 * 10} />');
623
- const expected = 100;
624
- const actual = getPropValue(prop);
625
- assert.equal(actual, expected);
626
- });
627
- it('should evaluate the `/` operator correctly', ()=>{
628
- const prop = extractProp('<div foo={10 / 2} />');
629
- const expected = 5;
630
- const actual = getPropValue(prop);
631
- assert.equal(actual, expected);
632
- });
633
- it('should evaluate the `%` operator correctly', ()=>{
634
- const prop = extractProp('<div foo={10 % 3} />');
635
- const expected = 1;
636
- const actual = getPropValue(prop);
637
- assert.equal(actual, expected);
638
- });
639
- it('should evaluate the `|` operator correctly', ()=>{
640
- const prop = extractProp('<div foo={10 | 1} />');
641
- const expected = 11;
642
- const actual = getPropValue(prop);
643
- assert.equal(actual, expected);
644
- });
645
- it('should evaluate the `^` operator correctly', ()=>{
646
- const prop = extractProp('<div foo={10 ^ 1} />');
647
- const expected = 11;
648
- const actual = getPropValue(prop);
649
- assert.equal(actual, expected);
650
- });
651
- it('should evaluate the `&` operator correctly', ()=>{
652
- const prop = extractProp('<div foo={10 & 1} />');
653
- const expected = 0;
654
- const actual = getPropValue(prop);
655
- assert.equal(actual, expected);
656
- });
657
- it('should evaluate the `in` operator correctly', ()=>{
658
- const prop = extractProp('<div foo={foo in bar} />');
659
- const expected = false;
660
- const actual = getPropValue(prop);
661
- assert.equal(actual, expected);
662
- });
663
- it('should evaluate the `instanceof` operator correctly', ()=>{
664
- const prop = extractProp('<div foo={{} instanceof Object} />');
665
- const expected = true;
666
- const actual = getPropValue(prop);
667
- assert.equal(actual, expected);
668
- });
669
- it('should evaluate the `instanceof` operator when right side is not a function', ()=>{
670
- const prop = extractProp('<div foo={"bar" instanceof Baz} />');
671
- const expected = false;
672
- const actual = getPropValue(prop);
673
- assert.equal(actual, expected);
674
- });
675
- });
676
- describe('Object expression', ()=>{
677
- it('should evaluate to a correct representation of the object in props', ()=>{
678
- const prop = extractProp('<div foo={ { bar: "baz" } } />');
679
- const expected = {
680
- bar: 'baz'
681
- };
682
- const actual = getPropValue(prop);
683
- assert.deepEqual(actual, expected);
684
- });
685
- it('should evaluate to a correct representation of the object, ignore spread properties', ()=>{
686
- const prop = extractProp('<div foo={{bar: "baz", ...{baz: "bar", foo: {...{bar: "meh"}}}}} />');
687
- const expected = {
688
- bar: 'baz',
689
- baz: 'bar',
690
- foo: {
691
- bar: 'meh'
692
- }
693
- };
694
- const actual = getPropValue(prop);
695
- assert.deepEqual(actual, expected);
696
- });
697
- it('should evaluate to a correct representation of the object, ignore spread properties', ()=>{
698
- const prop = extractProp('<div foo={{ pathname: manageRoute, state: {...data}}} />');
699
- const expected = {
700
- pathname: 'manageRoute',
701
- state: {}
702
- };
703
- const actual = getPropValue(prop);
704
- assert.deepEqual(actual, expected);
705
- });
706
- });
707
- describe('New expression', ()=>{
708
- it('should return a new empty object', ()=>{
709
- const prop = extractProp('<div foo={new Bar()} />');
710
- const expected = {};
711
- const actual = getPropValue(prop);
712
- assert.deepEqual(actual, expected);
713
- });
714
- });
715
- describe('Array expression', ()=>{
716
- it('should evaluate to correct representation of the the array in props', ()=>{
717
- const prop = extractProp('<div foo={["bar", 42, , null]} />');
718
- const expected = [
719
- 'bar',
720
- 42,
721
- undefined,
722
- null
723
- ];
724
- const actual = getPropValue(prop);
725
- assert.deepEqual(actual, expected);
726
- });
727
- it('should evaluate to a correct representation of an array with spread elements', ()=>{
728
- const prop = extractProp('<div foo={[...this.props.params, bar]} />');
729
- const expected = [
730
- undefined,
731
- 'bar'
732
- ];
733
- const actual = getPropValue(prop);
734
- assert.deepEqual(actual, expected);
735
- });
736
- });
737
- it('should return an empty array provided an empty array in props', ()=>{
738
- const prop = extractProp('<div foo={[]} />');
739
- const expected = [];
740
- const actual = getPropValue(prop);
741
- assert.deepEqual(actual, expected);
742
- });
743
- describe('Bind expression', ()=>{
744
- it('should return string representation of bind function call when object is null', ()=>{
745
- const prop = extractProp('<div foo={::this.handleClick} />');
746
- const expected = 'this.handleClick.bind(this)';
747
- const actual = getPropValue(prop);
748
- assert.deepEqual(actual, expected);
749
- });
750
- it('should return string representation of bind function call when object is not null', ()=>{
751
- const prop = extractProp('<div foo={foo::bar} />');
752
- const expected = 'bar.bind(foo)';
753
- const actual = getPropValue(prop);
754
- assert.deepEqual(actual, expected);
755
- });
756
- it('should return string representation of bind function call when binding to object properties', ()=>{
757
- const prop = extractProp('<div foo={a.b::c} />');
758
- const otherProp = extractProp('<div foo={::a.b.c} />');
759
- const expected = 'a.b.c.bind(a.b)';
760
- const actual = getPropValue(prop);
761
- const otherExpected = 'a.b.c.bind(a.b)';
762
- const otherActual = getPropValue(otherProp);
763
- assert.deepEqual(actual, expected);
764
- assert.deepEqual(otherExpected, otherActual);
765
- });
766
- });
767
- describe('Type Cast Expression', ()=>{
768
- it('should throw a parsing error', ()=>{
769
- let counter = 0;
770
- // eslint-disable-next-line no-console
771
- const warnOrig = console.warn;
772
- // eslint-disable-next-line no-console
773
- console.warn = ()=>{
774
- counter += 1;
775
- };
776
- // eslint-disable-next-line no-undef
777
- expect(()=>{
778
- extractProp('<div foo={(this.handleClick: (event: MouseEvent) => void))} />');
779
- }).toThrow();
780
- assert.equal(counter, 1);
781
- // eslint-disable-next-line no-console
782
- console.warn = warnOrig;
783
- });
784
- });
785
- describe('AssignmentExpression', ()=>{
786
- it('should recognize and extract assignment', ()=>{
787
- const prop = extractProp('<div foo={foo = bar} />');
788
- const expected = 'foo = bar';
789
- const actual = getPropValue(prop);
790
- assert.deepEqual(actual, expected);
791
- });
792
- it('should recognize and extract combination assignments', ()=>{
793
- const prop = extractProp('<div foo={foo += bar} />');
794
- const expected = 'foo += bar';
795
- const actual = getPropValue(prop);
796
- assert.deepEqual(actual, expected);
797
- });
798
- });
799
- describeIfNotBabylon('Typescript', ()=>{
800
- beforeEach(()=>{
801
- changePlugins((pls)=>[
802
- ...pls,
803
- 'typescript'
804
- ]);
805
- });
806
- it('should return string representation of variable identifier wrapped in a Typescript non-null assertion', ()=>{
807
- const prop = extractProp('<div foo={bar!} />');
808
- const expected = 'bar!';
809
- const actual = getPropValue(prop);
810
- assert.equal(actual, expected);
811
- });
812
- it('should return string representation of variable identifier wrapped in a deep Typescript non-null assertion', ()=>{
813
- const prop = extractProp('<div foo={(bar!)!} />');
814
- const expected = '(bar!)!';
815
- const actual = getPropValue(prop);
816
- assert.equal(actual, expected);
817
- });
818
- it('should return string representation of variable identifier wrapped in a Typescript type coercion', ()=>{
819
- const prop = extractProp('<div foo={bar as any} />');
820
- const expected = 'bar';
821
- const actual = getPropValue(prop);
822
- assert.equal(actual, expected);
823
- });
824
- });
825
- describeIfNotBabylon('TSNonNullExpression', ()=>{
826
- beforeEach(()=>{
827
- changePlugins((pls)=>[
828
- ...pls,
829
- 'typescript'
830
- ]);
831
- });
832
- it('should return string representation of a TSNonNullExpression of form `variable!`', ()=>{
833
- const prop = extractProp('<div foo={bar!} />');
834
- const expected = 'bar!';
835
- const actual = getPropValue(prop);
836
- assert.equal(actual, expected);
837
- });
838
- it('should return string representation of a TSNonNullExpression of form `object!.property`', ()=>{
839
- const prop = extractProp('<div foo={bar!.bar} />');
840
- const expected = 'bar!.bar';
841
- const actual = getPropValue(prop);
842
- assert.equal(actual, expected);
843
- });
844
- it('should return string representation of a TSNonNullExpression of form `object!.property!`', ()=>{
845
- const prop = extractProp('<div foo={bar!.bar!} />');
846
- const actual = getPropValue(prop);
847
- const expected = 'bar!.bar!';
848
- assert.equal(actual, expected);
849
- });
850
- it('should return string representation of a TSNonNullExpression of form `object.property!`', ()=>{
851
- const prop = extractProp('<div foo={bar.bar!} />');
852
- const actual = getPropValue(prop);
853
- const expected = 'bar.bar!';
854
- assert.equal(actual, expected);
855
- });
856
- it('should return string representation of a TSNonNullExpression of form `object.property.property!`', ()=>{
857
- const prop = extractProp('<div foo={bar.bar.bar!} />');
858
- const actual = getPropValue(prop);
859
- const expected = 'bar.bar.bar!';
860
- assert.equal(actual, expected);
861
- });
862
- it('should return string representation of a TSNonNullExpression of form `object!.property.property!`', ()=>{
863
- const prop = extractProp('<div foo={bar!.bar.bar!} />');
864
- const actual = getPropValue(prop);
865
- const expected = 'bar!.bar.bar!';
866
- assert.equal(actual, expected);
867
- });
868
- it('should return string representation of an object wrapped in a deep Typescript non-null assertion', ()=>{
869
- const prop = extractProp('<div foo={(bar!.bar)!} />');
870
- const expected = '(bar!.bar)!';
871
- const actual = getPropValue(prop);
872
- assert.equal(actual, expected);
873
- });
874
- it('should return string representation of a cast wrapped in a deep Typescript non-null assertion', ()=>{
875
- const prop = extractProp('<div foo={(bar as Bar).baz!} />');
876
- const actual = getPropValue(prop);
877
- const expected = 'bar.baz!';
878
- assert.equal(actual, expected);
879
- });
880
- it('should return string representation of an object wrapped in a deep Typescript non-null assertion', ()=>{
881
- const prop = extractProp('<div foo={(bar.bar)!} />');
882
- const expected = '(bar.bar)!';
883
- const actual = getPropValue(prop);
884
- assert.equal(actual, expected);
885
- });
886
- it('should return string representation of an object wrapped in a deep Typescript non-null assertion', ()=>{
887
- const prop = extractProp('<div foo={(bar!.bar.bar!)!} />');
888
- const expected = '(bar!.bar.bar!)!';
889
- const actual = getPropValue(prop);
890
- assert.equal(actual, expected);
891
- });
892
- it('should return string representation of variable identifier wrapped in a deep Typescript non-null assertion', ()=>{
893
- const prop = extractProp('<div foo={(bar!)!} />');
894
- const expected = '(bar!)!';
895
- const actual = getPropValue(prop);
896
- assert.equal(actual, expected);
897
- });
898
- it('should work with a this.props value', ()=>{
899
- const prop = extractProp('<a foo={this.props.href!}>Download</a>');
900
- const expected = 'this.props.href!';
901
- const actual = getPropValue(prop);
902
- assert.equal(actual, expected);
903
- });
904
- it('should correctly evaluate a bracketed navigation expression that prefixes with !', ()=>{
905
- const prop = extractProp('<Link foo={data![0].url} />');
906
- const expected = 'data![0].url';
907
- const actual = getPropValue(prop);
908
- assert.equal(actual, expected);
909
- });
910
- it('works with an optional chain with an `as`', ()=>{
911
- const prop = extractProp('<img src={images?.footer as string} />', 'src');
912
- const expected = 'images?.footer';
913
- const actual = getPropValue(prop, 'src');
914
- assert.equal(actual, expected);
915
- });
916
- });
917
- describe('JSX empty expression', ()=>{
918
- it('should work with an empty expression', ()=>{
919
- const prop = extractProp('<div>\n{/* Hello there */}\n</div>', 'children');
920
- const expected = undefined;
921
- const actual = getPropValue(prop);
922
- assert.equal(actual, expected);
923
- });
924
- });
925
- });