diff --git a/frontend/src/components/LineChart.jsx b/frontend/src/components/LineChart.jsx index e8e59db..a1259b9 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 index fe41810..19bce10 100644 --- a/frontend/src/components/LineChart.test.jsx +++ b/frontend/src/components/LineChart.test.jsx @@ -27,6 +27,15 @@ const point = (year, month, day, y) => ({ y }) +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 renderChart(points) { act(() => root.render()) } @@ -71,4 +80,25 @@ describe('LineChart hover date', () => { const lastIso = isoOf(new Date(last.t)) expect(hoverAt(340)).toBe(`${fmtDate(lastIso, true, true)} ยท 71 kg`) }) -}) \ No newline at end of file +}) + +describe('LineChart hover state', () => { + it('clears the tooltip and hover markers when points are replaced, then allows hovering again', () => { + renderChart(firstPoints) + hoverAt(170) + + expect(container.querySelector('.ctip')).toBeTruthy() + expect(container.querySelector('.cvl')).toBeTruthy() + expect(container.querySelector('.chl')).toBeTruthy() + + renderChart(nextPoints) + + expect(container.querySelector('.ctip')).toBeNull() + expect(container.querySelector('.cvl')).toBeNull() + expect(container.querySelector('.chl')).toBeNull() + + hoverAt(170) + expect(container.querySelector('.ctip')).toBeTruthy() + expect(container.querySelector('.cvl')).toBeTruthy() + }) +})