|
| 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#27365 — `DataTableSchema.isRowEditable`, the host's per-row |
| 11 | + * inline-edit verdict. |
| 12 | + * |
| 13 | + * `editable` and the per-column locks answer for every row alike. A host that |
| 14 | + * knows one row is refused by the server (ObjectGrid, from the record-grained |
| 15 | + * write verdict) hands the table a per-row predicate; a row it answers `false` |
| 16 | + * for must read exactly like a row of a read-only table: |
| 17 | + * |
| 18 | + * - no trigger opens an editor on it — click, double-click, Enter; |
| 19 | + * - its cells take no edit cursor, and a click on them is NOT swallowed, so |
| 20 | + * the row's own click (ObjectGrid's record drawer) still fires; |
| 21 | + * - every other row is untouched, and with no predicate every row is |
| 22 | + * editable exactly as before. |
| 23 | + */ |
| 24 | +import { describe, it, expect, vi } from 'vitest'; |
| 25 | +import { fireEvent } from '@testing-library/react'; |
| 26 | +import '@testing-library/jest-dom'; |
| 27 | +import { renderComponent } from './test-utils'; |
| 28 | +// Registers the renderers at module scope, NOT inside a `beforeAll` (objectui#3010). |
| 29 | +import '../renderers'; |
| 30 | + |
| 31 | +const ROWS = [ |
| 32 | + { id: 'own', name: 'My Project' }, |
| 33 | + { id: 'other', name: 'Legacy Sunset' }, |
| 34 | +]; |
| 35 | + |
| 36 | +const baseSchema = (extra: Record<string, unknown> = {}) => |
| 37 | + ({ |
| 38 | + type: 'data-table' as const, |
| 39 | + editable: true, |
| 40 | + columns: [{ header: 'Name', accessorKey: 'name' }], |
| 41 | + data: ROWS, |
| 42 | + ...extra, |
| 43 | + }) as any; |
| 44 | + |
| 45 | +/** The `<td>` showing `text`. */ |
| 46 | +const cellOf = (container: HTMLElement, text: string) => |
| 47 | + Array.from(container.querySelectorAll('tbody td')).find( |
| 48 | + (td) => td.textContent?.trim() === text, |
| 49 | + ) as HTMLElement; |
| 50 | + |
| 51 | +const refuseOther = (row: { id: string }) => row.id !== 'other'; |
| 52 | + |
| 53 | +describe('data-table — isRowEditable narrows editing per row (objectstack#27365)', () => { |
| 54 | + it('a refused row opens no editor on click, and its click reaches the row', () => { |
| 55 | + const onRowClick = vi.fn(); |
| 56 | + const { container } = renderComponent( |
| 57 | + baseSchema({ singleClickEdit: true, isRowEditable: refuseOther, onRowClick }), |
| 58 | + ); |
| 59 | + const cell = cellOf(container, 'Legacy Sunset'); |
| 60 | + expect(cell).not.toHaveClass('cursor-text'); |
| 61 | + fireEvent.click(cell); |
| 62 | + expect(cell.querySelector('input')).toBeNull(); |
| 63 | + expect(onRowClick).toHaveBeenCalledTimes(1); |
| 64 | + }); |
| 65 | + |
| 66 | + it('a refused row opens no editor on double-click', () => { |
| 67 | + const { container } = renderComponent(baseSchema({ isRowEditable: refuseOther })); |
| 68 | + const cell = cellOf(container, 'Legacy Sunset'); |
| 69 | + fireEvent.doubleClick(cell); |
| 70 | + expect(cell.querySelector('input')).toBeNull(); |
| 71 | + }); |
| 72 | + |
| 73 | + it('a refused row opens no editor on Enter', () => { |
| 74 | + const { container } = renderComponent(baseSchema({ isRowEditable: refuseOther })); |
| 75 | + const cell = cellOf(container, 'Legacy Sunset'); |
| 76 | + fireEvent.keyDown(cell, { key: 'Enter' }); |
| 77 | + expect(cell.querySelector('input')).toBeNull(); |
| 78 | + }); |
| 79 | + |
| 80 | + it('CONTROL: the allowed row of the same table still opens its editor, on each trigger', () => { |
| 81 | + const { container } = renderComponent( |
| 82 | + baseSchema({ singleClickEdit: true, isRowEditable: refuseOther }), |
| 83 | + ); |
| 84 | + const cell = cellOf(container, 'My Project'); |
| 85 | + expect(cell).toHaveClass('cursor-text'); |
| 86 | + fireEvent.click(cell); |
| 87 | + expect(cell.querySelector('input')).not.toBeNull(); |
| 88 | + }); |
| 89 | + |
| 90 | + it('CONTROL: Enter on the allowed row opens its editor', () => { |
| 91 | + const { container } = renderComponent(baseSchema({ isRowEditable: refuseOther })); |
| 92 | + const cell = cellOf(container, 'My Project'); |
| 93 | + fireEvent.keyDown(cell, { key: 'Enter' }); |
| 94 | + expect(cell.querySelector('input')).not.toBeNull(); |
| 95 | + }); |
| 96 | + |
| 97 | + it('CONTROL: with no predicate every row is editable, as before', () => { |
| 98 | + const { container } = renderComponent(baseSchema({ singleClickEdit: true })); |
| 99 | + const cell = cellOf(container, 'Legacy Sunset'); |
| 100 | + fireEvent.click(cell); |
| 101 | + expect(cell.querySelector('input')).not.toBeNull(); |
| 102 | + }); |
| 103 | + |
| 104 | + it('narrows only: a predicate answering `true` cannot open a table that is not `editable`', () => { |
| 105 | + const { container } = renderComponent( |
| 106 | + baseSchema({ editable: false, singleClickEdit: true, isRowEditable: () => true }), |
| 107 | + ); |
| 108 | + const cell = cellOf(container, 'My Project'); |
| 109 | + fireEvent.click(cell); |
| 110 | + expect(cell.querySelector('input')).toBeNull(); |
| 111 | + }); |
| 112 | +}); |
0 commit comments