From bbf46e2aa2bf395168252dce69352a5affd94750 Mon Sep 17 00:00:00 2001 From: Duarte Santos Date: Tue, 25 Aug 2026 20:19:01 +0200 Subject: [PATCH] test: mount the note sheet for real MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The note sheet was checked structurally — imports resolve, symbols exist — but nothing ever rendered it, so a bad hook order or a wrong store path would have reached a phone before it reached a test. This drives it through the real sheet stack: both textareas, the pin (disabled while the note is empty, since pinning nothing means nothing), and a save that has to land in two different places — today's note on the entry, the standing note on S.exNotes. The third case is the one that would have bitten: clearing the note has to drop the pin with it, or a stale pin resurfaces a note that no longer exists. --- frontend/src/lib/notes-render.test.jsx | 80 ++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 frontend/src/lib/notes-render.test.jsx diff --git a/frontend/src/lib/notes-render.test.jsx b/frontend/src/lib/notes-render.test.jsx new file mode 100644 index 0000000..153f829 --- /dev/null +++ b/frontend/src/lib/notes-render.test.jsx @@ -0,0 +1,80 @@ +// @vitest-environment happy-dom +// The note sheet is new JSX and nothing else mounts it, so a bad hook order, a missing import or +// a wrong store path would only surface on a real device. Render it through the real sheet stack +// and drive a save, so the wiring is checked and not just the shape of the module. +import { describe, expect, it, beforeEach } from 'vitest' +import React, { act } from 'react' +import { createRoot } from 'react-dom/client' + +// React only enables act() when it is told it is in a test environment. +globalThis.IS_REACT_ACT_ENVIRONMENT = true +import { useStore } from '../store/useStore.js' +import { useUI } from '../store/useUI.js' +import { exerciseNoteSheet } from '../sheets.jsx' + +const activeWith = entry => ({ id: 'w1', d: '2026-08-25', start: 1, routineId: 'r1', name: 'Push', entries: [entry] }) + +function renderSheet() { + exerciseNoteSheet(0) + const sheet = useUI.getState().sheets.at(-1) + const host = document.createElement('div') + document.body.appendChild(host) + const root = createRoot(host) + act(() => root.render(sheet.render(sheet_close(sheet)))) + return host +} +const sheet_close = sheet => () => useUI.getState().closeSheet(sheet.id) + +const type = (el, value) => { + const setter = Object.getOwnPropertyDescriptor(el.constructor.prototype, 'value').set + setter.call(el, value) + el.dispatchEvent(new Event('input', { bubbles: true })) +} + +describe('exercise note sheet', () => { + beforeEach(() => { + useUI.setState({ sheets: [] }) + useStore.setState(s => ({ + S: { ...s.S, exNotes: {}, active: activeWith({ id: 'bench', sets: [] }) }, + })) + document.body.innerHTML = '' + }) + + it('renders the session note, the pin and the standing note', () => { + const host = renderSheet() + const areas = host.querySelectorAll('textarea') + expect(areas).toHaveLength(2) + // The pin cannot be set on an empty note — it would pin nothing. + expect(host.querySelector('[role="switch"]').disabled).toBe(true) + }) + + it('saves the session note with its pin and the standing note separately', () => { + const host = renderSheet() + const [today, always] = host.querySelectorAll('textarea') + act(() => { type(today, 'shoulder twinged'); type(always, 'seat at 4') }) + act(() => { host.querySelector('[role="switch"]').click() }) + const save = [...host.querySelectorAll('button')].find(b => /save/i.test(b.textContent)) + act(() => { save.click() }) + + const S = useStore.getState().S + expect(S.active.entries[0].note).toBe('shoulder twinged') + expect(S.active.entries[0].notePin).toBe(true) + // The standing note belongs to the exercise, not to today's entry. + expect(S.exNotes.bench).toBe('seat at 4') + expect(useUI.getState().sheets).toHaveLength(0) + }) + + it('clearing the session note drops the pin with it', () => { + useStore.setState(s => ({ + S: { ...s.S, active: activeWith({ id: 'bench', sets: [], note: 'old', notePin: true }) }, + })) + const host = renderSheet() + const [today] = host.querySelectorAll('textarea') + act(() => { type(today, '') }) + const save = [...host.querySelectorAll('button')].find(b => /save/i.test(b.textContent)) + act(() => { save.click() }) + const e = useStore.getState().S.active.entries[0] + expect(e.note).toBeUndefined() + expect(e.notePin).toBeUndefined() + }) +})