|
| 1 | +/** |
| 2 | + * ObjectUI |
| 3 | + * Copyright (c) 2024-present ObjectStack Inc. |
| 4 | + * |
| 5 | + * This source code is licensed under the MIT license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. |
| 7 | + */ |
| 8 | + |
| 9 | +/** |
| 10 | + * objectstack-ai/objectstack#27520 (row 6) — a refused inline save says WHY, |
| 11 | + * WHERE: the save bar says every reason whole, one line each, and the cells |
| 12 | + * the refusal names are marked. |
| 13 | + * |
| 14 | + * The server refuses a row field by field (`VALIDATION_FAILED` with one |
| 15 | + * `fields[]` entry per failed rule). The save bar used to cut that to one |
| 16 | + * truncated line, the rest only in a native tooltip, and no cell said which |
| 17 | + * value was wrong. |
| 18 | + */ |
| 19 | +import { describe, it, expect, vi } from 'vitest'; |
| 20 | +import { fireEvent, waitFor, within } from '@testing-library/react'; |
| 21 | +import '@testing-library/jest-dom'; |
| 22 | +import { renderComponent } from './test-utils'; |
| 23 | +// Registers the renderers at module scope, NOT inside a `beforeAll`; see |
| 24 | +// object-ui/no-dynamic-import-in-test-hook (objectui#3010/#3021). |
| 25 | +import '../renderers'; |
| 26 | + |
| 27 | +const schema = { |
| 28 | + type: 'data-table' as const, |
| 29 | + editable: true, |
| 30 | + singleClickEdit: true, |
| 31 | + columns: [ |
| 32 | + { header: 'Title', accessorKey: 'title' }, |
| 33 | + { header: 'Estimate', accessorKey: 'estimate', type: 'number' }, |
| 34 | + ], |
| 35 | + data: [ |
| 36 | + { id: '1', title: 'Ingest pipeline', estimate: 60 }, |
| 37 | + { id: '2', title: 'Warehouse schema', estimate: 33 }, |
| 38 | + ], |
| 39 | +} as any; |
| 40 | + |
| 41 | +type Entry = { rowIndex: number; changes: Record<string, any>; row: any }; |
| 42 | + |
| 43 | +const TITLE = 'Title is required and cannot be cleared'; |
| 44 | +const ESTIMATE = 'Estimate (h) must be ≥ 0'; |
| 45 | + |
| 46 | +/** The refusal `PATCH /data/:object/:id` answers, as `@objectstack/client` decorates it. */ |
| 47 | +function fieldRefusal(fields: Array<{ field: string; message: string }>) { |
| 48 | + const message = fields.map((f) => f.message).join('; '); |
| 49 | + return Object.assign(new Error(message), { |
| 50 | + code: 'VALIDATION_FAILED', |
| 51 | + httpStatus: 400, |
| 52 | + details: { error: message, code: 'VALIDATION_FAILED', fields: fields.map((f) => ({ ...f, code: 'invalid' })) }, |
| 53 | + }); |
| 54 | +} |
| 55 | + |
| 56 | +const cellOf = (container: HTMLElement, rowNumber: number, colNumber: number) => |
| 57 | + container.querySelectorAll('tbody tr')[rowNumber].querySelectorAll('td')[colNumber] as HTMLElement; |
| 58 | + |
| 59 | +/** Stage a new value in a body cell. */ |
| 60 | +function stage(container: HTMLElement, rowNumber: number, colNumber: number, value: string) { |
| 61 | + const cell = cellOf(container, rowNumber, colNumber); |
| 62 | + fireEvent.click(cell); |
| 63 | + const input = cell.querySelector('input') as HTMLInputElement; |
| 64 | + fireEvent.change(input, { target: { value } }); |
| 65 | + fireEvent.blur(input); |
| 66 | +} |
| 67 | + |
| 68 | +const refusalBar = (container: HTMLElement) => |
| 69 | + container.querySelector('[data-testid="table-save-refusal"]') as HTMLElement | null; |
| 70 | + |
| 71 | +describe('data-table — a refused save says every reason and marks its cells (#27520)', () => { |
| 72 | + it('Save All: one line per reason, whole, and the two named cells of the refused row marked', async () => { |
| 73 | + const onBatchSave = vi.fn(async (entries: Entry[]) => ({ |
| 74 | + failed: entries |
| 75 | + .filter((e) => e.row.id === '1') |
| 76 | + .map((e) => ({ |
| 77 | + rowIndex: e.rowIndex, |
| 78 | + error: fieldRefusal([{ field: 'title', message: TITLE }, { field: 'estimate', message: ESTIMATE }]), |
| 79 | + })), |
| 80 | + })); |
| 81 | + const { container, getByText } = renderComponent({ ...schema, onBatchSave }); |
| 82 | + |
| 83 | + stage(container, 0, 0, ''); |
| 84 | + stage(container, 0, 1, '-5'); |
| 85 | + stage(container, 1, 1, '31'); |
| 86 | + fireEvent.click(getByText(/Save All/)); |
| 87 | + |
| 88 | + await waitFor(() => expect(refusalBar(container)).not.toBeNull()); |
| 89 | + const bar = refusalBar(container)!; |
| 90 | + expect(bar).toHaveAttribute('role', 'alert'); |
| 91 | + expect(within(bar).getAllByRole('listitem').map((li) => li.textContent)).toEqual([TITLE, ESTIMATE]); |
| 92 | + // Said whole: not the `; `-joined summary, and not cut. |
| 93 | + expect(bar.textContent).not.toContain('; '); |
| 94 | + expect(bar.querySelector('.truncate')).toBeNull(); |
| 95 | + |
| 96 | + // The refused row's two cells carry their reasons; the saved row's none. |
| 97 | + expect(cellOf(container, 0, 0)).toHaveAttribute('aria-invalid', 'true'); |
| 98 | + expect(cellOf(container, 0, 0)).toHaveAttribute('data-cell-error', TITLE); |
| 99 | + expect(cellOf(container, 0, 1)).toHaveAttribute('aria-invalid', 'true'); |
| 100 | + expect(cellOf(container, 0, 1)).toHaveAttribute('data-cell-error', ESTIMATE); |
| 101 | + expect(container.querySelectorAll('tbody tr')[1].querySelector('[aria-invalid="true"]')).toBeNull(); |
| 102 | + }); |
| 103 | + |
| 104 | + it('editing a marked cell takes its mark away and leaves the other', async () => { |
| 105 | + const onBatchSave = vi.fn(async (entries: Entry[]) => ({ |
| 106 | + failed: entries.map((e) => ({ |
| 107 | + rowIndex: e.rowIndex, |
| 108 | + error: fieldRefusal([{ field: 'title', message: TITLE }, { field: 'estimate', message: ESTIMATE }]), |
| 109 | + })), |
| 110 | + })); |
| 111 | + const { container, getByText } = renderComponent({ ...schema, onBatchSave }); |
| 112 | + |
| 113 | + stage(container, 0, 0, ''); |
| 114 | + stage(container, 0, 1, '-5'); |
| 115 | + fireEvent.click(getByText(/Save All/)); |
| 116 | + await waitFor(() => expect(cellOf(container, 0, 1)).toHaveAttribute('aria-invalid', 'true')); |
| 117 | + |
| 118 | + stage(container, 0, 1, '5'); |
| 119 | + |
| 120 | + await waitFor(() => expect(cellOf(container, 0, 1)).not.toHaveAttribute('aria-invalid')); |
| 121 | + expect(cellOf(container, 0, 0)).toHaveAttribute('aria-invalid', 'true'); |
| 122 | + }); |
| 123 | + |
| 124 | + it('a single-row save marks the cell its refusal names, and a later save that lands clears it', async () => { |
| 125 | + const onRowSave = vi |
| 126 | + .fn() |
| 127 | + .mockRejectedValueOnce(fieldRefusal([{ field: 'estimate', message: ESTIMATE }])) |
| 128 | + .mockResolvedValueOnce(undefined); |
| 129 | + // `rowActions` draws the edit column that holds the row's own save button. |
| 130 | + const { container } = renderComponent({ ...schema, rowActions: true, onRowSave }); |
| 131 | + |
| 132 | + stage(container, 0, 1, '-5'); |
| 133 | + // The row's own save button (the edit column's save icon). |
| 134 | + const saveRow = () => |
| 135 | + fireEvent.click(within(container.querySelectorAll('tbody tr')[0] as HTMLElement).getByTitle(/^Save row$/i)); |
| 136 | + saveRow(); |
| 137 | + |
| 138 | + await waitFor(() => expect(cellOf(container, 0, 1)).toHaveAttribute('data-cell-error', ESTIMATE)); |
| 139 | + expect(refusalBar(container)?.textContent).toContain(ESTIMATE); |
| 140 | + |
| 141 | + stage(container, 0, 1, '5'); |
| 142 | + saveRow(); |
| 143 | + await waitFor(() => expect(onRowSave).toHaveBeenCalledTimes(2)); |
| 144 | + await waitFor(() => expect(container.querySelector('tbody [aria-invalid="true"]')).toBeNull()); |
| 145 | + }); |
| 146 | + |
| 147 | + it('a whole-batch rejection says its reasons but marks no cell: no row is named', async () => { |
| 148 | + const onBatchSave = vi.fn(async () => { |
| 149 | + throw fieldRefusal([{ field: 'title', message: TITLE }]); |
| 150 | + }); |
| 151 | + const { container, getByText } = renderComponent({ ...schema, onBatchSave }); |
| 152 | + |
| 153 | + stage(container, 0, 0, ''); |
| 154 | + stage(container, 1, 1, '31'); |
| 155 | + fireEvent.click(getByText(/Save All/)); |
| 156 | + |
| 157 | + await waitFor(() => expect(refusalBar(container)?.textContent).toContain(TITLE)); |
| 158 | + expect(container.querySelector('tbody [aria-invalid="true"]')).toBeNull(); |
| 159 | + }); |
| 160 | +}); |
0 commit comments