Repository navigation
Expand file tree
/
Copy pathObjectGrid.richTextOneLine-12480.test.tsx
More file actions
192 lines (172 loc) · 7.78 KB
/
Copy pathObjectGrid.richTextOneLine-12480.test.tsx
File metadata and controls
192 lines (172 loc) · 7.78 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/**
* 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.
*/
/**
* objectui#12480 — a rich-content cell keeps the grid's one-line row.
*
* The Studio Data pillar's Records grid on HotCRM's `crm_account` (10 records)
* drew rows 1–8, a tall empty band, row 9, and no row 10, under a footer
* reading "10 records". Root cause: `data-table`'s one-line clamp is
* `truncate`, which bounds INLINE content only. The `markdown` / `html` /
* `richtext` cell renderers draw a block document, and blocks stack under
* nowrap, so the one record with a long `description` grew its row to the
* document's height (456 px in Chromium) and pushed row 10 below the scroll
* box's fold. The console list pages render through the same `ObjectGrid`, so
* they did the same.
*
* The fix wraps a rich-content column's renderer in a one-line clamp unless the
* column is authored `wrap: true`. jsdom does no layout, so these pins read the
* class contract and the row count; the pixel readings (before and after, 8 /
* 9 / 10 rows, Studio and the console list body) are in the PR body.
*/
import React from 'react';
import { describe, it, expect, afterEach, beforeAll, vi } from 'vitest';
import { render, screen, waitFor, cleanup } from '@testing-library/react';
import '@testing-library/jest-dom';
import { ActionProvider, SchemaRendererProvider } from '@object-ui/react';
import { registerAllFields } from '@object-ui/fields';
import { ObjectGrid } from '../ObjectGrid';
registerAllFields();
const DOC = '**Strategic Customer**\n\nA long account history.\n\n- one\n- two\n- three';
const FIELDS = {
id: { type: 'text' },
name: { type: 'text', label: 'Name' },
description: { type: 'markdown', label: 'Description' },
notes_html: { type: 'html', label: 'Html' },
notes_rich: { type: 'richtext', label: 'Rich' },
};
function makeRows(n: number, rich: boolean) {
return Array.from({ length: n }, (_, i) => ({
id: `r${i + 1}`,
name: `Account ${i + 1}`,
...(rich && i === n - 2
? {
description: DOC,
notes_html: '<h2>Title</h2><p>one</p><p>two</p>',
notes_rich: '<p>Rich <strong>text</strong></p><p>second</p>',
}
: {}),
}));
}
function renderGrid(rows: Record<string, unknown>[], columns: any[]) {
const ds = {
find: vi.fn(async () => ({ data: rows, total: rows.length, hasMore: false, pageSize: 50 })),
getObjectSchema: vi.fn(async (name: string) => ({ name, fields: FIELDS })),
} as any;
return render(
<ActionProvider>
<SchemaRendererProvider dataSource={ds}>
<ObjectGrid
schema={{
type: 'object-grid',
objectName: 'test_object',
columns,
pagination: { pageSize: 50 },
} as any}
dataSource={ds}
/>
</SchemaRendererProvider>
</ActionProvider>,
);
}
const bodyRows = () =>
Array.from(document.querySelectorAll('tbody > tr')).filter(
(tr) => tr.getAttribute('data-testid') !== 'add-record-row',
);
const clamps = () => Array.from(document.querySelectorAll<HTMLElement>('[data-testid="grid-rich-text-one-line"]'));
const ORIGINAL_INNER_WIDTH = window.innerWidth;
beforeAll(() => {
if (!Element.prototype.scrollIntoView) {
Element.prototype.scrollIntoView = vi.fn() as any;
}
});
afterEach(() => {
Object.defineProperty(window, 'innerWidth', { writable: true, configurable: true, value: ORIGINAL_INNER_WIDTH });
cleanup();
});
function desktop() {
Object.defineProperty(window, 'innerWidth', { writable: true, configurable: true, value: 1280 });
}
describe('objectui#12480 — a rich-content cell keeps the one-line row', () => {
it('10 rows with a markdown, an html and a richtext column: 10 rows drawn, each rich cell inside a one-line clamp', async () => {
desktop();
renderGrid(makeRows(10, true), [
{ field: 'name', label: 'Name' },
{ field: 'description', label: 'Description' },
{ field: 'notes_html', label: 'Html' },
{ field: 'notes_rich', label: 'Rich' },
]);
await waitFor(() => expect(screen.getByText('Account 10')).toBeInTheDocument());
expect(bodyRows()).toHaveLength(10);
// One clamp per rich-content cell: three rich columns x ten rows.
await waitFor(() => expect(clamps()).toHaveLength(30));
for (const el of clamps()) {
expect(el).toHaveClass('line-clamp-1');
expect(el.closest('td')).not.toBeNull();
}
// The documents themselves are drawn INSIDE the clamp, not beside it.
const row9 = bodyRows()[8] as HTMLElement;
const rowClamps = Array.from(row9.querySelectorAll<HTMLElement>('[data-testid="grid-rich-text-one-line"]'));
expect(rowClamps).toHaveLength(3);
await waitFor(() => expect(rowClamps[0].querySelector('ul')).not.toBeNull());
expect(rowClamps[1].querySelector('h2')).not.toBeNull();
expect(rowClamps[2].querySelector('strong')).not.toBeNull();
// Over-correction control: the plain text column is not clamped this way.
expect(screen.getByText('Account 9').closest('[data-testid="grid-rich-text-one-line"]')).toBeNull();
});
it('a rich-content column authored `wrap: true` keeps the whole document (no clamp)', async () => {
desktop();
renderGrid(makeRows(10, true), [
{ field: 'name', label: 'Name' },
{ field: 'description', label: 'Description', wrap: true },
]);
await waitFor(() => expect(screen.getByText('Account 10')).toBeInTheDocument());
expect(bodyRows()).toHaveLength(10);
await waitFor(() => expect((bodyRows()[8] as HTMLElement).querySelector('ul')).not.toBeNull());
expect(clamps()).toHaveLength(0);
});
it('CONTROL — an 8-row object with no rich-content column is unchanged: 8 rows, no clamp, cells still truncate', async () => {
desktop();
renderGrid(makeRows(8, false), [{ field: 'name', label: 'Name' }]);
await waitFor(() => expect(screen.getByText('Account 8')).toBeInTheDocument());
expect(bodyRows()).toHaveLength(8);
expect(clamps()).toHaveLength(0);
expect(screen.getByText('Account 8').closest('td')?.querySelector('.truncate')).not.toBeNull();
});
});
/**
* objectstack#36344 — the one line the clamp shows is a line of the cell's
* text. A markdown value that starts with `# Heading` filled the clamp's one
* line with an H1, so the row grew to the heading's line height. The grid
* passes `inGridColumn` to every column cell, and the markdown cell then draws
* its value in the `cell` face: no heading element, the cell's own font size
* and line height. jsdom does no layout; the pixel readings are in the PR.
*/
describe('objectstack#36344 — a markdown heading in a grid cell does not draw at heading size', () => {
it('a markdown cell whose value starts with a heading draws no heading element inside the clamp', async () => {
desktop();
const rows = [
{ id: 'r1', name: 'Account 1', description: '# Heading\n\nBody' },
{ id: 'r2', name: 'Account 2' },
];
renderGrid(rows, [
{ field: 'name', label: 'Name' },
{ field: 'description', label: 'Description' },
]);
await waitFor(() => expect(screen.getByText('Account 2')).toBeInTheDocument());
const row1 = bodyRows()[0] as HTMLElement;
const face = await waitFor(() => {
const el = row1.querySelector<HTMLElement>('[data-testid="grid-rich-text-one-line"] [data-markdown-face]');
if (!el) throw new Error('markdown cell not rendered yet');
return el;
});
expect(face).toHaveAttribute('data-markdown-face', 'cell');
expect(face).toHaveClass('text-[length:inherit]', 'leading-[inherit]');
expect(face.querySelector('[data-markdown-heading]')).toHaveTextContent('Heading');
expect(document.querySelector('tbody h1, tbody h2, tbody h3, tbody h4, tbody h5, tbody h6')).toBeNull();
});
});