Skip to content

Commit

Permalink
feat(core): add test for core
Browse files Browse the repository at this point in the history
  • Loading branch information
wumail authored and boyongjiong committed Dec 4, 2023
1 parent 48daefe commit 6780155
Show file tree
Hide file tree
Showing 21 changed files with 1,769 additions and 28 deletions.
30 changes: 15 additions & 15 deletions babel.config.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
module.exports = {
presets: [
[
'@babel/preset-env', {targets: {node: 'current'}}
],
['@babel/preset-env', { targets: { node: 'current' } }],
'@babel/preset-typescript',
],
"plugins": [
plugins: [
[
"@babel/plugin-transform-react-jsx",
'@babel/plugin-transform-react-jsx',
{
"pragma": "h"
}
],
[
"@babel/plugin-proposal-decorators",
{ "legacy": true }
pragma: 'h',
},
],
[
"@babel/plugin-proposal-class-properties",
'babel-plugin-jsx-pragmatic',
{
module: 'preact',
import: 'h',
export: 'h',
},
],
]
}

['@babel/plugin-proposal-decorators', { legacy: true }],
['@babel/plugin-proposal-class-properties'],
],
};
13 changes: 6 additions & 7 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ module.exports = {
// collectCoverageFrom: undefined,

// The directory where Jest should output its coverage files
coverageDirectory: "coverage",
coverageDirectory: 'coverage',

// An array of regexp pattern strings used to skip coverage collection
// coveragePathIgnorePatterns: [
// "/node_modules/"
// ],

// Indicates which provider should be used to instrument code for coverage
coverageProvider: "v8",
coverageProvider: 'v8',

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

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

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

// An array of regexp pattern strings that are matched against all modules before the module loader will automatically return a mock for them
// unmockedModulePathPatterns: undefined,
Expand Down
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,16 @@
"watch:extension": "lerna watch --scope @logicflow/extension -- lerna run build --scope @logicflow/extension",
"test": "jest"
},
"dependencies": {},
"dependencies": {
"babel-plugin-jsx-pragmatic": "^1.0.2"
},
"devDependencies": {
"@commitlint/cli": "^8.3.5",
"@commitlint/config-conventional": "^8.3.4",
"@typescript-eslint/eslint-plugin": "^3.6.1",
"@typescript-eslint/parser": "^3.2.0",
"@vuepress-reco/vuepress-plugin-back-to-top": "^1.6.0",
"babel-jest": "^29.7.0",
"commitizen": "^4.2.4",
"cz-lerna-changelog": "^2.0.3",
"eslint": "^7.0.0",
Expand Down
118 changes: 118 additions & 0 deletions packages/core/__tests__/algorithm/egde.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
import { getCrossPointOfLine, isInSegment } from '../../src/algorithm/edge';

describe('algorithm/edge', () => {
// one intersection
test('one intersection', () => {
const line1 = [
{
x: 0,
y: 0,
},
{
x: 10,
y: 10,
},
];
const line2 = [
{
x: 10,
y: 0,
},
{
x: 0,
y: 10,
},
];
expect(
getCrossPointOfLine(line1[0], line1[1], line2[0], line2[1]),
).toBeTruthy();
});
// multiple intersection
test('multiple intersection', () => {
const line1 = [
{
x: 0,
y: 0,
},
{
x: 10,
y: 10,
},
];
const line2 = [
{
x: 0,
y: 0,
},
{
x: 10,
y: 10,
},
];
expect(
getCrossPointOfLine(line1[0], line1[1], line2[0], line2[1]),
).toBeFalsy();
});
// no intersection
test('intersection', () => {
const line1 = [
{
x: 0,
y: 0,
},
{
x: 10,
y: 10,
},
];
const line2 = [
{
x: 10,
y: 0,
},
{
x: 20,
y: 10,
},
];
expect(
getCrossPointOfLine(line1[0], line1[1], line2[0], line2[1]),
).toBeFalsy();
});

test('in segment', () => {
const point = {
x: 0,
y: 0,
};
const line = [
{
x: -10,
y: -10,
},
{
x: 10,
y: 10,
},
];
expect(isInSegment(point, line[0], line[1])).toBeTruthy();
});
// not in segment
test('not in segment', () => {
const point = {
x: 10,
y: 0,
};
const line = [
{
x: -10,
y: -10,
},
{
x: 10,
y: 10,
},
];
expect(isInSegment(point, line[0], line[1])).toBeFalsy();
});
});
69 changes: 69 additions & 0 deletions packages/core/__tests__/algorithm/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { getVerticalPointOfLine } from '../../src/algorithm';

describe('algorithm/index', () => {
test('getVerticalPointOfLine', () => {
const config1 = {
start: {
x: 0,
y: 0,
},
end: {
x: 0,
y: 10,
},
offset: 3,
verticalLength: 3,
type: 'start',
};
const config2 = {
start: {
x: 0,
y: 0,
},
end: {
x: 10,
y: 0,
},
offset: 3,
verticalLength: 3,
type: 'end',
};
const config3 = {
start: {
x: 10,
y: 10,
},
end: {
x: 0,
y: 0,
},
offset: 3,
verticalLength: 3,
type: 'start',
};
const config4 = {
start: {
x: 10,
y: 10,
},
end: {
x: 0,
y: 0,
},
offset: 3,
verticalLength: 3,
type: 'end',
};
const res1 = getVerticalPointOfLine(config1);
expect(Math.abs(res1.leftX) - 5 < Number.EPSILON).toBeTruthy();
expect(Math.abs(res1.leftY) - 3 < Number.EPSILON).toBeTruthy();
expect(Math.abs(res1.rightX) - 5 < Number.EPSILON).toBeTruthy();
expect(Math.abs(res1.rightY) - 3 < Number.EPSILON).toBeTruthy();

const res2 = getVerticalPointOfLine(config2);
expect(Math.abs(res2.leftX) - 7 < Number.EPSILON).toBeTruthy();
expect(Math.abs(res2.leftY) - 3 < Number.EPSILON).toBeTruthy();
expect(Math.abs(res2.rightX) - 7 < Number.EPSILON).toBeTruthy();
expect(Math.abs(res2.rightY) - 3 < Number.EPSILON).toBeTruthy();
});
});
43 changes: 43 additions & 0 deletions packages/core/__tests__/algorithm/outline.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { getEdgeOutline } from '../../src/algorithm/outline';

describe('algorithm/outline', () => {
test('get edge outline', () => {
const edge1 = {
modelType: 'line-edge',
startPoint: {
x: 0,
y: 0,
},
endPoint: {
x: 10,
y: 10,
},
};
expect(getEdgeOutline(edge1)).toEqual({
x: -5,
y: -5,
x1: 15,
y1: 15,
});
const edge2 = {
modelType: 'polyline-edge',
points: '0,0 10,10',
};
expect(getEdgeOutline(edge2)).toEqual({
x: -4,
y: -4,
x1: 14,
y1: 14,
});
const edge3 = {
modelType: 'bezier-edge',
path: 'M 270 195C 370 195,305 290,405 290',
};
expect(getEdgeOutline(edge3)).toEqual({
x: 266,
y: 191,
x1: 409,
y1: 294,
});
});
});
22 changes: 22 additions & 0 deletions packages/core/__tests__/event/event.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import EventEmitter from '../../src/event/eventEmitter';

describe('event/eventEmitter', () => {
const em = new EventEmitter();
test('event emitter', () => {
const fn = jest.fn();
em.on('test', fn);
em.emit('test', { a: 1 });
expect(fn).toBeCalledWith({ a: 1 });
em.off('test', fn);
em.emit('test', { a: 1 });
expect(fn).toBeCalledTimes(1);

em.once('test1', fn);
em.emit('test1', { a: 1 });
expect(fn).toBeCalledTimes(2);
em.once('test1', fn);
em.emit('test1', { a: 1 });
const test1Events = em.getEvents().test1;
expect(test1Events).toBeUndefined();
});
});
28 changes: 28 additions & 0 deletions packages/core/__tests__/history/history.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import History from '../../src/history/History';
import EventEmitter from '../../src/event/eventEmitter';

describe('history', () => {
const event = new EventEmitter();
const history = new History(event);
expect(history).toBeDefined();
test('add', () => {
history.add(1);
expect(history.undos).toEqual([1]);
expect(history.redos).toEqual([]);
});
test('undo', () => {
history.add(1);
history.add(2);
history.undo();
expect(history.undos).toEqual([]);
expect(history.redos).toEqual([2]);
});
test('redo', () => {
history.add(1);
history.add(2);
history.undo();
history.redo();
expect(history.undos).toEqual([]);
expect(history.redos).toEqual([]);
});
});
Loading

0 comments on commit 6780155

Please sign in to comment.