Skip to content

Commit d55b604

Browse files
phateddanez
authored andcommitted
Add importer tests to propTypeHandler, fix one resolveToValue need
1 parent 4f43d93 commit d55b604

File tree

3 files changed

+180
-10
lines changed

3 files changed

+180
-10
lines changed

src/handlers/__tests__/__snapshots__/propTypeHandler-test.js.snap

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,30 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`propTypeHandler React.createClass can resolve individual imported variables assigned to props 1`] = `
4+
Object {
5+
"@computed#bar": Object {
6+
"required": false,
7+
"type": Object {},
8+
},
9+
"baz": Object {
10+
"required": false,
11+
"type": Object {},
12+
},
13+
"complex_prop": Object {
14+
"required": true,
15+
"type": Object {},
16+
},
17+
"foo": Object {
18+
"required": false,
19+
"type": Object {},
20+
},
21+
"simple_prop": Object {
22+
"required": true,
23+
"type": Object {},
24+
},
25+
}
26+
`;
27+
328
exports[`propTypeHandler React.createClass handles computed properties 1`] = `
429
Object {
530
"@computed#foo": Object {
@@ -22,6 +47,31 @@ Object {
2247
}
2348
`;
2449

50+
exports[`propTypeHandler class definition class property can resolve individual imported variables assigned to props 1`] = `
51+
Object {
52+
"@computed#bar": Object {
53+
"required": false,
54+
"type": Object {},
55+
},
56+
"baz": Object {
57+
"required": false,
58+
"type": Object {},
59+
},
60+
"complex_prop": Object {
61+
"required": true,
62+
"type": Object {},
63+
},
64+
"foo": Object {
65+
"required": false,
66+
"type": Object {},
67+
},
68+
"simple_prop": Object {
69+
"required": true,
70+
"type": Object {},
71+
},
72+
}
73+
`;
74+
2575
exports[`propTypeHandler class definition class property handles computed properties 1`] = `
2676
Object {
2777
"@computed#foo": Object {
@@ -44,6 +94,31 @@ Object {
4494
}
4595
`;
4696

97+
exports[`propTypeHandler class definition static getter can resolve individual imported variables assigned to props 1`] = `
98+
Object {
99+
"@computed#bar": Object {
100+
"required": false,
101+
"type": Object {},
102+
},
103+
"baz": Object {
104+
"required": false,
105+
"type": Object {},
106+
},
107+
"complex_prop": Object {
108+
"required": true,
109+
"type": Object {},
110+
},
111+
"foo": Object {
112+
"required": false,
113+
"type": Object {},
114+
},
115+
"simple_prop": Object {
116+
"required": true,
117+
"type": Object {},
118+
},
119+
}
120+
`;
121+
47122
exports[`propTypeHandler class definition static getter handles computed properties 1`] = `
48123
Object {
49124
"@computed#foo": Object {
@@ -66,6 +141,31 @@ Object {
66141
}
67142
`;
68143

144+
exports[`propTypeHandler stateless component can resolve individual imported variables assigned to props 1`] = `
145+
Object {
146+
"@computed#bar": Object {
147+
"required": false,
148+
"type": Object {},
149+
},
150+
"baz": Object {
151+
"required": false,
152+
"type": Object {},
153+
},
154+
"complex_prop": Object {
155+
"required": true,
156+
"type": Object {},
157+
},
158+
"foo": Object {
159+
"required": false,
160+
"type": Object {},
161+
},
162+
"simple_prop": Object {
163+
"required": true,
164+
"type": Object {},
165+
},
166+
}
167+
`;
168+
69169
exports[`propTypeHandler stateless component handles computed properties 1`] = `
70170
Object {
71171
"@computed#foo": Object {

src/handlers/__tests__/propTypeHandler-test.js

Lines changed: 79 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
jest.mock('../../Documentation');
1010
jest.mock('../../utils/getPropType', () => jest.fn(() => ({})));
1111

12-
import { statement, expression } from '../../../tests/utils';
12+
import { statement, expression, noopImporter, makeMockImporter } from '../../../tests/utils';
1313
import Documentation from '../../Documentation';
1414
import { propTypeHandler } from '../propTypeHandler';
1515

@@ -22,6 +22,39 @@ describe('propTypeHandler', () => {
2222
documentation = new Documentation();
2323
});
2424

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+
2558
function template(src) {
2659
return `
2760
${src}
@@ -43,7 +76,7 @@ describe('propTypeHandler', () => {
4376
const fooPath = propTypesAST.get('properties', 0, 'value');
4477
const xyzPath = propTypesAST.get('properties', 1, 'value');
4578

46-
propTypeHandler(documentation, definition);
79+
propTypeHandler(documentation, definition, noopImporter);
4780

4881
expect(getPropTypeMock.mock.calls[0][0]).toEqualASTNode(fooPath);
4982
expect(getPropTypeMock.mock.calls[1][0]).toEqualASTNode(xyzPath);
@@ -60,7 +93,7 @@ describe('propTypeHandler', () => {
6093
),
6194
);
6295

63-
propTypeHandler(documentation, definition);
96+
propTypeHandler(documentation, definition, noopImporter);
6497
expect(documentation.descriptors).toEqual({
6598
foo: {
6699
type: {},
@@ -86,7 +119,7 @@ describe('propTypeHandler', () => {
86119
),
87120
);
88121

89-
propTypeHandler(documentation, definition);
122+
propTypeHandler(documentation, definition, noopImporter);
90123
expect(documentation.descriptors).toEqual({
91124
foo: {
92125
type: {},
@@ -106,7 +139,7 @@ describe('propTypeHandler', () => {
106139
),
107140
);
108141

109-
propTypeHandler(documentation, definition);
142+
propTypeHandler(documentation, definition, noopImporter);
110143
expect(documentation.descriptors).toEqual({
111144
simple_prop: {
112145
type: {},
@@ -130,7 +163,7 @@ describe('propTypeHandler', () => {
130163
),
131164
);
132165

133-
propTypeHandler(documentation, definition);
166+
propTypeHandler(documentation, definition, noopImporter);
134167
expect(documentation.descriptors).toMatchSnapshot();
135168
});
136169

@@ -145,7 +178,7 @@ describe('propTypeHandler', () => {
145178
),
146179
);
147180

148-
propTypeHandler(documentation, definition);
181+
propTypeHandler(documentation, definition, noopImporter);
149182
expect(documentation.descriptors).toMatchSnapshot();
150183
});
151184

@@ -159,7 +192,7 @@ describe('propTypeHandler', () => {
159192
),
160193
);
161194

162-
propTypeHandler(documentation, definition);
195+
propTypeHandler(documentation, definition, noopImporter);
163196
expect(documentation.descriptors).toEqual({
164197
custom_propA: {
165198
type: {},
@@ -182,14 +215,51 @@ describe('propTypeHandler', () => {
182215
var props = {bar: PropTypes.bool};
183216
`);
184217

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);
186235
expect(documentation.descriptors).toEqual({
187236
bar: {
188237
type: {},
189238
required: false,
190239
},
191240
});
192241
});
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+
});
193263
}
194264

195265
describe('React.createClass', () => {

src/handlers/propTypeHandler.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ function amendPropTypes(getDescriptor, path, importer) {
3939
if (!propName) return;
4040

4141
const propDescriptor = getDescriptor(propName);
42-
const valuePath = propertyPath.get('value');
42+
const valuePath = resolveToValue(propertyPath.get('value'), importer);
4343
const type = isPropTypesExpression(valuePath, importer)
4444
? getPropType(valuePath, importer)
4545
: { name: 'custom', raw: printValue(valuePath) };

0 commit comments

Comments
 (0)