Skip to content

Commit 6780155

Browse files
wumailboyongjiong
authored andcommitted
feat(core): add test for core
1 parent 48daefe commit 6780155

21 files changed

+1769
-28
lines changed

babel.config.js

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
module.exports = {
22
presets: [
3-
[
4-
'@babel/preset-env', {targets: {node: 'current'}}
5-
],
3+
['@babel/preset-env', { targets: { node: 'current' } }],
64
'@babel/preset-typescript',
75
],
8-
"plugins": [
6+
plugins: [
97
[
10-
"@babel/plugin-transform-react-jsx",
8+
'@babel/plugin-transform-react-jsx',
119
{
12-
"pragma": "h"
13-
}
14-
],
15-
[
16-
"@babel/plugin-proposal-decorators",
17-
{ "legacy": true }
10+
pragma: 'h',
11+
},
1812
],
1913
[
20-
"@babel/plugin-proposal-class-properties",
14+
'babel-plugin-jsx-pragmatic',
15+
{
16+
module: 'preact',
17+
import: 'h',
18+
export: 'h',
19+
},
2120
],
22-
]
23-
}
24-
21+
['@babel/plugin-proposal-decorators', { legacy: true }],
22+
['@babel/plugin-proposal-class-properties'],
23+
],
24+
};

jest.config.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,15 @@ module.exports = {
2323
// collectCoverageFrom: undefined,
2424

2525
// The directory where Jest should output its coverage files
26-
coverageDirectory: "coverage",
26+
coverageDirectory: 'coverage',
2727

2828
// An array of regexp pattern strings used to skip coverage collection
2929
// coveragePathIgnorePatterns: [
3030
// "/node_modules/"
3131
// ],
3232

3333
// Indicates which provider should be used to instrument code for coverage
34-
coverageProvider: "v8",
34+
coverageProvider: 'v8',
3535

3636
// A list of reporter names that Jest uses when writing coverage reports
3737
// coverageReporters: [
@@ -172,13 +172,12 @@ module.exports = {
172172
// timers: "real",
173173

174174
// A map from regular expressions to paths to transformers
175-
// transform: undefined,
175+
transform: {
176+
'\\.[jt]sx?$': 'babel-jest',
177+
},
176178

177179
// An array of regexp pattern strings that are matched against all source file paths, matched files will skip transformation
178-
// transformIgnorePatterns: [
179-
// "/node_modules/",
180-
// "\\.pnp\\.[^\\/]+$"
181-
// ],
180+
transformIgnorePatterns: ['node_modules/(?!@ngrx|(?!deck.gl)|ng-dynamic)'],
182181

183182
// An array of regexp pattern strings that are matched against all modules before the module loader will automatically return a mock for them
184183
// unmockedModulePathPatterns: undefined,

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,16 @@
2727
"watch:extension": "lerna watch --scope @logicflow/extension -- lerna run build --scope @logicflow/extension",
2828
"test": "jest"
2929
},
30-
"dependencies": {},
30+
"dependencies": {
31+
"babel-plugin-jsx-pragmatic": "^1.0.2"
32+
},
3133
"devDependencies": {
3234
"@commitlint/cli": "^8.3.5",
3335
"@commitlint/config-conventional": "^8.3.4",
3436
"@typescript-eslint/eslint-plugin": "^3.6.1",
3537
"@typescript-eslint/parser": "^3.2.0",
3638
"@vuepress-reco/vuepress-plugin-back-to-top": "^1.6.0",
39+
"babel-jest": "^29.7.0",
3740
"commitizen": "^4.2.4",
3841
"cz-lerna-changelog": "^2.0.3",
3942
"eslint": "^7.0.0",
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
import { getCrossPointOfLine, isInSegment } from '../../src/algorithm/edge';
2+
3+
describe('algorithm/edge', () => {
4+
// one intersection
5+
test('one intersection', () => {
6+
const line1 = [
7+
{
8+
x: 0,
9+
y: 0,
10+
},
11+
{
12+
x: 10,
13+
y: 10,
14+
},
15+
];
16+
const line2 = [
17+
{
18+
x: 10,
19+
y: 0,
20+
},
21+
{
22+
x: 0,
23+
y: 10,
24+
},
25+
];
26+
expect(
27+
getCrossPointOfLine(line1[0], line1[1], line2[0], line2[1]),
28+
).toBeTruthy();
29+
});
30+
// multiple intersection
31+
test('multiple intersection', () => {
32+
const line1 = [
33+
{
34+
x: 0,
35+
y: 0,
36+
},
37+
{
38+
x: 10,
39+
y: 10,
40+
},
41+
];
42+
const line2 = [
43+
{
44+
x: 0,
45+
y: 0,
46+
},
47+
{
48+
x: 10,
49+
y: 10,
50+
},
51+
];
52+
expect(
53+
getCrossPointOfLine(line1[0], line1[1], line2[0], line2[1]),
54+
).toBeFalsy();
55+
});
56+
// no intersection
57+
test('intersection', () => {
58+
const line1 = [
59+
{
60+
x: 0,
61+
y: 0,
62+
},
63+
{
64+
x: 10,
65+
y: 10,
66+
},
67+
];
68+
const line2 = [
69+
{
70+
x: 10,
71+
y: 0,
72+
},
73+
{
74+
x: 20,
75+
y: 10,
76+
},
77+
];
78+
expect(
79+
getCrossPointOfLine(line1[0], line1[1], line2[0], line2[1]),
80+
).toBeFalsy();
81+
});
82+
83+
test('in segment', () => {
84+
const point = {
85+
x: 0,
86+
y: 0,
87+
};
88+
const line = [
89+
{
90+
x: -10,
91+
y: -10,
92+
},
93+
{
94+
x: 10,
95+
y: 10,
96+
},
97+
];
98+
expect(isInSegment(point, line[0], line[1])).toBeTruthy();
99+
});
100+
// not in segment
101+
test('not in segment', () => {
102+
const point = {
103+
x: 10,
104+
y: 0,
105+
};
106+
const line = [
107+
{
108+
x: -10,
109+
y: -10,
110+
},
111+
{
112+
x: 10,
113+
y: 10,
114+
},
115+
];
116+
expect(isInSegment(point, line[0], line[1])).toBeFalsy();
117+
});
118+
});
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import { getVerticalPointOfLine } from '../../src/algorithm';
2+
3+
describe('algorithm/index', () => {
4+
test('getVerticalPointOfLine', () => {
5+
const config1 = {
6+
start: {
7+
x: 0,
8+
y: 0,
9+
},
10+
end: {
11+
x: 0,
12+
y: 10,
13+
},
14+
offset: 3,
15+
verticalLength: 3,
16+
type: 'start',
17+
};
18+
const config2 = {
19+
start: {
20+
x: 0,
21+
y: 0,
22+
},
23+
end: {
24+
x: 10,
25+
y: 0,
26+
},
27+
offset: 3,
28+
verticalLength: 3,
29+
type: 'end',
30+
};
31+
const config3 = {
32+
start: {
33+
x: 10,
34+
y: 10,
35+
},
36+
end: {
37+
x: 0,
38+
y: 0,
39+
},
40+
offset: 3,
41+
verticalLength: 3,
42+
type: 'start',
43+
};
44+
const config4 = {
45+
start: {
46+
x: 10,
47+
y: 10,
48+
},
49+
end: {
50+
x: 0,
51+
y: 0,
52+
},
53+
offset: 3,
54+
verticalLength: 3,
55+
type: 'end',
56+
};
57+
const res1 = getVerticalPointOfLine(config1);
58+
expect(Math.abs(res1.leftX) - 5 < Number.EPSILON).toBeTruthy();
59+
expect(Math.abs(res1.leftY) - 3 < Number.EPSILON).toBeTruthy();
60+
expect(Math.abs(res1.rightX) - 5 < Number.EPSILON).toBeTruthy();
61+
expect(Math.abs(res1.rightY) - 3 < Number.EPSILON).toBeTruthy();
62+
63+
const res2 = getVerticalPointOfLine(config2);
64+
expect(Math.abs(res2.leftX) - 7 < Number.EPSILON).toBeTruthy();
65+
expect(Math.abs(res2.leftY) - 3 < Number.EPSILON).toBeTruthy();
66+
expect(Math.abs(res2.rightX) - 7 < Number.EPSILON).toBeTruthy();
67+
expect(Math.abs(res2.rightY) - 3 < Number.EPSILON).toBeTruthy();
68+
});
69+
});
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { getEdgeOutline } from '../../src/algorithm/outline';
2+
3+
describe('algorithm/outline', () => {
4+
test('get edge outline', () => {
5+
const edge1 = {
6+
modelType: 'line-edge',
7+
startPoint: {
8+
x: 0,
9+
y: 0,
10+
},
11+
endPoint: {
12+
x: 10,
13+
y: 10,
14+
},
15+
};
16+
expect(getEdgeOutline(edge1)).toEqual({
17+
x: -5,
18+
y: -5,
19+
x1: 15,
20+
y1: 15,
21+
});
22+
const edge2 = {
23+
modelType: 'polyline-edge',
24+
points: '0,0 10,10',
25+
};
26+
expect(getEdgeOutline(edge2)).toEqual({
27+
x: -4,
28+
y: -4,
29+
x1: 14,
30+
y1: 14,
31+
});
32+
const edge3 = {
33+
modelType: 'bezier-edge',
34+
path: 'M 270 195C 370 195,305 290,405 290',
35+
};
36+
expect(getEdgeOutline(edge3)).toEqual({
37+
x: 266,
38+
y: 191,
39+
x1: 409,
40+
y1: 294,
41+
});
42+
});
43+
});
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import EventEmitter from '../../src/event/eventEmitter';
2+
3+
describe('event/eventEmitter', () => {
4+
const em = new EventEmitter();
5+
test('event emitter', () => {
6+
const fn = jest.fn();
7+
em.on('test', fn);
8+
em.emit('test', { a: 1 });
9+
expect(fn).toBeCalledWith({ a: 1 });
10+
em.off('test', fn);
11+
em.emit('test', { a: 1 });
12+
expect(fn).toBeCalledTimes(1);
13+
14+
em.once('test1', fn);
15+
em.emit('test1', { a: 1 });
16+
expect(fn).toBeCalledTimes(2);
17+
em.once('test1', fn);
18+
em.emit('test1', { a: 1 });
19+
const test1Events = em.getEvents().test1;
20+
expect(test1Events).toBeUndefined();
21+
});
22+
});
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import History from '../../src/history/History';
2+
import EventEmitter from '../../src/event/eventEmitter';
3+
4+
describe('history', () => {
5+
const event = new EventEmitter();
6+
const history = new History(event);
7+
expect(history).toBeDefined();
8+
test('add', () => {
9+
history.add(1);
10+
expect(history.undos).toEqual([1]);
11+
expect(history.redos).toEqual([]);
12+
});
13+
test('undo', () => {
14+
history.add(1);
15+
history.add(2);
16+
history.undo();
17+
expect(history.undos).toEqual([]);
18+
expect(history.redos).toEqual([2]);
19+
});
20+
test('redo', () => {
21+
history.add(1);
22+
history.add(2);
23+
history.undo();
24+
history.redo();
25+
expect(history.undos).toEqual([]);
26+
expect(history.redos).toEqual([]);
27+
});
28+
});

0 commit comments

Comments
 (0)