Skip to content

Commit be381f3

Browse files
authored
Merge pull request #17 from asantos00/fix-nested-shape-objects
Fix bug with nested shape objects
2 parents 530e8c4 + 44990c9 commit be381f3

File tree

4 files changed

+35
-1
lines changed

4 files changed

+35
-1
lines changed

__tests__/__snapshots__/propTypes.js.snap

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,15 @@ Object {
5050
"requiredFunc": [Function],
5151
}
5252
`;
53+
54+
exports[`fakeProps should work with nested shape objects 1`] = `
55+
Object {
56+
"requiredShape": Object {
57+
"nestedRequiredShape": Object {
58+
"optionalString": "requiredShape.nestedRequiredShape.optionalString",
59+
},
60+
"optionalString": "requiredShape.optionalString",
61+
},
62+
"requiredString": "requiredString",
63+
}
64+
`;

__tests__/propTypes.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const ComponentFile = path.join(
88
const SimpleFile = path.join(__dirname, '../fixtures/propTypes/Simple.jsx')
99
const NoProps = require.resolve('../fixtures/propTypes/NoProps.jsx')
1010
const Multiple = require.resolve('../fixtures/propTypes/Multiple.jsx')
11+
const NestedShape = require.resolve('../fixtures/propTypes/NestedShape.jsx')
1112

1213
describe('fakeProps', () => {
1314
it('should return an object with no props (snapshot)', () => {
@@ -22,6 +23,10 @@ describe('fakeProps', () => {
2223
expect(fakeProps(ComponentFile, { optional: true })).toMatchSnapshot()
2324
})
2425

26+
it('should work with nested shape objects', () => {
27+
expect(fakeProps(NestedShape, { optional: true })).toMatchSnapshot()
28+
})
29+
2530
it('should return more props with optional', () => {
2631
const allProps = fakeProps(ComponentFile, { optional: true })
2732
const requiredProps = fakeProps(ComponentFile)

fixtures/propTypes/NestedShape.jsx

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import PropTypes from 'prop-types'
2+
3+
function NestedShape () {
4+
return <div />
5+
}
6+
7+
NestedShape.propTypes = {
8+
requiredString: PropTypes.string.isRequired,
9+
requiredShape: PropTypes.shape({
10+
optionalString: PropTypes.string,
11+
nestedRequiredShape: PropTypes.shape({
12+
optionalString: PropTypes.string
13+
})
14+
}).isRequired
15+
}
16+
17+
export default NestedShape

src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ function getShape (prefix, object, opts) {
2929
Object.keys(object).forEach(key => {
3030
const type = object[key]
3131
if (type.required || opts.optional) {
32-
res[key] = getFakeProp(`${prefix}.${key}`, { type })
32+
res[key] = getFakeProp(`${prefix}.${key}`, { type }, opts)
3333
}
3434
})
3535

0 commit comments

Comments
 (0)