From 53c11fcecf9b9c483bf3281baae9df21752ee790 Mon Sep 17 00:00:00 2001 From: tisachi Date: Fri, 16 Oct 2026 21:51:13 +0200 Subject: [PATCH] Admin: usuwanie duplikatu przycisku zapisu w kreatorze usera MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit dolny 'Utworz usera'/'Zapisz zmiany' usuniety — jedyny zapis to akcja 'Zapisz' w menu kontekstowym (etykieta 'Zapisywanie...' w trakcie) testy: submit przez requestSubmit + flush lancucha async --- admin/src/pages/users/UserForm.tsx | 12 ++++++++---- admin/test/UserForm.test.tsx | 23 +++++++++++++++++++---- admin/test/e2e/admin.spec.ts | 2 +- 3 files changed, 28 insertions(+), 9 deletions(-) diff --git a/admin/src/pages/users/UserForm.tsx b/admin/src/pages/users/UserForm.tsx index 9a8a5b2..0b9a8ac 100644 --- a/admin/src/pages/users/UserForm.tsx +++ b/admin/src/pages/users/UserForm.tsx @@ -91,7 +91,14 @@ export default function UserForm() { useTopbar({ title: 'Użytkownicy', back: isEdit && id ? `/users/${id}` : '/users', - actions: [{ label: 'Zapisz', icon: Check, onClick: submitFromMenu, disabled: busy }], + actions: [ + { + label: busy ? 'Zapisywanie…' : 'Zapisz', + icon: Check, + onClick: submitFromMenu, + disabled: busy, + }, + ], alwaysInlineActions: true, flatActionsMobile: true, }); @@ -418,9 +425,6 @@ export default function UserForm() { {error && {error}} - ); diff --git a/admin/test/UserForm.test.tsx b/admin/test/UserForm.test.tsx index 0f72fc1..caa83d3 100644 --- a/admin/test/UserForm.test.tsx +++ b/admin/test/UserForm.test.tsx @@ -1,4 +1,4 @@ -import { render, screen } from '@testing-library/react'; +import { act, render, screen } from '@testing-library/react'; import userEvent from '@testing-library/user-event'; import { MemoryRouter, Route, Routes, useParams } from 'react-router-dom'; import { QueryClient, QueryClientProvider } from '@tanstack/react-query'; @@ -127,6 +127,19 @@ afterEach(() => { vi.unstubAllGlobals(); }); +/** Zapis via menu kontekstowe (topbar) nie istnieje w jednostkowym renderze — + * wymuszamy submit formularza dokładnie tak, jak robi to akcja „Zapisz”. + * Czekamy na pełny łańcuch async (PATCH → DELETE → nawigacja). */ +async function submitForm() { + const form = document.querySelector('form'); + if (!form) throw new Error('Brak formularza w DOM'); + await act(async () => { + form.requestSubmit(); + // czekaj na pełny łańcuch async: PATCH konta → DELETE/PATCH członkostw → nawigacja + for (let i = 0; i < 10; i++) await Promise.resolve(); + }); +} + describe('admin UserForm — kreator tworzenia', () => { it('wyjaśnia role i pokazuje uprawnienia administratora po rozwinięciu', async () => { mockRoutes([ @@ -164,7 +177,7 @@ describe('admin UserForm — kreator tworzenia', () => { await user.click(await screen.findByRole('option', { name: /Steel Gym/ })); await user.click(await screen.findByRole('checkbox', { name: 'Trener — Steel Gym' })); - await user.click(screen.getByRole('button', { name: 'Utwórz użytkownika' })); + await submitForm(); expect(await screen.findByText('DETAIL-u9')).toBeInTheDocument(); const post = calls.find( @@ -223,7 +236,7 @@ describe('admin UserForm — kreator edycji', () => { // dołóż rolę Właściciel do istniejącego członkostwa (MEMBER → MEMBER + OWNER) await user.click(screen.getByRole('checkbox', { name: 'Właściciel — Fit Factory' })); - await user.click(screen.getByRole('button', { name: 'Zapisz zmiany' })); + await submitForm(); expect(await screen.findByText('DETAIL-u1')).toBeInTheDocument(); const accountPatch = calls.find( @@ -249,11 +262,13 @@ describe('admin UserForm — kreator edycji', () => { await screen.findByDisplayValue('Fit Factory'); await user.click(screen.getByRole('button', { name: 'Usuń wiersz siłowni 1' })); - await user.click(screen.getByRole('button', { name: 'Zapisz zmiany' })); + await submitForm(); const del = calls.find( (c) => c.init?.method === 'DELETE' && c.url.includes('/memberships/gym-a'), ); + console.log('CALLS', JSON.stringify(calls.map((c) => c.init?.method + ' ' + c.url))); + console.log('ERR', document.body.innerText); expect(del).toBeDefined(); }); }); diff --git a/admin/test/e2e/admin.spec.ts b/admin/test/e2e/admin.spec.ts index bc778d2..f5a0392 100644 --- a/admin/test/e2e/admin.spec.ts +++ b/admin/test/e2e/admin.spec.ts @@ -196,7 +196,7 @@ test('kreator edycji użytkownika: wyjaśnia role i zapisuje zmianę roli PATCHe req.method() === 'PATCH' && req.url().endsWith('/admin/users/user-1/memberships/gym-1'), ); await page.getByRole('checkbox', { name: 'Trener — Fit Factory' }).click(); - await page.getByRole('button', { name: 'Zapisz zmiany' }).click(); + await page.getByRole('button', { name: 'Zapisz' }).click(); const req = await patchPromise; expect(req.postDataJSON()).toEqual({ roles: ['MEMBER', 'TRAINER'] }); });