From bd79ea78235f39a42b9ea2a57d69b0ae58596051 Mon Sep 17 00:00:00 2001 From: Jerel Miller Date: Sun, 7 Jun 2020 01:46:11 -0700 Subject: [PATCH] chore: Add advanced case for getting prop info --- src/utils/__tests__/propTypeInfo.js | 147 +++++++++++++++++++++++++++- src/utils/propTypeInfo.js | 2 +- 2 files changed, 146 insertions(+), 3 deletions(-) diff --git a/src/utils/__tests__/propTypeInfo.js b/src/utils/__tests__/propTypeInfo.js index 38b036cb8..a1ad8a741 100644 --- a/src/utils/__tests__/propTypeInfo.js +++ b/src/utils/__tests__/propTypeInfo.js @@ -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; @@ -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'] }, + ], + }); + }); }); diff --git a/src/utils/propTypeInfo.js b/src/utils/propTypeInfo.js index fdaa96532..0bac6ad66 100644 --- a/src/utils/propTypeInfo.js +++ b/src/utils/propTypeInfo.js @@ -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);