diff --git a/frontend/src/components/Heatmap.jsx b/frontend/src/components/Heatmap.jsx index 03c6179..8ecc29a 100644 --- a/frontend/src/components/Heatmap.jsx +++ b/frontend/src/components/Heatmap.jsx @@ -1,5 +1,5 @@ import { useEffect, useRef } from 'react' -import { fmtVol, isoOf, todayISO, MONTHS } from '../lib/format.js' +import { fmtVol, isoOf, todayISO, MONTHS, weekStartOf, weekDayOffset } from '../lib/format.js' import { t } from '../lib/i18n.js' import { tappable } from '../lib/use-sheet-keyboard.js' @@ -20,9 +20,15 @@ export default function Heatmap({ S, onDay }) { const level = a => !a ? 0 : !a.min ? 1 : a.min >= t3 ? 4 : a.min >= t2 ? 3 : a.min >= t1 ? 2 : 1 const today = new Date(); today.setHours(12, 0, 0, 0) - const end = new Date(today); end.setDate(today.getDate() - ((today.getDay() + 6) % 7)) + const ws = weekStartOf(S) + const end = new Date(today); end.setDate(today.getDate() - weekDayOffset(today.getDay(), ws)) const start = new Date(end); start.setDate(end.getDate() - 52 * 7) + const dayLabels = Array(7).fill(null) + dayLabels[weekDayOffset(1, ws)] = 'Mon' + dayLabels[weekDayOffset(3, ws)] = 'Wed' + dayLabels[weekDayOffset(5, ws)] = 'Fri' + const months = [], cols = [] let lastMonth = -1 for (let wk = 0; wk <= 52; wk++) { @@ -48,7 +54,7 @@ export default function Heatmap({ S, onDay }) {
{months}
-
{t('Mon')}{t('Wed')}{t('Fri')}
+
{dayLabels.map((lbl, i) => {lbl ? t(lbl) : ''})}
{cols}
diff --git a/frontend/src/components/Heatmap.test.jsx b/frontend/src/components/Heatmap.test.jsx new file mode 100644 index 0000000..158a051 --- /dev/null +++ b/frontend/src/components/Heatmap.test.jsx @@ -0,0 +1,53 @@ +// @vitest-environment happy-dom +// The heatmap used to hardcode Monday-first weeks. It should follow S.weekStart like every +// other "this week" surface (see lib/week-start.test.js and views/week-start.test.jsx). +import React, { act } from 'react' +import { createRoot } from 'react-dom/client' +import { afterEach, beforeEach, describe, expect, it } from 'vitest' +import Heatmap from './Heatmap.jsx' +import { isoOf } from '../lib/format.js' + +globalThis.IS_REACT_ACT_ENVIRONMENT = true + +let container, root +beforeEach(() => { + container = document.createElement('div') + document.body.appendChild(container) + root = createRoot(container) +}) +afterEach(() => { + act(() => root.unmount()) + container.remove() +}) + +const baseS = { workouts: [], unit: 'kg' } +const render = weekStart => act(() => root.render( {}} />)) +const dayLabels = () => [...container.querySelectorAll('.hm-days span')].map(s => s.textContent) +const lastColumn = () => { + const cols = container.querySelectorAll('.hm-grid .hm-col') + return [...cols[cols.length - 1].querySelectorAll('.hm-c')] +} + +describe('Heatmap — week start', () => { + it('rows read Mon/Wed/Fri for a Monday-first week (default)', () => { + render(undefined) + expect(dayLabels()).toEqual(['Mon', '', 'Wed', '', 'Fri', '', '']) + }) + + it('shifts the same three labels down a row for a Sunday-first week', () => { + render(0) + expect(dayLabels()).toEqual(['', 'Mon', '', 'Wed', '', 'Fri', '']) + }) + + it('the current (rightmost) column starts on the chosen week-start day and includes today', () => { + render(1) + let cells = lastColumn() + expect(new Date(cells[0].title.slice(0, 10) + 'T12:00:00').getDay()).toBe(1) + expect(cells.map(c => c.title.slice(0, 10))).toContain(isoOf(new Date())) + + render(0) + cells = lastColumn() + expect(new Date(cells[0].title.slice(0, 10) + 'T12:00:00').getDay()).toBe(0) + expect(cells.map(c => c.title.slice(0, 10))).toContain(isoOf(new Date())) + }) +})