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

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