import { test, expect, uniqueSuffix, createCategory, findOrFail } from './fixtures'; test.describe('Inline category creation from the item form', () => { test('creates a category without leaving the item form and assigns it', async ({ page, admin, adminInventory }) => { const RUN = `i${uniqueSuffix()}`; const categoryName = `Inline ${RUN}`; const itemName = `Item ${RUN}`; await admin.goto(); await adminInventory.openItemFormLoaded(); await adminInventory.name.fill(itemName); await adminInventory.price.fill('99'); await adminInventory.categoryField.click(); await expect(adminInventory.newCategoryName).toBeVisible(); await expect(adminInventory.createCategoryButton).toBeVisible(); await adminInventory.newCategoryName.fill(categoryName); // Submitted with Enter rather than a click: the popup sits over a // virtualized tree that keeps re-measuring, so a click target inside it is // never geometrically stable. Enter runs the same handler as the button. await adminInventory.newCategoryName.press('Enter'); // The new category should be selected straight away — having to hunt for it // in the tree afterwards defeats the point of creating it inline. await expect(adminInventory.formDialog.getByText(categoryName)).toBeVisible(); await adminInventory.confirmButton.click(); await expect(page.getByText('Item added')).toBeVisible(); const items = await (await page.request.get('/api/admin/items')).json(); // findOrFail already fails with the row count when the item is missing, // which the toBeTruthy assertion this replaces could not report. const saved = findOrFail( items, (item: { name: string }) => item.name === itemName, `the item named ${itemName}` ); expect(saved.category_name).toBe(categoryName); }); test('reports a duplicate category name instead of silently doing nothing', async ({ page, admin, adminInventory }) => { const categoryName = `Dupe i${uniqueSuffix()}`; await createCategory(page.request, categoryName); await admin.goto(); await adminInventory.openItemFormLoaded(); await adminInventory.categoryField.click(); await expect(adminInventory.newCategoryName).toBeVisible(); await expect(adminInventory.createCategoryButton).toBeVisible(); await adminInventory.newCategoryName.fill(categoryName); await adminInventory.newCategoryName.press('Enter'); await expect(page.getByText(/already exists/i)).toBeVisible(); }); });