Skip to content

Commit

Permalink
feat: Add test and implementation to return meta for a func type
Browse files Browse the repository at this point in the history
  • Loading branch information
jerelmiller committed Jun 7, 2020
1 parent 9a74761 commit 004dde1
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 3 deletions.
77 changes: 74 additions & 3 deletions src/utils/__tests__/propTypeInfo.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,32 @@
import { getNormalizedTypeName, getDefaultValue } from '../propTypeInfo';
import {
getNormalizedTypeName,
getDefaultValue,
getTypeMeta,
} from '../propTypeInfo';

const createDocs = (type, { description, returnValue, params } = {}) => {
const docs = {
text: description,
};

switch (type) {
case 'func':
docs.tags = {};

if (returnValue) {
docs.tags.returns = returnValue;
}

if (params) {
docs.tags.param = params;
}
break;
}

return docs;
};

const createPropType = (name, args, { isRequired = false } = {}) => {
const createPropType = (name, args, { isRequired = false, docs } = {}) => {
const propType = [{ name: 'PropTypes' }, { name }];

if (args) {
Expand All @@ -11,7 +37,7 @@ const createPropType = (name, args, { isRequired = false } = {}) => {
propType.push({ name: 'isRequired' });
}

return { __reflect__: propType };
return { __reflect__: propType, __docs__: createDocs(name, docs) };
};

describe('getNormalizedTypeName', () => {
Expand Down Expand Up @@ -233,3 +259,48 @@ describe('getDefaultValue', () => {
expect(getDefaultValue(component, 'gap')).toEqual('Grid.GAP.SMALL');
});
});

describe('getTypeMeta', () => {
[
'any',
'array',
'element',
'elementType',
'number',
'node',
'object',
'string',
'symbol',
].forEach((type) => {
test(`returns null for ${type} types`, () => {
const propType = createPropType(type);
const component = {
propTypes: {
[type]: propType,
},
};

expect(getTypeMeta(type, propType, { component })).toBeNull();
});
});

test('returns function information for func types', () => {
const propType = createPropType('func', undefined, {
docs: {
description: 'A click handler',
params: [{ description: '', name: 'event', type: 'Event' }],
},
});

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

expect(getTypeMeta('onClick', propType, { component })).toEqual({
returnValue: { type: 'undefined' },
params: [{ description: '', name: 'event', type: 'Event' }],
});
});
});
14 changes: 14 additions & 0 deletions src/utils/propTypeInfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,20 @@ export const getDefaultValue = (component, propTypeName) => {
return defaultValue;
};

export const getTypeMeta = (name, propType, { component }) => {
const propTypeDocs = propType.__docs__;

switch (getRawTypeName(propType)) {
case 'func':
return {
returnValue: propTypeDocs.tags?.returnValue ?? { type: 'undefined' },
params: propTypeDocs.tags?.param,
};
default:
return null;
}
};

export const getPropTypeDefinition = (component, name, propType) => {
const propDocs = propType.__docs__;
const propMeta = propType.__reflect__;
Expand Down

0 comments on commit 004dde1

Please sign in to comment.