Skip to content

Commit

Permalink
feat: Add test and implementation to get meta for shape types
Browse files Browse the repository at this point in the history
  • Loading branch information
jerelmiller committed Jun 7, 2020
1 parent 004dde1 commit a1e86c2
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 3 deletions.
57 changes: 57 additions & 0 deletions src/utils/__tests__/propTypeInfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -303,4 +303,61 @@ describe('getTypeMeta', () => {
params: [{ description: '', name: 'event', type: 'Event' }],
});
});

test('returns nested prop type info for shape types', () => {
const propType = createPropType('shape', [
{
pathname: createPropType('string', undefined, { isRequired: true }),
search: createPropType('string'),
hash: createPropType('string'),
},
]);

const component = {
propTypes: {
to: propType,
},
};

expect(getTypeMeta('to', propType, { component })).toEqual({
types: [
{
name: 'pathname',
defaultValue: undefined,
description: undefined,
deprecation: null,
isRequired: true,
type: {
meta: null,
raw: 'string',
name: 'string',
},
},
{
name: 'search',
defaultValue: undefined,
description: undefined,
deprecation: null,
isRequired: false,
type: {
meta: null,
raw: 'string',
name: 'string',
},
},
{
name: 'hash',
defaultValue: undefined,
description: undefined,
deprecation: null,
isRequired: false,
type: {
meta: null,
raw: 'string',
name: 'string',
},
},
],
});
});
});
16 changes: 13 additions & 3 deletions src/utils/propTypeInfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,15 @@ export const getTypeMeta = (name, propType, { component }) => {
returnValue: propTypeDocs.tags?.returnValue ?? { type: 'undefined' },
params: propTypeDocs.tags?.param,
};
case 'shape': {
const [shape] = getArgs(propType);

return {
types: Object.entries(shape).map(([name, propType]) =>
getPropTypeDefinition(component, name, propType)
),
};
}
default:
return null;
}
Expand All @@ -113,10 +122,11 @@ export const getPropTypeDefinition = (component, name, propType) => {
return {
name,
defaultValue: getDefaultValue(component, name),
description: propDocs.text,
deprecation: propDocs.tags.deprecated?.[0] ?? null,
isRequired: propMeta.some((item) => item.name === 'isRequired'),
description: propDocs?.text,
deprecation: propDocs?.tags?.deprecated?.[0] ?? null,
isRequired: propMeta?.some((item) => item.name === 'isRequired') ?? false,
type: {
meta: getTypeMeta(name, propType, { component }),
raw: getRawTypeName(propType),
name: getNormalizedTypeName(propType),
},
Expand Down

0 comments on commit a1e86c2

Please sign in to comment.