Plany: wizard 3-etapowy tworzenia/edycji, silownia i alias tygodnia (UX + API + migracja)
This commit is contained in:
@@ -40,7 +40,7 @@ interface Capture {
|
||||
createPlanBody?: Record<string, unknown>;
|
||||
}
|
||||
|
||||
function mockApi(capture: Capture) {
|
||||
function mockApi(capture: Capture, gyms: { id: string; name: string }[] = []) {
|
||||
vi.stubGlobal(
|
||||
'fetch',
|
||||
vi.fn(async (input: RequestInfo | URL, init?: RequestInit) => {
|
||||
@@ -62,7 +62,7 @@ function mockApi(capture: Capture) {
|
||||
extra: null,
|
||||
missing: null,
|
||||
});
|
||||
if (url.includes('/gyms')) return jsonResponse(200, { gyms: [] });
|
||||
if (url.includes('/gyms')) return jsonResponse(200, { gyms });
|
||||
if (url.includes('/trainer/invitations')) return jsonResponse(200, { invitations: [] });
|
||||
if (url.includes('/plans/plan-new'))
|
||||
return jsonResponse(200, {
|
||||
@@ -212,6 +212,70 @@ describe('PlanEditor — data startu i menu kontekstowe', () => {
|
||||
expect(capture.createPlanBody).not.toHaveProperty('startDate');
|
||||
});
|
||||
|
||||
it('wizard: krok 1 blokuje „Dalej” bez tytułu; select siłowni z /gyms; zapis z gymId i aliasem', async () => {
|
||||
const capture: Capture = {};
|
||||
mockApi(capture, [{ id: 'gym-1', name: 'Siłownia Testowa' }]);
|
||||
const user = userEvent.setup();
|
||||
renderApp();
|
||||
await loginAndOpenNewPlan(user);
|
||||
|
||||
// krok 1: „Dalej” zablokowany przy pustym tytule
|
||||
expect(screen.getByRole('button', { name: 'Dalej' })).toBeDisabled();
|
||||
await user.type(screen.getByPlaceholderText('np. Plan masowy 8-tygodniowy'), 'Plan wizard');
|
||||
expect(screen.getByRole('button', { name: 'Dalej' })).toBeEnabled();
|
||||
// 1 tydzień — podsumowanie bez lustrzanych tygodni
|
||||
const weeksField = screen.getByText('Długość (tygodnie)').closest('[data-slot="field"]') as HTMLElement;
|
||||
await user.selectOptions(within(weeksField).getByRole('combobox'), '1');
|
||||
await user.click(screen.getByRole('button', { name: 'Dalej' }));
|
||||
|
||||
// krok 2: select siłowni (cłonkostwa zalogowanego) + dzień + ćwiczenie + alias
|
||||
const gymField = screen.getByText('Siłownia').closest('[data-slot="field"]') as HTMLElement;
|
||||
const gymSelect = within(gymField).getByRole('combobox');
|
||||
expect(within(gymSelect).getByRole('option', { name: 'Bez siłowni' })).toBeInTheDocument();
|
||||
expect(within(gymSelect).getByRole('option', { name: 'Siłownia Testowa' })).toBeInTheDocument();
|
||||
await user.selectOptions(gymSelect, 'gym-1');
|
||||
|
||||
await user.click(screen.getByRole('button', { name: 'Poniedziałek' }));
|
||||
await user.click(screen.getByRole('button', { name: 'dodaj ćwiczenie…' }));
|
||||
await user.click(await screen.findByRole('option', { name: /Przysiady/ }));
|
||||
const aliasField = screen
|
||||
.getByText('Nazwa tygodnia (opcjonalnie)')
|
||||
.closest('[data-slot="field"]') as HTMLElement;
|
||||
await user.type(within(aliasField).getByRole('textbox'), 'Deload');
|
||||
|
||||
// krok 3: podsumowanie — siłownia, alias, „szkic — bez startu”, liczniki
|
||||
await user.click(screen.getByRole('button', { name: 'Dalej' }));
|
||||
expect(await screen.findByText('Deload')).toBeInTheDocument();
|
||||
expect(screen.getByText('Siłownia Testowa')).toBeInTheDocument();
|
||||
expect(screen.getByText('szkic — bez startu')).toBeInTheDocument();
|
||||
// liczniki w karcie podsumowania (w stopce jest identyczny tekst — zakres = karta)
|
||||
const summaryCard = screen
|
||||
.getByText('Łącznie: 1 dzień · 1 ćwiczenie · 1 seria')
|
||||
.closest('[data-slot="card"]') as HTMLElement;
|
||||
expect(within(summaryCard).getByText('Pon · 1 dzień · 1 ćwiczenie · 1 seria')).toBeInTheDocument();
|
||||
|
||||
// zapis — body z gymId i customName
|
||||
await user.click(screen.getByRole('button', { name: 'Zapisz' }));
|
||||
await waitFor(() => expect(capture.createPlanBody).toBeDefined());
|
||||
expect(capture.createPlanBody).toMatchObject({
|
||||
title: 'Plan wizard',
|
||||
gymId: 'gym-1',
|
||||
weeks: [expect.objectContaining({ index: 0, customName: 'Deload' })],
|
||||
});
|
||||
});
|
||||
|
||||
it('wizard: pusta lista siłowni — informacja z linkiem do /gyms', async () => {
|
||||
mockApi({});
|
||||
const user = userEvent.setup();
|
||||
renderApp();
|
||||
await loginAndOpenNewPlan(user);
|
||||
|
||||
await user.type(screen.getByPlaceholderText('np. Plan masowy 8-tygodniowy'), 'Plan');
|
||||
await user.click(screen.getByRole('button', { name: 'Dalej' }));
|
||||
expect(screen.getByText('Nie należysz do żadnej siłowni.')).toBeInTheDocument();
|
||||
expect(screen.getByRole('link', { name: 'Zarządzaj siłowniami' })).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('pokazuje „Wstecz” w treści formularza na /plans/new', async () => {
|
||||
mockApi({});
|
||||
const user = userEvent.setup();
|
||||
|
||||
@@ -405,6 +405,7 @@ export const TEST_TRAINEE_PLANS = [
|
||||
status: 'DRAFT',
|
||||
weeksCount: 1,
|
||||
startDate: null,
|
||||
gym: null,
|
||||
creator: { id: 'trainer-2', firstName: 'Jan', lastName: 'Kowalski' },
|
||||
weeks: [],
|
||||
},
|
||||
@@ -414,6 +415,7 @@ export const TEST_TRAINEE_PLANS = [
|
||||
status: 'ACTIVE',
|
||||
weeksCount: 4,
|
||||
startDate: '2026-08-24',
|
||||
gym: { id: 'gym-1', name: 'Żelazna Świątynia' },
|
||||
creator: null,
|
||||
weeks: [],
|
||||
},
|
||||
@@ -425,11 +427,13 @@ export const TEST_TRAINEE_PLAN_DETAIL = {
|
||||
status: 'DRAFT',
|
||||
weeksCount: 1,
|
||||
startDate: null,
|
||||
gym: null,
|
||||
creator: { id: 'trainer-2', firstName: 'Jan', lastName: 'Kowalski' },
|
||||
weeks: [
|
||||
{
|
||||
id: 'tw-1',
|
||||
index: 0,
|
||||
customName: null,
|
||||
days: [
|
||||
{
|
||||
id: 'td-1',
|
||||
@@ -461,11 +465,13 @@ export const TEST_TRAINEE_PLAN_DETAIL_ACTIVE = {
|
||||
status: 'ACTIVE',
|
||||
weeksCount: 4,
|
||||
startDate: '2026-08-24',
|
||||
gym: { id: 'gym-1', name: 'Żelazna Świątynia' },
|
||||
creator: null,
|
||||
weeks: [
|
||||
{
|
||||
id: 'tw-1',
|
||||
index: 0,
|
||||
customName: null,
|
||||
days: [
|
||||
{
|
||||
id: 'td-1',
|
||||
@@ -1380,6 +1386,7 @@ export function mockPlans(page: Page) {
|
||||
status: 'ACTIVE',
|
||||
weeksCount: 4,
|
||||
startDate: '2026-08-24',
|
||||
gym: null,
|
||||
weeks: [],
|
||||
},
|
||||
}),
|
||||
@@ -1388,10 +1395,18 @@ export function mockPlans(page: Page) {
|
||||
page.route('**/api/v1/plans', (route) => {
|
||||
const req = route.request();
|
||||
if (req.method() === 'POST') {
|
||||
// echo gymId z ciała żądania (customName w tygodniach nie ma wpływu na odpowiedź)
|
||||
let gym: { id: string; name: string } | null = null;
|
||||
try {
|
||||
const body = req.postDataJSON() as { gymId?: string | null };
|
||||
if (body.gymId) gym = { id: body.gymId, name: 'Żelazna Świątynia' };
|
||||
} catch {
|
||||
// brak ciała — bez siłowni
|
||||
}
|
||||
return route.fulfill({
|
||||
status: 201,
|
||||
contentType: 'application/json',
|
||||
body: JSON.stringify({ plan: { id: 'plan-new' } }),
|
||||
body: JSON.stringify({ plan: { id: 'plan-new', gym } }),
|
||||
});
|
||||
}
|
||||
return route.fulfill({
|
||||
@@ -1445,7 +1460,19 @@ export function mockTraineePlans(page: Page, opts: MockApiOptions = {}) {
|
||||
page.route('**/api/v1/trainer/trainees/*/plans', (route) => {
|
||||
const req = route.request();
|
||||
if (req.method() === 'POST') {
|
||||
return route.fulfill({ status: 201, contentType: 'application/json', body: JSON.stringify({ plan: { id: 'tp-new' } }) });
|
||||
// echo gymId z ciała żądania (customName w tygodniach nie ma wpływu na odpowiedź)
|
||||
let gym: { id: string; name: string } | null = null;
|
||||
try {
|
||||
const body = req.postDataJSON() as { gymId?: string | null };
|
||||
if (body.gymId) gym = { id: body.gymId, name: 'Żelazna Świątynia' };
|
||||
} catch {
|
||||
// brak ciała — bez siłowni
|
||||
}
|
||||
return route.fulfill({
|
||||
status: 201,
|
||||
contentType: 'application/json',
|
||||
body: JSON.stringify({ plan: { id: 'tp-new', gym } }),
|
||||
});
|
||||
}
|
||||
return route.fulfill({ status: 200, contentType: 'application/json', body: JSON.stringify({ plans: TEST_TRAINEE_PLANS }) });
|
||||
});
|
||||
|
||||
94
app/test/e2e/plan-wizard.spec.ts
Normal file
94
app/test/e2e/plan-wizard.spec.ts
Normal file
@@ -0,0 +1,94 @@
|
||||
import { test, expect, type Page } from './fixtures';
|
||||
import { mockPlans } from './mock-api';
|
||||
|
||||
async function login(page: Page) {
|
||||
await page.goto('/login');
|
||||
await page.locator('input[type="email"]').fill('test@fitos.pl');
|
||||
await page.locator('input[type="password"]').fill('haslo123');
|
||||
await page.getByRole('button', { name: 'Zaloguj' }).click();
|
||||
await expect(page.getByRole('heading', { name: 'Dashboard' })).toBeVisible();
|
||||
}
|
||||
|
||||
async function openNewPlan(page: Page) {
|
||||
await page.getByRole('link', { name: 'Plany' }).click();
|
||||
await page.getByRole('button', { name: 'Nowy plan' }).first().click();
|
||||
await expect(page.getByRole('heading', { name: 'Nowy plan' })).toBeVisible();
|
||||
}
|
||||
|
||||
test('wizard: 3 kroki — szczegóły, ćwiczenia (siłownia + alias), podsumowanie i zapis', async ({
|
||||
page,
|
||||
}) => {
|
||||
mockPlans(page);
|
||||
await login(page);
|
||||
await openNewPlan(page);
|
||||
|
||||
// stepper: 3 kroki, krok 1 aktywny
|
||||
await expect(page.getByRole('button', { name: 'Szczegóły' })).toBeVisible();
|
||||
await expect(page.getByRole('button', { name: 'Ćwiczenia' })).toBeVisible();
|
||||
await expect(page.getByRole('button', { name: 'Podsumowanie' })).toBeVisible();
|
||||
|
||||
// krok 1: „Dalej” zablokowany przy pustym tytule
|
||||
await expect(page.getByRole('button', { name: 'Dalej' })).toBeDisabled();
|
||||
await page.getByPlaceholder('np. Plan masowy 8-tygodniowy').fill('Plan wizard');
|
||||
await expect(page.getByRole('button', { name: 'Dalej' })).toBeEnabled();
|
||||
await page.getByRole('button', { name: 'Dalej' }).click();
|
||||
|
||||
// krok 2: select siłowni (cłonkostwa zalogowanego — mock: Żelazna Świątynia)
|
||||
const gymSelect = page.locator('select').first();
|
||||
await expect(gymSelect).toBeVisible();
|
||||
await expect(gymSelect.locator('option')).toHaveText(['Bez siłowni', 'Żelazna Świątynia']);
|
||||
await gymSelect.selectOption({ label: 'Żelazna Świątynia' });
|
||||
|
||||
// dzień + ćwiczenie + alias tygodnia
|
||||
await page.getByRole('button', { name: 'Pon' }).click();
|
||||
await page.getByRole('button', { name: 'dodaj ćwiczenie…' }).click();
|
||||
await page.getByRole('option', { name: /Przysiady/ }).click();
|
||||
const aliasInput = page.getByPlaceholder('np. Deload');
|
||||
await aliasInput.fill('Deload');
|
||||
|
||||
// krok 3: podsumowanie — alias, siłownia, liczniki; „Zapisz” tylko tutaj
|
||||
await page.getByRole('button', { name: 'Dalej' }).click();
|
||||
await expect(page.getByRole('heading', { name: 'Deload' })).toBeVisible();
|
||||
await expect(page.getByText('Żelazna Świątynia')).toBeVisible();
|
||||
await expect(page.getByText('szkic — bez startu')).toBeVisible();
|
||||
await expect(page.getByRole('button', { name: 'Zapisz' })).toBeVisible();
|
||||
|
||||
// zapis: ciało POST /plans z gymId i customName
|
||||
const [request] = await Promise.all([
|
||||
page.waitForRequest((r) => r.url().includes('/api/v1/plans') && r.method() === 'POST'),
|
||||
page.getByRole('button', { name: 'Zapisz' }).click(),
|
||||
]);
|
||||
const body = request.postDataJSON() as {
|
||||
title: string;
|
||||
gymId: string | null;
|
||||
weeks: { index: number; customName: string | null }[];
|
||||
};
|
||||
expect(body.title).toBe('Plan wizard');
|
||||
expect(body.gymId).toBe('gym-1');
|
||||
expect(body.weeks[0]).toMatchObject({ index: 0, customName: 'Deload' });
|
||||
|
||||
// po utworzeniu — szczegóły planu
|
||||
await expect(page.getByRole('heading', { name: 'Plan e2e' })).toBeVisible();
|
||||
});
|
||||
|
||||
test('wizard: zwrot do kroku 1 ze steppera i ostrzeżenie o pustych tygodniach w podsumowaniu', async ({
|
||||
page,
|
||||
}) => {
|
||||
mockPlans(page);
|
||||
await login(page);
|
||||
await openNewPlan(page);
|
||||
|
||||
// krok 2 bez ćwiczeń: podsumowanie pokazuje ostrzeżenie o pustych tygodniach
|
||||
await page.getByPlaceholder('np. Plan masowy 8-tygodniowy').fill('Plan pusty');
|
||||
await page.getByRole('button', { name: 'Dalej' }).click();
|
||||
// zwrot do kroku 1 przez stepper
|
||||
await page.getByRole('button', { name: 'Szczegóły' }).click();
|
||||
await expect(page.getByPlaceholder('np. Plan masowy 8-tygodniowy')).toBeVisible();
|
||||
// z powrotem do podsumowania (krok 2 → 3)
|
||||
await page.getByRole('button', { name: 'Ćwiczenia' }).click();
|
||||
await page.getByRole('button', { name: 'Dalej' }).click();
|
||||
// ostrzeżenie o pustych tygodniach w karcie podsumowania („Łącznie:” = unikalne)
|
||||
await expect(
|
||||
page.getByText('Łącznie: 0 dni · 0 ćwiczeń · 0 serii · 4 tygodnie bez ćwiczeń'),
|
||||
).toBeVisible();
|
||||
});
|
||||
@@ -30,7 +30,9 @@ test('mobile: tworzenie planu — bezpieczny tap dnia, bottom sheet, edycja seri
|
||||
await login(page);
|
||||
await openNewPlan(page);
|
||||
|
||||
// dodaj dzień (Poniedziałek)
|
||||
// dodaj dzień (Poniedziałek) — krok 2 po tytule i przejściu „Dalej”
|
||||
await page.getByPlaceholder('np. Plan masowy 8-tygodniowy').fill('Plan');
|
||||
await page.getByRole('button', { name: 'Dalej' }).click();
|
||||
await page.getByRole('button', { name: 'Pon' }).click();
|
||||
|
||||
// dodaj ćwiczenie przez bottom sheet (Radix Dialog na mobile)
|
||||
@@ -49,11 +51,10 @@ test('mobile: tworzenie planu — bezpieczny tap dnia, bottom sheet, edycja seri
|
||||
await page.getByRole('button', { name: 'Usuń dzień' }).click();
|
||||
await expect(page.getByText('Przysiady')).toHaveCount(0);
|
||||
|
||||
// ponownie dodaj dzień + ćwiczenie, utwórz plan
|
||||
// ponownie dodaj dzień + ćwiczenie, utwórz plan (tytuł wpisany na kroku 1)
|
||||
await page.getByRole('button', { name: 'Pon' }).click();
|
||||
await page.getByRole('button', { name: 'dodaj ćwiczenie…' }).click();
|
||||
await page.getByRole('option', { name: /Wyciskanie/ }).click();
|
||||
await page.getByPlaceholder('np. Plan masowy 8-tygodniowy').fill('Plan mobile');
|
||||
// „Zatwierdź plan" w grupie „Akcje" dolnego paska
|
||||
await page.getByRole('button', { name: 'Akcje' }).click();
|
||||
await page.getByRole('button', { name: 'Zatwierdź plan' }).click();
|
||||
@@ -68,7 +69,9 @@ test('mobile 320px: brak poziomego scrolla w edytorze', async ({ page }) => {
|
||||
await login(page);
|
||||
await openNewPlan(page);
|
||||
|
||||
// dodaj dzień + ćwiczenie — tabela serii (min-w) nie może przelewać strony
|
||||
// dodaj dzień + ćwiczenie (krok 2) — tabela serii (min-w) nie może przelewać strony
|
||||
await page.getByPlaceholder('np. Plan masowy 8-tygodniowy').fill('Plan');
|
||||
await page.getByRole('button', { name: 'Dalej' }).click();
|
||||
await page.getByRole('button', { name: 'Pon' }).click();
|
||||
await page.getByRole('button', { name: 'dodaj ćwiczenie…' }).click();
|
||||
await page.getByRole('option', { name: /Przysiady/ }).click();
|
||||
|
||||
@@ -66,7 +66,9 @@ test('walidacja tytułu i podsumowanie stopki', async ({ page }) => {
|
||||
await expect(page.getByText(/0 dni · 0 ćwiczeń · 0 serii/)).toBeVisible();
|
||||
await expect(page.getByText(/4 tygodnie bez ćwiczeń/)).toBeVisible();
|
||||
|
||||
// dodaj dzień + ćwiczenie → podsumowanie się zmienia (tyg. 2–4 odzwierciedlają tyg. 1)
|
||||
// dodaj dzień + ćwiczenie (krok 2) → podsumowanie się zmienia (tyg. 2–4 odzwierciedlają tyg. 1)
|
||||
await page.getByPlaceholder('np. Plan masowy 8-tygodniowy').fill('Plan');
|
||||
await page.getByRole('button', { name: 'Dalej' }).click();
|
||||
await page.getByRole('button', { name: 'Pon' }).click();
|
||||
await page.getByRole('button', { name: 'dodaj ćwiczenie…' }).click();
|
||||
await page.getByRole('option', { name: /Przysiady/ }).click();
|
||||
@@ -82,7 +84,9 @@ test('zakładki tygodni: klik taba przełącza na dany tydzień', async ({ page
|
||||
await page.getByRole('button', { name: 'Nowy plan' }).first().click();
|
||||
await expect(page.getByRole('heading', { name: 'Nowy plan' })).toBeVisible();
|
||||
|
||||
// zakładki tygodni (4 tygodnie domyślnie)
|
||||
// zakładki tygodni są na kroku 2 (4 tygodnie domyślnie)
|
||||
await page.getByPlaceholder('np. Plan masowy 8-tygodniowy').fill('Plan');
|
||||
await page.getByRole('button', { name: 'Dalej' }).click();
|
||||
await expect(page.getByRole('button', { name: 'Tydzień 1' })).toBeVisible();
|
||||
await expect(page.getByRole('button', { name: 'Tydzień 4' })).toBeVisible();
|
||||
|
||||
@@ -102,7 +106,9 @@ test('zmiana kolejności ćwiczeń i kopiowanie serii', async ({ page }) => {
|
||||
await page.getByRole('button', { name: 'Nowy plan' }).first().click();
|
||||
await expect(page.getByRole('heading', { name: 'Nowy plan' })).toBeVisible();
|
||||
|
||||
// dodaj dzień i dwa ćwiczenia
|
||||
// dodaj dzień i dwa ćwiczenia (krok 2)
|
||||
await page.getByPlaceholder('np. Plan masowy 8-tygodniowy').fill('Plan');
|
||||
await page.getByRole('button', { name: 'Dalej' }).click();
|
||||
await page.getByRole('button', { name: 'Pon' }).click();
|
||||
await page.getByRole('button', { name: 'dodaj ćwiczenie…' }).click();
|
||||
await page.getByRole('option', { name: /Przysiady/ }).click();
|
||||
|
||||
@@ -85,7 +85,9 @@ test('edytor planu podopiecznego: „Wstaw wyniki" prefylluje serie z ostatniej
|
||||
// (lista: akcja tworzenia w topbarze) — w edytorze zapis = „Zatwierdź plan"
|
||||
|
||||
await expect(page.getByRole('heading', { name: 'Plan dla podopiecznego' })).toBeVisible();
|
||||
// dodaj dzień (Poniedziałek)
|
||||
// dodaj dzień (Poniedziałek) — krok 2 po tytule i przejściu „Dalej”
|
||||
await page.getByPlaceholder('np. Plan masowy 8-tygodniowy').fill('Plan');
|
||||
await page.getByRole('button', { name: 'Dalej' }).click();
|
||||
await page.getByRole('button', { name: 'Pon' }).click();
|
||||
// dodaj ćwiczenie Przysiady (wyszukiwarka ćwiczeń zamiast selecta)
|
||||
await page.getByRole('button', { name: 'dodaj ćwiczenie…' }).click();
|
||||
|
||||
@@ -88,6 +88,47 @@ describe('usePlanEditorState — akcje mutacji', () => {
|
||||
expect(sets[1].loadKg).toBe('20');
|
||||
});
|
||||
|
||||
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; copyPrevWeek 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.copyPrevWeek(1));
|
||||
// copyPrevWeek 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.copyPrevWeek(2));
|
||||
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);
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { render, screen } from '@testing-library/react';
|
||||
import { render, screen, waitFor, within } from '@testing-library/react';
|
||||
import userEvent from '@testing-library/user-event';
|
||||
import { MemoryRouter } from 'react-router-dom';
|
||||
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
|
||||
@@ -37,7 +37,7 @@ const EXERCISES = [
|
||||
|
||||
const TRAINEE_DETAIL = {
|
||||
user: { id: TRAINEE_ID, firstName: 'Anna', lastName: 'Nowak' },
|
||||
gyms: [],
|
||||
gyms: [{ id: 'gym-1', name: 'Siłownia Podopiecznej' }],
|
||||
activePlan: null,
|
||||
progress: null,
|
||||
killstreak: 0,
|
||||
@@ -193,7 +193,7 @@ function jsonResponse(status: number, body: unknown) {
|
||||
});
|
||||
}
|
||||
|
||||
function mockApi(opts: { plans?: unknown[] } = {}) {
|
||||
function mockApi(opts: { plans?: unknown[]; capture?: { createBody?: Record<string, unknown> } } = {}) {
|
||||
const plans = opts.plans ?? PLANS;
|
||||
vi.stubGlobal(
|
||||
'fetch',
|
||||
@@ -228,7 +228,10 @@ function mockApi(opts: { plans?: unknown[] } = {}) {
|
||||
if (url.includes(`/trainer/trainees/${TRAINEE_ID}/plans/plan-2`))
|
||||
return jsonResponse(200, { plan: ACTIVE_DETAIL });
|
||||
if (url.includes(`/trainer/trainees/${TRAINEE_ID}/plans`)) {
|
||||
if (method === 'POST') return jsonResponse(201, { plan: { id: 'plan-new' } });
|
||||
if (method === 'POST') {
|
||||
if (opts.capture) opts.capture.createBody = JSON.parse(String(init?.body));
|
||||
return jsonResponse(201, { plan: { id: 'plan-new' } });
|
||||
}
|
||||
return jsonResponse(200, { plans });
|
||||
}
|
||||
if (url.includes(`/trainer/trainees/${TRAINEE_ID}/exercise-latest`))
|
||||
@@ -331,14 +334,16 @@ describe('Zakładka „Plany" w panelu podopiecznego', () => {
|
||||
describe('PlanEditor — tryb podopiecznego', () => {
|
||||
it('edycja: formularz inicjalizuje się z danych planu po załadowaniu', async () => {
|
||||
mockApi();
|
||||
const user = userEvent.setup();
|
||||
renderAt(`/trainees/${TRAINEE_ID}/plans/plan-1/edit`);
|
||||
|
||||
expect(
|
||||
await screen.findByRole('heading', { name: 'Edycja planu podopiecznego' }),
|
||||
).toBeInTheDocument();
|
||||
// tytuł i serie wczytane z API (DRAFT_DETAIL: Przysiady 1 seria 10 @ 60)
|
||||
// tytuł wczytany z API (krok 1); serie (krok 2) po przejściu „Dalej”
|
||||
expect(await screen.findByDisplayValue('Plan masowy')).toBeInTheDocument();
|
||||
expect(screen.getByDisplayValue('60')).toBeInTheDocument();
|
||||
await user.click(screen.getByRole('button', { name: 'Dalej' }));
|
||||
expect(await screen.findByDisplayValue('60')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('„Wstecz" przy niezapisanych zmianach pyta o potwierdzenie', async () => {
|
||||
@@ -363,6 +368,9 @@ describe('PlanEditor — tryb podopiecznego', () => {
|
||||
renderAt(`/trainees/${TRAINEE_ID}/plans/new`);
|
||||
|
||||
await screen.findByRole('heading', { name: 'Plan dla podopiecznego' });
|
||||
// krok 2 (ćwiczenia) — po tytule i przejściu „Dalej”
|
||||
await user.type(screen.getByPlaceholderText('np. Plan masowy 8-tygodniowy'), 'Plan');
|
||||
await user.click(screen.getByRole('button', { name: 'Dalej' }));
|
||||
await user.click(screen.getByRole('button', { name: 'Poniedziałek' }));
|
||||
await user.click(screen.getByRole('button', { name: 'dodaj ćwiczenie…' }));
|
||||
await user.click(await screen.findByRole('option', { name: /Przysiady/ }));
|
||||
@@ -392,6 +400,9 @@ describe('PlanEditor — tryb podopiecznego', () => {
|
||||
await screen.findByRole('heading', { name: 'Plan dla podopiecznego' }),
|
||||
).toBeInTheDocument();
|
||||
|
||||
// krok 2 (ćwiczenia) — po tytule i przejściu „Dalej”
|
||||
await user.type(screen.getByPlaceholderText('np. Plan masowy 8-tygodniowy'), 'Plan');
|
||||
await user.click(screen.getByRole('button', { name: 'Dalej' }));
|
||||
// dodaj dzień (Poniedziałek — aria-label z pełną nazwą)
|
||||
await user.click(screen.getByRole('button', { name: 'Poniedziałek' }));
|
||||
// dodaj ćwiczenie Przysiady (wyszukiwarka ćwiczeń)
|
||||
@@ -408,3 +419,66 @@ describe('PlanEditor — tryb podopiecznego', () => {
|
||||
expect(screen.getByDisplayValue('70')).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
||||
describe('PlanEditor — wizard 3-etapowy (tryb podopiecznego)', () => {
|
||||
it('krok 1 blokuje „Dalej” przy pustym tytule', async () => {
|
||||
mockApi();
|
||||
const user = userEvent.setup();
|
||||
renderAt(`/trainees/${TRAINEE_ID}/plans/new`);
|
||||
|
||||
await screen.findByRole('heading', { name: 'Plan dla podopiecznego' });
|
||||
expect(screen.getByRole('button', { name: 'Dalej' })).toBeDisabled();
|
||||
await user.type(screen.getByPlaceholderText('np. Plan masowy 8-tygodniowy'), 'Plan');
|
||||
expect(screen.getByRole('button', { name: 'Dalej' })).toBeEnabled();
|
||||
});
|
||||
|
||||
it('krok 2: select siłowni z członkostw podopiecznego + alias tygodnia; krok 3: podsumowanie i zapis', async () => {
|
||||
const capture: { createBody?: Record<string, unknown> } = {};
|
||||
mockApi({ capture });
|
||||
const user = userEvent.setup();
|
||||
renderAt(`/trainees/${TRAINEE_ID}/plans/new`);
|
||||
|
||||
await screen.findByRole('heading', { name: 'Plan dla podopiecznego' });
|
||||
await user.type(screen.getByPlaceholderText('np. Plan masowy 8-tygodniowy'), 'Plan wizard');
|
||||
// 1 tydzień — podsumowanie bez lustrzanych tygodni
|
||||
const weeksField = screen.getByText('Długość (tygodnie)').closest('[data-slot="field"]') as HTMLElement;
|
||||
await user.selectOptions(within(weeksField).getByRole('combobox'), '1');
|
||||
await user.click(screen.getByRole('button', { name: 'Dalej' }));
|
||||
|
||||
// select siłowni: opcja „Bez siłowni” + siłownia podopiecznego
|
||||
// (Field bez <label for> — selekcja przez kontener pola)
|
||||
const gymField = screen.getByText('Siłownia').closest('[data-slot="field"]') as HTMLElement;
|
||||
const gymSelect = within(gymField).getByRole('combobox');
|
||||
expect(within(gymSelect).getByRole('option', { name: 'Bez siłowni' })).toBeInTheDocument();
|
||||
expect(within(gymSelect).getByRole('option', { name: 'Siłownia Podopiecznej' })).toBeInTheDocument();
|
||||
await user.selectOptions(gymSelect, 'gym-1');
|
||||
|
||||
// dzień + ćwiczenie + alias tygodnia
|
||||
await user.click(screen.getByRole('button', { name: 'Poniedziałek' }));
|
||||
await user.click(screen.getByRole('button', { name: 'dodaj ćwiczenie…' }));
|
||||
await user.click(await screen.findByRole('option', { name: /Przysiady/ }));
|
||||
const aliasField = screen
|
||||
.getByText('Nazwa tygodnia (opcjonalnie)')
|
||||
.closest('[data-slot="field"]') as HTMLElement;
|
||||
await user.type(within(aliasField).getByRole('textbox'), 'Deload');
|
||||
|
||||
// krok 3: podsumowanie — alias, siłownia, dni i ćwiczenia
|
||||
await user.click(screen.getByRole('button', { name: 'Dalej' }));
|
||||
expect(await screen.findByText('Deload')).toBeInTheDocument();
|
||||
expect(screen.getByText('Siłownia Podopiecznej')).toBeInTheDocument();
|
||||
// liczniki w karcie podsumowania (w stopce jest identyczny tekst — zakres = karta)
|
||||
const summaryCard = screen
|
||||
.getByText('Łącznie: 1 dzień · 1 ćwiczenie · 1 seria')
|
||||
.closest('[data-slot="card"]') as HTMLElement;
|
||||
expect(within(summaryCard).getByText('Pon · 1 dzień · 1 ćwiczenie · 1 seria')).toBeInTheDocument();
|
||||
|
||||
// zapis (krok 3) — body z gymId i customName
|
||||
await user.click(screen.getByRole('button', { name: 'Zapisz' }));
|
||||
await waitFor(() => expect(capture.createBody).toBeDefined());
|
||||
expect(capture.createBody).toMatchObject({
|
||||
title: 'Plan wizard',
|
||||
gymId: 'gym-1',
|
||||
weeks: [expect.objectContaining({ index: 0, customName: 'Deload' })],
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user