diff --git a/app/src/pages/plans/plan-editor/ExerciseEditor.tsx b/app/src/pages/plans/plan-editor/ExerciseEditor.tsx index 9af5453..42c5b3d 100644 --- a/app/src/pages/plans/plan-editor/ExerciseEditor.tsx +++ b/app/src/pages/plans/plan-editor/ExerciseEditor.tsx @@ -1,7 +1,8 @@ -import { useState } from 'react'; +import { useEffect, useRef, useState } from 'react'; import { useTranslation } from 'react-i18next'; import { Activity, ChevronDown, ChevronUp, Minus, Plus, X } from 'lucide-react'; import { Badge, Button } from '../../../components/ui'; +import { cn } from '../../../lib/utils'; import { MUSCLE_COLORS, muscleName } from '../../../lib/labels'; import type { Exercise, ExerciseProgressPoint, LatestExerciseResult } from '../../../api/types'; import type { EditorExercise, EditorSet } from './types'; @@ -80,13 +81,56 @@ export function ExerciseEditor({ const [advanced, setAdvanced] = useState(false); const columns = SET_COLUMNS.filter((c) => !c.advanced || advanced); + /* Długa nazwa ćwiczenia jest przycinana — kliknięcie zwija/rozwija pełną nazwę, + hover (desktop) pokazuje title. Przycisk pojawia się dopiero, gdy nazwa + realnie się nie mieści (scrollWidth > clientWidth — nie zmieści się w całości). */ + const name = exerciseName(exercise.exerciseId); + const [nameExpanded, setNameExpanded] = useState(false); + const [nameTruncated, setNameTruncated] = useState(false); + const nameRef = useRef(null); + const attachNameRef = (el: HTMLElement | null) => { + nameRef.current = el; + }; + + useEffect(() => { + setNameExpanded(false); + }, [name]); + + useEffect(() => { + const el = nameRef.current; + if (!el) return; + const check = () => setNameTruncated(el.scrollWidth > el.clientWidth + 1); + check(); + const ro = new ResizeObserver(check); + ro.observe(el); + return () => ro.disconnect(); + }, [name, nameExpanded]); + + const nameInteractive = nameTruncated || nameExpanded; + return (
- - {exerciseName(exercise.exerciseId)} - + {nameInteractive ? ( + + ) : ( + + {name} + + )} {exerciseMeta && ( {muscleName(exerciseMeta.primaryMuscle)} diff --git a/app/test/ExerciseEditor.test.tsx b/app/test/ExerciseEditor.test.tsx new file mode 100644 index 0000000..0373b0c --- /dev/null +++ b/app/test/ExerciseEditor.test.tsx @@ -0,0 +1,83 @@ +import { render, screen } from '@testing-library/react'; +import userEvent from '@testing-library/user-event'; +import { describe, it, expect, afterEach } from 'vitest'; +import { ExerciseEditor } from '../src/pages/plans/plan-editor/ExerciseEditor'; +import type { EditorExercise } from '../src/pages/plans/plan-editor/types'; + +const LONG_NAME = 'Wiosłowanie sztangą nachwytem w pełnym opadzie tułowia'; + +const EXERCISE: EditorExercise = { + exerciseId: 'ex-1', + sets: [{ reps: '10', loadKg: '50', restSec: '90', rir: '2' }], +}; + +function renderEditor() { + return render( + LONG_NAME} + isTraineeMode={false} + resultsOpen={false} + onToggleResults={() => {}} + onRemove={() => {}} + onMoveUp={() => {}} + onMoveDown={() => {}} + canMoveUp={false} + canMoveDown={false} + onSetField={() => {}} + onAddSetCopy={() => {}} + onRemoveSet={() => {}} + latest={undefined} + points={[]} + onPrefill={() => {}} + />, + ); +} + +/** Symulacja realnego przycięcia tekstu: scrollWidth > clientWidth (jsdom zwraca same 0). */ +function mockTruncated(truncated: boolean) { + Object.defineProperty(Element.prototype, 'scrollWidth', { + configurable: true, + get: () => (truncated ? 400 : 0), + }); +} + +const scrollWidthDescriptor = Object.getOwnPropertyDescriptor(Element.prototype, 'scrollWidth'); + +afterEach(() => { + if (scrollWidthDescriptor) { + Object.defineProperty(Element.prototype, 'scrollWidth', scrollWidthDescriptor); + } else { + delete (Element.prototype as unknown as Record).scrollWidth; + } +}); + +describe('ExerciseEditor — rozwijanie długiej nazwy ćwiczenia', () => { + it('bez przycięcia renderuje samą nazwę (bez przycisku)', () => { + mockTruncated(false); + renderEditor(); + expect(screen.getByText(LONG_NAME)).toBeInTheDocument(); + expect(screen.queryByRole('button', { name: LONG_NAME })).not.toBeInTheDocument(); + }); + + it('po przycięciu nazwa staje się przyciskiem z title i rozwija się po kliknięciu', async () => { + const user = userEvent.setup(); + mockTruncated(true); + renderEditor(); + + const btn = screen.getByRole('button', { name: LONG_NAME }); + expect(btn).toHaveAttribute('title', LONG_NAME); + expect(btn).toHaveAttribute('aria-expanded', 'false'); + expect(btn).toHaveClass('truncate'); + + await user.click(btn); + expect(btn).toHaveAttribute('aria-expanded', 'true'); + expect(btn).toHaveClass('break-words'); + expect(btn).not.toHaveClass('truncate'); + + await user.click(btn); + expect(btn).toHaveAttribute('aria-expanded', 'false'); + expect(btn).toHaveClass('truncate'); + }); +});