From 95474f955bb10c1bd10b7966cf18ad0c777bfb4e Mon Sep 17 00:00:00 2001 From: Manuel Date: Tue, 25 Aug 2026 18:55:43 +0000 Subject: [PATCH] fix: clear stale line chart hover state when points change --- frontend/src/components/LineChart.jsx | 6 ++ frontend/src/components/LineChart.test.jsx | 82 ++++++++++++++++++++++ 2 files changed, 88 insertions(+) create mode 100644 frontend/src/components/LineChart.test.jsx diff --git a/frontend/src/components/LineChart.jsx b/frontend/src/components/LineChart.jsx index c236464..5be34ac 100644 --- a/frontend/src/components/LineChart.jsx +++ b/frontend/src/components/LineChart.jsx @@ -18,6 +18,12 @@ export default function LineChart({ points, h = 150, unit = '', color = 'var(--a const tipRef = useRef(null) const [hover, setHover] = useState(null) // { x, y, iso, v } + // A new dataset has different coordinates and meaning, so never carry the previous + // point selection into it. Layout timing removes the marker before the new chart paints. + useLayoutEffect(() => { + setHover(null) + }, [points]) + // The tooltip is placed after layout, from its measured size, because the chart // lives in an overflow-clipped box: a fixed half-width offset (what this used to // do) hangs the label off the edge on the first and last point, and the clip then diff --git a/frontend/src/components/LineChart.test.jsx b/frontend/src/components/LineChart.test.jsx new file mode 100644 index 0000000..ea77a2d --- /dev/null +++ b/frontend/src/components/LineChart.test.jsx @@ -0,0 +1,82 @@ +import React, { act } from 'react' +import { createRoot } from 'react-dom/client' +import { Window } from 'happy-dom' +import { afterEach, beforeEach, describe, expect, it } from 'vitest' +import LineChart from './LineChart.jsx' + +let dom +let root +let container + +const firstPoints = [ + { t: Date.UTC(2026, 0, 1), y: 80, d: '2026-01-01' }, + { t: Date.UTC(2026, 0, 15), y: 82, d: '2026-01-15' }, +] +const nextPoints = [ + { t: Date.UTC(2026, 1, 1), y: 78, d: '2026-02-01' }, + { t: Date.UTC(2026, 1, 15), y: 79, d: '2026-02-15' }, +] + +function installDom() { + dom = new Window({ url: 'http://localhost/' }) + globalThis.window = dom + globalThis.document = dom.document + Object.defineProperty(globalThis, 'navigator', { configurable: true, value: dom.navigator }) + for (const key of ['HTMLElement', 'SVGElement', 'Node', 'Element', 'Event', 'MouseEvent']) { + globalThis[key] = dom[key] + } + globalThis.IS_REACT_ACT_ENVIRONMENT = true + container = document.createElement('div') + document.body.append(container) + root = createRoot(container) +} + +async function render(points) { + await act(async () => { root.render() }) + const svg = container.querySelector('svg') + Object.defineProperty(svg, 'getBoundingClientRect', { + configurable: true, + value: () => ({ left: 0, width: 340 }), + }) +} + +async function hoverAt(clientX) { + await act(async () => { + container.querySelector('.chart-i').dispatchEvent(new dom.MouseEvent('mousemove', { + bubbles: true, + clientX, + })) + }) +} + +beforeEach(() => { + installDom() +}) + +afterEach(async () => { + if (root) await act(async () => { root.unmount() }) + root = null + container = null + dom.close() + dom = null +}) + +describe('LineChart hover state', () => { + it('clears the tooltip and hover marker when points are replaced, then allows hovering again', async () => { + await render(firstPoints) + await hoverAt(170) + + expect(container.querySelector('.ctip')).toBeTruthy() + expect(container.querySelector('.cvl')).toBeTruthy() + + await render(nextPoints) + + expect(container.querySelector('.ctip')).toBeNull() + expect(container.querySelector('.cvl')).toBeNull() + expect(container.querySelector('.chl')).toBeNull() + + await hoverAt(170) + expect(container.querySelector('.ctip')).toBeTruthy() + expect(container.querySelector('.cvl')).toBeTruthy() + }) +})