6
6
*
7
7
*/
8
8
9
- import * as utils from '../../../tests/utils' ;
9
+ import {
10
+ getParser ,
11
+ parse as parseSource ,
12
+ statement ,
13
+ noopImporter ,
14
+ makeMockImporter ,
15
+ } from '../../../tests/utils' ;
10
16
11
17
import findAllExportedComponentDefinitions from '../findAllExportedComponentDefinitions' ;
12
18
13
19
describe ( 'findAllExportedComponentDefinitions' , ( ) => {
14
20
function parse ( source ) {
15
- return utils . parse ( source ) ;
21
+ return parseSource ( source ) ;
16
22
}
17
23
18
- function findComponents ( path , importer = utils . noopImporter ) {
19
- return findAllExportedComponentDefinitions (
20
- path ,
21
- utils . getParser ( ) ,
22
- importer ,
23
- ) ;
24
+ function findComponents ( path , importer = noopImporter ) {
25
+ return findAllExportedComponentDefinitions ( path , getParser ( ) , importer ) ;
24
26
}
25
27
28
+ const mockImporter = makeMockImporter ( {
29
+ createClass : statement ( `
30
+ export default React.createClass({});
31
+ import React from 'react';
32
+ ` ) . get ( 'declaration' ) ,
33
+
34
+ classDec : statement ( `
35
+ export default class Component extends React.Component {};
36
+ import React from 'react';
37
+ ` ) . get ( 'declaration' ) ,
38
+
39
+ classExpr : statement ( `
40
+ export default Component;
41
+ var Component = class extends React.Component {};
42
+ import React from 'react';
43
+ ` ) . get ( 'declaration' ) ,
44
+
45
+ statelessJsx : statement ( `
46
+ export default () => <div />;
47
+ ` ) . get ( 'declaration' ) ,
48
+
49
+ statelessCreateElement : statement ( `
50
+ export default () => React.createElement('div', {});
51
+ import React from 'react';
52
+ ` ) . get ( 'declaration' ) ,
53
+
54
+ forwardRef : statement ( `
55
+ export default React.forwardRef((props, ref) => (
56
+ <div ref={ref} style={{backgroundColor: props.color}} />
57
+ ));
58
+ import React from 'react';
59
+ ` ) . get ( 'declaration' ) ,
60
+ } ) ;
61
+
26
62
describe ( 'CommonJS module exports' , ( ) => {
27
63
describe ( 'React.createClass' , ( ) => {
28
64
it ( 'finds React.createClass' , ( ) => {
@@ -67,6 +103,16 @@ describe('findAllExportedComponentDefinitions', () => {
67
103
68
104
expect ( actual . length ) . toBe ( 0 ) ;
69
105
} ) ;
106
+
107
+ it ( 'resolves an imported variable to React.createClass' , ( ) => {
108
+ const parsed = parse ( `
109
+ import Component from 'createClass';
110
+ module.exports = Component;
111
+ ` ) ;
112
+ const actual = findComponents ( parsed , mockImporter ) ;
113
+
114
+ expect ( actual . length ) . toBe ( 1 ) ;
115
+ } ) ;
70
116
} ) ;
71
117
72
118
describe ( 'class definitions' , ( ) => {
@@ -104,6 +150,26 @@ describe('findAllExportedComponentDefinitions', () => {
104
150
105
151
expect ( actual . length ) . toBe ( 1 ) ;
106
152
} ) ;
153
+
154
+ it ( 'resolves an imported variable to class declaration' , ( ) => {
155
+ const parsed = parse ( `
156
+ import Component from 'classDec';
157
+ module.exports = Component;
158
+ ` ) ;
159
+ const actual = findComponents ( parsed , mockImporter ) ;
160
+
161
+ expect ( actual . length ) . toBe ( 1 ) ;
162
+ } ) ;
163
+
164
+ it ( 'resolves an imported variable to class expression' , ( ) => {
165
+ const parsed = parse ( `
166
+ import Component from 'classExpr';
167
+ module.exports = Component;
168
+ ` ) ;
169
+ const actual = findComponents ( parsed , mockImporter ) ;
170
+
171
+ expect ( actual . length ) . toBe ( 1 ) ;
172
+ } ) ;
107
173
} ) ;
108
174
109
175
describe ( 'stateless components' , ( ) => {
@@ -141,6 +207,26 @@ describe('findAllExportedComponentDefinitions', () => {
141
207
142
208
expect ( actual . length ) . toBe ( 0 ) ;
143
209
} ) ;
210
+
211
+ it ( 'resolves an imported stateless component with JSX' , ( ) => {
212
+ const parsed = parse ( `
213
+ import Component from 'statelessJsx';
214
+ module.exports = Component;
215
+ ` ) ;
216
+ const actual = findComponents ( parsed , mockImporter ) ;
217
+
218
+ expect ( actual . length ) . toBe ( 1 ) ;
219
+ } ) ;
220
+
221
+ it ( 'resolves an imported stateless component with React.createElement' , ( ) => {
222
+ const parsed = parse ( `
223
+ import Component from 'statelessCreateElement';
224
+ module.exports = Component;
225
+ ` ) ;
226
+ const actual = findComponents ( parsed , mockImporter ) ;
227
+
228
+ expect ( actual . length ) . toBe ( 1 ) ;
229
+ } ) ;
144
230
} ) ;
145
231
146
232
describe ( 'forwardRef components' , ( ) => {
@@ -185,6 +271,17 @@ describe('findAllExportedComponentDefinitions', () => {
185
271
expect ( actual . length ) . toBe ( 1 ) ;
186
272
expect ( actual [ 0 ] . value . type ) . toEqual ( 'CallExpression' ) ;
187
273
} ) ;
274
+
275
+ it ( 'resolves an imported forwardRef component' , ( ) => {
276
+ const parsed = parse ( `
277
+ import Component from 'forwardRef';
278
+ module.exports = Component;
279
+ ` ) ;
280
+ const actual = findComponents ( parsed , mockImporter ) ;
281
+
282
+ expect ( actual . length ) . toBe ( 1 ) ;
283
+ expect ( actual [ 0 ] . value . type ) . toEqual ( 'CallExpression' ) ;
284
+ } ) ;
188
285
} ) ;
189
286
190
287
describe ( 'module.exports = <C>; / exports.foo = <C>;' , ( ) => {
@@ -310,6 +407,17 @@ describe('findAllExportedComponentDefinitions', () => {
310
407
311
408
expect ( actual . length ) . toBe ( 1 ) ;
312
409
} ) ;
410
+
411
+ it ( 'supports imported components' , ( ) => {
412
+ const parsed = parse ( `
413
+ import Component from 'createClass';
414
+ exports.ComponentA = Component;
415
+ exports.ComponentB = Component;
416
+ ` ) ;
417
+ const actual = findComponents ( parsed , mockImporter ) ;
418
+
419
+ expect ( actual . length ) . toBe ( 1 ) ;
420
+ } ) ;
313
421
} ) ;
314
422
315
423
describe ( 'class definition' , ( ) => {
@@ -377,6 +485,17 @@ describe('findAllExportedComponentDefinitions', () => {
377
485
378
486
expect ( actual . length ) . toBe ( 1 ) ;
379
487
} ) ;
488
+
489
+ it ( 'supports imported components' , ( ) => {
490
+ const parsed = parse ( `
491
+ import Component from 'classDec';
492
+ exports.ComponentA = Component;
493
+ exports.ComponentB = Component;
494
+ ` ) ;
495
+ const actual = findComponents ( parsed , mockImporter ) ;
496
+
497
+ expect ( actual . length ) . toBe ( 1 ) ;
498
+ } ) ;
380
499
} ) ;
381
500
} ) ;
382
501
} ) ;
@@ -463,6 +582,16 @@ describe('findAllExportedComponentDefinitions', () => {
463
582
464
583
expect ( actual . length ) . toBe ( 1 ) ;
465
584
} ) ;
585
+
586
+ it ( 'supports imported components' , ( ) => {
587
+ const parsed = parse ( `
588
+ import Component from 'createClass';
589
+ export default Component;
590
+ ` ) ;
591
+ const actual = findComponents ( parsed , mockImporter ) ;
592
+
593
+ expect ( actual . length ) . toBe ( 1 ) ;
594
+ } ) ;
466
595
} ) ;
467
596
468
597
describe ( 'class definition' , ( ) => {
@@ -529,6 +658,16 @@ describe('findAllExportedComponentDefinitions', () => {
529
658
530
659
expect ( actual . length ) . toBe ( 1 ) ;
531
660
} ) ;
661
+
662
+ it ( 'supports imported components' , ( ) => {
663
+ const parsed = parse ( `
664
+ import Component from 'classDec';
665
+ export default Component;
666
+ ` ) ;
667
+ const actual = findComponents ( parsed , mockImporter ) ;
668
+
669
+ expect ( actual . length ) . toBe ( 1 ) ;
670
+ } ) ;
532
671
} ) ;
533
672
534
673
describe ( 'forwardRef components' , ( ) => {
@@ -573,6 +712,16 @@ describe('findAllExportedComponentDefinitions', () => {
573
712
expect ( actual . length ) . toBe ( 1 ) ;
574
713
expect ( actual [ 0 ] . value . type ) . toEqual ( 'CallExpression' ) ;
575
714
} ) ;
715
+
716
+ it ( 'supports imported components' , ( ) => {
717
+ const parsed = parse ( `
718
+ import Component from 'forwardRef';
719
+ export default Component;
720
+ ` ) ;
721
+ const actual = findComponents ( parsed , mockImporter ) ;
722
+
723
+ expect ( actual . length ) . toBe ( 1 ) ;
724
+ } ) ;
576
725
} ) ;
577
726
} ) ;
578
727
@@ -657,6 +806,17 @@ describe('findAllExportedComponentDefinitions', () => {
657
806
658
807
expect ( actual . length ) . toBe ( 1 ) ;
659
808
} ) ;
809
+
810
+ it ( 'supports imported components' , ( ) => {
811
+ const parsed = parse ( `
812
+ import Component from 'createClass';
813
+ export let ComponentA = Component;
814
+ export let ComponentB = Component;
815
+ ` ) ;
816
+ const actual = findComponents ( parsed , mockImporter ) ;
817
+
818
+ expect ( actual . length ) . toBe ( 1 ) ;
819
+ } ) ;
660
820
} ) ;
661
821
662
822
describe ( 'class definition' , ( ) => {
@@ -743,6 +903,17 @@ describe('findAllExportedComponentDefinitions', () => {
743
903
expect ( actual . length ) . toBe ( 1 ) ;
744
904
expect ( actual [ 0 ] . node . type ) . toBe ( 'ClassExpression' ) ;
745
905
} ) ;
906
+
907
+ it ( 'supports imported components' , ( ) => {
908
+ const parsed = parse ( `
909
+ import Component from 'classDec';
910
+ export let ComponentA = Component;
911
+ export let ComponentB = Component;
912
+ ` ) ;
913
+ const actual = findComponents ( parsed , mockImporter ) ;
914
+
915
+ expect ( actual . length ) . toBe ( 1 ) ;
916
+ } ) ;
746
917
} ) ;
747
918
748
919
describe ( 'stateless components' , ( ) => {
@@ -821,6 +992,17 @@ describe('findAllExportedComponentDefinitions', () => {
821
992
expect ( actual . length ) . toBe ( 1 ) ;
822
993
expect ( actual [ 0 ] . node . type ) . toBe ( 'FunctionExpression' ) ;
823
994
} ) ;
995
+
996
+ it ( 'supports imported components' , ( ) => {
997
+ const parsed = parse ( `
998
+ import Component1 from 'statelessJsx';
999
+ import Component2 from 'statelessCreateElement';
1000
+ export var ComponentA = Component1, ComponentB = Component2;
1001
+ ` ) ;
1002
+ const actual = findComponents ( parsed , mockImporter ) ;
1003
+
1004
+ expect ( actual . length ) . toBe ( 2 ) ;
1005
+ } ) ;
824
1006
} ) ;
825
1007
826
1008
describe ( 'forwardRef components' , ( ) => {
@@ -841,6 +1023,17 @@ describe('findAllExportedComponentDefinitions', () => {
841
1023
expect ( actual . length ) . toBe ( 1 ) ;
842
1024
expect ( actual [ 0 ] . value . type ) . toEqual ( 'CallExpression' ) ;
843
1025
} ) ;
1026
+
1027
+ it ( 'supports imported components' , ( ) => {
1028
+ const parsed = parse ( `
1029
+ import Component from 'forwardRef';
1030
+ export let ComponentA = Component;
1031
+ export let ComponentB = Component;
1032
+ ` ) ;
1033
+ const actual = findComponents ( parsed , mockImporter ) ;
1034
+
1035
+ expect ( actual . length ) . toBe ( 1 ) ;
1036
+ } ) ;
844
1037
} ) ;
845
1038
} ) ;
846
1039
@@ -935,6 +1128,16 @@ describe('findAllExportedComponentDefinitions', () => {
935
1128
936
1129
expect ( actual . length ) . toBe ( 1 ) ;
937
1130
} ) ;
1131
+
1132
+ it ( 'supports imported components' , ( ) => {
1133
+ const parsed = parse ( `
1134
+ import Component from 'createClass';
1135
+ export { Component, Component as ComponentB };
1136
+ ` ) ;
1137
+ const actual = findComponents ( parsed , mockImporter ) ;
1138
+
1139
+ expect ( actual . length ) . toBe ( 1 ) ;
1140
+ } ) ;
938
1141
} ) ;
939
1142
940
1143
describe ( 'class definition' , ( ) => {
@@ -1025,6 +1228,16 @@ describe('findAllExportedComponentDefinitions', () => {
1025
1228
expect ( actual . length ) . toBe ( 1 ) ;
1026
1229
expect ( actual [ 0 ] . node . type ) . toBe ( 'ClassExpression' ) ;
1027
1230
} ) ;
1231
+
1232
+ it ( 'supports imported components' , ( ) => {
1233
+ const parsed = parse ( `
1234
+ import Component from 'classDec';
1235
+ export { Component, Component as ComponentB };
1236
+ ` ) ;
1237
+ const actual = findComponents ( parsed , mockImporter ) ;
1238
+
1239
+ expect ( actual . length ) . toBe ( 1 ) ;
1240
+ } ) ;
1028
1241
} ) ;
1029
1242
1030
1243
describe ( 'stateless components' , ( ) => {
@@ -1101,6 +1314,17 @@ describe('findAllExportedComponentDefinitions', () => {
1101
1314
expect ( actual . length ) . toBe ( 1 ) ;
1102
1315
expect ( actual [ 0 ] . node . type ) . toBe ( 'ArrowFunctionExpression' ) ;
1103
1316
} ) ;
1317
+
1318
+ it ( 'supports imported components' , ( ) => {
1319
+ const parsed = parse ( `
1320
+ import ComponentA from 'statelessJsx';
1321
+ import ComponentB from 'statelessCreateElement';
1322
+ export { ComponentA, ComponentB };
1323
+ ` ) ;
1324
+ const actual = findComponents ( parsed , mockImporter ) ;
1325
+
1326
+ expect ( actual . length ) . toBe ( 2 ) ;
1327
+ } ) ;
1104
1328
} ) ;
1105
1329
1106
1330
describe ( 'forwardRef components' , ( ) => {
@@ -1123,6 +1347,16 @@ describe('findAllExportedComponentDefinitions', () => {
1123
1347
expect ( actual . length ) . toBe ( 1 ) ;
1124
1348
expect ( actual [ 0 ] . value . type ) . toEqual ( 'CallExpression' ) ;
1125
1349
} ) ;
1350
+
1351
+ it ( 'supports imported components' , ( ) => {
1352
+ const parsed = parse ( `
1353
+ import Component from 'forwardRef';
1354
+ export { Component, Component as ComponentB };
1355
+ ` ) ;
1356
+ const actual = findComponents ( parsed , mockImporter ) ;
1357
+
1358
+ expect ( actual . length ) . toBe ( 1 ) ;
1359
+ } ) ;
1126
1360
} ) ;
1127
1361
} ) ;
1128
1362
0 commit comments