|
| 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 | + * objectui#11541 — `record:details` edit mode gives a `markdown` field an |
| 11 | + * editor, and that editor is MULTI-line. |
| 12 | + * |
| 13 | + * Before, a markdown row offered no pencil and, with the section in edit mode, |
| 14 | + * no editor at all: `markdown` is in the fields package's shared exclusion, and |
| 15 | + * the detail hosts consult it since #4228. A markdown field that no form names |
| 16 | + * therefore had no editing surface anywhere. |
| 17 | + * |
| 18 | + * #4228 recorded its reason for excluding markdown from the detail row: "heavy |
| 19 | + * editor; a one-line text box is lossy". The one-line text box is this |
| 20 | + * component's terminal input (an `<input type="text">`), and the loss is |
| 21 | + * concrete: the browser strips every line break from the value such an input |
| 22 | + * is seeded with, so one keystroke writes the flattened text back. That is why |
| 23 | + * the detail row routes markdown to the fields package's multi-line |
| 24 | + * `TextAreaField` instead, and why the pins below compare values BYTE for byte with blank |
| 25 | + * lines and a trailing newline in them: a single-line editor cannot pass them. |
| 26 | + * |
| 27 | + * `html` and `richtext` stay excluded, unchanged; the grid cell keeps the |
| 28 | + * exclusion for `markdown` too (pinned in `plugin-grid`'s |
| 29 | + * `inline-edit-options.test.ts`, at the grid's own gate). |
| 30 | + */ |
| 31 | + |
| 32 | +import * as React from 'react'; |
| 33 | +import { describe, it, expect, vi, beforeAll } from 'vitest'; |
| 34 | +import { render, screen, fireEvent, waitFor, act } from '@testing-library/react'; |
| 35 | +import { InlineEditProvider, useInlineEdit } from '@object-ui/react'; |
| 36 | +import type { DetailViewSection } from '@object-ui/types'; |
| 37 | +import { DetailSection } from '../DetailSection'; |
| 38 | +import { HeaderHighlight } from '../HeaderHighlight'; |
| 39 | +import { InlineEditSaveBar } from '../InlineEditSaveBar'; |
| 40 | +import { InlineFieldInput, INLINE_PLAIN_TEXT_INPUT_TESTID } from '../InlineFieldInput'; |
| 41 | + |
| 42 | +/** Blank lines inside, and a trailing newline: the bytes a one-line box drops. */ |
| 43 | +const STORED = '# Deal notes\n\nFirst paragraph.\n\n- one\n- two\n'; |
| 44 | +/** The edit appends a section that ends in a newline too. */ |
| 45 | +const EDITED = `${STORED}\n## Follow-up\n\nSecond paragraph.\n`; |
| 46 | + |
| 47 | +const plainInput = (root: ParentNode = document) => |
| 48 | + root.querySelector(`[data-testid="${INLINE_PLAIN_TEXT_INPUT_TESTID}"]`); |
| 49 | +const editors = (root: ParentNode) => root.querySelectorAll('input, textarea'); |
| 50 | +const hasPencil = () => screen.queryAllByLabelText('Double-click to edit').length > 0; |
| 51 | + |
| 52 | +beforeAll(() => { |
| 53 | + // Desktop layout — the mobile branch renders its own read-only row shape. |
| 54 | + Object.defineProperty(window, 'innerWidth', { configurable: true, value: 1280 }); |
| 55 | +}); |
| 56 | + |
| 57 | +// --------------------------------------------------------------------------- |
| 58 | +// The editor itself |
| 59 | +// --------------------------------------------------------------------------- |
| 60 | + |
| 61 | +describe('objectui#11541 — InlineFieldInput edits markdown in a multi-line textarea', () => { |
| 62 | + it('renders a textarea seeded with the stored value, byte for byte, never the terminal input', () => { |
| 63 | + const { container } = render( |
| 64 | + <InlineFieldInput field={{ name: 'notes', type: 'markdown' }} value={STORED} onChange={vi.fn()} />, |
| 65 | + ); |
| 66 | + expect(plainInput(container)).toBeNull(); |
| 67 | + const textarea = container.querySelector('textarea'); |
| 68 | + expect(textarea).not.toBeNull(); |
| 69 | + expect((textarea as HTMLTextAreaElement).value).toBe(STORED); |
| 70 | + }); |
| 71 | + |
| 72 | + it('emits the typed string exactly, newlines included', () => { |
| 73 | + const onChange = vi.fn(); |
| 74 | + const { container } = render( |
| 75 | + <InlineFieldInput field={{ name: 'notes', type: 'markdown' }} value={STORED} onChange={onChange} />, |
| 76 | + ); |
| 77 | + fireEvent.change(container.querySelector('textarea') as HTMLTextAreaElement, { |
| 78 | + target: { value: EDITED }, |
| 79 | + }); |
| 80 | + expect(onChange).toHaveBeenCalledTimes(1); |
| 81 | + expect(onChange.mock.calls[0][0]).toBe(EDITED); |
| 82 | + }); |
| 83 | + |
| 84 | + it('takes the entered field’s focus, like the other routed editors', () => { |
| 85 | + const { container } = render( |
| 86 | + <InlineFieldInput field={{ name: 'notes', type: 'markdown' }} value={STORED} onChange={vi.fn()} autoFocus />, |
| 87 | + ); |
| 88 | + expect(container.querySelector('textarea')).toBe(document.activeElement); |
| 89 | + }); |
| 90 | +}); |
| 91 | + |
| 92 | +// --------------------------------------------------------------------------- |
| 93 | +// The two detail hosts open it |
| 94 | +// --------------------------------------------------------------------------- |
| 95 | + |
| 96 | +const markdownSchema = { fields: { notes: { type: 'markdown', label: 'Notes' } } }; |
| 97 | + |
| 98 | +describe('objectui#11541 — both detail hosts offer the markdown editor', () => { |
| 99 | + it('the details body shows the inline-edit affordance on a markdown row', () => { |
| 100 | + render( |
| 101 | + <DetailSection |
| 102 | + section={{ fields: [{ name: 'notes', label: 'Notes' }] } as unknown as DetailViewSection} |
| 103 | + data={{ notes: STORED }} |
| 104 | + objectSchema={markdownSchema} |
| 105 | + onEnterInlineEdit={vi.fn()} |
| 106 | + />, |
| 107 | + ); |
| 108 | + expect(hasPencil()).toBe(true); |
| 109 | + }); |
| 110 | + |
| 111 | + it('the details body renders one editor for the markdown row in edit mode', () => { |
| 112 | + const { container } = render( |
| 113 | + <DetailSection |
| 114 | + section={{ fields: [{ name: 'notes', label: 'Notes' }] } as unknown as DetailViewSection} |
| 115 | + data={{ notes: STORED }} |
| 116 | + objectSchema={markdownSchema} |
| 117 | + isEditing |
| 118 | + onEnterInlineEdit={vi.fn()} |
| 119 | + />, |
| 120 | + ); |
| 121 | + expect(editors(container)).toHaveLength(1); |
| 122 | + expect((container.querySelector('textarea') as HTMLTextAreaElement).value).toBe(STORED); |
| 123 | + }); |
| 124 | + |
| 125 | + it('the highlights strip opens the same editor inside the edit session', () => { |
| 126 | + function EnterEdit() { |
| 127 | + const inline = useInlineEdit(); |
| 128 | + return <button type="button" onClick={() => inline!.enter('notes')}>enter-edit</button>; |
| 129 | + } |
| 130 | + const { container } = render( |
| 131 | + <InlineEditProvider canEdit> |
| 132 | + <HeaderHighlight fields={[{ name: 'notes', label: 'Notes' }] as any} data={{ notes: STORED }} objectSchema={markdownSchema} /> |
| 133 | + <EnterEdit /> |
| 134 | + </InlineEditProvider>, |
| 135 | + ); |
| 136 | + fireEvent.click(screen.getByText('enter-edit')); |
| 137 | + expect(plainInput(container)).toBeNull(); |
| 138 | + expect((container.querySelector('textarea') as HTMLTextAreaElement).value).toBe(STORED); |
| 139 | + }); |
| 140 | +}); |
| 141 | + |
| 142 | +// --------------------------------------------------------------------------- |
| 143 | +// The save reads back unchanged |
| 144 | +// --------------------------------------------------------------------------- |
| 145 | + |
| 146 | +describe('objectui#11541 — a markdown save reads back unchanged, newlines included', () => { |
| 147 | + it('the PATCH body carries the edited string byte for byte, and the refetched record seeds it back', async () => { |
| 148 | + /** The server's copy of the record; `update` writes through the wire's JSON. */ |
| 149 | + const store: Record<string, unknown> = { id: 'r1', notes: STORED, updated_at: 'v1' }; |
| 150 | + const wire: string[] = []; |
| 151 | + const update = vi.fn(async (_object: string, _id: string, patch: Record<string, unknown>) => { |
| 152 | + const body = JSON.stringify(patch); |
| 153 | + wire.push(body); |
| 154 | + Object.assign(store, JSON.parse(body), { updated_at: 'v2' }); |
| 155 | + return {}; |
| 156 | + }); |
| 157 | + const section = { fields: [{ name: 'notes', label: 'Notes' }] } as unknown as DetailViewSection; |
| 158 | + |
| 159 | + /** Stand-in for `DetailView`: the fetched record overlaid with the draft. */ |
| 160 | + function Body({ record }: { record: Record<string, unknown> }) { |
| 161 | + const inline = useInlineEdit()!; |
| 162 | + return ( |
| 163 | + <> |
| 164 | + <button type="button" onClick={() => inline.enter()}>enter-edit</button> |
| 165 | + <DetailSection |
| 166 | + section={section} |
| 167 | + data={{ ...record, ...inline.draft }} |
| 168 | + objectSchema={markdownSchema} |
| 169 | + isEditing={inline.editing} |
| 170 | + onFieldChange={inline.setField} |
| 171 | + /> |
| 172 | + </> |
| 173 | + ); |
| 174 | + } |
| 175 | + function Page() { |
| 176 | + const [record, setRecord] = React.useState<Record<string, unknown>>(() => ({ ...store })); |
| 177 | + const refresh = React.useCallback(async () => setRecord({ ...store }), []); |
| 178 | + return ( |
| 179 | + <InlineEditProvider canEdit> |
| 180 | + <Body record={record} /> |
| 181 | + <InlineEditSaveBar dataSource={{ update }} objectName="deal" recordId="r1" data={record} refresh={refresh} /> |
| 182 | + </InlineEditProvider> |
| 183 | + ); |
| 184 | + } |
| 185 | + |
| 186 | + const { container } = render(<Page />); |
| 187 | + fireEvent.click(screen.getByText('enter-edit')); |
| 188 | + const textarea = container.querySelector('textarea') as HTMLTextAreaElement; |
| 189 | + expect(textarea.value).toBe(STORED); |
| 190 | + fireEvent.change(textarea, { target: { value: EDITED } }); |
| 191 | + |
| 192 | + await act(async () => { |
| 193 | + fireEvent.click(screen.getByRole('button', { name: 'Save' })); |
| 194 | + }); |
| 195 | + await waitFor(() => expect(update).toHaveBeenCalledTimes(1)); |
| 196 | + expect(wire).toHaveLength(1); |
| 197 | + expect(JSON.parse(wire[0])).toEqual({ notes: EDITED }); |
| 198 | + expect(store.notes).toBe(EDITED); |
| 199 | + // Save leaves edit mode; re-entering seeds the editor from the refetched record. |
| 200 | + await waitFor(() => expect(container.querySelector('textarea')).toBeNull()); |
| 201 | + fireEvent.click(screen.getByText('enter-edit')); |
| 202 | + expect((container.querySelector('textarea') as HTMLTextAreaElement).value).toBe(EDITED); |
| 203 | + }); |
| 204 | +}); |
| 205 | + |
| 206 | +// --------------------------------------------------------------------------- |
| 207 | +// html / richtext — unchanged: still no editor |
| 208 | +// --------------------------------------------------------------------------- |
| 209 | + |
| 210 | +describe('objectui#11541 — `html` and `richtext` still open no editor', () => { |
| 211 | + const MARKUP: Record<string, string> = { html: '<p>a</p>\n<p>b</p>', richtext: '<p>a</p>' }; |
| 212 | + |
| 213 | + for (const type of ['html', 'richtext']) { |
| 214 | + it(`a \`${type}\` row has no affordance, and no editor in edit mode`, () => { |
| 215 | + const schema = { fields: { body: { type, label: 'Body' } } }; |
| 216 | + const section = { fields: [{ name: 'body', label: 'Body' }] } as unknown as DetailViewSection; |
| 217 | + render( |
| 218 | + <DetailSection section={section} data={{ body: MARKUP[type] }} objectSchema={schema} onEnterInlineEdit={vi.fn()} />, |
| 219 | + ); |
| 220 | + expect(hasPencil()).toBe(false); |
| 221 | + const { container } = render( |
| 222 | + <DetailSection section={section} data={{ body: MARKUP[type] }} objectSchema={schema} isEditing onEnterInlineEdit={vi.fn()} />, |
| 223 | + ); |
| 224 | + expect(editors(container)).toHaveLength(0); |
| 225 | + }); |
| 226 | + |
| 227 | + it(`a \`${type}\` highlight opens no editor inside the edit session`, () => { |
| 228 | + function EnterEdit() { |
| 229 | + const inline = useInlineEdit(); |
| 230 | + return <button type="button" onClick={() => inline!.enter('body')}>enter-edit</button>; |
| 231 | + } |
| 232 | + const { container } = render( |
| 233 | + <InlineEditProvider canEdit> |
| 234 | + <HeaderHighlight |
| 235 | + fields={[{ name: 'body', label: 'Body' }] as any} |
| 236 | + data={{ body: MARKUP[type] }} |
| 237 | + objectSchema={{ fields: { body: { type, label: 'Body' } } }} |
| 238 | + /> |
| 239 | + <EnterEdit /> |
| 240 | + </InlineEditProvider>, |
| 241 | + ); |
| 242 | + fireEvent.click(screen.getByText('enter-edit')); |
| 243 | + expect(editors(container)).toHaveLength(0); |
| 244 | + }); |
| 245 | + } |
| 246 | +}); |
0 commit comments