415 lines
18 KiB
TypeScript
415 lines
18 KiB
TypeScript
import { act, renderHook } from '@testing-library/react';
|
|
import { describe, it, expect } from 'vitest';
|
|
import {
|
|
usePlanEditorState,
|
|
type PlanLike,
|
|
} from '../src/pages/plans/plan-editor/usePlanEditorState';
|
|
import { withoutIds } from '../src/pages/plans/plan-editor/types';
|
|
|
|
describe('usePlanEditorState — akcje mutacji', () => {
|
|
it('addDay sortuje dni rosnąco, addExercise dodaje serię domyślną, dirty=true', () => {
|
|
const { result } = renderHook(() => usePlanEditorState());
|
|
act(() => result.current.addDay(0, 3));
|
|
act(() => result.current.addDay(0, 1));
|
|
expect(result.current.weeks[0].days.map((d) => d.dayOfWeek)).toEqual([1, 3]);
|
|
act(() => result.current.addExercise(0, 0, 'ex-1'));
|
|
expect(result.current.weeks[0].days[0].exercises).toHaveLength(1);
|
|
expect(result.current.weeks[0].days[0].exercises[0].sets).toHaveLength(1);
|
|
expect(result.current.dirty).toBe(true);
|
|
});
|
|
|
|
it('moveExercise zamienia kolejność ćwiczeń (poza zakresem — bez zmian)', () => {
|
|
const { result } = renderHook(() => usePlanEditorState());
|
|
act(() => result.current.addDay(0, 1));
|
|
act(() => result.current.addExercise(0, 0, 'ex-1'));
|
|
act(() => result.current.addExercise(0, 0, 'ex-2'));
|
|
act(() => result.current.moveExercise(0, 0, 1, -1));
|
|
expect(result.current.weeks[0].days[0].exercises.map((e) => e.exerciseId)).toEqual([
|
|
'ex-2',
|
|
'ex-1',
|
|
]);
|
|
act(() => result.current.moveExercise(0, 0, 0, -1));
|
|
expect(result.current.weeks[0].days[0].exercises.map((e) => e.exerciseId)).toEqual([
|
|
'ex-2',
|
|
'ex-1',
|
|
]);
|
|
});
|
|
|
|
it('duplicateSet kopiuje serię za nią; removeSet usuwa (ostatnia seria usuwa ćwiczenie)', () => {
|
|
const { result } = renderHook(() => usePlanEditorState());
|
|
act(() => result.current.addDay(0, 1));
|
|
act(() => result.current.addExercise(0, 0, 'ex-1'));
|
|
act(() => result.current.setSetField(0, 0, 0, 0, 'reps', '12'));
|
|
act(() => result.current.duplicateSet(0, 0, 0, 0));
|
|
const sets = result.current.weeks[0].days[0].exercises[0].sets;
|
|
expect(sets).toHaveLength(2);
|
|
expect(sets[1].reps).toBe('12');
|
|
act(() => result.current.removeSet(0, 0, 0, 0));
|
|
expect(result.current.weeks[0].days[0].exercises[0].sets).toHaveLength(1);
|
|
act(() => result.current.removeSet(0, 0, 0, 0));
|
|
expect(result.current.weeks[0].days[0].exercises).toHaveLength(0);
|
|
});
|
|
|
|
it('addSetCopy kopiuje ostatnią serię (stepper „+”); addSet zostawia wartości domyślne', () => {
|
|
const { result } = renderHook(() => usePlanEditorState());
|
|
act(() => result.current.addDay(0, 1));
|
|
act(() => result.current.addExercise(0, 0, 'ex-1'));
|
|
act(() => result.current.setSetField(0, 0, 0, 0, 'reps', '12'));
|
|
act(() => result.current.addSetCopy(0, 0, 0));
|
|
const sets = result.current.weeks[0].days[0].exercises[0].sets;
|
|
expect(sets).toHaveLength(2);
|
|
expect(sets[1].reps).toBe('12');
|
|
expect(sets[1].loadKg).toBe('');
|
|
expect(sets[1].restSec).toBe('180');
|
|
act(() => result.current.addSet(0, 0, 0));
|
|
const added = result.current.weeks[0].days[0].exercises[0].sets[2];
|
|
expect(added.reps).toBe('10');
|
|
// domyślna przerwa nowej serii to pełna minuta (3:00), nie sekundy
|
|
expect(added.restSec).toBe('180');
|
|
});
|
|
|
|
it('detachWeek odłącza lustrzany tydzień (niezależna kopia dni z tygodnia 1)', () => {
|
|
const { result } = renderHook(() => usePlanEditorState());
|
|
act(() => result.current.addDay(0, 1));
|
|
act(() => result.current.addExercise(0, 0, 'ex-1'));
|
|
expect(result.current.weeks[1].detached).toBe(false);
|
|
act(() => result.current.detachWeek(1));
|
|
expect(result.current.weeks[1].detached).toBe(true);
|
|
expect(result.current.weeks[1].days).toHaveLength(1);
|
|
expect(result.current.weeks[1].days[0].exercises).toHaveLength(1);
|
|
// niezależna kopia — rozbudowa tygodnia 1 nie zmienia odłączonego tygodnia
|
|
act(() => result.current.addDay(0, 3));
|
|
expect(result.current.weeks[1].days).toHaveLength(1);
|
|
// tydzień 1 — detachWeek to no-op (dni zostają)
|
|
act(() => result.current.detachWeek(0));
|
|
expect(result.current.weeks[0].days).toHaveLength(2);
|
|
});
|
|
|
|
it('copyFromWeek kopiuje z dowolnego tygodnia (rozwiązuje lustrzane źródło; self = no-op)', () => {
|
|
const { result } = renderHook(() => usePlanEditorState());
|
|
act(() => result.current.addDay(0, 1));
|
|
act(() => result.current.addDay(0, 3));
|
|
// źródło lustrzane: tydzień 2 (indeks 1) nieodłączony → kopiowane są dni tygodnia 1
|
|
act(() => result.current.copyFromWeek(3, 1));
|
|
expect(result.current.weeks[3].detached).toBe(true);
|
|
expect(result.current.weeks[3].days.map((d) => d.dayOfWeek)).toEqual([1, 3]);
|
|
// własne dni źródła odłączonego: tydzień 3 (indeks 2) po edycji ma inny zestaw
|
|
act(() => result.current.addDay(2, 5));
|
|
act(() => result.current.copyFromWeek(0, 2));
|
|
expect(result.current.weeks[0].days.map((d) => d.dayOfWeek)).toEqual([1, 3, 5]);
|
|
// kopiowanie „z siebie” — bez zmian
|
|
act(() => result.current.copyFromWeek(2, 2));
|
|
expect(result.current.weeks[2].days).toHaveLength(3);
|
|
});
|
|
|
|
it('copyFromWeek odzwierciedla tydzień 1; clearWeek czyści; setWeeksCount skaluje', () => {
|
|
const { result } = renderHook(() => usePlanEditorState());
|
|
act(() => result.current.addDay(0, 1));
|
|
act(() => result.current.addExercise(0, 0, 'ex-1'));
|
|
act(() => result.current.copyFromWeek(1, 0));
|
|
expect(result.current.weeks[1].days).toHaveLength(1);
|
|
expect(result.current.weeks[1].detached).toBe(true);
|
|
act(() => result.current.clearWeek(1));
|
|
expect(result.current.weeks[1].days).toHaveLength(0);
|
|
act(() => result.current.setWeeksCount(2));
|
|
expect(result.current.weeks).toHaveLength(2);
|
|
act(() => result.current.setWeeksCount(1));
|
|
expect(result.current.weeks).toHaveLength(1);
|
|
});
|
|
|
|
it('copyDayTo podmienia bieżący dzień treścią dnia z innego dnia/tygodnia (kopia bez id)', () => {
|
|
const { result } = renderHook(() => usePlanEditorState());
|
|
act(() => result.current.addDay(0, 1));
|
|
act(() => result.current.addExercise(0, 0, 'ex-1'));
|
|
act(() => result.current.addDay(0, 3));
|
|
act(() => result.current.addExercise(0, 1, 'ex-2'));
|
|
act(() => result.current.addDay(0, 5));
|
|
|
|
// Piątek przejmuje zawartość Środy — dzień zostaje pod Piątkiem
|
|
act(() => result.current.copyDayTo(0, 5, 0, 3));
|
|
const fri = result.current.weeks[0].days.find((d) => d.dayOfWeek === 5);
|
|
expect(fri?.exercises.map((e) => e.exerciseId)).toEqual(['ex-2']);
|
|
expect(result.current.weeks[0].days.map((d) => d.dayOfWeek)).toEqual([1, 3, 5]);
|
|
|
|
// źródło lustrzane: tydzień 2 (mirror) rozwiązuje się do tygodnia 1; cel się odłącza
|
|
act(() => result.current.copyDayTo(2, 7, 1, 1));
|
|
expect(result.current.weeks[2].detached).toBe(true);
|
|
const sun = result.current.weeks[2].days.find((d) => d.dayOfWeek === 7);
|
|
expect(sun?.exercises.map((e) => e.exerciseId)).toEqual(['ex-1']);
|
|
|
|
// kopiowanie w siebie (ten sam tydzień + ten sam dzień) — bez zmian
|
|
act(() => result.current.copyDayTo(0, 1, 0, 1));
|
|
expect(result.current.weeks[0].days).toHaveLength(3);
|
|
|
|
// nieistniejące źródło (tydzień 2 po odłączeniu ma tylko Niedzielę) — bez zmian
|
|
act(() => result.current.copyDayTo(0, 1, 2, 5));
|
|
expect(
|
|
result.current.weeks[0].days
|
|
.find((d) => d.dayOfWeek === 1)
|
|
?.exercises.map((e) => e.exerciseId),
|
|
).toEqual(['ex-1']);
|
|
|
|
// kopia = nowy wiersz — id wierszy źródła zrzucone
|
|
const strippedFri = result.current.weeks[0].days.find((d) => d.dayOfWeek === 5);
|
|
expect(strippedFri?.id).toBeUndefined();
|
|
expect(strippedFri?.exercises[0].id).toBeUndefined();
|
|
});
|
|
|
|
it('prefillExercise wypełnia serie z ostatniej sesji (brak danych → domyślne)', () => {
|
|
const { result } = renderHook(() => usePlanEditorState());
|
|
act(() => result.current.addDay(0, 1));
|
|
act(() => result.current.addExercise(0, 0, 'ex-1'));
|
|
act(() =>
|
|
result.current.prefillExercise(0, 0, 0, {
|
|
exerciseId: 'ex-1',
|
|
name: 'Przysiady',
|
|
muscle: 'legs',
|
|
date: '2026-08-20',
|
|
sets: [
|
|
{ reps: 12, loadKg: 70 },
|
|
{ reps: null, loadKg: null },
|
|
],
|
|
oneRm: 80.6,
|
|
}),
|
|
);
|
|
const sets = result.current.weeks[0].days[0].exercises[0].sets;
|
|
expect(sets[0].reps).toBe('12');
|
|
expect(sets[0].loadKg).toBe('70');
|
|
expect(sets[1].reps).toBe('10');
|
|
// seria historii bez obciążenia (masa ciała) → puste, nie sztywne 20 kg
|
|
expect(sets[1].loadKg).toBe('');
|
|
});
|
|
|
|
it('gymId: setGymId zmienia stan i dirty; initFromPlan wczytuje siłownię planu', () => {
|
|
const { result } = renderHook(() => usePlanEditorState());
|
|
expect(result.current.gymId).toBeNull();
|
|
act(() => result.current.setGymId('gym-1'));
|
|
expect(result.current.gymId).toBe('gym-1');
|
|
expect(result.current.dirty).toBe(true);
|
|
act(() => result.current.setGymId(null));
|
|
expect(result.current.gymId).toBeNull();
|
|
|
|
const plan: PlanLike = {
|
|
title: 'Plan',
|
|
weeksCount: 1,
|
|
gym: { id: 'gym-9', name: 'Siłownia' },
|
|
weeks: [{ index: 0, customName: 'Deload', days: [] }],
|
|
};
|
|
act(() => result.current.initFromPlan(plan));
|
|
expect(result.current.gymId).toBe('gym-9');
|
|
expect(result.current.weeks[0].customName).toBe('Deload');
|
|
expect(result.current.dirty).toBe(false);
|
|
});
|
|
|
|
it('setWeekAlias ustawia alias bez odłączania tygodnia; copyFromWeek nie kopiuje aliasu', () => {
|
|
const { result } = renderHook(() => usePlanEditorState());
|
|
act(() => result.current.addDay(0, 1));
|
|
act(() => result.current.setWeekAlias(1, 'Deload'));
|
|
// alias nie odłącza tygodnia od lustrzanego tygodnia 1
|
|
expect(result.current.weeks[1].customName).toBe('Deload');
|
|
expect(result.current.weeks[1].detached).toBe(false);
|
|
// alias dłuższy niż 40 znaków jest obcięty
|
|
act(() => result.current.setWeekAlias(1, 'a'.repeat(60)));
|
|
expect(result.current.weeks[1].customName).toHaveLength(40);
|
|
|
|
act(() => result.current.copyFromWeek(1, 0));
|
|
// copyFromWeek kopiuje dni, ale nie alias
|
|
expect(result.current.weeks[1].days).toHaveLength(1);
|
|
expect(result.current.weeks[1].customName).toBe('a'.repeat(40));
|
|
act(() => result.current.setWeekAlias(2, 'Przerwa'));
|
|
act(() => result.current.copyFromWeek(2, 1));
|
|
expect(result.current.weeks[2].customName).toBe('Przerwa');
|
|
});
|
|
|
|
it('dirty: start=false, po zmianie=true; initFromPlan resetuje snapshot', () => {
|
|
const { result } = renderHook(() => usePlanEditorState());
|
|
expect(result.current.dirty).toBe(false);
|
|
act(() => result.current.setTitle('X'));
|
|
expect(result.current.dirty).toBe(true);
|
|
const plan: PlanLike = {
|
|
title: 'Plan',
|
|
weeksCount: 2,
|
|
weeks: [
|
|
{
|
|
index: 0,
|
|
days: [
|
|
{
|
|
dayOfWeek: 1,
|
|
exercises: [
|
|
{
|
|
exerciseId: 'ex-1',
|
|
sets: [{ reps: 10, loadKg: 20, restSec: 90, rir: 2 }],
|
|
},
|
|
],
|
|
},
|
|
],
|
|
},
|
|
{ index: 1, days: [] },
|
|
],
|
|
};
|
|
act(() => result.current.initFromPlan(plan));
|
|
expect(result.current.initialized).toBe(true);
|
|
expect(result.current.title).toBe('Plan');
|
|
expect(result.current.weeksCount).toBe(2);
|
|
expect(result.current.dirty).toBe(false);
|
|
act(() => result.current.setTitle('Plan 2'));
|
|
expect(result.current.dirty).toBe(true);
|
|
});
|
|
|
|
it('id wierszy: initFromPlan zachowuje; kopie (copyFromWeek, rozbudowa tygodni) zrzucają', () => {
|
|
const { result } = renderHook(() => usePlanEditorState());
|
|
const plan: PlanLike = {
|
|
title: 'Plan',
|
|
weeksCount: 2,
|
|
weeks: [
|
|
{
|
|
index: 0,
|
|
days: [
|
|
{
|
|
id: 'td-1',
|
|
dayOfWeek: 1,
|
|
exercises: [
|
|
{
|
|
id: 'tpe-1',
|
|
exerciseId: 'ex-1',
|
|
sets: [{ id: 'tps-1', reps: 10, loadKg: 20, restSec: 90, rir: 2 }],
|
|
},
|
|
],
|
|
},
|
|
],
|
|
},
|
|
{
|
|
index: 1,
|
|
days: [
|
|
{
|
|
id: 'td-2',
|
|
dayOfWeek: 3,
|
|
exercises: [{ id: 'tpe-2', exerciseId: 'ex-2', sets: [{ id: 'tps-2', reps: 8 }] }],
|
|
},
|
|
],
|
|
},
|
|
],
|
|
};
|
|
act(() => result.current.initFromPlan(plan));
|
|
expect(result.current.weeks[0].days[0].id).toBe('td-1');
|
|
expect(result.current.weeks[0].days[0].exercises[0].id).toBe('tpe-1');
|
|
expect(result.current.weeks[0].days[0].exercises[0].sets[0].id).toBe('tps-1');
|
|
|
|
// „Kopiuj z…” = nowe wiersze w bazie — kopia nie dziedziczy id źródła
|
|
act(() => result.current.copyFromWeek(1, 0));
|
|
expect(result.current.weeks[1].days[0].id).toBeUndefined();
|
|
expect(result.current.weeks[1].days[0].exercises[0].id).toBeUndefined();
|
|
expect(result.current.weeks[1].days[0].exercises[0].sets[0].id).toBeUndefined();
|
|
|
|
// „Kopiuj dzień z…” = podmiana dnia nowym wierszem — id celu też zrzucane
|
|
act(() => result.current.copyDayTo(0, 1, 1, 1));
|
|
expect(result.current.weeks[0].days[0].id).toBeUndefined();
|
|
expect(result.current.weeks[0].days[0].exercises[0].sets[0].id).toBeUndefined();
|
|
|
|
// rozbudowa liczby tygodni: kopia ostatniego tygodnia tez bez id
|
|
act(() => result.current.initFromPlan(plan));
|
|
act(() => result.current.setWeeksCount(3));
|
|
expect(result.current.weeks[2].days[0].id).toBeUndefined();
|
|
expect(result.current.weeks[2].days[0].exercises[0].sets[0].id).toBeUndefined();
|
|
|
|
// edycja w obrębie tygodnia zachowuje id (ten sam wiersz), nowe ćwiczenie — bez id
|
|
act(() => result.current.addExercise(0, 0, 'ex-3'));
|
|
expect(result.current.weeks[0].days[0].id).toBe('td-1');
|
|
expect(result.current.weeks[0].days[0].exercises[1].id).toBeUndefined();
|
|
});
|
|
|
|
it('kopia tygodnia/dnia omija dni wypadające w przeszłości (start = bieżący tydzień)', () => {
|
|
const { result } = renderHook(() => usePlanEditorState());
|
|
const now = new Date();
|
|
const mon = new Date(now.getFullYear(), now.getMonth(), now.getDate());
|
|
mon.setDate(mon.getDate() - ((mon.getDay() + 6) % 7)); // poniedziałek bieżącego tygodnia
|
|
const dowToday = ((now.getDay() + 6) % 7) + 1; // 1=Pn .. 7=Nd
|
|
if (dowToday <= 1) return; // w poniedziałek bieżący tydzień nie ma minionych dni
|
|
const start = `${mon.getFullYear()}-${String(mon.getMonth() + 1).padStart(2, '0')}-${String(
|
|
mon.getDate(),
|
|
).padStart(2, '0')}`;
|
|
const plan: PlanLike = {
|
|
title: 't',
|
|
weeksCount: 2,
|
|
startDate: start,
|
|
weeks: [
|
|
{ index: 0, days: [] },
|
|
{
|
|
index: 1,
|
|
days: [1, 2, 3, 4, 5, 6, 7].map((dow) => ({
|
|
dayOfWeek: dow,
|
|
exercises: [{ exerciseId: `ex-${dow}`, sets: [{ reps: '10' }] }],
|
|
})),
|
|
},
|
|
],
|
|
};
|
|
act(() => result.current.initFromPlan(plan));
|
|
|
|
// przyszły tydzień → tydzień startu: dni sprzed dzisiaj nie przejdą (3. tydzień
|
|
// ma poniedziałek w przyszłości prawidłowo, ale kopia do bieżącego go omija)
|
|
act(() => result.current.copyFromWeek(0, 1));
|
|
const expected = Array.from({ length: 8 - dowToday }, (_, i) => dowToday + i);
|
|
expect(result.current.weeks[0].days.map((d) => d.dayOfWeek)).toEqual(expected);
|
|
|
|
// copyDayTo na cel w przeszłości — bez zmian (zabezpieczenie ścieżek poza UI)
|
|
act(() => result.current.copyDayTo(0, dowToday - 1, 1, 7));
|
|
expect(result.current.weeks[0].days.some((d) => d.dayOfWeek === dowToday - 1)).toBe(false);
|
|
|
|
// ten sam dzień w TYGODNIU 2 (przyszłym) — cel poprawny, kopia przechodzi
|
|
act(() => result.current.copyDayTo(1, dowToday - 1, 1, 7));
|
|
expect(
|
|
result.current.weeks[1].days
|
|
.find((d) => d.dayOfWeek === dowToday - 1)
|
|
?.exercises.map((e) => e.exerciseId),
|
|
).toEqual(['ex-7']);
|
|
});
|
|
|
|
it('withoutIds zrzuca id dni/ćwiczeń/serii (bezpieczeństwo lustrzanych kopii)', () => {
|
|
const stripped = withoutIds([
|
|
{
|
|
id: 'td-1',
|
|
dayOfWeek: 1,
|
|
exercises: [{ id: 'tpe-1', exerciseId: 'ex-1', sets: [{ id: 'tps-1', reps: '10' }] }],
|
|
},
|
|
]);
|
|
expect(stripped[0].id).toBeUndefined();
|
|
expect(stripped[0].exercises[0].id).toBeUndefined();
|
|
expect(stripped[0].exercises[0].sets[0].id).toBeUndefined();
|
|
expect(stripped[0].exercises[0].sets[0].reps).toBe('10');
|
|
});
|
|
|
|
it('dzień z historią (status/sesje z API): hasHistory ustawione; kopia tygodnia je gubi', () => {
|
|
const { result } = renderHook(() => usePlanEditorState());
|
|
const plan: PlanLike = {
|
|
title: 't',
|
|
weeksCount: 2,
|
|
weeks: [
|
|
{
|
|
index: 0,
|
|
days: [
|
|
{
|
|
id: 'td-1',
|
|
dayOfWeek: 1,
|
|
status: 'COMPLETED',
|
|
sessions: [{ id: 'ts-1' }],
|
|
exercises: [],
|
|
},
|
|
{ id: 'td-2', dayOfWeek: 3, status: 'SKIPPED', sessions: [], exercises: [] },
|
|
{ id: 'td-3', dayOfWeek: 5, status: 'PLANNED', sessions: [], exercises: [] },
|
|
],
|
|
},
|
|
{ index: 1, days: [] },
|
|
],
|
|
};
|
|
act(() => result.current.initFromPlan(plan));
|
|
expect(result.current.weeks[0].days[0].hasHistory).toBe(true);
|
|
expect(result.current.weeks[0].days[1].hasHistory).toBe(true);
|
|
expect(result.current.weeks[0].days[2].hasHistory).toBe(false);
|
|
|
|
// kopia dni to nowe wiersze — historia należy do wierszy źródłowych
|
|
act(() => result.current.copyFromWeek(1, 0));
|
|
expect(result.current.weeks[1].days[0].id).toBeUndefined();
|
|
expect(result.current.weeks[1].days[0].hasHistory).toBeUndefined();
|
|
});
|
|
});
|