Skip to content

Commit dd11880

Browse files
authored
test: coverage for util/path (#1849)
* test: coverage for util/path * fix: type define lint
1 parent a4340dc commit dd11880

File tree

3 files changed

+141
-9
lines changed

3 files changed

+141
-9
lines changed

__tests__/unit/plots/histogram/index-spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ describe('histogram', () => {
111111
data: histogramStackData,
112112
binField: 'value',
113113
binWidth: 4,
114-
legend: true,
114+
legend: {},
115115
stackField: 'type',
116116
});
117117

__tests__/unit/utils/path-spec.ts

+138-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,139 @@
1-
import { getSplinePath } from '../../../src/utils/path';
1+
import { points2Path, smoothBezier, catmullRom2bezier, getSplinePath } from '../../../src/utils/path';
22

3-
describe('PathUtil', () => {
4-
test('getSplinePath(), two points', () => {
3+
describe('path', () => {
4+
it('points2Path', () => {
5+
expect(points2Path([], false)).toEqual([]);
6+
expect(points2Path([{ x: 100, y: 100 }], false)).toEqual([['M', 100, 100]]);
7+
expect(points2Path([{ x: 100, y: 100 }], true)).toEqual([['M', 100, 100], ['Z']]);
8+
});
9+
10+
it('smoothBezier', () => {
11+
expect(
12+
smoothBezier(
13+
[
14+
[0, 0],
15+
[0.1, 0.1],
16+
],
17+
0.4,
18+
true,
19+
[
20+
[0.04, 0.04],
21+
[0.1, 0.1],
22+
]
23+
)
24+
).toEqual([
25+
[0, 0],
26+
[0.1, 0.1],
27+
[0.1, 0.1],
28+
[0, 0],
29+
]);
30+
expect(
31+
smoothBezier(
32+
[
33+
[0, 0],
34+
[0.1, 0.1],
35+
],
36+
0.4,
37+
false,
38+
[
39+
[0.04, 0.04],
40+
[0.1, 0.1],
41+
]
42+
)
43+
).toEqual([
44+
[0, 0],
45+
[0.1, 0.1],
46+
]);
47+
expect(
48+
smoothBezier(
49+
[
50+
[0, 0],
51+
[0.1, 0.1],
52+
],
53+
0.4,
54+
false,
55+
undefined
56+
)
57+
).toEqual([
58+
[0, 0],
59+
[0.1, 0.1],
60+
]);
61+
expect(
62+
smoothBezier(
63+
[
64+
[0, 0],
65+
[0.1, 0.1],
66+
],
67+
0.4,
68+
true,
69+
undefined
70+
)
71+
).toEqual([
72+
[0, 0],
73+
[0.1, 0.1],
74+
[0.1, 0.1],
75+
[0, 0],
76+
]);
77+
78+
expect(
79+
smoothBezier(
80+
[
81+
[0, 0],
82+
[0.1, 0.1],
83+
[0, 0],
84+
],
85+
1,
86+
false,
87+
undefined
88+
)
89+
).toEqual([
90+
[0, 0],
91+
[0.1, 0.1],
92+
[0.1, 0.1],
93+
[0, 0],
94+
]);
95+
});
96+
97+
it('catmullRom2bezier', () => {
98+
expect(
99+
catmullRom2bezier([0, 0, 0.1, 0.1], true, [
100+
[0.04, 0.04],
101+
[0.1, 0.1],
102+
])
103+
).toEqual([
104+
['C', 0, 0, 0.1, 0.1, 0.1, 0.1],
105+
['C', 0.1, 0.1, 0, 0, 0, 0],
106+
]);
107+
expect(
108+
catmullRom2bezier([0, 0, 0.1, 0.1], false, [
109+
[0.04, 0.04],
110+
[0.1, 0.1],
111+
])
112+
).toEqual([['C', 0, 0, 0.1, 0.1, 0.1, 0.1]]);
113+
114+
expect(catmullRom2bezier([0, 0, 0.1, 0.1], true, undefined)).toEqual([
115+
['C', 0, 0, 0.1, 0.1, 0.1, 0.1],
116+
['C', 0.1, 0.1, 0, 0, 0, 0],
117+
]);
118+
expect(catmullRom2bezier([0, 0, 0.1, 0.1], false, undefined)).toEqual([['C', 0, 0, 0.1, 0.1, 0.1, 0.1]]);
119+
});
120+
121+
it('getSplinePath same points', () => {
122+
const points = [
123+
{ x: 0, y: 0 },
124+
{ x: 0.1, y: 0.1 },
125+
{ x: 0.1, y: 0.1 },
126+
{ x: 0.2, y: 0.2 },
127+
];
128+
const path = getSplinePath(points);
129+
expect(path).toEqual([
130+
['M', 0, 0],
131+
['C', 0, 0, 0.06, 0.06, 0.1, 0.1],
132+
['C', 0.14, 0.14, 0.2, 0.2, 0.2, 0.2],
133+
]);
134+
});
135+
136+
it('getSplinePath(), two points', () => {
5137
const points = [
6138
{ x: 0, y: 0 },
7139
{ x: 0.1, y: 0.1 },
@@ -13,7 +145,7 @@ describe('PathUtil', () => {
13145
]);
14146
});
15147

16-
test('getSplinePath(), two points isInCircle', () => {
148+
it('getSplinePath(), two points isInCircle', () => {
17149
const points = [
18150
{ x: 0, y: 0 },
19151
{ x: 0.1, y: 0.1 },
@@ -22,7 +154,7 @@ describe('PathUtil', () => {
22154
expect(path).toEqual([['M', 0, 0], ['L', 0.1, 0.1], ['Z']]);
23155
});
24156

25-
test('getSplinePath(), more than two points', () => {
157+
it('getSplinePath(), more than two points', () => {
26158
const points = [
27159
{ x: 0, y: 0 },
28160
{ x: 0.1, y: 0.5 },
@@ -32,7 +164,7 @@ describe('PathUtil', () => {
32164
expect(path.length).toBe(3);
33165
});
34166

35-
test('getSplinePath(), more than two points isInCircle', () => {
167+
it('getSplinePath(), more than two points isInCircle', () => {
36168
const points = [
37169
{ x: 0, y: 0 },
38170
{ x: 0.1, y: 0.1 },

src/utils/path.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { vec2 } from '@antv/matrix-util';
22
import { Position, Point } from '@antv/g2/lib/interface';
33

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

0 commit comments

Comments
 (0)