import { test, expect, uniqueSuffix } from './fixtures'; test.describe('Admin save failures', () => { test('does not claim an item was saved when the request failed', async ({ page, admin, adminInventory }) => { await page.route('**/api/admin/items', (route) => { if (route.request().method() === 'POST') { return route.fulfill({ status: 500, contentType: 'application/json', body: '{"error":"internal error"}' }); } return route.continue(); }); await admin.goto(); await adminInventory.addItem(`Broken ${uniqueSuffix()}`, '12'); // Reporting success for a failed save is worse than failing loudly: the // item is silently absent and the user has no reason to look for it. await expect(page.getByText('Item added')).toBeHidden(); await expect(page.getByText("Couldn't save item")).toBeVisible(); // The form must stay open so the entered values aren't lost. await expect(adminInventory.formDialog).toBeVisible(); }); test('reports a failed delete rather than claiming success', async ({ page, admin, adminInventory }) => { // Seeded through the API so the test owns a known row rather than clicking // whichever Delete button happens to be first in a paginated table. const name = `Doomed ${uniqueSuffix()}`; const created = await page.request.post('/api/admin/items', { multipart: { name, description: '', price: '10', category_id: '', tags: '[]' } }); expect(created.ok()).toBeTruthy(); await page.route('**/api/admin/items/*', (route) => { if (route.request().method() === 'DELETE') { return route.fulfill({ status: 500, contentType: 'application/json', body: '{"error":"nope"}' }); } return route.continue(); }); await admin.goto(); // Items list newest-first, so the seeded row is on the first page. await expect(adminInventory.row(name)).toBeVisible(); await adminInventory.deleteButton(name).click(); await expect(page.getByText('Item deleted')).toBeHidden(); await expect(page.getByText("Couldn't delete item")).toBeVisible(); // The row must survive a failed delete. await expect(adminInventory.row(name)).toBeVisible(); }); test('saves an item successfully when the server accepts it', async ({ page, admin, adminInventory }) => { const name = `Good ${uniqueSuffix()}`; await admin.goto(); await adminInventory.addItem(name, '34'); await expect(page.getByText('Item added')).toBeVisible(); // Confirm the row actually reached the database, not just that a toast fired. const items = await (await page.request.get('/api/admin/items')).json(); expect(items.some((item: { name: string }) => item.name === name)).toBeTruthy(); }); });