From dd11880c69a166a88ebd8f3aec104a3ab42078e8 Mon Sep 17 00:00:00 2001 From: hustcc Date: Tue, 3 Nov 2020 21:41:58 +0800 Subject: [PATCH] test: coverage for util/path (#1849) * test: coverage for util/path * fix: type define lint --- __tests__/unit/plots/histogram/index-spec.ts | 2 +- __tests__/unit/utils/path-spec.ts | 144 ++++++++++++++++++- src/utils/path.ts | 4 +- 3 files changed, 141 insertions(+), 9 deletions(-) diff --git a/__tests__/unit/plots/histogram/index-spec.ts b/__tests__/unit/plots/histogram/index-spec.ts index 48c1700ec5..f858b731a1 100644 --- a/__tests__/unit/plots/histogram/index-spec.ts +++ b/__tests__/unit/plots/histogram/index-spec.ts @@ -111,7 +111,7 @@ describe('histogram', () => { data: histogramStackData, binField: 'value', binWidth: 4, - legend: true, + legend: {}, stackField: 'type', }); diff --git a/__tests__/unit/utils/path-spec.ts b/__tests__/unit/utils/path-spec.ts index fa0df0d856..40d5dbfcbc 100644 --- a/__tests__/unit/utils/path-spec.ts +++ b/__tests__/unit/utils/path-spec.ts @@ -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 }, @@ -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 }, @@ -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 }, @@ -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 }, diff --git a/src/utils/path.ts b/src/utils/path.ts index 532510cb19..4f05938805 100644 --- a/src/utils/path.ts +++ b/src/utils/path.ts @@ -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]); @@ -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];