diff --git a/apps/studio/components/grid/components/grid/Grid.tsx b/apps/studio/components/grid/components/grid/Grid.tsx index d57ac84322e..0ee375115af 100644 --- a/apps/studio/components/grid/components/grid/Grid.tsx +++ b/apps/studio/components/grid/components/grid/Grid.tsx @@ -230,6 +230,7 @@ export const Grid = memo( return (
{ await deleteTable(page, ref, sourceTableName) await deleteTable(page, ref, targetTableName) }) + + test('CSV drag and drop imports data on empty table', async ({ page, ref }) => { + const tableName = 'pw_table_csv_drag_drop' + + await dropTable(tableName) + await dbCreateTable(tableName, columnName) + + const loadPromise = waitForTableToLoad(page, ref) + await page.goto(toUrl(`/project/${ref}/editor?schema=public`)) + await loadPromise + + await page.getByRole('button', { name: `View ${tableName}`, exact: true }).click() + await page.waitForURL(/\/editor\/\d+\?schema=public$/) + + await expect( + page.getByText('or drag and drop a CSV file here'), + 'Empty table should show drag and drop hint' + ).toBeVisible() + + const csvFilePath = path.join(import.meta.dirname, 'files', 'table-editor-drag-drop.csv') + const csvBuffer = fs.readFileSync(csvFilePath) + + // Synthesize a DataTransfer with the CSV file to simulate a browser file drag-and-drop + const dataTransfer = await page.evaluateHandle((csvBase64: string) => { + const dt = new DataTransfer() + const bytes = Uint8Array.from(atob(csvBase64), (c) => c.charCodeAt(0)) + const file = new File([bytes], 'table-editor-drag-drop.csv', { type: 'text/csv' }) + dt.items.add(file) + return dt + }, csvBuffer.toString('base64')) + + const gridContainer = page.getByTestId('table-editor-grid-container') + + await gridContainer.dispatchEvent('dragover', { dataTransfer }) + await expect( + page.getByText('Drop your CSV file here'), + 'Drag feedback should show when CSV is dragged over' + ).toBeVisible() + + await gridContainer.dispatchEvent('drop', { dataTransfer }) + + await expect( + page.getByText('A total of 3 rows will be'), + 'Import dialog should show correct row count from CSV' + ).toBeVisible({ timeout: 10_000 }) + + const waitForCsvInsert = createApiResponseWaiter(page, 'pg-meta', ref, 'query?key=', { + method: 'POST', + }) + await page.getByRole('button', { name: 'Import data' }).click() + await waitForCsvInsert + await waitForGridDataToLoad(page, ref) + + await expect( + page.getByText('3 records'), + 'Table should show 3 records after drag and drop import' + ).toBeVisible() + await expect(page.getByRole('gridcell', { name: 'drag drop value 1' })).toBeVisible() + + await dropTable(tableName) + }) })