Skip to content

Open-source gym & body-weight tracker, now in the App Store (iPhone + Apple Watch) and as an Android APK. Plan routines, log workouts, see which muscles are trained, import from Strong/Hevy/FitNotes. Self-host it or use it on the phone alone.

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

doby-llm0a69ee845046
diff --cc frontend/src/components/LineChart.test.jsx
index ea77a2d07,fe4181004..19bce1041
--- a/frontend/src/components/LineChart.test.jsx
+++ b/frontend/src/components/LineChart.test.jsx
@@@ -1,82 -1,74 +1,104 @@@
+ // @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' },
+ { 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)
+ 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])
+
+ 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)
- afterEach(async () => {
- if (root) await act(async () => { root.unmount() })
- root = null
- container = null
- dom.close()
- dom = null
+ 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()
+ })
+})