Skip to content

Commit

Permalink
test: coverage for util/path (#1849)
Browse files Browse the repository at this point in the history
* test: coverage for util/path

* fix: type define lint
  • Loading branch information
hustcc authored Nov 3, 2020
1 parent a4340dc commit dd11880
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 9 deletions.
2 changes: 1 addition & 1 deletion __tests__/unit/plots/histogram/index-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ describe('histogram', () => {
data: histogramStackData,
binField: 'value',
binWidth: 4,
legend: true,
legend: {},
stackField: 'type',
});

Expand Down
144 changes: 138 additions & 6 deletions __tests__/unit/utils/path-spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,139 @@
import { getSplinePath } from '../../../src/utils/path';
import { points2Path, smoothBezier, catmullRom2bezier, getSplinePath } from '../../../src/utils/path';

describe('PathUtil', () => {
test('getSplinePath(), two points', () => {
describe('path', () => {
it('points2Path', () => {
expect(points2Path([], false)).toEqual([]);
expect(points2Path([{ x: 100, y: 100 }], false)).toEqual([['M', 100, 100]]);
expect(points2Path([{ x: 100, y: 100 }], true)).toEqual([['M', 100, 100], ['Z']]);
});

it('smoothBezier', () => {
expect(
smoothBezier(
[
[0, 0],
[0.1, 0.1],
],
0.4,
true,
[
[0.04, 0.04],
[0.1, 0.1],
]
)
).toEqual([
[0, 0],
[0.1, 0.1],
[0.1, 0.1],
[0, 0],
]);
expect(
smoothBezier(
[
[0, 0],
[0.1, 0.1],
],
0.4,
false,
[
[0.04, 0.04],
[0.1, 0.1],
]
)
).toEqual([
[0, 0],
[0.1, 0.1],
]);
expect(
smoothBezier(
[
[0, 0],
[0.1, 0.1],
],
0.4,
false,
undefined
)
).toEqual([
[0, 0],
[0.1, 0.1],
]);
expect(
smoothBezier(
[
[0, 0],
[0.1, 0.1],
],
0.4,
true,
undefined
)
).toEqual([
[0, 0],
[0.1, 0.1],
[0.1, 0.1],
[0, 0],
]);

expect(
smoothBezier(
[
[0, 0],
[0.1, 0.1],
[0, 0],
],
1,
false,
undefined
)
).toEqual([
[0, 0],
[0.1, 0.1],
[0.1, 0.1],
[0, 0],
]);
});

it('catmullRom2bezier', () => {
expect(
catmullRom2bezier([0, 0, 0.1, 0.1], true, [
[0.04, 0.04],
[0.1, 0.1],
])
).toEqual([
['C', 0, 0, 0.1, 0.1, 0.1, 0.1],
['C', 0.1, 0.1, 0, 0, 0, 0],
]);
expect(
catmullRom2bezier([0, 0, 0.1, 0.1], false, [
[0.04, 0.04],
[0.1, 0.1],
])
).toEqual([['C', 0, 0, 0.1, 0.1, 0.1, 0.1]]);

expect(catmullRom2bezier([0, 0, 0.1, 0.1], true, undefined)).toEqual([
['C', 0, 0, 0.1, 0.1, 0.1, 0.1],
['C', 0.1, 0.1, 0, 0, 0, 0],
]);
expect(catmullRom2bezier([0, 0, 0.1, 0.1], false, undefined)).toEqual([['C', 0, 0, 0.1, 0.1, 0.1, 0.1]]);
});

it('getSplinePath same points', () => {
const points = [
{ x: 0, y: 0 },
{ x: 0.1, y: 0.1 },
{ x: 0.1, y: 0.1 },
{ x: 0.2, y: 0.2 },
];
const path = getSplinePath(points);
expect(path).toEqual([
['M', 0, 0],
['C', 0, 0, 0.06, 0.06, 0.1, 0.1],
['C', 0.14, 0.14, 0.2, 0.2, 0.2, 0.2],
]);
});

it('getSplinePath(), two points', () => {
const points = [
{ x: 0, y: 0 },
{ x: 0.1, y: 0.1 },
Expand All @@ -13,7 +145,7 @@ describe('PathUtil', () => {
]);
});

test('getSplinePath(), two points isInCircle', () => {
it('getSplinePath(), two points isInCircle', () => {
const points = [
{ x: 0, y: 0 },
{ x: 0.1, y: 0.1 },
Expand All @@ -22,7 +154,7 @@ describe('PathUtil', () => {
expect(path).toEqual([['M', 0, 0], ['L', 0.1, 0.1], ['Z']]);
});

test('getSplinePath(), more than two points', () => {
it('getSplinePath(), more than two points', () => {
const points = [
{ x: 0, y: 0 },
{ x: 0.1, y: 0.5 },
Expand All @@ -32,7 +164,7 @@ describe('PathUtil', () => {
expect(path.length).toBe(3);
});

test('getSplinePath(), more than two points isInCircle', () => {
it('getSplinePath(), more than two points isInCircle', () => {
const points = [
{ x: 0, y: 0 },
{ x: 0.1, y: 0.1 },
Expand Down
4 changes: 2 additions & 2 deletions src/utils/path.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { vec2 } from '@antv/matrix-util';
import { Position, Point } from '@antv/g2/lib/interface';

function points2path(points: Point[], isInCircle: boolean) {
export function points2Path(points: Point[], isInCircle: boolean) {
const path = [];
if (points.length) {
path.push(['M', points[0].x, points[0].y]);
Expand Down Expand Up @@ -140,7 +140,7 @@ export function getSplinePath(points: Point[], isInCircle?: boolean, constaint?:
let prePoint = null;
if (points.length <= 2) {
// 两点以内直接绘制成路径
return points2path(points, isInCircle);
return points2Path(points, isInCircle);
}
for (let i = 0, len = points.length; i < len; i++) {
const point = points[i];
Expand Down

0 comments on commit dd11880

Please sign in to comment.