6
6
*
7
7
*/
8
8
9
- import { expression , statement , noopImporter } from '../../../tests/utils' ;
9
+ import {
10
+ expression ,
11
+ statement ,
12
+ noopImporter ,
13
+ makeMockImporter ,
14
+ } from '../../../tests/utils' ;
10
15
import getPropType from '../getPropType' ;
11
16
12
17
describe ( 'getPropType' , ( ) => {
@@ -176,6 +181,42 @@ describe('getPropType', () => {
176
181
} ) ;
177
182
178
183
describe ( 'resolve identifier to their values' , ( ) => {
184
+ const mockImporter = makeMockImporter ( {
185
+ shape : statement ( `
186
+ export default {bar: PropTypes.string};
187
+ ` ) . get ( 'declaration' ) ,
188
+
189
+ types : statement ( `
190
+ export default ["foo", "bar"];
191
+ ` ) . get ( 'declaration' ) ,
192
+
193
+ foo : statement ( `
194
+ export default "foo";
195
+ ` ) . get ( 'declaration' ) ,
196
+
197
+ bar : statement ( `
198
+ export default "bar";
199
+ ` ) . get ( 'declaration' ) ,
200
+
201
+ obj : statement ( `
202
+ export default { FOO: "foo", BAR: "bar" };
203
+ ` ) . get ( 'declaration' ) ,
204
+
205
+ arr : statement ( `
206
+ export default ["foo", "bar"];
207
+ ` ) . get ( 'declaration' ) ,
208
+
209
+ keys : statement ( `
210
+ export default Object.keys(obj);
211
+ import obj from 'obj';
212
+ ` ) . get ( 'declaration' ) ,
213
+
214
+ values : statement ( `
215
+ export default Object.values(obj);
216
+ import obj from 'obj';
217
+ ` ) . get ( 'declaration' ) ,
218
+ } ) ;
219
+
179
220
it ( 'resolves variables to their values' , ( ) => {
180
221
const propTypeExpression = statement ( `
181
222
PropTypes.shape(shape);
@@ -185,6 +226,15 @@ describe('getPropType', () => {
185
226
expect ( getPropType ( propTypeExpression , noopImporter ) ) . toMatchSnapshot ( ) ;
186
227
} ) ;
187
228
229
+ it ( 'resolves imported variables to their values' , ( ) => {
230
+ const propTypeExpression = statement ( `
231
+ PropTypes.shape(shape);
232
+ import shape from 'shape';
233
+ ` ) . get ( 'expression' ) ;
234
+
235
+ expect ( getPropType ( propTypeExpression , mockImporter ) ) . toMatchSnapshot ( ) ;
236
+ } ) ;
237
+
188
238
it ( 'resolves simple identifier to their initialization value' , ( ) => {
189
239
const propTypeIdentifier = statement ( `
190
240
PropTypes.oneOf(TYPES);
@@ -194,6 +244,15 @@ describe('getPropType', () => {
194
244
expect ( getPropType ( propTypeIdentifier , noopImporter ) ) . toMatchSnapshot ( ) ;
195
245
} ) ;
196
246
247
+ it ( 'resolves importer identifier to initialization value' , ( ) => {
248
+ const propTypeIdentifier = statement ( `
249
+ PropTypes.oneOf(TYPES);
250
+ import TYPES from 'types';
251
+ ` ) . get ( 'expression' ) ;
252
+
253
+ expect ( getPropType ( propTypeIdentifier , mockImporter ) ) . toMatchSnapshot ( ) ;
254
+ } ) ;
255
+
197
256
it ( 'resolves simple identifier to their initialization value in array' , ( ) => {
198
257
const identifierInsideArray = statement ( `
199
258
PropTypes.oneOf([FOO, BAR]);
@@ -206,6 +265,18 @@ describe('getPropType', () => {
206
265
) . toMatchSnapshot ( ) ;
207
266
} ) ;
208
267
268
+ it ( 'resolves imported identifier to their initialization value in array' , ( ) => {
269
+ const identifierInsideArray = statement ( `
270
+ PropTypes.oneOf([FOO, BAR]);
271
+ import FOO from 'foo';
272
+ import BAR from 'bar';
273
+ ` ) . get ( 'expression' ) ;
274
+
275
+ expect (
276
+ getPropType ( identifierInsideArray , mockImporter ) ,
277
+ ) . toMatchSnapshot ( ) ;
278
+ } ) ;
279
+
209
280
it ( 'resolves memberExpressions' , ( ) => {
210
281
const propTypeExpression = statement ( `
211
282
PropTypes.oneOf([TYPES.FOO, TYPES.BAR]);
@@ -215,6 +286,15 @@ describe('getPropType', () => {
215
286
expect ( getPropType ( propTypeExpression , noopImporter ) ) . toMatchSnapshot ( ) ;
216
287
} ) ;
217
288
289
+ it ( 'resolves memberExpressions from imported objects' , ( ) => {
290
+ const propTypeExpression = statement ( `
291
+ PropTypes.oneOf([TYPES.FOO, TYPES.BAR]);
292
+ import TYPES from 'obj';
293
+ ` ) . get ( 'expression' ) ;
294
+
295
+ expect ( getPropType ( propTypeExpression , mockImporter ) ) . toMatchSnapshot ( ) ;
296
+ } ) ;
297
+
218
298
it ( 'correctly resolves SpreadElements in arrays' , ( ) => {
219
299
const propTypeExpression = statement ( `
220
300
PropTypes.oneOf([...TYPES]);
@@ -224,6 +304,15 @@ describe('getPropType', () => {
224
304
expect ( getPropType ( propTypeExpression , noopImporter ) ) . toMatchSnapshot ( ) ;
225
305
} ) ;
226
306
307
+ it ( 'correctly resolves SpreadElements in arrays from imported values' , ( ) => {
308
+ const propTypeExpression = statement ( `
309
+ PropTypes.oneOf([...TYPES]);
310
+ import TYPES from 'arr';
311
+ ` ) . get ( 'expression' ) ;
312
+
313
+ expect ( getPropType ( propTypeExpression , mockImporter ) ) . toMatchSnapshot ( ) ;
314
+ } ) ;
315
+
227
316
it ( 'correctly resolves nested SpreadElements in arrays' , ( ) => {
228
317
const propTypeExpression = statement ( `
229
318
PropTypes.oneOf([...TYPES]);
@@ -243,6 +332,15 @@ describe('getPropType', () => {
243
332
expect ( getPropType ( propTypeExpression , noopImporter ) ) . toMatchSnapshot ( ) ;
244
333
} ) ;
245
334
335
+ it ( 'resolves values from imported Object.keys call' , ( ) => {
336
+ const propTypeExpression = statement ( `
337
+ PropTypes.oneOf(keys);
338
+ import keys from 'keys';
339
+ ` ) . get ( 'expression' ) ;
340
+
341
+ expect ( getPropType ( propTypeExpression , mockImporter ) ) . toMatchSnapshot ( ) ;
342
+ } ) ;
343
+
246
344
it ( 'does resolve object values' , ( ) => {
247
345
const propTypeExpression = statement ( `
248
346
PropTypes.oneOf(Object.values(TYPES));
@@ -252,7 +350,16 @@ describe('getPropType', () => {
252
350
expect ( getPropType ( propTypeExpression , noopImporter ) ) . toMatchSnapshot ( ) ;
253
351
} ) ;
254
352
255
- it ( 'does not resolve external values' , ( ) => {
353
+ it ( 'resolves values from imported Object.values call' , ( ) => {
354
+ const propTypeExpression = statement ( `
355
+ PropTypes.oneOf(values);
356
+ import values from 'values';
357
+ ` ) . get ( 'expression' ) ;
358
+
359
+ expect ( getPropType ( propTypeExpression , mockImporter ) ) . toMatchSnapshot ( ) ;
360
+ } ) ;
361
+
362
+ it ( 'does not resolve external values without proper importer' , ( ) => {
256
363
const propTypeExpression = statement ( `
257
364
PropTypes.oneOf(TYPES);
258
365
import { TYPES } from './foo';
0 commit comments