From 26a8cc2160ee772d94371466f612ea6b7b5c6a77 Mon Sep 17 00:00:00 2001 From: xiaoiver Date: Mon, 26 Oct 2020 09:59:33 +0800 Subject: [PATCH] Fix 634 (#642) * fix: getPointAtLength failed on an empty path * fix: add test case for #634 --- packages/g-svg/src/shape/path.ts | 4 +++ packages/g-svg/tests/bugs/issue-634-spec.js | 30 +++++++++++++++++++++ packages/g-svg/tests/mocha.opts | 1 + 3 files changed, 35 insertions(+) create mode 100644 packages/g-svg/tests/bugs/issue-634-spec.js diff --git a/packages/g-svg/src/shape/path.ts b/packages/g-svg/src/shape/path.ts index cba095fed..2bd5df36b 100644 --- a/packages/g-svg/src/shape/path.ts +++ b/packages/g-svg/src/shape/path.ts @@ -73,6 +73,10 @@ class Path extends ShapeBase { getPoint(ratio: number): Point { const el = this.get('el'); const totalLength = this.getTotalLength(); + // @see https://github.com/antvis/g/issues/634 + if (totalLength === 0) { + return null; + } const point = el ? el.getPointAtLength(ratio * totalLength) : null; return point ? { diff --git a/packages/g-svg/tests/bugs/issue-634-spec.js b/packages/g-svg/tests/bugs/issue-634-spec.js new file mode 100644 index 000000000..bdf95e40a --- /dev/null +++ b/packages/g-svg/tests/bugs/issue-634-spec.js @@ -0,0 +1,30 @@ +const expect = require('chai').expect; +import Canvas from '../../../g-svg/src/canvas'; +import Path from '../../../g-svg/src/shape/path'; + +const dom = document.createElement('div'); +document.body.appendChild(dom); +dom.id = 'c1'; + +describe('#634', () => { + const canvas = new Canvas({ + container: dom, + width: 400, + height: 400, + }); + + it('should get null point on an empty path', () => { + const path = new Path({ + attrs: { + path: [ + ['M', 0, 0], + ['L', 0, 0], + ], + lineWidth: 1, + stroke: 'red', + }, + }); + canvas.add(path); + expect(path.getPoint(0)).eqls(null); + }); +}); diff --git a/packages/g-svg/tests/mocha.opts b/packages/g-svg/tests/mocha.opts index 50e024f88..2a4d97165 100644 --- a/packages/g-svg/tests/mocha.opts +++ b/packages/g-svg/tests/mocha.opts @@ -1,3 +1,4 @@ --require source-map-support/register --recursive tests/unit +tests/bugs \ No newline at end of file