7
7
*/
8
8
9
9
import { builders } from 'ast-types' ;
10
- import * as utils from '../../../tests/utils' ;
10
+ import {
11
+ parse as parseSource ,
12
+ statement ,
13
+ noopImporter ,
14
+ makeMockImporter ,
15
+ } from '../../../tests/utils' ;
11
16
import resolveHOC from '../resolveHOC' ;
12
17
13
18
describe ( 'resolveHOC' , ( ) => {
14
19
function parse ( src ) {
15
- const root = utils . parse ( src ) ;
20
+ const root = parseSource ( src ) ;
16
21
return root . get ( 'body' , root . node . body . length - 1 , 'expression' ) ;
17
22
}
18
23
24
+ const mockImporter = makeMockImporter ( {
25
+ component : statement ( `
26
+ export default Component;
27
+ ` ) . get ( 'declaration' ) ,
28
+
29
+ hoc : statement ( `
30
+ export default hoc1(foo);
31
+ import foo from 'component';
32
+ ` ) . get ( 'declaration' ) ,
33
+ } ) ;
34
+
19
35
it ( 'resolves simple hoc' , ( ) => {
20
36
const path = parse ( [ 'hoc(Component);' ] . join ( '\n' ) ) ;
21
- expect ( resolveHOC ( path ) ) . toEqualASTNode ( builders . identifier ( 'Component' ) ) ;
37
+ expect ( resolveHOC ( path , noopImporter ) ) . toEqualASTNode (
38
+ builders . identifier ( 'Component' ) ,
39
+ ) ;
22
40
} ) ;
23
41
24
42
it ( 'resolves simple hoc w/ multiple args' , ( ) => {
25
43
const path = parse ( [ 'hoc1(arg1a, arg1b)(Component);' ] . join ( '\n' ) ) ;
26
- expect ( resolveHOC ( path ) ) . toEqualASTNode ( builders . identifier ( 'Component' ) ) ;
44
+ expect ( resolveHOC ( path , noopImporter ) ) . toEqualASTNode (
45
+ builders . identifier ( 'Component' ) ,
46
+ ) ;
27
47
} ) ;
28
48
29
49
it ( 'resolves nested hocs' , ( ) => {
@@ -32,7 +52,9 @@ describe('resolveHOC', () => {
32
52
hoc1(arg1a, arg2a)(Component)
33
53
);` ,
34
54
) ;
35
- expect ( resolveHOC ( path ) ) . toEqualASTNode ( builders . identifier ( 'Component' ) ) ;
55
+ expect ( resolveHOC ( path , noopImporter ) ) . toEqualASTNode (
56
+ builders . identifier ( 'Component' ) ,
57
+ ) ;
36
58
} ) ;
37
59
38
60
it ( 'resolves really nested hocs' , ( ) => {
@@ -43,38 +65,82 @@ describe('resolveHOC', () => {
43
65
)
44
66
);` ,
45
67
) ;
46
- expect ( resolveHOC ( path ) ) . toEqualASTNode ( builders . identifier ( 'Component' ) ) ;
68
+ expect ( resolveHOC ( path , noopImporter ) ) . toEqualASTNode (
69
+ builders . identifier ( 'Component' ) ,
70
+ ) ;
47
71
} ) ;
48
72
49
73
it ( 'resolves HOC with additional params' , ( ) => {
50
74
const path = parse ( `hoc3(Component, {})` ) ;
51
- expect ( resolveHOC ( path ) ) . toEqualASTNode ( builders . identifier ( 'Component' ) ) ;
75
+ expect ( resolveHOC ( path , noopImporter ) ) . toEqualASTNode (
76
+ builders . identifier ( 'Component' ) ,
77
+ ) ;
52
78
} ) ;
53
79
54
80
it ( 'resolves HOC as last element if first is literal' , ( ) => {
55
81
const path = parse ( `hoc3(41, Component)` ) ;
56
- expect ( resolveHOC ( path ) ) . toEqualASTNode ( builders . identifier ( 'Component' ) ) ;
82
+ expect ( resolveHOC ( path , noopImporter ) ) . toEqualASTNode (
83
+ builders . identifier ( 'Component' ) ,
84
+ ) ;
57
85
} ) ;
58
86
59
87
it ( 'resolves HOC as last element if first is array' , ( ) => {
60
88
const path = parse ( `hoc3([], Component)` ) ;
61
- expect ( resolveHOC ( path ) ) . toEqualASTNode ( builders . identifier ( 'Component' ) ) ;
89
+ expect ( resolveHOC ( path , noopImporter ) ) . toEqualASTNode (
90
+ builders . identifier ( 'Component' ) ,
91
+ ) ;
62
92
} ) ;
63
93
64
94
it ( 'resolves HOC as last element if first is object' , ( ) => {
65
95
const path = parse ( `hoc3({}, Component)` ) ;
66
- expect ( resolveHOC ( path ) ) . toEqualASTNode ( builders . identifier ( 'Component' ) ) ;
96
+ expect ( resolveHOC ( path , noopImporter ) ) . toEqualASTNode (
97
+ builders . identifier ( 'Component' ) ,
98
+ ) ;
67
99
} ) ;
68
100
69
101
it ( 'resolves HOC as last element if first is spread' , ( ) => {
70
102
const path = parse ( `hoc3(...params, Component)` ) ;
71
- expect ( resolveHOC ( path ) ) . toEqualASTNode ( builders . identifier ( 'Component' ) ) ;
103
+ expect ( resolveHOC ( path , noopImporter ) ) . toEqualASTNode (
104
+ builders . identifier ( 'Component' ) ,
105
+ ) ;
72
106
} ) ;
73
107
74
108
it ( 'resolves intermediate hocs' , ( ) => {
75
109
const path = parse (
76
110
[ 'const Component = React.memo(42);' , 'hoc()(Component);' ] . join ( '\n' ) ,
77
111
) ;
78
- expect ( resolveHOC ( path ) ) . toEqualASTNode ( builders . literal ( 42 ) ) ;
112
+ expect ( resolveHOC ( path , noopImporter ) ) . toEqualASTNode ( builders . literal ( 42 ) ) ;
113
+ } ) ;
114
+
115
+ it ( 'can resolve an imported component passed to hoc' , ( ) => {
116
+ const path = parse ( `
117
+ import foo from 'component';
118
+ hoc(foo);
119
+ ` ) ;
120
+ expect ( resolveHOC ( path , mockImporter ) ) . toEqualASTNode (
121
+ builders . identifier ( 'Component' ) ,
122
+ ) ;
123
+ } ) ;
124
+
125
+ it ( 'can resolve an imported component passed to nested hoc' , ( ) => {
126
+ const path = parse ( `
127
+ import foo from 'component';
128
+ hoc2(arg2b, arg2b)(
129
+ hoc1(arg1a, arg2a)(foo)
130
+ );
131
+ ` ) ;
132
+ expect ( resolveHOC ( path , mockImporter ) ) . toEqualASTNode (
133
+ builders . identifier ( 'Component' ) ,
134
+ ) ;
135
+ } ) ;
136
+
137
+ it ( 'can resolve an hocs inside imported component passed to hoc' , ( ) => {
138
+ const path = parse ( `
139
+ import bar from 'hoc';
140
+ hoc(bar);
141
+ ` ) ;
142
+ expect ( resolveHOC ( path , mockImporter ) ) . toEqualASTNode (
143
+ builders . identifier ( 'Component' ) ,
144
+ ) ;
79
145
} ) ;
80
146
} ) ;
0 commit comments