From 8873b452303a471f4cb0ad3abbddaacadde46147 Mon Sep 17 00:00:00 2001 From: Tanner Linsley Date: Tue, 13 Oct 2026 23:29:23 -0600 Subject: [PATCH] test: cover SVG coordinate changes between pointer samples --- benchmarks/conformance/previews/manifest.json | 2 +- packages/charts-core/src/svg-surface.test.ts | 35 +++++++++++++++---- 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/benchmarks/conformance/previews/manifest.json b/benchmarks/conformance/previews/manifest.json index d38b3b7c..8e27d4b0 100644 --- a/benchmarks/conformance/previews/manifest.json +++ b/benchmarks/conformance/previews/manifest.json @@ -2,7 +2,7 @@ "schemaVersion": 1, "width": 288, "height": 192, - "sourceHash": "b503388bda59296b32222df9ec499915bd86d0c8d93852609d596973cfd21f71", + "sourceHash": "2ce2590f5c943219b694d3c51bb4806ed785ff0a4058c108fc520b0e6087c183", "assets": [ { "id": "01-line-gaps", diff --git a/packages/charts-core/src/svg-surface.test.ts b/packages/charts-core/src/svg-surface.test.ts index 4f0ba6c5..d078ba09 100644 --- a/packages/charts-core/src/svg-surface.test.ts +++ b/packages/charts-core/src/svg-surface.test.ts @@ -327,7 +327,7 @@ describe('SVG surface coordinates', () => { surface.destroy() }) - it('maps client coordinates through the rendered SVG transform', () => { + it('refreshes the rendered SVG transform between coordinate samples', () => { const scene = createChartScene( defineChart({ marks: [dot([{ x: 1, y: 1 }], { x: 'x', y: 'y' })], @@ -352,6 +352,8 @@ describe('SVG surface coordinates', () => { const renderedHeight = 480 const scale = renderedWidth / scene.width const verticalInset = (renderedHeight - scene.height * scale) / 2 + let translationX = 0 + let translationY = 0 vi.spyOn(svg, 'getBoundingClientRect').mockReturnValue( DOMRect.fromRect({ @@ -363,16 +365,17 @@ describe('SVG surface coordinates', () => { ) Object.defineProperty(svg, 'getScreenCTM', { configurable: true, - value: vi.fn(() => ({ - inverse: () => ({ + value: vi.fn(() => { + const inverse = { a: 1 / scale, b: 0, c: 0, d: 1 / scale, - e: -left / scale, - f: -(top + verticalInset) / scale, - }), - })), + e: -(left + translationX) / scale, + f: -(top + verticalInset + translationY) / scale, + } + return { inverse: () => inverse } + }), }) const expected = { x: 603.84, y: 28.76 } @@ -385,6 +388,24 @@ describe('SVG surface coordinates', () => { expect(resolved?.x).toBeCloseTo(expected.x) expect(resolved?.y).toBeCloseTo(expected.y) + // Ancestor transforms, preceding layout and scroll can move the SVG + // without resizing it or rendering another chart scene. + for (const { x, y } of [ + { x: 40, y: 30 }, + { x: 0, y: 75 }, + { x: 0, y: -50 }, + ]) { + translationX = x + translationY = y + const moved = surface.clientToScene?.( + scene, + left + translationX + expected.x * scale, + top + verticalInset + translationY + expected.y * scale, + ) + expect(moved?.x).toBeCloseTo(expected.x) + expect(moved?.y).toBeCloseTo(expected.y) + } + surface.destroy() })