Skip to content

Commit 24a8034

Browse files
phateddanez
authored andcommitted
Add importer tests for getPropType
1 parent 6d476ef commit 24a8034

File tree

2 files changed

+218
-3
lines changed

2 files changed

+218
-3
lines changed

src/utils/__tests__/__snapshots__/getPropType-test.js.snap

Lines changed: 109 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,22 @@ Object {
146146
}
147147
`;
148148

149+
exports[`getPropType resolve identifier to their values correctly resolves SpreadElements in arrays from imported values 1`] = `
150+
Object {
151+
"name": "enum",
152+
"value": Array [
153+
Object {
154+
"computed": false,
155+
"value": "\\"foo\\"",
156+
},
157+
Object {
158+
"computed": false,
159+
"value": "\\"bar\\"",
160+
},
161+
],
162+
}
163+
`;
164+
149165
exports[`getPropType resolve identifier to their values correctly resolves nested SpreadElements in arrays 1`] = `
150166
Object {
151167
"name": "enum",
@@ -162,7 +178,7 @@ Object {
162178
}
163179
`;
164180

165-
exports[`getPropType resolve identifier to their values does not resolve external values 1`] = `
181+
exports[`getPropType resolve identifier to their values does not resolve external values without proper importer 1`] = `
166182
Object {
167183
"computed": true,
168184
"name": "enum",
@@ -202,6 +218,50 @@ Object {
202218
}
203219
`;
204220

221+
exports[`getPropType resolve identifier to their values resolves imported identifier to their initialization value in array 1`] = `
222+
Object {
223+
"name": "enum",
224+
"value": Array [
225+
Object {
226+
"computed": false,
227+
"value": "\\"foo\\"",
228+
},
229+
Object {
230+
"computed": false,
231+
"value": "\\"bar\\"",
232+
},
233+
],
234+
}
235+
`;
236+
237+
exports[`getPropType resolve identifier to their values resolves imported variables to their values 1`] = `
238+
Object {
239+
"name": "shape",
240+
"value": Object {
241+
"bar": Object {
242+
"name": "string",
243+
"required": false,
244+
},
245+
},
246+
}
247+
`;
248+
249+
exports[`getPropType resolve identifier to their values resolves importer identifier to initialization value 1`] = `
250+
Object {
251+
"name": "enum",
252+
"value": Array [
253+
Object {
254+
"computed": false,
255+
"value": "\\"foo\\"",
256+
},
257+
Object {
258+
"computed": false,
259+
"value": "\\"bar\\"",
260+
},
261+
],
262+
}
263+
`;
264+
205265
exports[`getPropType resolve identifier to their values resolves memberExpressions 1`] = `
206266
Object {
207267
"name": "enum",
@@ -218,6 +278,22 @@ Object {
218278
}
219279
`;
220280

281+
exports[`getPropType resolve identifier to their values resolves memberExpressions from imported objects 1`] = `
282+
Object {
283+
"name": "enum",
284+
"value": Array [
285+
Object {
286+
"computed": false,
287+
"value": "\\"foo\\"",
288+
},
289+
Object {
290+
"computed": false,
291+
"value": "\\"bar\\"",
292+
},
293+
],
294+
}
295+
`;
296+
221297
exports[`getPropType resolve identifier to their values resolves simple identifier to their initialization value 1`] = `
222298
Object {
223299
"name": "enum",
@@ -250,6 +326,38 @@ Object {
250326
}
251327
`;
252328

329+
exports[`getPropType resolve identifier to their values resolves values from imported Object.keys call 1`] = `
330+
Object {
331+
"name": "enum",
332+
"value": Array [
333+
Object {
334+
"computed": false,
335+
"value": "\\"FOO\\"",
336+
},
337+
Object {
338+
"computed": false,
339+
"value": "\\"BAR\\"",
340+
},
341+
],
342+
}
343+
`;
344+
345+
exports[`getPropType resolve identifier to their values resolves values from imported Object.values call 1`] = `
346+
Object {
347+
"name": "enum",
348+
"value": Array [
349+
Object {
350+
"computed": false,
351+
"value": "\\"bar\\"",
352+
},
353+
Object {
354+
"computed": false,
355+
"value": "\\"foo\\"",
356+
},
357+
],
358+
}
359+
`;
360+
253361
exports[`getPropType resolve identifier to their values resolves variables to their values 1`] = `
254362
Object {
255363
"name": "shape",

src/utils/__tests__/getPropType-test.js

Lines changed: 109 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,12 @@
66
*
77
*/
88

9-
import { expression, statement, noopImporter } from '../../../tests/utils';
9+
import {
10+
expression,
11+
statement,
12+
noopImporter,
13+
makeMockImporter,
14+
} from '../../../tests/utils';
1015
import getPropType from '../getPropType';
1116

1217
describe('getPropType', () => {
@@ -176,6 +181,42 @@ describe('getPropType', () => {
176181
});
177182

178183
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+
179220
it('resolves variables to their values', () => {
180221
const propTypeExpression = statement(`
181222
PropTypes.shape(shape);
@@ -185,6 +226,15 @@ describe('getPropType', () => {
185226
expect(getPropType(propTypeExpression, noopImporter)).toMatchSnapshot();
186227
});
187228

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+
188238
it('resolves simple identifier to their initialization value', () => {
189239
const propTypeIdentifier = statement(`
190240
PropTypes.oneOf(TYPES);
@@ -194,6 +244,15 @@ describe('getPropType', () => {
194244
expect(getPropType(propTypeIdentifier, noopImporter)).toMatchSnapshot();
195245
});
196246

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+
197256
it('resolves simple identifier to their initialization value in array', () => {
198257
const identifierInsideArray = statement(`
199258
PropTypes.oneOf([FOO, BAR]);
@@ -206,6 +265,18 @@ describe('getPropType', () => {
206265
).toMatchSnapshot();
207266
});
208267

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+
209280
it('resolves memberExpressions', () => {
210281
const propTypeExpression = statement(`
211282
PropTypes.oneOf([TYPES.FOO, TYPES.BAR]);
@@ -215,6 +286,15 @@ describe('getPropType', () => {
215286
expect(getPropType(propTypeExpression, noopImporter)).toMatchSnapshot();
216287
});
217288

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+
218298
it('correctly resolves SpreadElements in arrays', () => {
219299
const propTypeExpression = statement(`
220300
PropTypes.oneOf([...TYPES]);
@@ -224,6 +304,15 @@ describe('getPropType', () => {
224304
expect(getPropType(propTypeExpression, noopImporter)).toMatchSnapshot();
225305
});
226306

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+
227316
it('correctly resolves nested SpreadElements in arrays', () => {
228317
const propTypeExpression = statement(`
229318
PropTypes.oneOf([...TYPES]);
@@ -243,6 +332,15 @@ describe('getPropType', () => {
243332
expect(getPropType(propTypeExpression, noopImporter)).toMatchSnapshot();
244333
});
245334

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+
246344
it('does resolve object values', () => {
247345
const propTypeExpression = statement(`
248346
PropTypes.oneOf(Object.values(TYPES));
@@ -252,7 +350,16 @@ describe('getPropType', () => {
252350
expect(getPropType(propTypeExpression, noopImporter)).toMatchSnapshot();
253351
});
254352

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', () => {
256363
const propTypeExpression = statement(`
257364
PropTypes.oneOf(TYPES);
258365
import { TYPES } from './foo';

0 commit comments

Comments
 (0)