Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(render-core): update logger to console & add test ut #2646

Merged
merged 1 commit into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/designer/src/builtin-simulator/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export * from './host';
export * from './host-view';
export * from './renderer';
export * from './live-editing/live-editing';
export { LowcodeTypes } from './utils/parse-metadata';
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,15 @@ function define(propType: any = PropTypes.any, lowcodeType: string | object = {}
return lowcodeCheckType;
}

const LowcodeTypes: any = {
export const LowcodeTypes: any = {
...PropTypes,
define,
};

(window as any).PropTypes = LowcodeTypes;
(window as any).React.PropTypes = LowcodeTypes;
if ((window as any).React) {
(window as any).React.PropTypes = LowcodeTypes;
}

// override primitive type checkers
primitiveTypes.forEach((type) => {
Expand Down
1 change: 1 addition & 0 deletions packages/renderer-core/babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('../../babel.config');
3 changes: 2 additions & 1 deletion packages/renderer-core/src/hoc/leaf.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { isReactComponent, cloneEnumerableProperty } from '@alilc/lowcode-utils'
import { debounce } from '../utils/common';
import adapter from '../adapter';
import * as types from '../types/index';
import logger from '../utils/logger';

export interface IComponentHocInfo {
schema: any;
Expand Down Expand Up @@ -183,7 +184,7 @@ export function leafWrapper(Comp: types.IBaseRenderComponent, {
}

if (!isReactComponent(Comp)) {
console.error(`${schema.componentName} component may be has errors: `, Comp);
logger.error(`${schema.componentName} component may be has errors: `, Comp);
}

initRerenderEvent({
Expand Down
3 changes: 2 additions & 1 deletion packages/renderer-core/src/renderer/addon.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import PropTypes from 'prop-types';
import baseRendererFactory from './base';
import { isEmpty } from '../utils';
import { IRendererAppHelper, IBaseRendererProps, IBaseRenderComponent } from '../types';
import logger from '../utils/logger';

export default function addonRendererFactory(): IBaseRenderComponent {
const BaseRenderer = baseRendererFactory();
Expand Down Expand Up @@ -32,7 +33,7 @@ export default function addonRendererFactory(): IBaseRenderComponent {
const schema = props.__schema || {};
this.state = this.__parseData(schema.state || {});
if (isEmpty(props.config) || !props.config?.addonKey) {
console.warn('lce addon has wrong config');
logger.warn('lce addon has wrong config');
this.setState({
__hasError: true,
});
Expand Down
14 changes: 7 additions & 7 deletions packages/renderer-core/src/renderer/base.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,14 @@ export function executeLifeCycleMethod(context: any, schema: IPublicTypeNodeSche
}

if (typeof fn !== 'function') {
console.error(`生命周期${method}类型不符`, fn);
logger.error(`生命周期${method}类型不符`, fn);
return;
}

try {
return fn.apply(context, args);
} catch (e) {
console.error(`[${schema.componentName}]生命周期${method}出错`, e);
logger.error(`[${schema.componentName}]生命周期${method}出错`, e);
}
}

Expand Down Expand Up @@ -208,7 +208,7 @@ export default function baseRendererFactory(): IBaseRenderComponent {

async componentDidCatch(...args: any[]) {
this.__executeLifeCycleMethod('componentDidCatch', args);
console.warn(args);
logger.warn(args);
}

reloadDataSource = () => new Promise((resolve, reject) => {
Expand Down Expand Up @@ -278,7 +278,7 @@ export default function baseRendererFactory(): IBaseRenderComponent {
value = this.__parseExpression(value, this);
}
if (typeof value !== 'function') {
console.error(`custom method ${key} can not be parsed to a valid function`, value);
logger.error(`custom method ${key} can not be parsed to a valid function`, value);
return;
}
this[key] = value.bind(this);
Expand Down Expand Up @@ -369,7 +369,7 @@ export default function baseRendererFactory(): IBaseRenderComponent {
this.setLocale = (loc: string) => {
const setLocaleFn = this.appHelper?.utils?.i18n?.setLocale;
if (!setLocaleFn || typeof setLocaleFn !== 'function') {
console.warn('initI18nAPIs Failed, i18n only works when appHelper.utils.i18n.setLocale() exists');
logger.warn('initI18nAPIs Failed, i18n only works when appHelper.utils.i18n.setLocale() exists');
return undefined;
}
return setLocaleFn(loc);
Expand Down Expand Up @@ -527,7 +527,7 @@ export default function baseRendererFactory(): IBaseRenderComponent {
: {};

if (!Comp) {
console.error(`${schema.componentName} component is not found in components list! component list is:`, components || this.props.__container?.components);
logger.error(`${schema.componentName} component is not found in components list! component list is:`, components || this.props.__container?.components);
return engine.createElement(
engine.getNotFoundComponent(),
{
Expand Down Expand Up @@ -749,7 +749,7 @@ export default function baseRendererFactory(): IBaseRenderComponent {

__createLoopVirtualDom = (schema: IPublicTypeNodeSchema, scope: any, parentInfo: INodeInfo, idx: number | string) => {
if (isFileSchema(schema)) {
console.warn('file type not support Loop');
logger.warn('file type not support Loop');
return null;
}
if (!Array.isArray(schema.loop)) {
Expand Down
3 changes: 2 additions & 1 deletion packages/renderer-core/src/renderer/temp.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { IBaseRenderComponent } from '../types';
import logger from '../utils/logger';
import baseRendererFactory from './base';

export default function tempRendererFactory(): IBaseRenderComponent {
Expand Down Expand Up @@ -41,7 +42,7 @@ export default function tempRendererFactory(): IBaseRenderComponent {
}

async componentDidCatch(e: any) {
console.warn(e);
logger.warn(e);
this.__debug(`componentDidCatch - ${this.props.__schema.fileName}`);
}

Expand Down
6 changes: 3 additions & 3 deletions packages/renderer-core/src/utils/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,13 +183,13 @@ export function transformArrayToMap(arr: any[], key: string, overwrite = true) {
return res;
}

export function checkPropTypes(value: any, name: string, rule: any, componentName: string) {
export function checkPropTypes(value: any, name: string, rule: any, componentName: string): boolean {
let ruleFunction = rule;
if (typeof rule === 'string') {
ruleFunction = new Function(`"use strict"; const PropTypes = arguments[0]; return ${rule}`)(PropTypes2);
}
if (!ruleFunction || typeof ruleFunction !== 'function') {
console.warn('checkPropTypes should have a function type rule argument');
logger.warn('checkPropTypes should have a function type rule argument');
return true;
}
const err = ruleFunction(
Expand All @@ -203,7 +203,7 @@ export function checkPropTypes(value: any, name: string, rule: any, componentNam
ReactPropTypesSecret,
);
if (err) {
console.warn(err);
logger.warn(err);
}
return !err;
}
Expand Down
10 changes: 5 additions & 5 deletions packages/renderer-core/src/utils/data-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ export class DataHelper {
}
const { headers, ...otherProps } = otherOptionsObj || {};
if (!req) {
console.warn(`getDataSource API named ${id} not exist`);
logger.warn(`getDataSource API named ${id} not exist`);
return;
}

Expand Down Expand Up @@ -215,15 +215,15 @@ export class DataHelper {
try {
callbackFn && callbackFn(res && res[id]);
} catch (e) {
console.error('load请求回调函数报错', e);
logger.error('load请求回调函数报错', e);
}
return res && res[id];
})
.catch((err) => {
try {
callbackFn && callbackFn(null, err);
} catch (e) {
console.error('load请求回调函数报错', e);
logger.error('load请求回调函数报错', e);
}
return err;
});
Expand Down Expand Up @@ -300,9 +300,9 @@ export class DataHelper {
return dataHandlerFun.call(this.host, data, error);
} catch (e) {
if (id) {
console.error(`[${id}]单个请求数据处理函数运行出错`, e);
logger.error(`[${id}]单个请求数据处理函数运行出错`, e);
} else {
console.error('请求数据处理函数运行出错', e);
logger.error('请求数据处理函数运行出错', e);
}
}
}
Expand Down
40 changes: 39 additions & 1 deletion packages/renderer-core/tests/utils/common.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// @ts-nocheck
import factoryWithTypeCheckers from 'prop-types/factoryWithTypeCheckers';
import {
isSchema,
isFileSchema,
Expand All @@ -18,9 +18,14 @@ import {
parseThisRequiredExpression,
parseI18n,
parseData,
checkPropTypes,
} from '../../src/utils/common';
import logger from '../../src/utils/logger';

var ReactIs = require('react-is');

const PropTypes = factoryWithTypeCheckers(ReactIs.isElement, true);

describe('test isSchema', () => {
it('should be false when empty value is passed', () => {
expect(isSchema(null)).toBeFalsy();
Expand Down Expand Up @@ -461,4 +466,37 @@ describe('test parseData ', () => {
expect(result.__privateKey).toBeUndefined();

});
});

describe('checkPropTypes', () => {
it('should validate correctly with valid prop type', () => {
expect(checkPropTypes(123, 'age', PropTypes.number, 'TestComponent')).toBe(true);
expect(checkPropTypes('123', 'age', PropTypes.string, 'TestComponent')).toBe(true);
});

it('should log a warning and return false with invalid prop type', () => {
expect(checkPropTypes(123, 'age', PropTypes.string, 'TestComponent')).toBe(false);
expect(checkPropTypes('123', 'age', PropTypes.number, 'TestComponent')).toBe(false);
});

it('should handle custom rule functions correctly', () => {
const customRule = (props, propName) => {
if (props[propName] !== 123) {
return new Error('Invalid value');
}
};
const result = checkPropTypes(123, 'customProp', customRule, 'TestComponent');
expect(result).toBe(true);
});


it('should interpret and validate a rule given as a string', () => {
const result = checkPropTypes(123, 'age', 'PropTypes.number', 'TestComponent');
expect(result).toBe(true);
});

it('should log a warning for invalid rule type', () => {
const result = checkPropTypes(123, 'age', 123, 'TestComponent');
expect(result).toBe(true);
});
});
10 changes: 0 additions & 10 deletions packages/renderer-core/tests/utils/data-helper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -346,19 +346,13 @@ describe('test DataHelper ', () => {
result = dataHelper.handleData('fullConfigGet', mockDataHandler, { data: 'mockDataValue' }, null);
expect(result).toStrictEqual({ data: 'mockDataValue' });

// test exception
const mockError = jest.fn();
const orginalConsole = global.console;
global.console = { error: mockError };

// exception with id
mockDataHandler = {
type: 'JSFunction',
value: 'function(res) { return res.data + \'+\' + JSON.parse({a:1}); }',
};
result = dataHelper.handleData('fullConfigGet', mockDataHandler, { data: 'mockDataValue' }, null);
expect(result).toBeUndefined();
expect(mockError).toBeCalledWith('[fullConfigGet]单个请求数据处理函数运行出错', expect.anything());

// exception without id
mockDataHandler = {
Expand All @@ -367,12 +361,8 @@ describe('test DataHelper ', () => {
};
result = dataHelper.handleData(null, mockDataHandler, { data: 'mockDataValue' }, null);
expect(result).toBeUndefined();
expect(mockError).toBeCalledWith('请求数据处理函数运行出错', expect.anything());

global.console = orginalConsole;
});


it('updateConfig should work', () => {
const mockHost = { stateA: 'aValue'};
const mockDataSourceConfig = {
Expand Down
Loading