mirror of
https://github.com/alibaba/lowcode-engine.git
synced 2026-01-19 05:48:17 +00:00
test: 补充designer单测
This commit is contained in:
parent
33f0fb43c9
commit
bac94d53ab
@ -10,7 +10,8 @@
|
|||||||
],
|
],
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"build": "build-scripts build --skip-demo",
|
"build": "build-scripts build --skip-demo",
|
||||||
"test": "build-scripts test --config build.test.json"
|
"test": "build-scripts test --config build.test.json",
|
||||||
|
"test:cov": "build-scripts test --config build.test.json --jest-coverage"
|
||||||
},
|
},
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
|||||||
@ -407,7 +407,7 @@ export class DocumentModel {
|
|||||||
* 是否已修改
|
* 是否已修改
|
||||||
*/
|
*/
|
||||||
isModified() {
|
isModified() {
|
||||||
return !this.history.isSavePoint();
|
return this.history.isSavePoint();
|
||||||
}
|
}
|
||||||
|
|
||||||
// FIXME: does needed?
|
// FIXME: does needed?
|
||||||
|
|||||||
@ -185,9 +185,14 @@ export class History {
|
|||||||
this.emitter.removeAllListeners();
|
this.emitter.removeAllListeners();
|
||||||
this.records = [];
|
this.records = [];
|
||||||
}
|
}
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @deprecated
|
||||||
|
* @returns
|
||||||
|
* @memberof History
|
||||||
|
*/
|
||||||
isModified() {
|
isModified() {
|
||||||
return this.point !== this.session.cursor;
|
return this.isSavePoint();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -58,7 +58,7 @@ export class Project {
|
|||||||
// TODO: future change this filter
|
// TODO: future change this filter
|
||||||
componentsMap: this.currentDocument?.getComponentsMap(),
|
componentsMap: this.currentDocument?.getComponentsMap(),
|
||||||
componentsTree: this.documents.filter((doc) => !doc.isBlank()).map((doc) => doc.schema),
|
componentsTree: this.documents.filter((doc) => !doc.isBlank()).map((doc) => doc.schema),
|
||||||
i18n: this._i18n || {},
|
i18n: this.i18n,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -99,6 +99,7 @@ export class Project {
|
|||||||
const documentInstances = this.data.componentsTree.map((data) => this.createDocument(data));
|
const documentInstances = this.data.componentsTree.map((data) => this.createDocument(data));
|
||||||
// TODO: 暂时先读 config tabBar 里的值,后面看整个 layout 结构是否能作为引擎规范
|
// TODO: 暂时先读 config tabBar 里的值,后面看整个 layout 结构是否能作为引擎规范
|
||||||
if (this.config?.layout?.props?.tabBar?.items?.length > 0) {
|
if (this.config?.layout?.props?.tabBar?.items?.length > 0) {
|
||||||
|
// slice(1)这个贼不雅,默认任务fileName 是类'/fileName'的形式
|
||||||
documentInstances.find((i) => i.fileName === this.config.layout.props.tabBar.items[0].path?.slice(1))?.open();
|
documentInstances.find((i) => i.fileName === this.config.layout.props.tabBar.items[0].path?.slice(1))?.open();
|
||||||
} else {
|
} else {
|
||||||
documentInstances[0].open();
|
documentInstances[0].open();
|
||||||
@ -222,13 +223,14 @@ export class Project {
|
|||||||
return null;
|
return null;
|
||||||
} else if (isDocumentModel(doc)) {
|
} else if (isDocumentModel(doc)) {
|
||||||
return doc.open();
|
return doc.open();
|
||||||
} else if (isPageSchema(doc)) {
|
}
|
||||||
|
// else if (isPageSchema(doc)) {
|
||||||
// 暂时注释掉,影响了 diff 功能
|
// 暂时注释掉,影响了 diff 功能
|
||||||
// const foundDoc = this.documents.find(curDoc => curDoc?.rootNode?.id && curDoc?.rootNode?.id === doc?.id);
|
// const foundDoc = this.documents.find(curDoc => curDoc?.rootNode?.id && curDoc?.rootNode?.id === doc?.id);
|
||||||
// if (foundDoc) {
|
// if (foundDoc) {
|
||||||
// foundDoc.remove();
|
// foundDoc.remove();
|
||||||
// }
|
// }
|
||||||
}
|
// }
|
||||||
|
|
||||||
doc = this.createDocument(doc);
|
doc = this.createDocument(doc);
|
||||||
return doc.open();
|
return doc.open();
|
||||||
|
|||||||
@ -1148,6 +1148,16 @@ Object {
|
|||||||
"sync": true,
|
"sync": true,
|
||||||
},
|
},
|
||||||
"hidden": false,
|
"hidden": false,
|
||||||
|
"i18n": Object {
|
||||||
|
"en-US": Object {
|
||||||
|
"i18n-jwg27yo3": "China",
|
||||||
|
"i18n-jwg27yo4": "Hello",
|
||||||
|
},
|
||||||
|
"zh-CN": Object {
|
||||||
|
"i18n-jwg27yo3": "中国",
|
||||||
|
"i18n-jwg27yo4": "你好",
|
||||||
|
},
|
||||||
|
},
|
||||||
"id": "page",
|
"id": "page",
|
||||||
"isLocked": false,
|
"isLocked": false,
|
||||||
"lifeCycles": Object {
|
"lifeCycles": Object {
|
||||||
|
|||||||
@ -29,7 +29,7 @@ describe('document-model 测试', () => {
|
|||||||
expect(doc.currentRoot).toBe(doc.rootNode);
|
expect(doc.currentRoot).toBe(doc.rootNode);
|
||||||
expect(doc.root).toBe(doc.rootNode);
|
expect(doc.root).toBe(doc.rootNode);
|
||||||
expect(doc.modalNode).toBeUndefined();
|
expect(doc.modalNode).toBeUndefined();
|
||||||
expect(doc.isBlank()).toBeFalsy();
|
expect(doc.isBlank()).toBeTruthy();
|
||||||
expect(doc.schema).toEqual({
|
expect(doc.schema).toEqual({
|
||||||
componentName: 'Page',
|
componentName: 'Page',
|
||||||
condition: true,
|
condition: true,
|
||||||
|
|||||||
10
packages/designer/tests/fixtures/schema/form.ts
vendored
10
packages/designer/tests/fixtures/schema/form.ts
vendored
@ -980,4 +980,14 @@ export default {
|
|||||||
condition: true,
|
condition: true,
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
|
i18n: {
|
||||||
|
'zh-CN': {
|
||||||
|
'i18n-jwg27yo4': '你好',
|
||||||
|
'i18n-jwg27yo3': '中国',
|
||||||
|
},
|
||||||
|
'en-US': {
|
||||||
|
'i18n-jwg27yo4': 'Hello',
|
||||||
|
'i18n-jwg27yo3': 'China',
|
||||||
|
},
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|||||||
@ -3,8 +3,6 @@ import cloneDeep from 'lodash/cloneDeep';
|
|||||||
import '../fixtures/window';
|
import '../fixtures/window';
|
||||||
import { Editor } from '@ali/lowcode-editor-core';
|
import { Editor } from '@ali/lowcode-editor-core';
|
||||||
import { Project } from '../../src/project/project';
|
import { Project } from '../../src/project/project';
|
||||||
import { DocumentModel } from '../../src/document/document-model';
|
|
||||||
import { Node } from '../../src/document/node/node';
|
|
||||||
import { Designer } from '../../src/designer/designer';
|
import { Designer } from '../../src/designer/designer';
|
||||||
import formSchema from '../fixtures/schema/form';
|
import formSchema from '../fixtures/schema/form';
|
||||||
import { getIdsFromSchema, getNodeFromSchemaById } from '../utils';
|
import { getIdsFromSchema, getNodeFromSchemaById } from '../utils';
|
||||||
@ -32,6 +30,7 @@ jest.mock('../../src/designer/designer', () => {
|
|||||||
let designer = null;
|
let designer = null;
|
||||||
beforeAll(() => {
|
beforeAll(() => {
|
||||||
designer = new Designer({});
|
designer = new Designer({});
|
||||||
|
designer.editor = new Editor();
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('schema 生成节点模型测试', () => {
|
describe('schema 生成节点模型测试', () => {
|
||||||
@ -66,6 +65,133 @@ describe('schema 生成节点模型测试', () => {
|
|||||||
expect(mockCreateSettingEntry).toBeCalledTimes(expectedNodeCnt);
|
expect(mockCreateSettingEntry).toBeCalledTimes(expectedNodeCnt);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('onSimulatorReady works', () => {
|
||||||
|
const project = new Project(designer, {
|
||||||
|
componentsTree: [
|
||||||
|
formSchema,
|
||||||
|
],
|
||||||
|
});
|
||||||
|
project.open();
|
||||||
|
expect(project).toBeTruthy();
|
||||||
|
const mockCallback = jest.fn();
|
||||||
|
const removeListener = project.onSimulatorReady(mockCallback);
|
||||||
|
project.mountSimulator(undefined);
|
||||||
|
expect(mockCallback).toBeCalled();
|
||||||
|
removeListener();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('open doc when doc is blank', () => {
|
||||||
|
const project = new Project(designer);
|
||||||
|
project.open();
|
||||||
|
expect(project).toBeTruthy();
|
||||||
|
const blankDoc = project.documents[0];
|
||||||
|
expect(blankDoc).toBeTruthy();
|
||||||
|
// 触发保存
|
||||||
|
blankDoc.history.savePoint();
|
||||||
|
expect(blankDoc.isModified()).toBeFalsy();
|
||||||
|
expect(blankDoc.isBlank()).toBeTruthy();
|
||||||
|
|
||||||
|
//二次打开doc,会使用前面那个
|
||||||
|
const openedDoc = project.open();
|
||||||
|
expect(openedDoc).toBe(blankDoc);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('load schema with autoOpen === true', () => {
|
||||||
|
const project = new Project(designer);
|
||||||
|
expect(project).toBeTruthy();
|
||||||
|
// trigger autoOpen case
|
||||||
|
project.load({
|
||||||
|
componentsTree: [
|
||||||
|
formSchema,
|
||||||
|
],
|
||||||
|
}, true);
|
||||||
|
const { currentDocument } = project;
|
||||||
|
const { nodesMap } = currentDocument;
|
||||||
|
const ids = getIdsFromSchema(formSchema);
|
||||||
|
const expectedNodeCnt = ids.length;
|
||||||
|
expect(nodesMap.size).toBe(expectedNodeCnt);
|
||||||
|
ids.forEach(id => {
|
||||||
|
expect(nodesMap.get(id).componentName).toBe(getNodeFromSchemaById(formSchema, id).componentName);
|
||||||
|
});
|
||||||
|
|
||||||
|
const exportSchema = currentDocument?.export(1);
|
||||||
|
expect(getIdsFromSchema(exportSchema).length).toBe(expectedNodeCnt);
|
||||||
|
nodesMap.forEach(node => {
|
||||||
|
// 触发 getter
|
||||||
|
node.settingEntry;
|
||||||
|
});
|
||||||
|
expect(mockCreateSettingEntry).toBeCalledTimes(expectedNodeCnt);
|
||||||
|
});
|
||||||
|
it('load schema with autoOpen === true, and config contains layout.props.tabBar.item', () => {
|
||||||
|
const project = new Project(designer);
|
||||||
|
expect(project).toBeTruthy();
|
||||||
|
// trigger autoOpen case
|
||||||
|
project.load({
|
||||||
|
componentsTree: [
|
||||||
|
{
|
||||||
|
...formSchema,
|
||||||
|
fileName: 'demoFile1',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
...formSchema,
|
||||||
|
fileName: 'demoFile2',
|
||||||
|
}
|
||||||
|
],
|
||||||
|
config: {
|
||||||
|
layout: {
|
||||||
|
props: {
|
||||||
|
tabBar: {
|
||||||
|
items: [
|
||||||
|
{
|
||||||
|
path: '/demoFile2',
|
||||||
|
}
|
||||||
|
],
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}, true);
|
||||||
|
const { currentDocument } = project;
|
||||||
|
expect(currentDocument.fileName).toBe('demoFile2');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('load schema with autoOpen === true', () => {
|
||||||
|
const project = new Project(designer);
|
||||||
|
expect(project).toBeTruthy();
|
||||||
|
// trigger autoOpen case
|
||||||
|
project.load({
|
||||||
|
componentsTree: [
|
||||||
|
{
|
||||||
|
...formSchema,
|
||||||
|
fileName: 'demoFile1',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
...formSchema,
|
||||||
|
fileName: 'demoFile2',
|
||||||
|
}
|
||||||
|
],
|
||||||
|
}, 'demoFile2');
|
||||||
|
const { currentDocument } = project;
|
||||||
|
expect(currentDocument.fileName).toBe('demoFile2');
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
it('setSchema works', () => {
|
||||||
|
const project = new Project(designer);
|
||||||
|
project.open();
|
||||||
|
expect(project).toBeTruthy();
|
||||||
|
project.setSchema({
|
||||||
|
componentsTree: [
|
||||||
|
{
|
||||||
|
...formSchema,
|
||||||
|
fileName: 'demoFile1',
|
||||||
|
},
|
||||||
|
],
|
||||||
|
});
|
||||||
|
const { currentDocument } = project;
|
||||||
|
expect(currentDocument.fileName).toBe('demoFile1');
|
||||||
|
});
|
||||||
|
|
||||||
it('基本的节点模型初始化,模型导出,project.open 传入 schema', () => {
|
it('基本的节点模型初始化,模型导出,project.open 传入 schema', () => {
|
||||||
const project = new Project(designer);
|
const project = new Project(designer);
|
||||||
project.open(formSchema);
|
project.open(formSchema);
|
||||||
@ -115,6 +241,29 @@ describe('schema 生成节点模型测试', () => {
|
|||||||
|
|
||||||
expect(documents).toHaveLength(0);
|
expect(documents).toHaveLength(0);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('get unknown document', () => {
|
||||||
|
const project = new Project(designer);
|
||||||
|
project.open(formSchema);
|
||||||
|
expect(project).toBeTruthy();
|
||||||
|
expect(project.getDocument('unknownId')).toBeNull();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('get set i18n works', () => {
|
||||||
|
const project = new Project(designer);
|
||||||
|
project.open(formSchema);
|
||||||
|
expect(project).toBeTruthy();
|
||||||
|
|
||||||
|
project.i18n = formSchema.i18n;
|
||||||
|
expect(project.i18n).toBe(formSchema.i18n);
|
||||||
|
project.i18n = null;
|
||||||
|
expect(project.i18n).toStrictEqual({});
|
||||||
|
|
||||||
|
project.set('i18n', formSchema.i18n);
|
||||||
|
expect(project.get('i18n')).toBe(formSchema.i18n);
|
||||||
|
project.set('i18n', null);
|
||||||
|
expect(project.get('i18n')).toStrictEqual({});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('block ❌ | component ❌ | slot ✅', () => {
|
describe('block ❌ | component ❌ | slot ✅', () => {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user