Admin: usuwanie duplikatu przycisku zapisu w kreatorze usera

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
This commit is contained in:
tisachi
2026-10-16 21:51:13 +02:00
parent 29f3f5af54
commit 53c11fcecf
3 changed files with 28 additions and 9 deletions

View File

@@ -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() {
</Card>
{error && <ErrorText>{error}</ErrorText>}
<Button type="submit" disabled={busy}>
{busy ? 'Zapisywanie…' : isEdit ? 'Zapisz zmiany' : 'Utwórz użytkownika'}
</Button>
</form>
</>
);

View File

@@ -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();
});
});

View File

@@ -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'] });
});