Merge upstream main into fix/clear-line-chart-hover

This commit is contained in:
doby-llm committed 2026-08-25 21:29:54 +02:00
commit 0a69ee8450
43 files changed
+1287 -118

No files matched your search

+6 -3
View File
@@ -97,7 +97,10 @@ export default function LineChart({ points, h = 150, unit = '', color = 'var(--a
const poly = pts.map(p => X(p.t).toFixed(1) + ',' + Y(p.y).toFixed(1)).join(' ')
const last = pts[pts.length - 1]
const gid = 'g' + Math.round(t0 % 1e7) + '_' + H
const hoverPts = (single ? [points[0]] : points).map(p => ({ x: X(p.t), y: Y(p.y), iso: p.d || isoOf(new Date(p.t)), v: p.y, note: p.note }))
const hoverSource = single ? [points[0]] : points
const hoverDates = hoverSource.map(p => p.d || isoOf(new Date(p.t)))
const showYear = new Set(hoverDates.map(iso => new Date(iso + 'T12:00:00').getFullYear())).size > 1
const hoverPts = hoverSource.map((p, i) => ({ x: X(p.t), y: Y(p.y), iso: hoverDates[i], v: p.y, note: p.note }))
const marked = points.some(p => p.m != null)
const onMove = e => {
@@ -138,8 +141,8 @@ export default function LineChart({ points, h = 150, unit = '', color = 'var(--a
</g>}
</svg>
{hover && <div className="ctip" ref={tipRef}>
{fmtDate(hover.iso, true)} · {fmtNum(hover.v)}{unit ? ' ' + unit : ''}{hover.note ? ' · ' + hover.note : ''}
{fmtDate(hover.iso, true, showYear)} · {fmtNum(hover.v)}{unit ? ' ' + unit : ''}{hover.note ? ' · ' + hover.note : ''}
</div>}
</div>
)
}
}
+64 -42
View File
@@ -1,12 +1,31 @@
// @vitest-environment happy-dom
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'
import { fmtDate, isoOf } from '../lib/format.js'
globalThis.IS_REACT_ACT_ENVIRONMENT = true
let dom
let root
let container
let root
beforeEach(() => {
container = document.createElement('div')
document.body.appendChild(container)
root = createRoot(container)
})
afterEach(() => {
act(() => root.unmount())
container.remove()
})
const point = (year, month, day, y) => ({
d: `${year}-${String(month).padStart(2, '0')}-${String(day).padStart(2, '0')}`,
t: new Date(year, month - 1, day, 12).getTime(),
y
})
const firstPoints = [
{ t: Date.UTC(2026, 0, 1), y: 80, d: '2026-01-01' },
@@ -17,65 +36,68 @@ const nextPoints = [
{ 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)
function renderChart(points) {
act(() => root.render(<LineChart points={points} axes={false} unit="kg" />))
}
async function render(points) {
await act(async () => { root.render(<LineChart points={points} unit="kg" />) })
const svg = container.querySelector('svg')
Object.defineProperty(svg, 'getBoundingClientRect', {
configurable: true,
value: () => ({ left: 0, width: 340 }),
function hoverAt(clientX) {
act(() => {
container.querySelector('.chart-i').dispatchEvent(new MouseEvent('mousemove', { bubbles: true, clientX }))
})
return container.querySelector('.ctip').textContent
}
async function hoverAt(clientX) {
await act(async () => {
container.querySelector('.chart-i').dispatchEvent(new dom.MouseEvent('mousemove', {
bubbles: true,
clientX,
}))
describe('LineChart hover date', () => {
it('keeps same-year points in the compact format', () => {
const first = point(2026, 1, 15, 70)
renderChart([first, point(2026, 2, 15, 71)])
expect(hoverAt(0)).toBe(`${fmtDate(first.d, true)} · 70 kg`)
})
}
beforeEach(() => {
installDom()
})
it('includes the calendar year at a short cross-year boundary', () => {
const first = point(2025, 11, 30, 70)
const last = point(2026, 2, 1, 71)
renderChart([first, last])
afterEach(async () => {
if (root) await act(async () => { root.unmount() })
root = null
container = null
dom.close()
dom = null
expect(hoverAt(0)).toBe(`${fmtDate(first.d, true, true)} · 70 kg`)
expect(hoverAt(340)).toBe(`${fmtDate(last.d, true, true)} · 71 kg`)
})
it('keeps a single point compact', () => {
const only = point(2026, 7, 4, 70)
renderChart([only])
expect(hoverAt(170)).toBe(`${fmtDate(only.d, true)} · 70 kg`)
})
it('uses timestamp-only points when deciding whether to show the year', () => {
const first = point(2025, 12, 31, 70)
const last = point(2026, 1, 1, 71)
const timestampOnly = [{ t: first.t, y: first.y }, { t: last.t, y: last.y }]
renderChart(timestampOnly)
const lastIso = isoOf(new Date(last.t))
expect(hoverAt(340)).toBe(`${fmtDate(lastIso, true, true)} · 71 kg`)
})
})
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)
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()
await render(nextPoints)
renderChart(nextPoints)
expect(container.querySelector('.ctip')).toBeNull()
expect(container.querySelector('.cvl')).toBeNull()
expect(container.querySelector('.chl')).toBeNull()
await hoverAt(170)
hoverAt(170)
expect(container.querySelector('.ctip')).toBeTruthy()
expect(container.querySelector('.cvl')).toBeTruthy()
})