9
9
jest . mock ( '../../Documentation' ) ;
10
10
jest . mock ( '../../utils/getPropType' , ( ) => jest . fn ( ( ) => ( { } ) ) ) ;
11
11
12
- import { statement , expression } from '../../../tests/utils' ;
12
+ import { statement , expression , noopImporter , makeMockImporter } from '../../../tests/utils' ;
13
13
import Documentation from '../../Documentation' ;
14
14
import { propTypeHandler } from '../propTypeHandler' ;
15
15
@@ -22,6 +22,39 @@ describe('propTypeHandler', () => {
22
22
documentation = new Documentation ( ) ;
23
23
} ) ;
24
24
25
+ const mockImporter = makeMockImporter ( {
26
+ props : statement ( `
27
+ export default {bar: PropTypes.bool};
28
+ import { PropTypes } from 'react';
29
+ ` ) . get ( 'declaration' ) ,
30
+
31
+ simpleProp : statement ( `
32
+ export default PropTypes.array.isRequired;
33
+ import { PropTypes } from 'react';
34
+ ` ) . get ( 'declaration' ) ,
35
+
36
+ complexProp : statement ( `
37
+ export default prop;
38
+ var prop = PropTypes.oneOfType([PropTypes.number, PropTypes.bool]).isRequired;
39
+ import { PropTypes } from 'react';
40
+ ` ) . get ( 'declaration' ) ,
41
+
42
+ foo : statement ( `
43
+ export default PropTypes.bool;
44
+ import { PropTypes } from 'react';
45
+ ` ) . get ( 'declaration' ) ,
46
+
47
+ bar : statement ( `
48
+ export default PropTypes.bool;
49
+ import { PropTypes } from 'react';
50
+ ` ) . get ( 'declaration' ) ,
51
+
52
+ baz : statement ( `
53
+ export default OtherPropTypes.bool;
54
+ import { PropTypes as OtherPropTypes } from 'react';
55
+ ` ) . get ( 'declaration' ) ,
56
+ } ) ;
57
+
25
58
function template ( src ) {
26
59
return `
27
60
${ src }
@@ -43,7 +76,7 @@ describe('propTypeHandler', () => {
43
76
const fooPath = propTypesAST . get ( 'properties' , 0 , 'value' ) ;
44
77
const xyzPath = propTypesAST . get ( 'properties' , 1 , 'value' ) ;
45
78
46
- propTypeHandler ( documentation , definition ) ;
79
+ propTypeHandler ( documentation , definition , noopImporter ) ;
47
80
48
81
expect ( getPropTypeMock . mock . calls [ 0 ] [ 0 ] ) . toEqualASTNode ( fooPath ) ;
49
82
expect ( getPropTypeMock . mock . calls [ 1 ] [ 0 ] ) . toEqualASTNode ( xyzPath ) ;
@@ -60,7 +93,7 @@ describe('propTypeHandler', () => {
60
93
) ,
61
94
) ;
62
95
63
- propTypeHandler ( documentation , definition ) ;
96
+ propTypeHandler ( documentation , definition , noopImporter ) ;
64
97
expect ( documentation . descriptors ) . toEqual ( {
65
98
foo : {
66
99
type : { } ,
@@ -86,7 +119,7 @@ describe('propTypeHandler', () => {
86
119
) ,
87
120
) ;
88
121
89
- propTypeHandler ( documentation , definition ) ;
122
+ propTypeHandler ( documentation , definition , noopImporter ) ;
90
123
expect ( documentation . descriptors ) . toEqual ( {
91
124
foo : {
92
125
type : { } ,
@@ -106,7 +139,7 @@ describe('propTypeHandler', () => {
106
139
) ,
107
140
) ;
108
141
109
- propTypeHandler ( documentation , definition ) ;
142
+ propTypeHandler ( documentation , definition , noopImporter ) ;
110
143
expect ( documentation . descriptors ) . toEqual ( {
111
144
simple_prop : {
112
145
type : { } ,
@@ -130,7 +163,7 @@ describe('propTypeHandler', () => {
130
163
) ,
131
164
) ;
132
165
133
- propTypeHandler ( documentation , definition ) ;
166
+ propTypeHandler ( documentation , definition , noopImporter ) ;
134
167
expect ( documentation . descriptors ) . toMatchSnapshot ( ) ;
135
168
} ) ;
136
169
@@ -145,7 +178,7 @@ describe('propTypeHandler', () => {
145
178
) ,
146
179
) ;
147
180
148
- propTypeHandler ( documentation , definition ) ;
181
+ propTypeHandler ( documentation , definition , noopImporter ) ;
149
182
expect ( documentation . descriptors ) . toMatchSnapshot ( ) ;
150
183
} ) ;
151
184
@@ -159,7 +192,7 @@ describe('propTypeHandler', () => {
159
192
) ,
160
193
) ;
161
194
162
- propTypeHandler ( documentation , definition ) ;
195
+ propTypeHandler ( documentation , definition , noopImporter ) ;
163
196
expect ( documentation . descriptors ) . toEqual ( {
164
197
custom_propA : {
165
198
type : { } ,
@@ -182,14 +215,51 @@ describe('propTypeHandler', () => {
182
215
var props = {bar: PropTypes.bool};
183
216
` ) ;
184
217
185
- propTypeHandler ( documentation , definition ) ;
218
+ propTypeHandler ( documentation , definition , noopImporter ) ;
219
+ expect ( documentation . descriptors ) . toEqual ( {
220
+ bar : {
221
+ type : { } ,
222
+ required : false ,
223
+ } ,
224
+ } ) ;
225
+ } ) ;
226
+
227
+ it ( 'resolves imported variables' , ( ) => {
228
+ const definitionSrc = getSrc ( 'props' ) ;
229
+ const definition = parse ( `
230
+ ${ definitionSrc }
231
+ import props from 'props';
232
+ ` ) ;
233
+
234
+ propTypeHandler ( documentation , definition , mockImporter ) ;
186
235
expect ( documentation . descriptors ) . toEqual ( {
187
236
bar : {
188
237
type : { } ,
189
238
required : false ,
190
239
} ,
191
240
} ) ;
192
241
} ) ;
242
+
243
+ it ( 'can resolve individual imported variables assigned to props' , ( ) => {
244
+ const definitionSrc = getSrc ( `{
245
+ foo: foo,
246
+ [bar]: bar,
247
+ baz: baz,
248
+ simple_prop: simpleProp,
249
+ complex_prop: complexProp,
250
+ }` ) ;
251
+ const definition = parse ( `
252
+ ${ definitionSrc }
253
+ import foo from 'foo';
254
+ import bar from 'bar';
255
+ import baz from 'baz';
256
+ import simpleProp from 'simpleProp';
257
+ import complexProp from 'complexProp';
258
+ ` ) ;
259
+
260
+ propTypeHandler ( documentation , definition , mockImporter ) ;
261
+ expect ( documentation . descriptors ) . toMatchSnapshot ( ) ;
262
+ } ) ;
193
263
}
194
264
195
265
describe ( 'React.createClass' , ( ) => {
0 commit comments