Skip to content

Commit

Permalink
chore: Add advanced case for getting prop info
Browse files Browse the repository at this point in the history
  • Loading branch information
jerelmiller committed Jun 7, 2020
1 parent 77022be commit bd79ea7
Show file tree
Hide file tree
Showing 2 changed files with 146 additions and 3 deletions.
147 changes: 145 additions & 2 deletions src/utils/__tests__/propTypeInfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,23 @@ import {
getTypeMeta,
} from '../propTypeInfo';

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

if (deprecation) {
docs.tags = {
deprecated: [deprecation],
};
}

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

if (returnValue) {
docs.tags.returns = returnValue;
Expand Down Expand Up @@ -460,4 +469,138 @@ describe('getTypeMeta', () => {
],
});
});

test('handles arrayOf union types', () => {
const SPACE = {
SMALL: 'sm',
MEDIUM: 'md',
LARGE: 'lg',
};
const propType = createPropType('arrayOf', [
createPropType('oneOf', [Object.values(SPACE)]),
]);

const component = {
name: 'Button',
propTypes: {
space: propType,
},
SPACE,
};

expect(getTypeMeta('space', propType, { component })).toEqual({
itemTypes: {
meta: {
constants: [
'Button.SPACE.SMALL',
'Button.SPACE.MEDIUM',
'Button.SPACE.LARGE',
],
},
raw: 'oneOf',
name: 'enum',
},
});
});

test('handles advanced case', () => {
const CRAZY = {
ONE: 1,
TWO: 2,
};

const propType = createPropType('oneOfType', [
[
createPropType('string'),
createPropType('arrayOf', [
createPropType('shape', [
{
name: createPropType('string', undefined, { isRequired: true }),
onHide: createPropType('func'),
onHidden: createPropType('func', undefined, {
docs: {
deprecation: {
date: 1591519180477,
description: 'Use onHide',
},
},
}),
},
]),
]),
createPropType('oneOf', [Object.values(CRAZY)]),
],
]);

const component = {
name: 'Wacky',
propTypes: {
crazy: propType,
},
CRAZY,
};

expect(getTypeMeta('crazy', propType, { component })).toEqual({
types: [
null,
{
itemTypes: {
meta: {
types: [
{
name: 'name',
defaultValue: undefined,
description: undefined,
deprecation: null,
isRequired: true,
type: {
meta: null,
raw: 'string',
name: 'string',
},
},
{
name: 'onHide',
defaultValue: undefined,
description: undefined,
deprecation: null,
isRequired: false,
type: {
meta: {
returnValue: { type: 'undefined' },
params: [],
},
raw: 'func',
name: 'function',
},
},
{
name: 'onHidden',
defaultValue: undefined,
description: undefined,
deprecation: null,
isRequired: false,
deprecation: {
date: 1591519180477,
description: 'Use onHide',
},
type: {
meta: {
returnValue: { type: 'undefined' },
params: [],
},
raw: 'func',
name: 'function',
},
},
],
},
raw: 'shape',
name: 'shape',
},
},
{ constants: ['Wacky.CRAZY.ONE', 'Wacky.CRAZY.TWO'] },
],
});
});
});
2 changes: 1 addition & 1 deletion src/utils/propTypeInfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ export const getTypeMeta = (name, propType, { component }) => {
case 'func':
return {
returnValue: propTypeDocs.tags?.returnValue ?? { type: 'undefined' },
params: propTypeDocs.tags?.param,
params: propTypeDocs.tags?.param ?? [],
};
case 'shape': {
const [shape] = getArgs(propType);
Expand Down

0 comments on commit bd79ea7

Please sign in to comment.