Repository navigation
Expand file tree
/
Copy pathGridField.sortFieldCellEdit-12286.test.tsx
More file actions
73 lines (66 loc) · 3.07 KB
/
Copy pathGridField.sortFieldCellEdit-12286.test.tsx
File metadata and controls
73 lines (66 loc) · 3.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/**
* ObjectUI
* Copyright (c) 2024-present ObjectStack Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
/**
* A cell edit leaves every row's `sortField` as it was (objectui#12286).
*
* The grid stamped `row[sortField] = index` on EVERY change, a plain cell edit
* included. Whenever the stored positions were not already 0, 1, 2, … in the
* order drawn (two rows tied on one position, rows with none, gaps), editing
* one cell gave every row a new position, and a master-detail save then wrote
* each of those rows although the user had touched one. A cell edit moves no
* row, so it renumbers nothing; adding, removing, duplicating and moving a row
* still renumber (the drag-reorder row is pinned in
* `GridField.fieldKeys-11070.test.tsx`).
*
* The pins read the rows the grid hands to `onChange`, which is everything a
* host persists.
*/
import { describe, expect, it, vi } from 'vitest';
import { fireEvent, render, screen } from '@testing-library/react';
import React from 'react';
import type { GridFieldMetadata } from '@object-ui/types';
import { GridField } from './GridField';
const field: GridFieldMetadata = {
type: 'grid',
name: 'lines',
columns: [{ name: 'description', label: 'Description', type: 'text' }],
sortField: 'position',
};
type Row = Record<string, unknown>;
/** Render `rows`, type `value` into row `row`'s Description cell, and return what the grid handed back. */
function typeInto(rows: Row[], row: number, value: string): Row[] {
const onChange = vi.fn();
render(<GridField value={rows} onChange={onChange} field={field} />);
fireEvent.change(screen.getAllByLabelText('Description')[row], { target: { value } });
expect(onChange).toHaveBeenCalledTimes(1);
return onChange.mock.calls[0][0];
}
describe('GridField: a cell edit renumbers no row (objectui#12286)', () => {
it.each([
['tied', [{ description: 'A', position: 0 }, { description: 'B', position: 0 }]],
['gapped', [{ description: 'A', position: 10 }, { description: 'B', position: 20 }]],
['empty', [{ description: 'A', position: null }, { description: 'B', position: null }]],
['absent', [{ description: 'A' }, { description: 'B' }]],
])('%s positions: editing row 2 changes its cell and every position stays as read', (_label, rows) => {
const next = typeInto(rows, 1, 'B2');
expect(next).toEqual([rows[0], { ...rows[1], description: 'B2' }]);
});
it('CONTROL: Add appends a line, and an add renumbers every row', () => {
// The trailing entry line this used to type into is gone (objectstack#35507);
// Add is the add, and it renumbers as the entry line did.
const onChange = vi.fn();
render(<GridField value={[{ description: 'A', position: 0 }, { description: 'B', position: 0 }]} onChange={onChange} field={field} />);
fireEvent.click(screen.getByTestId('line-items-add'));
const next: Row[] = onChange.mock.calls[0][0];
expect(next.map((r) => [r.description, r.position])).toEqual([
['A', 0],
['B', 1],
[null, 2],
]);
});
});