|
| 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#25897 — `record:details` READ mode wraps a plain `text` value |
| 11 | + * instead of cutting it to one line. |
| 12 | + * |
| 13 | + * ## The defect |
| 14 | + * |
| 15 | + * A HotCRM case read its subject as "Data export timing out for large…" in a |
| 16 | + * two-column section with room to its right. The row resolved `text` through |
| 17 | + * `getCellRenderer`, whose table maps it to `TextCellRenderer` — the GRID's |
| 18 | + * one-line cell, `TruncatedText` (`truncate`: no wrap, an ellipsis). The ruling |
| 19 | + * (triage on the card): "Read mode wraps text-like values; truncation only |
| 20 | + * where a layout declares it, with the full value reachable." |
| 21 | + * |
| 22 | + * ## What is pinned, and how |
| 23 | + * |
| 24 | + * The row now draws every value the table sends to the text cell through |
| 25 | + * `TextField`'s own read display, the move objectui#11577 made for `textarea`. |
| 26 | + * The value cell is compared, as markup, with what `TextField` renders |
| 27 | + * read-only for the same value, and no element between the drawn text and the |
| 28 | + * row may carry a utility that stops it wrapping. happy-dom applies no |
| 29 | + * Tailwind, so the class list is named here only for that negative check. |
| 30 | + * |
| 31 | + * Controls: |
| 32 | + * - the GRID keeps its one-line cell: `getCellRenderer('text')` is still |
| 33 | + * `TextCellRenderer`, which still truncates with the whole value as its |
| 34 | + * `title` (truncation where the layout declares it); |
| 35 | + * - a host that registered its own `text` face keeps it on the record page. |
| 36 | + */ |
| 37 | + |
| 38 | +import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'; |
| 39 | +import { render, screen, cleanup, act, waitFor } from '@testing-library/react'; |
| 40 | +import * as React from 'react'; |
| 41 | +import { RecordContextProvider } from '@object-ui/react'; |
| 42 | +import { TextField, TextCellRenderer, getCellRenderer, registerFieldRenderer } from '@object-ui/fields'; |
| 43 | +import { RecordDetailsRenderer } from '../record-details'; |
| 44 | + |
| 45 | +/** The case subject the browser pass read cut. */ |
| 46 | +const SUBJECT = 'Data export timing out for large datasets'; |
| 47 | + |
| 48 | +const DESKTOP_WIDTH = 1280; |
| 49 | +const MOBILE_WIDTH = 375; |
| 50 | +const pinViewport = (width: number) => () => { |
| 51 | + Object.defineProperty(window, 'innerWidth', { configurable: true, value: width }); |
| 52 | +}; |
| 53 | + |
| 54 | +const caseSchema = { |
| 55 | + name: 'support_case', |
| 56 | + label: 'Case', |
| 57 | + nameField: 'case_number', |
| 58 | + fields: { |
| 59 | + case_number: { type: 'text', label: 'Case Number' }, |
| 60 | + subject: { type: 'text', label: 'Subject' }, |
| 61 | + reference: { type: 'auto_number', label: 'Reference' }, |
| 62 | + }, |
| 63 | +}; |
| 64 | +const BODY_FIELDS = ['subject', 'reference']; |
| 65 | + |
| 66 | +async function renderBody(record: Record<string, unknown>) { |
| 67 | + const ds = { getObjectSchema: vi.fn(async () => caseSchema) }; |
| 68 | + render( |
| 69 | + <RecordContextProvider |
| 70 | + objectName="support_case" |
| 71 | + recordId="C2" |
| 72 | + data={{ id: 'C2', case_number: 'CASE-00002', ...record }} |
| 73 | + objectSchema={caseSchema} |
| 74 | + dataSource={ds as never} |
| 75 | + > |
| 76 | + <RecordDetailsRenderer schema={{ fields: BODY_FIELDS } as never} /> |
| 77 | + </RecordContextProvider>, |
| 78 | + ); |
| 79 | + await waitFor(() => expect(ds.getObjectSchema).toHaveBeenCalled()); |
| 80 | + await act(async () => { |
| 81 | + await ds.getObjectSchema.mock.results[0].value; |
| 82 | + }); |
| 83 | +} |
| 84 | + |
| 85 | +function markupOf(element: React.ReactElement): string { |
| 86 | + const { container, unmount } = render(element); |
| 87 | + const html = container.innerHTML; |
| 88 | + unmount(); |
| 89 | + return html; |
| 90 | +} |
| 91 | + |
| 92 | +const textReadDisplay = (name: string, type: string, value: string) => |
| 93 | + markupOf(<TextField field={{ name, type, label: name } as never} value={value} onChange={() => {}} readonly />); |
| 94 | + |
| 95 | +/** The element holding a row's drawn value, found from the label the row prints. */ |
| 96 | +function valueCellOf(label: string, layout: 'desktop' | 'mobile'): HTMLElement { |
| 97 | + const box = screen.getByText(label).nextElementSibling as HTMLElement | null; |
| 98 | + expect(box).not.toBeNull(); |
| 99 | + if (layout === 'mobile') return box!; |
| 100 | + expect(box!.firstElementChild).not.toBeNull(); |
| 101 | + return box!.firstElementChild as HTMLElement; |
| 102 | +} |
| 103 | + |
| 104 | +/** The innermost element whose text is exactly the value. */ |
| 105 | +function drawingNodeOf(cell: HTMLElement, value: string): HTMLElement { |
| 106 | + const hits = [cell, ...Array.from(cell.querySelectorAll<HTMLElement>('*'))].filter( |
| 107 | + (el) => el.textContent === value && !Array.from(el.children).some((c) => c.textContent === value), |
| 108 | + ); |
| 109 | + expect(hits).toHaveLength(1); |
| 110 | + return hits[0]; |
| 111 | +} |
| 112 | + |
| 113 | +/** Utilities that keep a value on one line. */ |
| 114 | +const ONE_LINE = ['truncate', 'whitespace-nowrap', 'text-ellipsis', 'line-clamp-1']; |
| 115 | + |
| 116 | +function oneLineClassesBetween(node: HTMLElement, stop: HTMLElement): string[] { |
| 117 | + const found: string[] = []; |
| 118 | + for (let el: HTMLElement | null = node; el; el = el.parentElement) { |
| 119 | + for (const cls of ONE_LINE) if (el.classList.contains(cls)) found.push(cls); |
| 120 | + if (el === stop) break; |
| 121 | + } |
| 122 | + return found; |
| 123 | +} |
| 124 | + |
| 125 | +beforeEach(() => { |
| 126 | + // `useRecordEditable` probes `POST /api/v1/security/explain`; happy-dom would |
| 127 | + // send that to a real socket, which the repo's network-escape guard fails the |
| 128 | + // file for (objectui#6640). Its answer is orthogonal to the read display. |
| 129 | + vi.stubGlobal('fetch', vi.fn(async () => ({ |
| 130 | + ok: true, |
| 131 | + status: 200, |
| 132 | + json: async () => ({ allowed: true }), |
| 133 | + text: async () => '{"allowed":true}', |
| 134 | + })) as never); |
| 135 | +}); |
| 136 | + |
| 137 | +afterEach(() => { |
| 138 | + cleanup(); |
| 139 | + vi.unstubAllGlobals(); |
| 140 | +}); |
| 141 | + |
| 142 | +for (const layout of ['desktop', 'mobile'] as const) { |
| 143 | + describe(`objectstack#25897 — record:details read mode, ${layout}: a text value wraps`, () => { |
| 144 | + beforeEach(pinViewport(layout === 'desktop' ? DESKTOP_WIDTH : MOBILE_WIDTH)); |
| 145 | + |
| 146 | + it('a `text` cell holds exactly the read display TextField renders for it', async () => { |
| 147 | + await renderBody({ subject: SUBJECT }); |
| 148 | + expect(valueCellOf('Subject', layout).innerHTML).toBe(textReadDisplay('subject', 'text', SUBJECT)); |
| 149 | + }); |
| 150 | + |
| 151 | + it('nothing between the drawn subject and the row keeps it on one line', async () => { |
| 152 | + await renderBody({ subject: SUBJECT }); |
| 153 | + const cell = valueCellOf('Subject', layout); |
| 154 | + const node = drawingNodeOf(cell, SUBJECT); |
| 155 | + expect(node.textContent).toBe(SUBJECT); |
| 156 | + expect(oneLineClassesBetween(node, cell)).toEqual([]); |
| 157 | + }); |
| 158 | + }); |
| 159 | +} |
| 160 | + |
| 161 | +describe('objectstack#25897 — every type the table sends to the text cell reads the same way', () => { |
| 162 | + beforeEach(pinViewport(DESKTOP_WIDTH)); |
| 163 | + |
| 164 | + it('an `auto_number` value reads through TextField too', async () => { |
| 165 | + await renderBody({ subject: SUBJECT, reference: 'REF-000123' }); |
| 166 | + const cell = valueCellOf('Reference', 'desktop'); |
| 167 | + expect(cell.innerHTML).toBe(textReadDisplay('reference', 'auto_number', 'REF-000123')); |
| 168 | + expect(oneLineClassesBetween(drawingNodeOf(cell, 'REF-000123'), cell)).toEqual([]); |
| 169 | + }); |
| 170 | +}); |
| 171 | + |
| 172 | +describe('objectstack#25897 — CONTROLS', () => { |
| 173 | + beforeEach(pinViewport(DESKTOP_WIDTH)); |
| 174 | + |
| 175 | + it('the grid keeps its one-line text cell, titled with the whole value', () => { |
| 176 | + expect(getCellRenderer('text')).toBe(TextCellRenderer); |
| 177 | + const { container } = render( |
| 178 | + <TextCellRenderer value={SUBJECT} field={{ name: 'subject', type: 'text' } as never} />, |
| 179 | + ); |
| 180 | + const span = container.querySelector('span'); |
| 181 | + expect(span).toHaveClass('truncate'); |
| 182 | + expect(span).toHaveAttribute('title', SUBJECT); |
| 183 | + }); |
| 184 | + |
| 185 | + it("a host's own `text` face is kept on the record page", async () => { |
| 186 | + const shipped = getCellRenderer('text'); |
| 187 | + const HostText = ({ value }: { value: unknown }) => <em data-host-text>{String(value)}</em>; |
| 188 | + try { |
| 189 | + registerFieldRenderer('text', HostText as never); |
| 190 | + await renderBody({ subject: SUBJECT }); |
| 191 | + const cell = valueCellOf('Subject', 'desktop'); |
| 192 | + expect(cell.querySelector('em[data-host-text]')?.textContent).toBe(SUBJECT); |
| 193 | + } finally { |
| 194 | + registerFieldRenderer('text', shipped); |
| 195 | + } |
| 196 | + }); |
| 197 | +}); |
0 commit comments