GeezerVison is showing github.com — test(utils): add more test case for checkPropTypes · cdlinsen/lowcode-engine@947c621
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: packages/utils/src/check-prop-types.ts +7-3Lines changed: 7 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -8,13 +8,13 @@ import { Logger } from './logger'; | 8 | 8 | const PropTypes2 = factoryWithTypeCheckers(ReactIs.isElement, true); | 9 | 9 | const logger = new Logger({ level: 'warn', bizName: 'utils' }); | 10 | 10 |
| 11 | | -export function transformPropTypesRuleToString(rule: IPublicTypePropType): string { | | 11 | +export function transformPropTypesRuleToString(rule: IPublicTypePropType | string): string { | 12 | 12 | if (!rule) { | 13 | 13 | return 'PropTypes.any'; | 14 | 14 | } | 15 | 15 |
| 16 | 16 | if (typeof rule === 'string') { | 17 | | - return `PropTypes.${rule}`; | | 17 | + return rule.startsWith('PropTypes.') ? rule : `PropTypes.${rule}`; | 18 | 18 | } | 19 | 19 |
| 20 | 20 | if (isRequiredPropType(rule)) { | @@ -34,7 +34,11 @@ export function transformPropTypesRuleToString(rule: IPublicTypePropType): strin | 34 | 34 | case 'shape': | 35 | 35 | case 'exact': | 36 | 36 | return `PropTypes.${type}({${value.map((item: any) => `${item.name}: ${transformPropTypesRuleToString(item.propType)}`).join(',')}})`; | | 37 | + default: | | 38 | + logger.error(`Unknown prop type: ${type}`); | 37 | 39 | } | | 40 | + | | 41 | + return 'PropTypes.any'; | 38 | 42 | } | 39 | 43 |
| 40 | 44 | export function checkPropTypes(value: any, name: string, rule: any, componentName: string): boolean { | @@ -45,7 +49,7 @@ export function checkPropTypes(value: any, name: string, rule: any, componentNam | 45 | 49 | } | 46 | 50 | if (typeof rule === 'string') { | 47 | 51 | // eslint-disable-next-line no-new-func | 48 | | - ruleFunction = new Function(`"use strict"; const PropTypes = arguments[0]; return ${rule}`)(PropTypes2); | | 52 | + ruleFunction = new Function(`"use strict"; const PropTypes = arguments[0]; return ${transformPropTypesRuleToString(rule)}`)(PropTypes2); | 49 | 53 | } | 50 | 54 | if (!ruleFunction || typeof ruleFunction !== 'function') { | 51 | 55 | logger.warn('checkPropTypes should have a function type rule argument'); | |
Collapse file Copy file name to clipboardExpand all lines: packages/utils/test/src/check-prop-types.test.ts +65Lines changed: 65 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -12,6 +12,26 @@ describe('checkPropTypes', () => { | 12 | 12 | expect(checkPropTypes('123', 'age', PropTypes.number, 'TestComponent')).toBe(false); | 13 | 13 | }); | 14 | 14 |
| | 15 | + it('should validate correctly with valid object prop type', () => { | | 16 | + expect(checkPropTypes({ a: 123 }, 'age', PropTypes.object, 'TestComponent')).toBe(true); | | 17 | + expect(checkPropTypes({ a: '123' }, 'age', PropTypes.object, 'TestComponent')).toBe(true); | | 18 | + }); | | 19 | + | | 20 | + it('should validate correctly with valid object string prop type', () => { | | 21 | + expect(checkPropTypes({ a: 123 }, 'age', 'object', 'TestComponent')).toBe(true); | | 22 | + expect(checkPropTypes({ a: '123' }, 'age', 'object', 'TestComponent')).toBe(true); | | 23 | + }); | | 24 | + | | 25 | + it('should validate correctly with valid isRequired prop type', () => { | | 26 | + const rule = { | | 27 | + type: 'string', | | 28 | + isRequired: true, | | 29 | + }; | | 30 | + expect(transformPropTypesRuleToString(rule)).toBe('PropTypes.string.isRequired'); | | 31 | + expect(checkPropTypes('News', 'type', rule, 'TestComponent')).toBe(true); | | 32 | + expect(checkPropTypes(undefined, 'type', rule, 'TestComponent')).toBe(false); | | 33 | + }); | | 34 | + | 15 | 35 | it('should handle custom rule functions correctly', () => { | 16 | 36 | const customRule = (props, propName) => { | 17 | 37 | if (props[propName] !== 123) { | @@ -28,6 +48,11 @@ describe('checkPropTypes', () => { | 28 | 48 | expect(result).toBe(true); | 29 | 49 | }); | 30 | 50 |
| | 51 | + it('should interpret and validate a rule given as a string', () => { | | 52 | + expect(checkPropTypes(123, 'age', 'number', 'TestComponent')).toBe(true); | | 53 | + expect(checkPropTypes('123', 'age', 'string', 'TestComponent')).toBe(true); | | 54 | + }); | | 55 | + | 31 | 56 | it('should log a warning for invalid rule type', () => { | 32 | 57 | const result = checkPropTypes(123, 'age', 123, 'TestComponent'); | 33 | 58 | expect(result).toBe(true); | @@ -60,6 +85,46 @@ describe('checkPropTypes', () => { | 60 | 85 | expect(checkPropTypes({}, 'type', rule, 'TestComponent')).toBe(false); | 61 | 86 | }); | 62 | 87 |
| | 88 | + it('should validate correctly with valid oneOfType prop type', () => { | | 89 | + const rule = { | | 90 | + type: 'oneOfType', | | 91 | + value: [ | | 92 | + 'bool', | | 93 | + { | | 94 | + type: 'shape', | | 95 | + value: [ | | 96 | + { | | 97 | + name: 'type', | | 98 | + propType: { | | 99 | + type: 'oneOf', | | 100 | + value: ['JSExpression'], | | 101 | + } | | 102 | + }, | | 103 | + { | | 104 | + name: 'value', | | 105 | + propType: 'string', | | 106 | + }, | | 107 | + ], | | 108 | + }, | | 109 | + ], | | 110 | + }; | | 111 | + expect(transformPropTypesRuleToString(rule)).toBe('PropTypes.oneOfType([PropTypes.bool, PropTypes.shape({type: PropTypes.oneOf(["JSExpression"]),value: PropTypes.string})])'); | | 112 | + expect(checkPropTypes(true, 'type', rule, 'TestComponent')).toBe(true); | | 113 | + expect(checkPropTypes({ type: 'JSExpression', value: '1 + 1 === 2' }, 'type', rule, 'TestComponent')).toBe(true); | | 114 | + expect(checkPropTypes({ type: 'JSExpression' }, 'type', rule, 'TestComponent')).toBe(true); | | 115 | + expect(checkPropTypes({ type: 'JSExpression', value: 123 }, 'type', rule, 'TestComponent')).toBe(false); | | 116 | + }); | | 117 | + | | 118 | + it('should log a warning for invalid type', () => { | | 119 | + const rule = { | | 120 | + type: 'inval', | | 121 | + value: ['News', 'Photos'], | | 122 | + } | | 123 | + expect(transformPropTypesRuleToString(rule)).toBe('PropTypes.any'); | | 124 | + expect(checkPropTypes('News', 'type', rule, 'TestComponent')).toBe(true); | | 125 | + expect(checkPropTypes('Others', 'type', rule, 'TestComponent')).toBe(true); | | 126 | + }); | | 127 | + | 63 | 128 | // arrayOf | 64 | 129 | it('should validate correctly with valid arrayOf prop type', () => { | 65 | 130 | const rule = { | |
|