mirror of
https://github.com/alibaba/lowcode-engine.git
synced 2026-03-10 09:56:20 +00:00
chore: 重构部分 reducer
chore(test): 增加 bus / env / reducer 部分单测
This commit is contained in:
parent
0e5f7fdf49
commit
c30e56a09a
@ -13,7 +13,7 @@ import { deepValueParser } from './deep-value-parser';
|
|||||||
import { liveEditingRule, liveEditingSaveHander } from './vc-live-editing';
|
import { liveEditingRule, liveEditingSaveHander } from './vc-live-editing';
|
||||||
import {
|
import {
|
||||||
compatibleReducer,
|
compatibleReducer,
|
||||||
compatiblePageReducer,
|
upgradePageLifeCyclesReducer,
|
||||||
stylePropsReducer,
|
stylePropsReducer,
|
||||||
upgradePropsReducer,
|
upgradePropsReducer,
|
||||||
filterReducer,
|
filterReducer,
|
||||||
@ -54,7 +54,7 @@ designer.addPropsReducer(filterReducer, TransformStage.Render);
|
|||||||
// FIXME: Dirty fix, will remove this reducer
|
// FIXME: Dirty fix, will remove this reducer
|
||||||
designer.addPropsReducer(compatibleReducer, TransformStage.Save);
|
designer.addPropsReducer(compatibleReducer, TransformStage.Save);
|
||||||
// 兼容历史版本的 Page 组件
|
// 兼容历史版本的 Page 组件
|
||||||
designer.addPropsReducer(compatiblePageReducer, TransformStage.Save);
|
designer.addPropsReducer(upgradePageLifeCyclesReducer, TransformStage.Save);
|
||||||
|
|
||||||
// 设计器组件样式处理
|
// 设计器组件样式处理
|
||||||
designer.addPropsReducer(stylePropsReducer, TransformStage.Render);
|
designer.addPropsReducer(stylePropsReducer, TransformStage.Render);
|
||||||
|
|||||||
@ -1,4 +1,3 @@
|
|||||||
import { getConvertedExtraKey } from '@ali/lowcode-designer';
|
|
||||||
import {
|
import {
|
||||||
isPlainObject,
|
isPlainObject,
|
||||||
} from '@ali/lowcode-utils';
|
} from '@ali/lowcode-utils';
|
||||||
@ -35,17 +34,3 @@ export function compatibleReducer(props: any) {
|
|||||||
});
|
});
|
||||||
return newProps;
|
return newProps;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function compatiblePageReducer(props: any, node: Node) {
|
|
||||||
const lifeCycleNames = ['didMount', 'willUnmount'];
|
|
||||||
if (node.isRoot()) {
|
|
||||||
lifeCycleNames.forEach(key => {
|
|
||||||
if (props[key]) {
|
|
||||||
const lifeCycles = node.props.getPropValue(getConvertedExtraKey('lifeCycles')) || {};
|
|
||||||
lifeCycles[key] = props[key];
|
|
||||||
node.props.setPropValue(getConvertedExtraKey('lifeCycles'), lifeCycles);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
return props;
|
|
||||||
}
|
|
||||||
|
|||||||
@ -1,3 +1,4 @@
|
|||||||
|
import { getConvertedExtraKey } from '@ali/lowcode-designer';
|
||||||
import {
|
import {
|
||||||
isPlainObject,
|
isPlainObject,
|
||||||
} from '@ali/lowcode-utils';
|
} from '@ali/lowcode-utils';
|
||||||
@ -36,3 +37,17 @@ export function upgradePropsReducer(props: any) {
|
|||||||
});
|
});
|
||||||
return newProps;
|
return newProps;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function upgradePageLifeCyclesReducer(props: any, node: Node) {
|
||||||
|
const lifeCycleNames = ['didMount', 'willUnmount'];
|
||||||
|
if (node.isRoot()) {
|
||||||
|
lifeCycleNames.forEach(key => {
|
||||||
|
if (props[key]) {
|
||||||
|
const lifeCycles = node.props.getPropValue(getConvertedExtraKey('lifeCycles')) || {};
|
||||||
|
lifeCycles[key] = props[key];
|
||||||
|
node.props.setPropValue(getConvertedExtraKey('lifeCycles'), lifeCycles);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return props;
|
||||||
|
}
|
||||||
|
|||||||
@ -0,0 +1,107 @@
|
|||||||
|
import '../fixtures/window';
|
||||||
|
import { Node, Designer, getConvertedExtraKey } from '@ali/lowcode-designer';
|
||||||
|
import { Editor } from '@ali/lowcode-editor-core';
|
||||||
|
import {
|
||||||
|
upgradePropsReducer,
|
||||||
|
upgradePageLifeCyclesReducer,
|
||||||
|
} from '../../src/props-reducers/upgrade-reducer';
|
||||||
|
import formSchema from '../fixtures/schema/form';
|
||||||
|
|
||||||
|
describe('upgradePropsReducer 测试', () => {
|
||||||
|
it('upgradePropsReducer 测试', () => {
|
||||||
|
const props = {
|
||||||
|
a: {
|
||||||
|
type: 'JSBlock',
|
||||||
|
value: {
|
||||||
|
componentName: 'Slot',
|
||||||
|
props: {
|
||||||
|
slotTitle: '标题',
|
||||||
|
slotName: 'title',
|
||||||
|
},
|
||||||
|
children: [],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
b: {
|
||||||
|
type: 'JSBlock',
|
||||||
|
value: {
|
||||||
|
componentName: 'Div',
|
||||||
|
props: {},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
c: {
|
||||||
|
c1: {
|
||||||
|
type: 'JSBlock',
|
||||||
|
value: {
|
||||||
|
componentName: 'Slot',
|
||||||
|
props: {
|
||||||
|
slotTitle: '标题',
|
||||||
|
slotName: 'title',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
d: {
|
||||||
|
type: 'variable',
|
||||||
|
variable: 'state.a',
|
||||||
|
value: '111',
|
||||||
|
},
|
||||||
|
__slot__haha: true,
|
||||||
|
};
|
||||||
|
|
||||||
|
expect(upgradePropsReducer(props)).toEqual({
|
||||||
|
a: {
|
||||||
|
type: 'JSSlot',
|
||||||
|
title: '标题',
|
||||||
|
name: 'title',
|
||||||
|
value: [],
|
||||||
|
},
|
||||||
|
b: {
|
||||||
|
componentName: 'Div',
|
||||||
|
props: {},
|
||||||
|
},
|
||||||
|
c: {
|
||||||
|
c1: {
|
||||||
|
type: 'JSSlot',
|
||||||
|
title: '标题',
|
||||||
|
name: 'title',
|
||||||
|
value: undefined,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
d: {
|
||||||
|
type: 'JSExpression',
|
||||||
|
value: 'state.a',
|
||||||
|
mock: '111',
|
||||||
|
},
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('空值', () => {
|
||||||
|
expect(upgradePropsReducer(null)).toBeNull;
|
||||||
|
expect(upgradePropsReducer(undefined)).toBeUndefined;
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
const editor = new Editor();
|
||||||
|
const designer = new Designer({ editor });
|
||||||
|
designer.project.open(formSchema);
|
||||||
|
|
||||||
|
it('upgradePageLifeCyclesReducer 测试', () => {
|
||||||
|
const rootNode = designer.currentDocument?.rootNode;
|
||||||
|
const mockDidMount = jest.fn();
|
||||||
|
const mockWillUnmount = jest.fn();
|
||||||
|
upgradePageLifeCyclesReducer({
|
||||||
|
didMount: mockDidMount,
|
||||||
|
willUnmount: mockWillUnmount,
|
||||||
|
}, rootNode);
|
||||||
|
|
||||||
|
const lifeCycles = rootNode?.getPropValue(getConvertedExtraKey('lifeCycles'));
|
||||||
|
|
||||||
|
expect(typeof lifeCycles.didMount).toBe('function');
|
||||||
|
expect(typeof lifeCycles.willUnmount).toBe('function');
|
||||||
|
|
||||||
|
lifeCycles.didMount();
|
||||||
|
lifeCycles.willUnmount();
|
||||||
|
|
||||||
|
expect(mockDidMount).toHaveBeenCalled();
|
||||||
|
expect(mockWillUnmount).toHaveBeenCalled();
|
||||||
|
});
|
||||||
@ -2,8 +2,6 @@ import set from 'lodash/set';
|
|||||||
import cloneDeep from 'lodash/clonedeep';
|
import cloneDeep from 'lodash/clonedeep';
|
||||||
import '../fixtures/window';
|
import '../fixtures/window';
|
||||||
// import { Project } from '../../src/project/project';
|
// import { Project } from '../../src/project/project';
|
||||||
// import { Node } from '../../src/document/node/node';
|
|
||||||
// import { Designer } from '../../src/designer/designer';
|
|
||||||
import formSchema from '../fixtures/schema/form';
|
import formSchema from '../fixtures/schema/form';
|
||||||
import VisualEngine, {
|
import VisualEngine, {
|
||||||
designer,
|
designer,
|
||||||
|
|||||||
@ -63,7 +63,7 @@ describe('VisualEngine.Pages 相关 API 测试', () => {
|
|||||||
// slot 会多出(1 + N)个节点
|
// slot 会多出(1 + N)个节点
|
||||||
expect(doc.nodesMap.size).toBe(expectedNodeCnt + 2);
|
expect(doc.nodesMap.size).toBe(expectedNodeCnt + 2);
|
||||||
});
|
});
|
||||||
it.only('基本的节点模型初始化,初始化传入 schema,构造 prototype', () => {
|
it('基本的节点模型初始化,初始化传入 schema,构造 prototype', () => {
|
||||||
const proto = new Prototype(divPrototypeConfig);
|
const proto = new Prototype(divPrototypeConfig);
|
||||||
const doc = VisualEngine.Pages.addPage(pageSchema)!;
|
const doc = VisualEngine.Pages.addPage(pageSchema)!;
|
||||||
expect(doc).toBeTruthy();
|
expect(doc).toBeTruthy();
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user