Skip to content

Commit 8f237b1

Browse files
LeoYuanJackLian
authored andcommitted
test: increase branch coverage percentage of designer to 81.66%
1 parent 3dd0b6d commit 8f237b1

File tree

11 files changed

+807
-116
lines changed

11 files changed

+807
-116
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
],
77
"nohoist": [
88
"**/css-modules-typescript-loader",
9-
"**/@alife/theme-lowcode-*"
9+
"**/@alifc/theme-lowcode-*",
10+
"**/jest"
1011
]
1112
},
1213
"scripts": {

packages/designer/jest.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const jestConfig = {
1010
// // '^.+\\.(js|jsx)$': 'babel-jest',
1111
// },
1212
// testMatch: ['**/document/node/node.test.ts'],
13-
// testMatch: ['**/component-meta.test.ts'],
13+
// testMatch: ['**/history/history.test.ts'],
1414
// testMatch: ['**/plugin/plugin-manager.test.ts'],
1515
// testMatch: ['(/tests?/.*(test))\\.[jt]s$'],
1616
transformIgnorePatterns: [

packages/designer/src/document/history.ts

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,12 @@ import { autorun, reaction, mobx, untracked, globalContext, Editor } from '@alil
33
import { NodeSchema } from '@alilc/lowcode-types';
44
import { History as ShellHistory } from '@alilc/lowcode-shell';
55

6-
// TODO: cache to localStorage
7-
8-
export interface Serialization<T = any> {
9-
serialize(data: NodeSchema): T;
10-
unserialize(data: T): NodeSchema;
11-
}
12-
13-
let currentSerialization: Serialization<any> = {
14-
serialize(data: NodeSchema): string {
15-
return JSON.stringify(data);
16-
},
17-
unserialize(data: string) {
18-
return JSON.parse(data);
19-
},
20-
};
21-
22-
export function setSerialization(serialization: Serialization) {
23-
currentSerialization = serialization;
6+
export interface Serialization<K = NodeSchema, T = string> {
7+
serialize(data: K): T;
8+
unserialize(data: T): K;
249
}
2510

26-
export class History {
11+
export class History<T = NodeSchema> {
2712
private session: Session;
2813

2914
private records: Session[];
@@ -34,16 +19,29 @@ export class History {
3419

3520
private asleep = false;
3621

37-
constructor(logger: () => any, private redoer: (data: NodeSchema) => void, private timeGap: number = 1000) {
22+
private currentSerialization: Serialization<T, string> = {
23+
serialize(data: T): string {
24+
return JSON.stringify(data);
25+
},
26+
unserialize(data: string) {
27+
return JSON.parse(data);
28+
},
29+
};
30+
31+
setSerialization(serialization: Serialization<T, string>) {
32+
this.currentSerialization = serialization;
33+
}
34+
35+
constructor(dataFn: () => T, private redoer: (data: T) => void, private timeGap: number = 1000) {
3836
this.session = new Session(0, null, this.timeGap);
3937
this.records = [this.session];
4038

4139
reaction(() => {
42-
return logger();
43-
}, (data) => {
40+
return dataFn();
41+
}, (data: T) => {
4442
if (this.asleep) return;
4543
untracked(() => {
46-
const log = currentSerialization.serialize(data);
44+
const log = this.currentSerialization.serialize(data);
4745
if (this.session.isActive()) {
4846
this.session.log(log);
4947
} else {
@@ -98,9 +96,9 @@ export class History {
9896

9997
this.sleep();
10098
try {
101-
this.redoer(currentSerialization.unserialize(hotData));
99+
this.redoer(this.currentSerialization.unserialize(hotData));
102100
this.emitter.emit('cursor', hotData);
103-
} catch (e) {
101+
} catch (e) /* istanbul ignore next */ {
104102
console.error(e);
105103
}
106104

@@ -201,7 +199,7 @@ export class History {
201199
}
202200
}
203201

204-
class Session {
202+
export class Session {
205203
private _data: any;
206204

207205
private activeTimer: any;

packages/designer/src/plugin/plugin-manager.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ export class LowCodePluginManager implements ILowCodePluginManager {
148148
for (const pluginName of sequence) {
149149
try {
150150
await this.pluginsMap.get(pluginName)!.init();
151-
} catch (e) {
151+
} catch (e) /* istanbul ignore next */ {
152152
logger.error(
153153
`Failed to init plugin:${pluginName}, it maybe affect those plugins which depend on this.`,
154154
);
@@ -189,6 +189,7 @@ export class LowCodePluginManager implements ILowCodePluginManager {
189189
});
190190
}
191191

192+
/* istanbul ignore next */
192193
setDisabled(pluginName: string, flag = true) {
193194
logger.warn(`plugin:${pluginName} has been set disable:${flag}`);
194195
this.pluginsMap.get(pluginName)?.setDisabled(flag);

packages/designer/tests/__mocks__/document-model.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ export class DocumentModel {
22
a = 1;
33
c = {};
44
constructor() {
5-
console.log('xxxxxxxxxxxxxxxxxxxx');
65
const b = { x: { y: 2 } };
76
const c: number = 2;
87
this.a = b?.x?.y;
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`History data function & records 1`] = `"{\\"data\\":1,\\"children\\":[{\\"data\\":2,\\"children\\":[]}]}"`;
4+
5+
exports[`History data function & records 2`] = `"{\\"data\\":3,\\"children\\":[{\\"data\\":2,\\"children\\":[]}]}"`;
6+
7+
exports[`History data function & records 3`] = `"{\\"data\\":5,\\"children\\":[{\\"data\\":2,\\"children\\":[]}]}"`;
8+
9+
exports[`History data function & records 4`] = `"{\\"data\\":7,\\"children\\":[{\\"data\\":2,\\"children\\":[]}]}"`;

0 commit comments

Comments
 (0)