@astryxdesign/richtext 0.7.6-canary.f73623d → 0.7.6-canary.f77aaf2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/RichTextEditor.d.ts +1 -1
- package/dist/RichTextEditor.d.ts.map +1 -1
- package/dist/RichTextEditor.js +2 -2
- package/dist/RichTextEditorToolbar.js +2 -2
- package/package.json +19 -5
- package/src/RichTextEditor.tsx +9 -3
- package/src/RichTextEditorToolbar.tsx +2 -2
- package/src/CodeBlockHeaderPlugin.test.tsx +0 -97
- package/src/CodeSyntaxPlugin.test.tsx +0 -60
- package/src/MarkdownExtensionsPlugin.test.tsx +0 -279
- package/src/RichTextEditor.test.tsx +0 -2258
- package/src/TableColumnFloorPlugin.test.tsx +0 -49
- package/src/TableScrollRegionPlugin.test.tsx +0 -138
- package/src/__tests__/MarkdownParitySandbox.a11y.chromium.spec.ts +0 -2092
- package/src/__tests__/MarkdownPlugins.a11y.chromium.spec.ts +0 -483
- package/src/__tests__/MarkdownPluginsImportExport.a11y.chromium.spec.ts +0 -152
- package/src/__tests__/Playground.a11y.chromium.spec.ts +0 -755
- package/src/__tests__/RichTextCodeLanguages.a11y.chromium.spec.ts +0 -61
- package/src/__tests__/RichTextEditorToolbar.a11y.chromium.spec.ts +0 -113
- package/src/__tests__/RichTextView.a11y.chromium.spec.ts +0 -101
- package/src/__tests__/TableCellSelection.a11y.chromium.spec.ts +0 -380
- package/src/markdownAngleCodeSpans.test.ts +0 -92
- package/src/markdownAngleDestinations.test.ts +0 -332
- package/src/markdownCharacterReferences.test.ts +0 -202
- package/src/markdownCodeFence.test.ts +0 -195
- package/src/markdownCodeSpans.test.ts +0 -399
- package/src/markdownEscapes.test.ts +0 -234
- package/src/markdownExtensionSelection.test.ts +0 -102
- package/src/markdownExtensions.test.ts +0 -765
- package/src/markdownHeadingMarkers.test.ts +0 -132
- package/src/markdownIndentedFences.test.ts +0 -43
- package/src/markdownIndentedTables.test.ts +0 -43
- package/src/markdownListContinuation.test.ts +0 -34
- package/src/markdownListIndentation.test.ts +0 -235
- package/src/markdownListThematicBreak.test.ts +0 -31
- package/src/markdownListThematicBreakInside.test.ts +0 -64
- package/src/markdownOpenFence.test.ts +0 -35
- package/src/markdownOrderedList.test.ts +0 -62
- package/src/markdownQuoteMarkers.test.ts +0 -206
- package/src/markdownRefusedLinks.test.tsx +0 -193
- package/src/markdownSource.test.tsx +0 -750
- package/src/markdownSpaceRuns.test.ts +0 -204
- package/src/markdownSpacedBlockStarts.test.ts +0 -96
- package/src/markdownTabRuns.test.ts +0 -75
- package/src/markdownTable.test.tsx +0 -214
- package/src/markdownTableLinks.test.ts +0 -69
- package/src/markdownTaskList.test.tsx +0 -445
- package/src/markdownThematicBreak.test.tsx +0 -185
- package/src/publicSurface.test.ts +0 -42
- package/src/textSemantics.test.tsx +0 -368
|
@@ -1,2258 +0,0 @@
|
|
|
1
|
-
// Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* @file RichTextEditor.test.tsx
|
|
5
|
-
* @input Uses vitest, @testing-library/react, RichTextEditor + RichTextView
|
|
6
|
-
* @output Unit tests for the opt-in Lexical editor components, including
|
|
7
|
-
* accessible label wiring, shared input visuals/status variants, theme-target
|
|
8
|
-
* metadata, placeholder semantics, canonical link-dialog layout, top-toolbar
|
|
9
|
-
* ordering and horizontal scrolling, and the read-only vs disabled split
|
|
10
|
-
* with editable-state sync across prop toggles
|
|
11
|
-
* @position Testing; validates RichTextEditor.tsx and RichTextView.tsx
|
|
12
|
-
*
|
|
13
|
-
* SYNC: When the editor components change, update these tests to match.
|
|
14
|
-
*/
|
|
15
|
-
|
|
16
|
-
import {describe, it, expect, vi, beforeAll, afterAll} from 'vitest';
|
|
17
|
-
import {render, screen, waitFor, fireEvent, act} from '@testing-library/react';
|
|
18
|
-
import userEvent from '@testing-library/user-event';
|
|
19
|
-
import {createRef, StrictMode, useEffect, useState} from 'react';
|
|
20
|
-
import * as stylex from '@stylexjs/stylex';
|
|
21
|
-
import {useLexicalComposerContext} from '@lexical/react/LexicalComposerContext';
|
|
22
|
-
import type {EditorState, LexicalEditor} from 'lexical';
|
|
23
|
-
import {
|
|
24
|
-
$createListItemNode,
|
|
25
|
-
$createListNode,
|
|
26
|
-
$isListItemNode,
|
|
27
|
-
} from '@lexical/list';
|
|
28
|
-
import {
|
|
29
|
-
$getRoot,
|
|
30
|
-
$createParagraphNode,
|
|
31
|
-
$createTextNode,
|
|
32
|
-
$isElementNode,
|
|
33
|
-
} from 'lexical';
|
|
34
|
-
import {HeadingNode} from '@lexical/rich-text';
|
|
35
|
-
import {TRANSFORMERS, $convertFromMarkdownString} from '@lexical/markdown';
|
|
36
|
-
import {
|
|
37
|
-
RichTextEditor,
|
|
38
|
-
type RichTextEditorProps,
|
|
39
|
-
type RichTextEditorRef,
|
|
40
|
-
} from './RichTextEditor';
|
|
41
|
-
import {RichTextView} from './RichTextView';
|
|
42
|
-
import {
|
|
43
|
-
markdownToEditorStateJSON,
|
|
44
|
-
editorStateJSONToMarkdown,
|
|
45
|
-
} from './markdownSerializers';
|
|
46
|
-
import {RichTextEditorToolbar} from './RichTextEditorToolbar';
|
|
47
|
-
import {registerIcons, resetIcons} from '@astryxdesign/core/Icon';
|
|
48
|
-
import {EDGE_COMP_ATTR} from '@astryxdesign/core/Layout';
|
|
49
|
-
import type {BaseProps} from '@astryxdesign/core';
|
|
50
|
-
import {Theme, defineTheme} from '@astryxdesign/core/theme';
|
|
51
|
-
import {rtlStyles} from '@astryxdesign/core/utils';
|
|
52
|
-
import {InternationalizationProvider} from '@astryxdesign/core/i18n';
|
|
53
|
-
import {
|
|
54
|
-
RichTextEditorAutoLinkPlugin,
|
|
55
|
-
DEFAULT_LINK_MATCHERS,
|
|
56
|
-
NEW_TAB_LINK_ATTRIBUTES,
|
|
57
|
-
} from './RichTextEditorAutoLinkPlugin';
|
|
58
|
-
import {sanitizeUrl, validateUrl} from './linkUtils';
|
|
59
|
-
import {docs as richTextEditorDoc} from './RichTextEditor.doc.mjs';
|
|
60
|
-
|
|
61
|
-
// Closed popover-backed tooltips are intentionally hidden from the default
|
|
62
|
-
// accessibility tree until their trigger opens them.
|
|
63
|
-
const h = {hidden: true} as const;
|
|
64
|
-
|
|
65
|
-
const originalShowModal = Object.getOwnPropertyDescriptor(
|
|
66
|
-
HTMLDialogElement.prototype,
|
|
67
|
-
'showModal',
|
|
68
|
-
);
|
|
69
|
-
const originalDialogClose = Object.getOwnPropertyDescriptor(
|
|
70
|
-
HTMLDialogElement.prototype,
|
|
71
|
-
'close',
|
|
72
|
-
);
|
|
73
|
-
|
|
74
|
-
beforeAll(() => {
|
|
75
|
-
// JSDOM does not implement the native dialog lifecycle used by Dialog.
|
|
76
|
-
HTMLDialogElement.prototype.showModal = function () {
|
|
77
|
-
this.setAttribute('open', '');
|
|
78
|
-
};
|
|
79
|
-
HTMLDialogElement.prototype.close = function () {
|
|
80
|
-
this.removeAttribute('open');
|
|
81
|
-
};
|
|
82
|
-
});
|
|
83
|
-
|
|
84
|
-
afterAll(() => {
|
|
85
|
-
if (originalShowModal) {
|
|
86
|
-
Object.defineProperty(
|
|
87
|
-
HTMLDialogElement.prototype,
|
|
88
|
-
'showModal',
|
|
89
|
-
originalShowModal,
|
|
90
|
-
);
|
|
91
|
-
} else {
|
|
92
|
-
delete (HTMLDialogElement.prototype as {showModal?: unknown}).showModal;
|
|
93
|
-
}
|
|
94
|
-
if (originalDialogClose) {
|
|
95
|
-
Object.defineProperty(
|
|
96
|
-
HTMLDialogElement.prototype,
|
|
97
|
-
'close',
|
|
98
|
-
originalDialogClose,
|
|
99
|
-
);
|
|
100
|
-
} else {
|
|
101
|
-
delete (HTMLDialogElement.prototype as {close?: unknown}).close;
|
|
102
|
-
}
|
|
103
|
-
});
|
|
104
|
-
|
|
105
|
-
// jsdom's Range does not implement getBoundingClientRect, which Lexical
|
|
106
|
-
// calls (via scroll-into-view) whenever it reconciles a selection while the
|
|
107
|
-
// editor is focused. Guarded stub shared by every describe that focuses the
|
|
108
|
-
// editor (Tab escape, imperative ref toggles).
|
|
109
|
-
function stubRangeRects() {
|
|
110
|
-
beforeAll(() => {
|
|
111
|
-
if (typeof Range.prototype.getBoundingClientRect !== 'function') {
|
|
112
|
-
Range.prototype.getBoundingClientRect = () => new DOMRect();
|
|
113
|
-
}
|
|
114
|
-
});
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
// Small plugin that captures the editor instance so tests can drive real
|
|
118
|
-
// Lexical updates (jsdom does not implement contenteditable editing).
|
|
119
|
-
function CaptureEditor({onReady}: {onReady: (editor: LexicalEditor) => void}) {
|
|
120
|
-
const [editor] = useLexicalComposerContext();
|
|
121
|
-
useEffect(() => {
|
|
122
|
-
onReady(editor);
|
|
123
|
-
}, [editor, onReady]);
|
|
124
|
-
return null;
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
// A minimal valid serialized Lexical editor state containing a single
|
|
128
|
-
// paragraph with the text "Hello world".
|
|
129
|
-
// Builds a serialized Lexical state containing a single paragraph with the
|
|
130
|
-
// given text. Used to verify that RichTextView reacts to `value` changes.
|
|
131
|
-
function makeParagraphState(text: string): string {
|
|
132
|
-
return JSON.stringify({
|
|
133
|
-
root: {
|
|
134
|
-
children: [
|
|
135
|
-
{
|
|
136
|
-
children: [
|
|
137
|
-
{
|
|
138
|
-
detail: 0,
|
|
139
|
-
format: 0,
|
|
140
|
-
mode: 'normal',
|
|
141
|
-
style: '',
|
|
142
|
-
text,
|
|
143
|
-
type: 'text',
|
|
144
|
-
version: 1,
|
|
145
|
-
},
|
|
146
|
-
],
|
|
147
|
-
direction: 'ltr',
|
|
148
|
-
format: '',
|
|
149
|
-
indent: 0,
|
|
150
|
-
type: 'paragraph',
|
|
151
|
-
version: 1,
|
|
152
|
-
},
|
|
153
|
-
],
|
|
154
|
-
direction: 'ltr',
|
|
155
|
-
format: '',
|
|
156
|
-
indent: 0,
|
|
157
|
-
type: 'root',
|
|
158
|
-
version: 1,
|
|
159
|
-
},
|
|
160
|
-
});
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
// Builds a serialized Lexical state containing a list. `listType` is
|
|
164
|
-
// 'bullet' (renders <ul>) or 'number' (renders <ol>). Used to verify that the
|
|
165
|
-
// editor theme applies visible list markers rather than bare indentation.
|
|
166
|
-
function makeListState(listType: 'bullet' | 'number'): string {
|
|
167
|
-
const tag = listType === 'number' ? 'ol' : 'ul';
|
|
168
|
-
return JSON.stringify({
|
|
169
|
-
root: {
|
|
170
|
-
children: [
|
|
171
|
-
{
|
|
172
|
-
children: [
|
|
173
|
-
{
|
|
174
|
-
children: [
|
|
175
|
-
{
|
|
176
|
-
detail: 0,
|
|
177
|
-
format: 0,
|
|
178
|
-
mode: 'normal',
|
|
179
|
-
style: '',
|
|
180
|
-
text: 'Item one',
|
|
181
|
-
type: 'text',
|
|
182
|
-
version: 1,
|
|
183
|
-
},
|
|
184
|
-
],
|
|
185
|
-
direction: 'ltr',
|
|
186
|
-
format: '',
|
|
187
|
-
indent: 0,
|
|
188
|
-
type: 'listitem',
|
|
189
|
-
version: 1,
|
|
190
|
-
value: 1,
|
|
191
|
-
},
|
|
192
|
-
],
|
|
193
|
-
direction: 'ltr',
|
|
194
|
-
format: '',
|
|
195
|
-
indent: 0,
|
|
196
|
-
type: 'list',
|
|
197
|
-
version: 1,
|
|
198
|
-
listType,
|
|
199
|
-
start: 1,
|
|
200
|
-
tag,
|
|
201
|
-
},
|
|
202
|
-
],
|
|
203
|
-
direction: 'ltr',
|
|
204
|
-
format: '',
|
|
205
|
-
indent: 0,
|
|
206
|
-
type: 'root',
|
|
207
|
-
version: 1,
|
|
208
|
-
},
|
|
209
|
-
});
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
// Builds a serialized Lexical state with a two-level nested bullet list:
|
|
213
|
-
// • Item one
|
|
214
|
-
// ◦ Nested item
|
|
215
|
-
// Lexical models nesting as a child `list` node inside a `listitem`. Used to
|
|
216
|
-
// verify that depth-2 markers differ from depth-1 (disc → circle) rather than
|
|
217
|
-
// falling back to bare indentation.
|
|
218
|
-
function makeNestedBulletState(): string {
|
|
219
|
-
const textNode = (text: string) => ({
|
|
220
|
-
detail: 0,
|
|
221
|
-
format: 0,
|
|
222
|
-
mode: 'normal',
|
|
223
|
-
style: '',
|
|
224
|
-
text,
|
|
225
|
-
type: 'text',
|
|
226
|
-
version: 1,
|
|
227
|
-
});
|
|
228
|
-
const listItem = (children: unknown[], value: number) => ({
|
|
229
|
-
children,
|
|
230
|
-
direction: 'ltr',
|
|
231
|
-
format: '',
|
|
232
|
-
indent: 0,
|
|
233
|
-
type: 'listitem',
|
|
234
|
-
version: 1,
|
|
235
|
-
value,
|
|
236
|
-
});
|
|
237
|
-
const bulletList = (children: unknown[]) => ({
|
|
238
|
-
children,
|
|
239
|
-
direction: 'ltr',
|
|
240
|
-
format: '',
|
|
241
|
-
indent: 0,
|
|
242
|
-
type: 'list',
|
|
243
|
-
version: 1,
|
|
244
|
-
listType: 'bullet',
|
|
245
|
-
start: 1,
|
|
246
|
-
tag: 'ul',
|
|
247
|
-
});
|
|
248
|
-
return JSON.stringify({
|
|
249
|
-
root: {
|
|
250
|
-
children: [
|
|
251
|
-
bulletList([
|
|
252
|
-
listItem([textNode('Item one')], 1),
|
|
253
|
-
// A nested list lives inside its own list item wrapper.
|
|
254
|
-
listItem([bulletList([listItem([textNode('Nested item')], 1)])], 2),
|
|
255
|
-
]),
|
|
256
|
-
],
|
|
257
|
-
direction: 'ltr',
|
|
258
|
-
format: '',
|
|
259
|
-
indent: 0,
|
|
260
|
-
type: 'root',
|
|
261
|
-
version: 1,
|
|
262
|
-
},
|
|
263
|
-
});
|
|
264
|
-
}
|
|
265
|
-
|
|
266
|
-
const HELLO_STATE = JSON.stringify({
|
|
267
|
-
root: {
|
|
268
|
-
children: [
|
|
269
|
-
{
|
|
270
|
-
children: [
|
|
271
|
-
{
|
|
272
|
-
detail: 0,
|
|
273
|
-
format: 0,
|
|
274
|
-
mode: 'normal',
|
|
275
|
-
style: '',
|
|
276
|
-
text: 'Hello world',
|
|
277
|
-
type: 'text',
|
|
278
|
-
version: 1,
|
|
279
|
-
},
|
|
280
|
-
],
|
|
281
|
-
direction: 'ltr',
|
|
282
|
-
format: '',
|
|
283
|
-
indent: 0,
|
|
284
|
-
type: 'paragraph',
|
|
285
|
-
version: 1,
|
|
286
|
-
},
|
|
287
|
-
],
|
|
288
|
-
direction: 'ltr',
|
|
289
|
-
format: '',
|
|
290
|
-
indent: 0,
|
|
291
|
-
type: 'root',
|
|
292
|
-
version: 1,
|
|
293
|
-
},
|
|
294
|
-
});
|
|
295
|
-
|
|
296
|
-
describe('RichTextEditor', () => {
|
|
297
|
-
it('documents every prop the editor declares', () => {
|
|
298
|
-
// The doc file is what `astryx component RichTextEditor` and the docsite
|
|
299
|
-
// read, so a prop missing from it is invisible to builders. The compile-time
|
|
300
|
-
// check below fails when RichTextEditorProps gains a prop this list lacks;
|
|
301
|
-
// the runtime check fails when the doc file lacks a listed prop.
|
|
302
|
-
const ownProps = [
|
|
303
|
-
'label',
|
|
304
|
-
'isLabelHidden',
|
|
305
|
-
'description',
|
|
306
|
-
'isOptional',
|
|
307
|
-
'isRequired',
|
|
308
|
-
'defaultValue',
|
|
309
|
-
'onChange',
|
|
310
|
-
'placeholder',
|
|
311
|
-
'isReadOnly',
|
|
312
|
-
'isDisabled',
|
|
313
|
-
'status',
|
|
314
|
-
'statusVariant',
|
|
315
|
-
'size',
|
|
316
|
-
'nodes',
|
|
317
|
-
'toolbar',
|
|
318
|
-
'plugins',
|
|
319
|
-
'hasMarkdownShortcuts',
|
|
320
|
-
'markdownExtensions',
|
|
321
|
-
'codeLanguages',
|
|
322
|
-
'transformers',
|
|
323
|
-
'hasAutoFocus',
|
|
324
|
-
'tabEscapeHint',
|
|
325
|
-
'maxLength',
|
|
326
|
-
'width',
|
|
327
|
-
'minHeight',
|
|
328
|
-
'labelTooltip',
|
|
329
|
-
'namespace',
|
|
330
|
-
] as const satisfies ReadonlyArray<keyof RichTextEditorProps>;
|
|
331
|
-
type Unlisted = Exclude<
|
|
332
|
-
keyof RichTextEditorProps,
|
|
333
|
-
keyof BaseProps | 'ref' | (typeof ownProps)[number]
|
|
334
|
-
>;
|
|
335
|
-
const everyPropListed: [Unlisted] extends [never] ? true : Unlisted = true;
|
|
336
|
-
expect(everyPropListed).toBe(true);
|
|
337
|
-
|
|
338
|
-
const docProps =
|
|
339
|
-
'props' in richTextEditorDoc ? richTextEditorDoc.props : [];
|
|
340
|
-
const documented = new Set(docProps.map(({name}) => name));
|
|
341
|
-
expect(ownProps.filter(name => !documented.has(name))).toEqual([]);
|
|
342
|
-
});
|
|
343
|
-
|
|
344
|
-
it('declares exactly the visual props its theme target reflects', () => {
|
|
345
|
-
// The core theming guards walk packages/core/src (and Lab) only, so
|
|
346
|
-
// nothing else compares this package's doc metadata with its
|
|
347
|
-
// themeProps() call. Set equality fails on drift in either direction: a
|
|
348
|
-
// reflected axis left undeclared, or a declared axis nothing reflects.
|
|
349
|
-
const target = richTextEditorDoc.theming?.targets.find(
|
|
350
|
-
({className}) => className === 'astryx-rich-text-editor',
|
|
351
|
-
);
|
|
352
|
-
const {container} = render(
|
|
353
|
-
<RichTextEditor label="Notes" size="lg" status={{type: 'error'}} />,
|
|
354
|
-
);
|
|
355
|
-
const wrapper = container.querySelector('.astryx-rich-text-editor');
|
|
356
|
-
expect(wrapper).not.toBeNull();
|
|
357
|
-
|
|
358
|
-
const reflected = [...(wrapper?.attributes ?? [])]
|
|
359
|
-
.filter(({name}) => name.startsWith('data-'))
|
|
360
|
-
.map(({name}) => name.slice('data-'.length))
|
|
361
|
-
.sort();
|
|
362
|
-
expect([...(target?.visualProps ?? [])].sort()).toEqual(reflected);
|
|
363
|
-
// The reflected values are the props, not stale defaults.
|
|
364
|
-
expect(wrapper).toHaveAttribute('data-size', 'lg');
|
|
365
|
-
expect(wrapper).toHaveAttribute('data-status', 'error');
|
|
366
|
-
});
|
|
367
|
-
|
|
368
|
-
it('keeps TextArea-style input visuals alongside consumer props', () => {
|
|
369
|
-
const {container} = render(
|
|
370
|
-
<RichTextEditor
|
|
371
|
-
label="Notes"
|
|
372
|
-
className="custom-editor"
|
|
373
|
-
style={{width: '42rem'}}
|
|
374
|
-
/>,
|
|
375
|
-
);
|
|
376
|
-
|
|
377
|
-
const wrapper = container.querySelector('.astryx-rich-text-editor');
|
|
378
|
-
expect(wrapper).toBeInTheDocument();
|
|
379
|
-
expect(wrapper).toHaveClass('custom-editor');
|
|
380
|
-
expect(wrapper).toHaveAttribute('data-size', 'md');
|
|
381
|
-
expect(wrapper).toHaveStyle({width: '42rem'});
|
|
382
|
-
expect(
|
|
383
|
-
[...(wrapper?.classList ?? [])].some(className =>
|
|
384
|
-
className.startsWith('x'),
|
|
385
|
-
),
|
|
386
|
-
).toBe(true);
|
|
387
|
-
});
|
|
388
|
-
|
|
389
|
-
it('renders a labelled editable textbox', () => {
|
|
390
|
-
render(<RichTextEditor label="Notes" />);
|
|
391
|
-
const textbox = screen.getByRole('textbox', {name: 'Notes'});
|
|
392
|
-
const label = screen.getByText('Notes');
|
|
393
|
-
|
|
394
|
-
expect(textbox).toBeInTheDocument();
|
|
395
|
-
expect(textbox).toHaveAttribute('contenteditable', 'true');
|
|
396
|
-
expect(label).toHaveAttribute(
|
|
397
|
-
'id',
|
|
398
|
-
textbox.getAttribute('aria-labelledby'),
|
|
399
|
-
);
|
|
400
|
-
expect(textbox).toHaveAttribute('id', label.getAttribute('for'));
|
|
401
|
-
});
|
|
402
|
-
|
|
403
|
-
it('applies minHeight only to the editable content surface', () => {
|
|
404
|
-
const {container, rerender} = render(<RichTextEditor label="Notes" />);
|
|
405
|
-
const wrapper = container.querySelector('.astryx-rich-text-editor');
|
|
406
|
-
|
|
407
|
-
expect(
|
|
408
|
-
screen.getByRole('textbox').style.getPropertyValue('--x-minHeight'),
|
|
409
|
-
).toBe('4.5rem');
|
|
410
|
-
expect(wrapper).not.toHaveStyle({'--x-minHeight': '4.5rem'});
|
|
411
|
-
|
|
412
|
-
rerender(<RichTextEditor label="Notes" minHeight={180} />);
|
|
413
|
-
expect(
|
|
414
|
-
screen.getByRole('textbox').style.getPropertyValue('--x-minHeight'),
|
|
415
|
-
).toBe('180px');
|
|
416
|
-
|
|
417
|
-
rerender(<RichTextEditor label="Notes" minHeight="12rem" />);
|
|
418
|
-
expect(
|
|
419
|
-
screen.getByRole('textbox').style.getPropertyValue('--x-minHeight'),
|
|
420
|
-
).toBe('12rem');
|
|
421
|
-
});
|
|
422
|
-
|
|
423
|
-
it('shows the placeholder when empty', () => {
|
|
424
|
-
render(<RichTextEditor label="Notes" placeholder="Write something…" />);
|
|
425
|
-
const textbox = screen.getByRole('textbox');
|
|
426
|
-
const visualPlaceholder = screen.getByText('Write something…');
|
|
427
|
-
|
|
428
|
-
expect(textbox).toHaveAttribute('aria-placeholder', 'Write something…');
|
|
429
|
-
expect(visualPlaceholder).toHaveAttribute('aria-hidden', 'true');
|
|
430
|
-
expect(textbox.parentElement).toContainElement(visualPlaceholder);
|
|
431
|
-
});
|
|
432
|
-
|
|
433
|
-
it('renders the initial value from defaultValue', async () => {
|
|
434
|
-
render(<RichTextEditor label="Notes" defaultValue={HELLO_STATE} />);
|
|
435
|
-
await waitFor(() =>
|
|
436
|
-
expect(screen.getByText('Hello world')).toBeInTheDocument(),
|
|
437
|
-
);
|
|
438
|
-
});
|
|
439
|
-
|
|
440
|
-
it('is not editable when isReadOnly', () => {
|
|
441
|
-
render(<RichTextEditor label="Notes" isReadOnly />);
|
|
442
|
-
expect(screen.getByRole('textbox')).toHaveAttribute(
|
|
443
|
-
'contenteditable',
|
|
444
|
-
'false',
|
|
445
|
-
);
|
|
446
|
-
});
|
|
447
|
-
|
|
448
|
-
it('is not editable when isDisabled', () => {
|
|
449
|
-
render(<RichTextEditor label="Notes" isDisabled />);
|
|
450
|
-
expect(screen.getByRole('textbox')).toHaveAttribute(
|
|
451
|
-
'contenteditable',
|
|
452
|
-
'false',
|
|
453
|
-
);
|
|
454
|
-
});
|
|
455
|
-
|
|
456
|
-
it('keeps a read-only editor reachable and announced as read-only', () => {
|
|
457
|
-
render(<RichTextEditor label="Notes" isReadOnly />);
|
|
458
|
-
const textbox = screen.getByRole('textbox');
|
|
459
|
-
// A read-only textbox stays in the tab order so keyboard and
|
|
460
|
-
// screen-reader users can reach, read, and copy its content.
|
|
461
|
-
expect(textbox).toHaveAttribute('tabindex', '0');
|
|
462
|
-
expect(textbox).toHaveAttribute('aria-readonly', 'true');
|
|
463
|
-
expect(textbox).not.toHaveAttribute('aria-disabled');
|
|
464
|
-
});
|
|
465
|
-
|
|
466
|
-
it('does not dim a read-only editor with the disabled treatment', () => {
|
|
467
|
-
const {container} = render(<RichTextEditor label="Notes" isReadOnly />);
|
|
468
|
-
const wrapper = container.querySelector('.astryx-rich-text-editor');
|
|
469
|
-
expect(wrapper).not.toBeNull();
|
|
470
|
-
expect(getComputedStyle(wrapper as Element).opacity).not.toBe('0.5');
|
|
471
|
-
});
|
|
472
|
-
|
|
473
|
-
it('announces a disabled editor as disabled, not read-only', () => {
|
|
474
|
-
render(<RichTextEditor label="Notes" isDisabled />);
|
|
475
|
-
const textbox = screen.getByRole('textbox');
|
|
476
|
-
expect(textbox).toHaveAttribute('aria-disabled', 'true');
|
|
477
|
-
expect(textbox).not.toHaveAttribute('aria-readonly');
|
|
478
|
-
// Disabled controls leave the tab order.
|
|
479
|
-
expect(textbox).not.toHaveAttribute('tabindex', '0');
|
|
480
|
-
});
|
|
481
|
-
|
|
482
|
-
it('keeps the dimmed treatment on a disabled editor', () => {
|
|
483
|
-
const {container} = render(<RichTextEditor label="Notes" isDisabled />);
|
|
484
|
-
const wrapper = container.querySelector('.astryx-rich-text-editor');
|
|
485
|
-
expect(wrapper).not.toBeNull();
|
|
486
|
-
expect(getComputedStyle(wrapper as Element).opacity).toBe('0.5');
|
|
487
|
-
});
|
|
488
|
-
|
|
489
|
-
it('marks the textbox invalid on error status', () => {
|
|
490
|
-
render(
|
|
491
|
-
<RichTextEditor
|
|
492
|
-
label="Notes"
|
|
493
|
-
status={{type: 'error', message: 'Required'}}
|
|
494
|
-
/>,
|
|
495
|
-
);
|
|
496
|
-
expect(screen.getByRole('textbox')).toHaveAttribute('aria-invalid', 'true');
|
|
497
|
-
expect(screen.getByText('Required')).toBeInTheDocument();
|
|
498
|
-
});
|
|
499
|
-
|
|
500
|
-
it.each(['error', 'warning', 'success'] as const)(
|
|
501
|
-
'renders an in-editor %s icon for the default attached status',
|
|
502
|
-
type => {
|
|
503
|
-
const {container} = render(
|
|
504
|
-
<RichTextEditor
|
|
505
|
-
label="Notes"
|
|
506
|
-
status={{type, message: `${type} message`}}
|
|
507
|
-
/>,
|
|
508
|
-
);
|
|
509
|
-
|
|
510
|
-
const statusIcon = container.querySelector('.astryx-input-status-icon');
|
|
511
|
-
expect(statusIcon).toBeInTheDocument();
|
|
512
|
-
expect(statusIcon).toHaveAttribute('data-status', type);
|
|
513
|
-
expect(statusIcon).toHaveAttribute('data-size', 'md');
|
|
514
|
-
expect(container.querySelector('.astryx-field-status')).toHaveAttribute(
|
|
515
|
-
'data-variant',
|
|
516
|
-
'attached',
|
|
517
|
-
);
|
|
518
|
-
},
|
|
519
|
-
);
|
|
520
|
-
|
|
521
|
-
it('uses the detached message icon and suppresses the in-editor icon', () => {
|
|
522
|
-
const {container} = render(
|
|
523
|
-
<RichTextEditor
|
|
524
|
-
label="Notes"
|
|
525
|
-
status={{type: 'warning', message: 'Review this value'}}
|
|
526
|
-
statusVariant="detached"
|
|
527
|
-
/>,
|
|
528
|
-
);
|
|
529
|
-
|
|
530
|
-
expect(
|
|
531
|
-
container.querySelector('.astryx-input-status-icon'),
|
|
532
|
-
).not.toBeInTheDocument();
|
|
533
|
-
expect(container.querySelector('.astryx-field-status')).toHaveAttribute(
|
|
534
|
-
'data-variant',
|
|
535
|
-
'detached',
|
|
536
|
-
);
|
|
537
|
-
expect(
|
|
538
|
-
container.querySelector('.astryx-field-status-icon'),
|
|
539
|
-
).toBeInTheDocument();
|
|
540
|
-
});
|
|
541
|
-
|
|
542
|
-
it('surfaces tooltip status through a focusable in-editor icon', () => {
|
|
543
|
-
const {container} = render(
|
|
544
|
-
<RichTextEditor
|
|
545
|
-
label="Notes"
|
|
546
|
-
status={{type: 'error', message: 'Required'}}
|
|
547
|
-
statusVariant="tooltip"
|
|
548
|
-
/>,
|
|
549
|
-
);
|
|
550
|
-
|
|
551
|
-
expect(
|
|
552
|
-
container.querySelector('.astryx-field-status'),
|
|
553
|
-
).not.toBeInTheDocument();
|
|
554
|
-
const statusButton = screen.getByRole('button', {
|
|
555
|
-
name: /error details/i,
|
|
556
|
-
});
|
|
557
|
-
const tooltip = screen.getByRole('tooltip', h);
|
|
558
|
-
const textbox = screen.getByRole('textbox');
|
|
559
|
-
expect(statusButton).toHaveAttribute('type', 'button');
|
|
560
|
-
expect(statusButton.getAttribute('aria-describedby')).toContain(tooltip.id);
|
|
561
|
-
expect(textbox.getAttribute('aria-describedby')).toContain(tooltip.id);
|
|
562
|
-
expect(tooltip).toHaveTextContent('Required');
|
|
563
|
-
});
|
|
564
|
-
|
|
565
|
-
it('sets aria-required when required', () => {
|
|
566
|
-
render(<RichTextEditor label="Notes" isRequired />);
|
|
567
|
-
expect(screen.getByRole('textbox')).toHaveAttribute(
|
|
568
|
-
'aria-required',
|
|
569
|
-
'true',
|
|
570
|
-
);
|
|
571
|
-
});
|
|
572
|
-
|
|
573
|
-
it('fires onChange when content changes', async () => {
|
|
574
|
-
// jsdom does not implement contenteditable editing, so we grab the editor
|
|
575
|
-
// instance via a small capture plugin and drive a real Lexical update,
|
|
576
|
-
// then assert onChange fires.
|
|
577
|
-
let editorRef: LexicalEditor | undefined;
|
|
578
|
-
const onChange = vi.fn();
|
|
579
|
-
render(
|
|
580
|
-
<RichTextEditor
|
|
581
|
-
label="Notes"
|
|
582
|
-
onChange={onChange}
|
|
583
|
-
plugins={<CaptureEditor onReady={e => (editorRef = e)} />}
|
|
584
|
-
/>,
|
|
585
|
-
);
|
|
586
|
-
await waitFor(() => expect(editorRef).toBeDefined());
|
|
587
|
-
onChange.mockClear();
|
|
588
|
-
editorRef!.update(() => {
|
|
589
|
-
const root = $getRoot();
|
|
590
|
-
root.clear();
|
|
591
|
-
const paragraph = $createParagraphNode();
|
|
592
|
-
paragraph.append($createTextNode('hello'));
|
|
593
|
-
root.append(paragraph);
|
|
594
|
-
});
|
|
595
|
-
await waitFor(() => expect(onChange).toHaveBeenCalled());
|
|
596
|
-
});
|
|
597
|
-
|
|
598
|
-
it('hides the visible label but keeps it accessible when isLabelHidden', () => {
|
|
599
|
-
render(<RichTextEditor label="Secret notes" isLabelHidden />);
|
|
600
|
-
expect(
|
|
601
|
-
screen.getByRole('textbox', {name: 'Secret notes'}),
|
|
602
|
-
).toBeInTheDocument();
|
|
603
|
-
});
|
|
604
|
-
|
|
605
|
-
it('renders custom plugins passed via the plugins prop', () => {
|
|
606
|
-
render(
|
|
607
|
-
<RichTextEditor
|
|
608
|
-
label="Notes"
|
|
609
|
-
plugins={<div data-testid="custom-plugin" />}
|
|
610
|
-
/>,
|
|
611
|
-
);
|
|
612
|
-
expect(screen.getByTestId('custom-plugin')).toBeInTheDocument();
|
|
613
|
-
});
|
|
614
|
-
|
|
615
|
-
it('accepts a custom transformers array without throwing', () => {
|
|
616
|
-
// Empty transformer set is a valid custom configuration (disables all
|
|
617
|
-
// markdown shortcuts while keeping the plugin mounted).
|
|
618
|
-
render(<RichTextEditor label="Notes" transformers={[]} />);
|
|
619
|
-
expect(screen.getByRole('textbox')).toBeInTheDocument();
|
|
620
|
-
});
|
|
621
|
-
|
|
622
|
-
it('renders when markdown shortcuts are disabled', () => {
|
|
623
|
-
render(<RichTextEditor label="Notes" hasMarkdownShortcuts={false} />);
|
|
624
|
-
expect(screen.getByRole('textbox')).toBeInTheDocument();
|
|
625
|
-
});
|
|
626
|
-
|
|
627
|
-
it('applies the default transformers to convert markdown to a heading', async () => {
|
|
628
|
-
// jsdom can't dispatch the keystrokes that trigger registerMarkdownShortcuts
|
|
629
|
-
// live, so we drive $convertFromMarkdownString with the same TRANSFORMERS
|
|
630
|
-
// the component registers by default. This proves the default transformer
|
|
631
|
-
// set actually produces the expected node structure (a heading), rather
|
|
632
|
-
// than only asserting the editor mounts.
|
|
633
|
-
let editorRef: LexicalEditor | undefined;
|
|
634
|
-
render(
|
|
635
|
-
<RichTextEditor
|
|
636
|
-
label="Notes"
|
|
637
|
-
plugins={<CaptureEditor onReady={e => (editorRef = e)} />}
|
|
638
|
-
/>,
|
|
639
|
-
);
|
|
640
|
-
await waitFor(() => expect(editorRef).toBeDefined());
|
|
641
|
-
editorRef!.update(() => {
|
|
642
|
-
$convertFromMarkdownString('# Title', TRANSFORMERS);
|
|
643
|
-
});
|
|
644
|
-
await waitFor(() => {
|
|
645
|
-
editorRef!.getEditorState().read(() => {
|
|
646
|
-
const first = $getRoot().getFirstChild();
|
|
647
|
-
expect(first?.getType()).toBe('heading');
|
|
648
|
-
expect(first?.getTextContent()).toBe('Title');
|
|
649
|
-
});
|
|
650
|
-
});
|
|
651
|
-
});
|
|
652
|
-
|
|
653
|
-
it('leaves markdown untransformed when given an empty transformers array', async () => {
|
|
654
|
-
// With no transformers, the same markdown text stays a plain paragraph —
|
|
655
|
-
// demonstrating the transformers prop is the effective source of truth for
|
|
656
|
-
// markdown behaviour, not a fixed internal default.
|
|
657
|
-
let editorRef: LexicalEditor | undefined;
|
|
658
|
-
render(
|
|
659
|
-
<RichTextEditor
|
|
660
|
-
label="Notes"
|
|
661
|
-
transformers={[]}
|
|
662
|
-
plugins={<CaptureEditor onReady={e => (editorRef = e)} />}
|
|
663
|
-
/>,
|
|
664
|
-
);
|
|
665
|
-
await waitFor(() => expect(editorRef).toBeDefined());
|
|
666
|
-
editorRef!.update(() => {
|
|
667
|
-
// Empty transformer set: markdown syntax is preserved verbatim.
|
|
668
|
-
$convertFromMarkdownString('# Title', []);
|
|
669
|
-
});
|
|
670
|
-
await waitFor(() => {
|
|
671
|
-
editorRef!.getEditorState().read(() => {
|
|
672
|
-
const first = $getRoot().getFirstChild();
|
|
673
|
-
expect(first?.getType()).toBe('paragraph');
|
|
674
|
-
expect(first?.getTextContent()).toBe('# Title');
|
|
675
|
-
});
|
|
676
|
-
});
|
|
677
|
-
});
|
|
678
|
-
|
|
679
|
-
it('exposes an imperative ref handle after mount', () => {
|
|
680
|
-
const ref = createRef<RichTextEditorRef>();
|
|
681
|
-
render(<RichTextEditor ref={ref} label="Notes" />);
|
|
682
|
-
expect(ref.current).not.toBeNull();
|
|
683
|
-
expect(typeof ref.current?.focus).toBe('function');
|
|
684
|
-
expect(typeof ref.current?.clear).toBe('function');
|
|
685
|
-
expect(typeof ref.current?.getEditorState).toBe('function');
|
|
686
|
-
expect(typeof ref.current?.getMarkdown).toBe('function');
|
|
687
|
-
expect(typeof ref.current?.getHTML).toBe('function');
|
|
688
|
-
expect(typeof ref.current?.getEditor).toBe('function');
|
|
689
|
-
});
|
|
690
|
-
|
|
691
|
-
it('ref.focus() runs without throwing and targets the editable surface', () => {
|
|
692
|
-
const ref = createRef<RichTextEditorRef>();
|
|
693
|
-
render(<RichTextEditor ref={ref} label="Notes" />);
|
|
694
|
-
// Lexical dispatches focus via its editor command; jsdom does not always
|
|
695
|
-
// reflect programmatic contenteditable focus onto document.activeElement,
|
|
696
|
-
// so assert the call is wired and the root element is reachable rather
|
|
697
|
-
// than asserting jsdom focus state.
|
|
698
|
-
expect(() => ref.current?.focus()).not.toThrow();
|
|
699
|
-
const root = ref.current?.getEditor().getRootElement();
|
|
700
|
-
expect(root).toBe(screen.getByRole('textbox'));
|
|
701
|
-
});
|
|
702
|
-
|
|
703
|
-
it('ref.getEditorState() returns the current EditorState', () => {
|
|
704
|
-
const ref = createRef<RichTextEditorRef>();
|
|
705
|
-
render(
|
|
706
|
-
<RichTextEditor ref={ref} label="Notes" defaultValue={HELLO_STATE} />,
|
|
707
|
-
);
|
|
708
|
-
const state = ref.current?.getEditorState();
|
|
709
|
-
expect(state).toBeDefined();
|
|
710
|
-
const text = state?.read(() => $getRoot().getTextContent());
|
|
711
|
-
expect(text).toBe('Hello world');
|
|
712
|
-
});
|
|
713
|
-
|
|
714
|
-
it('ref.getEditor() returns the underlying LexicalEditor', () => {
|
|
715
|
-
const ref = createRef<RichTextEditorRef>();
|
|
716
|
-
render(<RichTextEditor ref={ref} label="Notes" />);
|
|
717
|
-
const editor = ref.current?.getEditor();
|
|
718
|
-
expect(editor).toBeDefined();
|
|
719
|
-
expect(typeof editor?.update).toBe('function');
|
|
720
|
-
});
|
|
721
|
-
|
|
722
|
-
it('ref.getMarkdown() serializes plain text content', () => {
|
|
723
|
-
const ref = createRef<RichTextEditorRef>();
|
|
724
|
-
render(
|
|
725
|
-
<RichTextEditor ref={ref} label="Notes" defaultValue={HELLO_STATE} />,
|
|
726
|
-
);
|
|
727
|
-
expect(ref.current?.getMarkdown()).toBe('Hello world');
|
|
728
|
-
});
|
|
729
|
-
|
|
730
|
-
it('ref.getMarkdown() serializes a heading with the default transformers', async () => {
|
|
731
|
-
const ref = createRef<RichTextEditorRef>();
|
|
732
|
-
let editorRef: LexicalEditor | undefined;
|
|
733
|
-
render(
|
|
734
|
-
<RichTextEditor
|
|
735
|
-
ref={ref}
|
|
736
|
-
label="Notes"
|
|
737
|
-
plugins={<CaptureEditor onReady={e => (editorRef = e)} />}
|
|
738
|
-
/>,
|
|
739
|
-
);
|
|
740
|
-
await waitFor(() => expect(editorRef).toBeDefined());
|
|
741
|
-
editorRef!.update(() => {
|
|
742
|
-
$convertFromMarkdownString('# Title', TRANSFORMERS);
|
|
743
|
-
});
|
|
744
|
-
await waitFor(() => expect(ref.current?.getMarkdown()).toBe('# Title'));
|
|
745
|
-
});
|
|
746
|
-
|
|
747
|
-
it('ref.getMarkdown() honors a custom transformers prop', async () => {
|
|
748
|
-
// With an empty transformers set, a heading node cannot be represented in
|
|
749
|
-
// markdown, so its text is emitted as a plain paragraph (no "# "). This
|
|
750
|
-
// proves getMarkdown() uses the same transformers the editor is
|
|
751
|
-
// configured with, not a hardcoded default.
|
|
752
|
-
const ref = createRef<RichTextEditorRef>();
|
|
753
|
-
let editorRef: LexicalEditor | undefined;
|
|
754
|
-
render(
|
|
755
|
-
<RichTextEditor
|
|
756
|
-
ref={ref}
|
|
757
|
-
label="Notes"
|
|
758
|
-
transformers={[]}
|
|
759
|
-
plugins={<CaptureEditor onReady={e => (editorRef = e)} />}
|
|
760
|
-
/>,
|
|
761
|
-
);
|
|
762
|
-
await waitFor(() => expect(editorRef).toBeDefined());
|
|
763
|
-
// Seed a heading node directly (bypassing shortcuts) using the full set.
|
|
764
|
-
editorRef!.update(() => {
|
|
765
|
-
$convertFromMarkdownString('# Title', TRANSFORMERS);
|
|
766
|
-
});
|
|
767
|
-
await waitFor(() => expect(ref.current?.getMarkdown()).toBe('Title'));
|
|
768
|
-
});
|
|
769
|
-
|
|
770
|
-
it('ref.getHTML() serializes content to an HTML string', () => {
|
|
771
|
-
const ref = createRef<RichTextEditorRef>();
|
|
772
|
-
render(
|
|
773
|
-
<RichTextEditor ref={ref} label="Notes" defaultValue={HELLO_STATE} />,
|
|
774
|
-
);
|
|
775
|
-
const html = ref.current?.getHTML();
|
|
776
|
-
expect(typeof html).toBe('string');
|
|
777
|
-
// A paragraph with the seeded text renders as a <p> containing "Hello world".
|
|
778
|
-
expect(html).toContain('<p');
|
|
779
|
-
expect(html).toContain('Hello world');
|
|
780
|
-
});
|
|
781
|
-
|
|
782
|
-
it('ref.clear() resets the editor to a single empty paragraph', async () => {
|
|
783
|
-
const ref = createRef<RichTextEditorRef>();
|
|
784
|
-
render(
|
|
785
|
-
<RichTextEditor ref={ref} label="Notes" defaultValue={HELLO_STATE} />,
|
|
786
|
-
);
|
|
787
|
-
ref.current?.clear();
|
|
788
|
-
await waitFor(() => {
|
|
789
|
-
const state: EditorState | undefined = ref.current?.getEditorState();
|
|
790
|
-
const text = state?.read(() => $getRoot().getTextContent());
|
|
791
|
-
expect(text).toBe('');
|
|
792
|
-
});
|
|
793
|
-
// The root should hold exactly one (empty) paragraph, not zero children —
|
|
794
|
-
// a bare root breaks selection/typing.
|
|
795
|
-
const state = ref.current?.getEditorState();
|
|
796
|
-
const childCount = state?.read(() => $getRoot().getChildrenSize());
|
|
797
|
-
expect(childCount).toBe(1);
|
|
798
|
-
});
|
|
799
|
-
|
|
800
|
-
it('ref.clear() fires onChange', async () => {
|
|
801
|
-
const ref = createRef<RichTextEditorRef>();
|
|
802
|
-
const onChange = vi.fn();
|
|
803
|
-
render(
|
|
804
|
-
<RichTextEditor
|
|
805
|
-
ref={ref}
|
|
806
|
-
label="Notes"
|
|
807
|
-
defaultValue={HELLO_STATE}
|
|
808
|
-
onChange={onChange}
|
|
809
|
-
/>,
|
|
810
|
-
);
|
|
811
|
-
// Wait for the editor to mount and the initial state to settle so any
|
|
812
|
-
// seed-time onChange has already fired before we assert on clear().
|
|
813
|
-
await waitFor(() => expect(ref.current).not.toBeNull());
|
|
814
|
-
onChange.mockClear();
|
|
815
|
-
ref.current?.clear();
|
|
816
|
-
await waitFor(() => expect(onChange).toHaveBeenCalled());
|
|
817
|
-
});
|
|
818
|
-
|
|
819
|
-
it('ref.clear() and ref.focus() are no-ops when isReadOnly', async () => {
|
|
820
|
-
const ref = createRef<RichTextEditorRef>();
|
|
821
|
-
const onChange = vi.fn();
|
|
822
|
-
render(
|
|
823
|
-
<RichTextEditor
|
|
824
|
-
ref={ref}
|
|
825
|
-
label="Notes"
|
|
826
|
-
defaultValue={HELLO_STATE}
|
|
827
|
-
onChange={onChange}
|
|
828
|
-
isReadOnly
|
|
829
|
-
/>,
|
|
830
|
-
);
|
|
831
|
-
onChange.mockClear();
|
|
832
|
-
ref.current?.focus();
|
|
833
|
-
ref.current?.clear();
|
|
834
|
-
// Give any (unexpected) async update a chance to flush before asserting.
|
|
835
|
-
await new Promise(resolve => setTimeout(resolve, 0));
|
|
836
|
-
expect(onChange).not.toHaveBeenCalled();
|
|
837
|
-
const state = ref.current?.getEditorState();
|
|
838
|
-
const text = state?.read(() => $getRoot().getTextContent());
|
|
839
|
-
expect(text).toBe('Hello world');
|
|
840
|
-
});
|
|
841
|
-
|
|
842
|
-
it('ref.clear() is a no-op when isDisabled', async () => {
|
|
843
|
-
const ref = createRef<RichTextEditorRef>();
|
|
844
|
-
const onChange = vi.fn();
|
|
845
|
-
render(
|
|
846
|
-
<RichTextEditor
|
|
847
|
-
ref={ref}
|
|
848
|
-
label="Notes"
|
|
849
|
-
defaultValue={HELLO_STATE}
|
|
850
|
-
onChange={onChange}
|
|
851
|
-
isDisabled
|
|
852
|
-
/>,
|
|
853
|
-
);
|
|
854
|
-
onChange.mockClear();
|
|
855
|
-
ref.current?.clear();
|
|
856
|
-
await new Promise(resolve => setTimeout(resolve, 0));
|
|
857
|
-
expect(onChange).not.toHaveBeenCalled();
|
|
858
|
-
const state = ref.current?.getEditorState();
|
|
859
|
-
const text = state?.read(() => $getRoot().getTextContent());
|
|
860
|
-
expect(text).toBe('Hello world');
|
|
861
|
-
});
|
|
862
|
-
|
|
863
|
-
it('does not render a character counter when maxLength is not set', () => {
|
|
864
|
-
render(<RichTextEditor label="Notes" defaultValue={HELLO_STATE} />);
|
|
865
|
-
expect(screen.queryByText(/\d+\/\d+/)).not.toBeInTheDocument();
|
|
866
|
-
});
|
|
867
|
-
|
|
868
|
-
it('renders a character counter reflecting the seeded content length', async () => {
|
|
869
|
-
// HELLO_STATE is "Hello world" (11 chars).
|
|
870
|
-
render(
|
|
871
|
-
<RichTextEditor
|
|
872
|
-
label="Notes"
|
|
873
|
-
defaultValue={HELLO_STATE}
|
|
874
|
-
maxLength={100}
|
|
875
|
-
/>,
|
|
876
|
-
);
|
|
877
|
-
await waitFor(() => expect(screen.getByText('11/100')).toBeInTheDocument());
|
|
878
|
-
});
|
|
879
|
-
|
|
880
|
-
it('shows an over-limit counter when content exceeds maxLength', async () => {
|
|
881
|
-
// 11 chars with a limit of 5 -> over limit.
|
|
882
|
-
render(
|
|
883
|
-
<RichTextEditor label="Notes" defaultValue={HELLO_STATE} maxLength={5} />,
|
|
884
|
-
);
|
|
885
|
-
await waitFor(() => expect(screen.getByText('11/5')).toBeInTheDocument());
|
|
886
|
-
// aria-live region announces the overflow for screen readers.
|
|
887
|
-
expect(screen.getByText('6 characters over the limit')).toBeInTheDocument();
|
|
888
|
-
});
|
|
889
|
-
|
|
890
|
-
it('associates the counter with the editor via aria-describedby', async () => {
|
|
891
|
-
render(
|
|
892
|
-
<RichTextEditor
|
|
893
|
-
label="Notes"
|
|
894
|
-
defaultValue={HELLO_STATE}
|
|
895
|
-
maxLength={100}
|
|
896
|
-
/>,
|
|
897
|
-
);
|
|
898
|
-
const counter = await screen.findByText('11/100');
|
|
899
|
-
const describedBy = screen
|
|
900
|
-
.getByRole('textbox')
|
|
901
|
-
.getAttribute('aria-describedby');
|
|
902
|
-
expect(describedBy).toBeTruthy();
|
|
903
|
-
expect(describedBy!.split(' ')).toContain(counter.id);
|
|
904
|
-
});
|
|
905
|
-
});
|
|
906
|
-
|
|
907
|
-
describe('RichTextEditor Tab keyboard trap escape (WCAG 2.1.2)', () => {
|
|
908
|
-
const DEFAULT_HINT = 'Press Escape then Tab to move focus out of the editor.';
|
|
909
|
-
|
|
910
|
-
stubRangeRects();
|
|
911
|
-
|
|
912
|
-
/**
|
|
913
|
-
* Renders the editor followed by a button, focuses the contenteditable and
|
|
914
|
-
* seeds a single-item bullet list with the caret at the start of the item —
|
|
915
|
-
* the position where TabIndentationPlugin turns Tab into an indent (and
|
|
916
|
-
* calls preventDefault, which is the keyboard trap under test).
|
|
917
|
-
*/
|
|
918
|
-
async function setUpListEditor() {
|
|
919
|
-
let editorRef: LexicalEditor | undefined;
|
|
920
|
-
function CaptureEditor() {
|
|
921
|
-
const [editor] = useLexicalComposerContext();
|
|
922
|
-
useEffect(() => {
|
|
923
|
-
editorRef = editor;
|
|
924
|
-
}, [editor]);
|
|
925
|
-
return null;
|
|
926
|
-
}
|
|
927
|
-
render(
|
|
928
|
-
<>
|
|
929
|
-
<RichTextEditor label="Notes" plugins={<CaptureEditor />} />
|
|
930
|
-
<button type="button">after</button>
|
|
931
|
-
</>,
|
|
932
|
-
);
|
|
933
|
-
await waitFor(() => expect(editorRef).toBeDefined());
|
|
934
|
-
const textbox = screen.getByRole('textbox');
|
|
935
|
-
textbox.focus();
|
|
936
|
-
editorRef!.update(() => {
|
|
937
|
-
const root = $getRoot();
|
|
938
|
-
root.clear();
|
|
939
|
-
const list = $createListNode('bullet');
|
|
940
|
-
const item = $createListItemNode();
|
|
941
|
-
const text = $createTextNode('Item one');
|
|
942
|
-
item.append(text);
|
|
943
|
-
list.append(item);
|
|
944
|
-
root.append(list);
|
|
945
|
-
text.select(0, 0);
|
|
946
|
-
});
|
|
947
|
-
return {editor: editorRef!, textbox};
|
|
948
|
-
}
|
|
949
|
-
|
|
950
|
-
/** Indent of the list item containing the seeded text node. */
|
|
951
|
-
function getItemIndent(editor: LexicalEditor): number {
|
|
952
|
-
return editor.getEditorState().read(() => {
|
|
953
|
-
const text = $getRoot().getAllTextNodes()[0];
|
|
954
|
-
const item = text.getParent();
|
|
955
|
-
return $isListItemNode(item) ? item.getIndent() : -1;
|
|
956
|
-
});
|
|
957
|
-
}
|
|
958
|
-
|
|
959
|
-
it('indents the list item on Tab and keeps focus in the editor', async () => {
|
|
960
|
-
const user = userEvent.setup();
|
|
961
|
-
const {editor, textbox} = await setUpListEditor();
|
|
962
|
-
expect(getItemIndent(editor)).toBe(0);
|
|
963
|
-
await user.tab();
|
|
964
|
-
expect(getItemIndent(editor)).toBe(1);
|
|
965
|
-
// The keydown was consumed by indentation, so focus did not move.
|
|
966
|
-
expect(document.activeElement).toBe(textbox);
|
|
967
|
-
});
|
|
968
|
-
|
|
969
|
-
it('moves focus out (without indenting) on Tab after Escape', async () => {
|
|
970
|
-
const user = userEvent.setup();
|
|
971
|
-
const {editor, textbox} = await setUpListEditor();
|
|
972
|
-
await user.keyboard('{Escape}');
|
|
973
|
-
await user.tab();
|
|
974
|
-
expect(document.activeElement).not.toBe(textbox);
|
|
975
|
-
expect(document.activeElement).toBe(
|
|
976
|
-
screen.getByRole('button', {name: 'after'}),
|
|
977
|
-
);
|
|
978
|
-
expect(getItemIndent(editor)).toBe(0);
|
|
979
|
-
});
|
|
980
|
-
|
|
981
|
-
it('re-arms indentation when another key is pressed after Escape', async () => {
|
|
982
|
-
const user = userEvent.setup();
|
|
983
|
-
const {editor, textbox} = await setUpListEditor();
|
|
984
|
-
await user.keyboard('{Escape}');
|
|
985
|
-
// Any non-modifier key press (typing, arrows, …) cancels the escape.
|
|
986
|
-
fireEvent.keyDown(textbox, {key: 'a'});
|
|
987
|
-
await user.tab();
|
|
988
|
-
expect(document.activeElement).toBe(textbox);
|
|
989
|
-
expect(getItemIndent(editor)).toBe(1);
|
|
990
|
-
});
|
|
991
|
-
|
|
992
|
-
it('does not re-arm on a bare modifier, so Escape then Shift+Tab escapes', async () => {
|
|
993
|
-
const user = userEvent.setup();
|
|
994
|
-
const {textbox} = await setUpListEditor();
|
|
995
|
-
await user.keyboard('{Escape}');
|
|
996
|
-
// Pressing Shift on its own (the first half of Shift+Tab) must not
|
|
997
|
-
// cancel the escape. fireEvent returns false when preventDefault was
|
|
998
|
-
// called — an unprevented Tab keydown means native focus movement.
|
|
999
|
-
fireEvent.keyDown(textbox, {key: 'Shift'});
|
|
1000
|
-
expect(fireEvent.keyDown(textbox, {key: 'Tab', shiftKey: true})).toBe(true);
|
|
1001
|
-
});
|
|
1002
|
-
|
|
1003
|
-
it('advertises the escape via a visually hidden aria-describedby hint', () => {
|
|
1004
|
-
render(<RichTextEditor label="Notes" />);
|
|
1005
|
-
const textbox = screen.getByRole('textbox');
|
|
1006
|
-
const hint = screen.getByText(DEFAULT_HINT);
|
|
1007
|
-
expect(hint.id).not.toBe('');
|
|
1008
|
-
expect(
|
|
1009
|
-
(textbox.getAttribute('aria-describedby') ?? '').split(/\s+/),
|
|
1010
|
-
).toContain(hint.id);
|
|
1011
|
-
});
|
|
1012
|
-
|
|
1013
|
-
it('supports overriding and suppressing the hint text', () => {
|
|
1014
|
-
const {unmount} = render(
|
|
1015
|
-
<RichTextEditor label="Notes" tabEscapeHint="Custom escape hint" />,
|
|
1016
|
-
);
|
|
1017
|
-
expect(screen.getByText('Custom escape hint')).toBeInTheDocument();
|
|
1018
|
-
expect(screen.queryByText(DEFAULT_HINT)).not.toBeInTheDocument();
|
|
1019
|
-
unmount();
|
|
1020
|
-
render(<RichTextEditor label="Notes" tabEscapeHint="" />);
|
|
1021
|
-
expect(screen.queryByText(DEFAULT_HINT)).not.toBeInTheDocument();
|
|
1022
|
-
expect(screen.getByRole('textbox')).not.toHaveAttribute('aria-describedby');
|
|
1023
|
-
});
|
|
1024
|
-
|
|
1025
|
-
it('omits the hint when the editor is not editable', () => {
|
|
1026
|
-
render(<RichTextEditor label="Notes" isReadOnly />);
|
|
1027
|
-
expect(screen.queryByText(DEFAULT_HINT)).not.toBeInTheDocument();
|
|
1028
|
-
});
|
|
1029
|
-
});
|
|
1030
|
-
|
|
1031
|
-
describe('RichTextView', () => {
|
|
1032
|
-
it('renders serialized content read-only', async () => {
|
|
1033
|
-
const {container} = render(<RichTextView value={HELLO_STATE} />);
|
|
1034
|
-
await waitFor(() =>
|
|
1035
|
-
expect(screen.getByText('Hello world')).toBeInTheDocument(),
|
|
1036
|
-
);
|
|
1037
|
-
expect(container.querySelector('[data-lexical-editor]')).toHaveAttribute(
|
|
1038
|
-
'contenteditable',
|
|
1039
|
-
'false',
|
|
1040
|
-
);
|
|
1041
|
-
});
|
|
1042
|
-
|
|
1043
|
-
it('renders document content rather than a form field', async () => {
|
|
1044
|
-
const {container} = render(<RichTextView value={HELLO_STATE} />);
|
|
1045
|
-
await waitFor(() =>
|
|
1046
|
-
expect(screen.getByText('Hello world')).toBeInTheDocument(),
|
|
1047
|
-
);
|
|
1048
|
-
// Lexical's ContentEditable applies role="textbox" and widget-only ARIA
|
|
1049
|
-
// even when it is not editable. A view is content: an unnamed widget
|
|
1050
|
-
// fails axe aria-input-field-name, and the widget role exposes the
|
|
1051
|
-
// document as a form field rather than as content.
|
|
1052
|
-
expect(screen.queryByRole('textbox')).not.toBeInTheDocument();
|
|
1053
|
-
const content = container.querySelector('[data-lexical-editor]');
|
|
1054
|
-
expect(content).not.toBeNull();
|
|
1055
|
-
expect(content).not.toHaveAttribute('role');
|
|
1056
|
-
expect(content).not.toHaveAttribute('aria-readonly');
|
|
1057
|
-
expect(content).not.toHaveAttribute('aria-autocomplete');
|
|
1058
|
-
});
|
|
1059
|
-
|
|
1060
|
-
it('keeps its own StyleX class alongside a consumer className', () => {
|
|
1061
|
-
// A JSX attribute after an object spread wins even when it is undefined,
|
|
1062
|
-
// so `className={className}` after `{...stylex.props(...)}` dropped every
|
|
1063
|
-
// generated class, including styles.root's `width: 100%`.
|
|
1064
|
-
const {container} = render(
|
|
1065
|
-
<RichTextView value={HELLO_STATE} className="consumer-class" />,
|
|
1066
|
-
);
|
|
1067
|
-
const root = container.firstElementChild;
|
|
1068
|
-
expect(root).toHaveClass('consumer-class');
|
|
1069
|
-
expect(root?.classList.length).toBeGreaterThan(1);
|
|
1070
|
-
});
|
|
1071
|
-
|
|
1072
|
-
it('applies a consumer xstyle', () => {
|
|
1073
|
-
const overrides = stylex.create({custom: {opacity: 0.5}});
|
|
1074
|
-
const {container: withXstyle} = render(
|
|
1075
|
-
<RichTextView value={HELLO_STATE} xstyle={overrides.custom} />,
|
|
1076
|
-
);
|
|
1077
|
-
const {container: plain} = render(<RichTextView value={HELLO_STATE} />);
|
|
1078
|
-
const withClasses = withXstyle.firstElementChild?.className ?? '';
|
|
1079
|
-
expect(withClasses).not.toBe('');
|
|
1080
|
-
expect(withClasses).not.toBe(plain.firstElementChild?.className ?? '');
|
|
1081
|
-
});
|
|
1082
|
-
|
|
1083
|
-
it('merges className and its own styles on the error fallback too', () => {
|
|
1084
|
-
const {container} = render(
|
|
1085
|
-
<RichTextView
|
|
1086
|
-
value={'{ not valid json'}
|
|
1087
|
-
className="consumer-class"
|
|
1088
|
-
errorFallback={<div>Unavailable</div>}
|
|
1089
|
-
/>,
|
|
1090
|
-
);
|
|
1091
|
-
const root = container.firstElementChild;
|
|
1092
|
-
expect(root).toHaveClass('consumer-class');
|
|
1093
|
-
expect(root?.classList.length).toBeGreaterThan(1);
|
|
1094
|
-
});
|
|
1095
|
-
|
|
1096
|
-
it('reports a parse error after render, so the callback can update state', () => {
|
|
1097
|
-
// A consumer commonly surfaces the failure from its own state. Called
|
|
1098
|
-
// during RichTextView's render, that update would land while React is
|
|
1099
|
-
// rendering a different component, which React reports as an error.
|
|
1100
|
-
const consoleError = vi
|
|
1101
|
-
.spyOn(console, 'error')
|
|
1102
|
-
.mockImplementation(() => {});
|
|
1103
|
-
function Host() {
|
|
1104
|
-
const [failure, setFailure] = useState('');
|
|
1105
|
-
return (
|
|
1106
|
-
<>
|
|
1107
|
-
<RichTextView
|
|
1108
|
-
value={'{ not valid json'}
|
|
1109
|
-
onParseError={error => setFailure(error.name)}
|
|
1110
|
-
/>
|
|
1111
|
-
<output>{failure}</output>
|
|
1112
|
-
</>
|
|
1113
|
-
);
|
|
1114
|
-
}
|
|
1115
|
-
try {
|
|
1116
|
-
const {container} = render(<Host />);
|
|
1117
|
-
expect(container.querySelector('output')).toHaveTextContent(
|
|
1118
|
-
'SyntaxError',
|
|
1119
|
-
);
|
|
1120
|
-
expect(consoleError).not.toHaveBeenCalled();
|
|
1121
|
-
} finally {
|
|
1122
|
-
consoleError.mockRestore();
|
|
1123
|
-
}
|
|
1124
|
-
});
|
|
1125
|
-
|
|
1126
|
-
it('reports a parse error once under StrictMode', () => {
|
|
1127
|
-
const onParseError = vi.fn();
|
|
1128
|
-
render(
|
|
1129
|
-
<StrictMode>
|
|
1130
|
-
<RichTextView value={'{ not valid json'} onParseError={onParseError} />
|
|
1131
|
-
</StrictMode>,
|
|
1132
|
-
);
|
|
1133
|
-
expect(onParseError).toHaveBeenCalledTimes(1);
|
|
1134
|
-
expect(onParseError.mock.calls[0][0]).toBeInstanceOf(Error);
|
|
1135
|
-
});
|
|
1136
|
-
|
|
1137
|
-
it('reports each distinct parse failure once across re-renders', () => {
|
|
1138
|
-
const onParseError = vi.fn();
|
|
1139
|
-
const {rerender} = render(
|
|
1140
|
-
<RichTextView value={'{ bad'} onParseError={onParseError} />,
|
|
1141
|
-
);
|
|
1142
|
-
expect(onParseError).toHaveBeenCalledTimes(1);
|
|
1143
|
-
|
|
1144
|
-
// Re-rendering with the same bad value does not report it again…
|
|
1145
|
-
rerender(<RichTextView value={'{ bad'} onParseError={onParseError} />);
|
|
1146
|
-
expect(onParseError).toHaveBeenCalledTimes(1);
|
|
1147
|
-
|
|
1148
|
-
// …but a different bad value is a new failure.
|
|
1149
|
-
rerender(<RichTextView value={'{ worse'} onParseError={onParseError} />);
|
|
1150
|
-
expect(onParseError).toHaveBeenCalledTimes(2);
|
|
1151
|
-
});
|
|
1152
|
-
|
|
1153
|
-
it('renders custom read-only plugins passed via the plugins prop', () => {
|
|
1154
|
-
render(
|
|
1155
|
-
<RichTextView
|
|
1156
|
-
value={HELLO_STATE}
|
|
1157
|
-
plugins={<div data-testid="view-plugin" />}
|
|
1158
|
-
/>,
|
|
1159
|
-
);
|
|
1160
|
-
expect(screen.getByTestId('view-plugin')).toBeInTheDocument();
|
|
1161
|
-
});
|
|
1162
|
-
|
|
1163
|
-
it('accepts a custom namespace without throwing', async () => {
|
|
1164
|
-
render(<RichTextView value={HELLO_STATE} namespace="custom-view-ns" />);
|
|
1165
|
-
await waitFor(() =>
|
|
1166
|
-
expect(screen.getByText('Hello world')).toBeInTheDocument(),
|
|
1167
|
-
);
|
|
1168
|
-
});
|
|
1169
|
-
|
|
1170
|
-
it('registers extra nodes via the nodes prop without throwing', async () => {
|
|
1171
|
-
// Passing the default node set again is a no-op but exercises the merge
|
|
1172
|
-
// path; the point is that supplying `nodes` does not break rendering.
|
|
1173
|
-
render(<RichTextView value={HELLO_STATE} nodes={[HeadingNode]} />);
|
|
1174
|
-
await waitFor(() =>
|
|
1175
|
-
expect(screen.getByText('Hello world')).toBeInTheDocument(),
|
|
1176
|
-
);
|
|
1177
|
-
});
|
|
1178
|
-
|
|
1179
|
-
it('does not throw on malformed JSON — renders fallback and calls onError', () => {
|
|
1180
|
-
const onError = vi.fn();
|
|
1181
|
-
expect(() =>
|
|
1182
|
-
render(
|
|
1183
|
-
<RichTextView
|
|
1184
|
-
value={'{ not valid json'}
|
|
1185
|
-
onParseError={onError}
|
|
1186
|
-
errorFallback={<div data-testid="view-fallback">Unavailable</div>}
|
|
1187
|
-
/>,
|
|
1188
|
-
),
|
|
1189
|
-
).not.toThrow();
|
|
1190
|
-
expect(onError).toHaveBeenCalledTimes(1);
|
|
1191
|
-
expect(onError.mock.calls[0][0]).toBeInstanceOf(Error);
|
|
1192
|
-
expect(screen.getByTestId('view-fallback')).toBeInTheDocument();
|
|
1193
|
-
// No editor surface is rendered in the error state.
|
|
1194
|
-
expect(
|
|
1195
|
-
document.querySelector('[data-lexical-editor]'),
|
|
1196
|
-
).not.toBeInTheDocument();
|
|
1197
|
-
});
|
|
1198
|
-
|
|
1199
|
-
it('renders bullet lists with a disc marker (not bare indentation)', async () => {
|
|
1200
|
-
const {container} = render(
|
|
1201
|
-
<RichTextView value={makeListState('bullet')} />,
|
|
1202
|
-
);
|
|
1203
|
-
await waitFor(() =>
|
|
1204
|
-
expect(screen.getByText('Item one')).toBeInTheDocument(),
|
|
1205
|
-
);
|
|
1206
|
-
const ul = container.querySelector('ul');
|
|
1207
|
-
expect(ul).not.toBeNull();
|
|
1208
|
-
// The theme must give the <ul> a marker class so the browser draws a
|
|
1209
|
-
// bullet. A bare list with only padding would show indentation only —
|
|
1210
|
-
// the exact bug this guards against.
|
|
1211
|
-
expect(ul?.className.trim()).not.toBe('');
|
|
1212
|
-
expect(getComputedStyle(ul as Element).listStyleType).toBe('disc');
|
|
1213
|
-
});
|
|
1214
|
-
|
|
1215
|
-
it('renders numbered lists with a decimal marker (not bare indentation)', async () => {
|
|
1216
|
-
const {container} = render(
|
|
1217
|
-
<RichTextView value={makeListState('number')} />,
|
|
1218
|
-
);
|
|
1219
|
-
await waitFor(() =>
|
|
1220
|
-
expect(screen.getByText('Item one')).toBeInTheDocument(),
|
|
1221
|
-
);
|
|
1222
|
-
const ol = container.querySelector('ol');
|
|
1223
|
-
expect(ol).not.toBeNull();
|
|
1224
|
-
expect(ol?.className.trim()).not.toBe('');
|
|
1225
|
-
expect(getComputedStyle(ol as Element).listStyleType).toBe('decimal');
|
|
1226
|
-
});
|
|
1227
|
-
|
|
1228
|
-
it('renders nested bullet lists with a distinct depth-2 marker (circle)', async () => {
|
|
1229
|
-
const {container} = render(
|
|
1230
|
-
<RichTextView value={makeNestedBulletState()} />,
|
|
1231
|
-
);
|
|
1232
|
-
await waitFor(() =>
|
|
1233
|
-
expect(screen.getByText('Nested item')).toBeInTheDocument(),
|
|
1234
|
-
);
|
|
1235
|
-
const lists = container.querySelectorAll('ul');
|
|
1236
|
-
// Outer <ul> plus the nested <ul>.
|
|
1237
|
-
expect(lists.length).toBe(2);
|
|
1238
|
-
const [outer, nested] = lists;
|
|
1239
|
-
expect(getComputedStyle(outer).listStyleType).toBe('disc');
|
|
1240
|
-
// Depth-2 must cycle to a different marker so nesting is visually legible,
|
|
1241
|
-
// matching the native browser disc → circle progression.
|
|
1242
|
-
expect(getComputedStyle(nested).listStyleType).toBe('circle');
|
|
1243
|
-
});
|
|
1244
|
-
|
|
1245
|
-
it('renders nothing (no crash) on malformed JSON with no fallback', () => {
|
|
1246
|
-
expect(() => render(<RichTextView value={'garbage'} />)).not.toThrow();
|
|
1247
|
-
expect(
|
|
1248
|
-
document.querySelector('[data-lexical-editor]'),
|
|
1249
|
-
).not.toBeInTheDocument();
|
|
1250
|
-
});
|
|
1251
|
-
|
|
1252
|
-
it('updates the rendered content when the value prop changes', async () => {
|
|
1253
|
-
// LexicalComposer's initialConfig.editorState is only read on mount, so
|
|
1254
|
-
// without the internal sync plugin the view would freeze at the first
|
|
1255
|
-
// value. This guards that a changed `value` re-renders the content — the
|
|
1256
|
-
// exact bug in the "Markdown Serializers" story (RichTextView stayed stale
|
|
1257
|
-
// while the Markdown input changed).
|
|
1258
|
-
const {rerender} = render(
|
|
1259
|
-
<RichTextView value={makeParagraphState('first version')} />,
|
|
1260
|
-
);
|
|
1261
|
-
await waitFor(() =>
|
|
1262
|
-
expect(screen.getByText('first version')).toBeInTheDocument(),
|
|
1263
|
-
);
|
|
1264
|
-
|
|
1265
|
-
rerender(<RichTextView value={makeParagraphState('second version')} />);
|
|
1266
|
-
await waitFor(() =>
|
|
1267
|
-
expect(screen.getByText('second version')).toBeInTheDocument(),
|
|
1268
|
-
);
|
|
1269
|
-
expect(screen.queryByText('first version')).not.toBeInTheDocument();
|
|
1270
|
-
});
|
|
1271
|
-
|
|
1272
|
-
it('recovers from a malformed value once a valid value is supplied again', async () => {
|
|
1273
|
-
// A bad value renders the fallback; a subsequent valid value must re-mount
|
|
1274
|
-
// the composer and render the new content (hasError resets on change).
|
|
1275
|
-
const {rerender} = render(
|
|
1276
|
-
<RichTextView
|
|
1277
|
-
value={'{ not valid json'}
|
|
1278
|
-
errorFallback={<div data-testid="view-fallback">Unavailable</div>}
|
|
1279
|
-
/>,
|
|
1280
|
-
);
|
|
1281
|
-
expect(screen.getByTestId('view-fallback')).toBeInTheDocument();
|
|
1282
|
-
|
|
1283
|
-
rerender(<RichTextView value={makeParagraphState('recovered')} />);
|
|
1284
|
-
await waitFor(() =>
|
|
1285
|
-
expect(screen.getByText('recovered')).toBeInTheDocument(),
|
|
1286
|
-
);
|
|
1287
|
-
expect(screen.queryByTestId('view-fallback')).not.toBeInTheDocument();
|
|
1288
|
-
});
|
|
1289
|
-
});
|
|
1290
|
-
|
|
1291
|
-
describe('markdown serializers', () => {
|
|
1292
|
-
it('markdownToEditorStateJSON produces a heading node from "# "', () => {
|
|
1293
|
-
const json = markdownToEditorStateJSON('# Title');
|
|
1294
|
-
const parsed = JSON.parse(json);
|
|
1295
|
-
const first = parsed.root.children[0];
|
|
1296
|
-
expect(first.type).toBe('heading');
|
|
1297
|
-
expect(first.tag).toBe('h1');
|
|
1298
|
-
expect(first.children[0].text).toBe('Title');
|
|
1299
|
-
});
|
|
1300
|
-
|
|
1301
|
-
it('editorStateJSONToMarkdown round-trips a heading back to "# "', () => {
|
|
1302
|
-
const json = markdownToEditorStateJSON('# Title');
|
|
1303
|
-
expect(editorStateJSONToMarkdown(json)).toBe('# Title');
|
|
1304
|
-
});
|
|
1305
|
-
|
|
1306
|
-
it('markdown round-trips through both helpers', () => {
|
|
1307
|
-
const md = '# Heading\n\nSome **bold** text';
|
|
1308
|
-
const json = markdownToEditorStateJSON(md);
|
|
1309
|
-
expect(editorStateJSONToMarkdown(json)).toBe(md);
|
|
1310
|
-
});
|
|
1311
|
-
|
|
1312
|
-
it('honors a custom (empty) transformers set — no heading syntax', () => {
|
|
1313
|
-
// With no transformers, "# Title" is not recognized as a heading; it stays
|
|
1314
|
-
// a plain paragraph, so serializing back yields the literal text.
|
|
1315
|
-
const json = markdownToEditorStateJSON('# Title', {transformers: []});
|
|
1316
|
-
const parsed = JSON.parse(json);
|
|
1317
|
-
expect(parsed.root.children[0].type).toBe('paragraph');
|
|
1318
|
-
expect(parsed.root.children[0].children[0].text).toBe('# Title');
|
|
1319
|
-
});
|
|
1320
|
-
|
|
1321
|
-
it('produces JSON consumable as RichTextEditor defaultValue', async () => {
|
|
1322
|
-
const value = markdownToEditorStateJSON('Hello world');
|
|
1323
|
-
render(<RichTextEditor label="Notes" defaultValue={value} />);
|
|
1324
|
-
await waitFor(() =>
|
|
1325
|
-
expect(screen.getByRole('textbox').textContent).toContain('Hello world'),
|
|
1326
|
-
);
|
|
1327
|
-
});
|
|
1328
|
-
});
|
|
1329
|
-
|
|
1330
|
-
describe('RichTextEditorToolbar', () => {
|
|
1331
|
-
it('treats a false conditional toolbar as absent', () => {
|
|
1332
|
-
const {container, rerender} = render(
|
|
1333
|
-
<RichTextEditor label="Notes" toolbar={false} tabEscapeHint="" />,
|
|
1334
|
-
);
|
|
1335
|
-
const conditionalBodyClass = container.querySelector(
|
|
1336
|
-
'.astryx-rich-text-editor',
|
|
1337
|
-
)?.firstElementChild?.className;
|
|
1338
|
-
|
|
1339
|
-
rerender(<RichTextEditor label="Notes" tabEscapeHint="" />);
|
|
1340
|
-
|
|
1341
|
-
expect(
|
|
1342
|
-
container.querySelector('.astryx-rich-text-editor')?.firstElementChild
|
|
1343
|
-
?.className,
|
|
1344
|
-
).toBe(conditionalBodyClass);
|
|
1345
|
-
expect(screen.queryByRole('toolbar')).not.toBeInTheDocument();
|
|
1346
|
-
});
|
|
1347
|
-
|
|
1348
|
-
it('renders flush at the top before the editing surface', () => {
|
|
1349
|
-
const {container} = render(
|
|
1350
|
-
<RichTextEditor label="Notes" toolbar={<RichTextEditorToolbar />} />,
|
|
1351
|
-
);
|
|
1352
|
-
const toolbar = screen.getByRole('toolbar', {name: 'Text formatting'});
|
|
1353
|
-
const textbox = screen.getByRole('textbox');
|
|
1354
|
-
const editor = container.querySelector('.astryx-rich-text-editor');
|
|
1355
|
-
const toolbarRegion = toolbar.parentElement?.parentElement;
|
|
1356
|
-
const editorBody = toolbarRegion?.nextElementSibling;
|
|
1357
|
-
|
|
1358
|
-
expect(toolbar).toHaveClass('astryx-toolbar');
|
|
1359
|
-
expect(toolbar).toHaveAttribute('data-size', 'sm');
|
|
1360
|
-
expect(screen.getByRole('button', {name: 'Bold'})).toHaveAttribute(
|
|
1361
|
-
'data-size',
|
|
1362
|
-
'sm',
|
|
1363
|
-
);
|
|
1364
|
-
expect(
|
|
1365
|
-
toolbar.compareDocumentPosition(textbox) &
|
|
1366
|
-
Node.DOCUMENT_POSITION_FOLLOWING,
|
|
1367
|
-
).toBe(Node.DOCUMENT_POSITION_FOLLOWING);
|
|
1368
|
-
expect(toolbarRegion?.parentElement).toBe(editor);
|
|
1369
|
-
expect(toolbarRegion).toBeInstanceOf(HTMLElement);
|
|
1370
|
-
expect(editorBody).toBeInstanceOf(HTMLElement);
|
|
1371
|
-
// The compact gutter is emitted by StyleX; do not regress to a component
|
|
1372
|
-
// inline-style escape hatch. Browser coverage below owns the geometry.
|
|
1373
|
-
expect(toolbarRegion).not.toHaveAttribute('style');
|
|
1374
|
-
// Vitest's JSDOM setup does not load compiled StyleX CSS. The WithToolbar
|
|
1375
|
-
// story provides visual coverage for flush edges and preserved body inset;
|
|
1376
|
-
// here we verify that the dedicated body receives its StyleX layout.
|
|
1377
|
-
expect(
|
|
1378
|
-
[...(editorBody?.classList ?? [])].some(className =>
|
|
1379
|
-
className.startsWith('x'),
|
|
1380
|
-
),
|
|
1381
|
-
).toBe(true);
|
|
1382
|
-
|
|
1383
|
-
// Block formatting is consolidated into a selector, while history and
|
|
1384
|
-
// inline formatting remain direct buttons.
|
|
1385
|
-
expect(
|
|
1386
|
-
screen.getByRole('combobox', {name: 'Block format'}).parentElement,
|
|
1387
|
-
).toHaveAttribute('data-size', 'sm');
|
|
1388
|
-
const historyDivider = screen.getByRole('separator', {
|
|
1389
|
-
name: 'History and block formats',
|
|
1390
|
-
});
|
|
1391
|
-
const inlineDivider = screen.getByRole('separator', {
|
|
1392
|
-
name: 'Block and inline formats',
|
|
1393
|
-
});
|
|
1394
|
-
expect(historyDivider).toHaveAttribute('aria-orientation', 'vertical');
|
|
1395
|
-
expect(inlineDivider).toHaveAttribute('aria-orientation', 'vertical');
|
|
1396
|
-
|
|
1397
|
-
const blockSelector = screen.getByRole('combobox', {
|
|
1398
|
-
name: 'Block format',
|
|
1399
|
-
});
|
|
1400
|
-
expect(
|
|
1401
|
-
historyDivider.compareDocumentPosition(blockSelector) &
|
|
1402
|
-
Node.DOCUMENT_POSITION_FOLLOWING,
|
|
1403
|
-
).toBe(Node.DOCUMENT_POSITION_FOLLOWING);
|
|
1404
|
-
expect(
|
|
1405
|
-
blockSelector.compareDocumentPosition(inlineDivider) &
|
|
1406
|
-
Node.DOCUMENT_POSITION_FOLLOWING,
|
|
1407
|
-
).toBe(Node.DOCUMENT_POSITION_FOLLOWING);
|
|
1408
|
-
for (const name of [
|
|
1409
|
-
'Bold',
|
|
1410
|
-
'Italic',
|
|
1411
|
-
'Underline',
|
|
1412
|
-
'Strikethrough',
|
|
1413
|
-
'Inline code',
|
|
1414
|
-
'Undo',
|
|
1415
|
-
'Redo',
|
|
1416
|
-
]) {
|
|
1417
|
-
expect(screen.getByRole('button', {name})).toBeInTheDocument();
|
|
1418
|
-
}
|
|
1419
|
-
});
|
|
1420
|
-
|
|
1421
|
-
it('renders configured headings and block formats in the selector', async () => {
|
|
1422
|
-
const user = userEvent.setup();
|
|
1423
|
-
render(
|
|
1424
|
-
<RichTextEditor
|
|
1425
|
-
label="Notes"
|
|
1426
|
-
toolbar={<RichTextEditorToolbar headingLevels={['h1', 'h2']} />}
|
|
1427
|
-
/>,
|
|
1428
|
-
);
|
|
1429
|
-
await user.click(screen.getByRole('combobox', {name: 'Block format'}));
|
|
1430
|
-
|
|
1431
|
-
for (const name of [
|
|
1432
|
-
'Paragraph',
|
|
1433
|
-
'Heading 1',
|
|
1434
|
-
'Heading 2',
|
|
1435
|
-
'Bulleted list',
|
|
1436
|
-
'Numbered list',
|
|
1437
|
-
'Block quote',
|
|
1438
|
-
]) {
|
|
1439
|
-
expect(screen.getByRole('option', {name, ...h})).toBeInTheDocument();
|
|
1440
|
-
}
|
|
1441
|
-
expect(
|
|
1442
|
-
screen.queryByRole('option', {name: 'Heading 3', ...h}),
|
|
1443
|
-
).not.toBeInTheDocument();
|
|
1444
|
-
});
|
|
1445
|
-
|
|
1446
|
-
it('accepts a custom accessible label', () => {
|
|
1447
|
-
render(
|
|
1448
|
-
<RichTextEditor
|
|
1449
|
-
label="Notes"
|
|
1450
|
-
toolbar={<RichTextEditorToolbar label="Editor controls" />}
|
|
1451
|
-
/>,
|
|
1452
|
-
);
|
|
1453
|
-
expect(
|
|
1454
|
-
screen.getByRole('toolbar', {name: 'Editor controls'}),
|
|
1455
|
-
).toBeInTheDocument();
|
|
1456
|
-
});
|
|
1457
|
-
|
|
1458
|
-
it('renders composed endContent', () => {
|
|
1459
|
-
render(
|
|
1460
|
-
<RichTextEditor
|
|
1461
|
-
label="Notes"
|
|
1462
|
-
toolbar={
|
|
1463
|
-
<RichTextEditorToolbar
|
|
1464
|
-
endContent={<button type="button">Custom</button>}
|
|
1465
|
-
/>
|
|
1466
|
-
}
|
|
1467
|
-
/>,
|
|
1468
|
-
);
|
|
1469
|
-
expect(screen.getByRole('button', {name: 'Custom'})).toBeInTheDocument();
|
|
1470
|
-
});
|
|
1471
|
-
|
|
1472
|
-
it('keeps every formatting action directly available in the scroll row', () => {
|
|
1473
|
-
render(
|
|
1474
|
-
<RichTextEditor label="Notes" toolbar={<RichTextEditorToolbar />} />,
|
|
1475
|
-
);
|
|
1476
|
-
|
|
1477
|
-
const actionRow = screen.getByRole('group', {
|
|
1478
|
-
name: 'Formatting actions',
|
|
1479
|
-
});
|
|
1480
|
-
expect(actionRow).not.toHaveAttribute(EDGE_COMP_ATTR);
|
|
1481
|
-
for (const name of [
|
|
1482
|
-
'Bold',
|
|
1483
|
-
'Italic',
|
|
1484
|
-
'Underline',
|
|
1485
|
-
'Strikethrough',
|
|
1486
|
-
'Inline code',
|
|
1487
|
-
'Link',
|
|
1488
|
-
]) {
|
|
1489
|
-
expect(actionRow).toContainElement(screen.getByRole('button', {name}));
|
|
1490
|
-
}
|
|
1491
|
-
expect(
|
|
1492
|
-
screen.queryByRole('button', {name: 'More text formatting options'}),
|
|
1493
|
-
).not.toBeInTheDocument();
|
|
1494
|
-
});
|
|
1495
|
-
|
|
1496
|
-
it('disables formatting controls when the editor is read-only', () => {
|
|
1497
|
-
render(
|
|
1498
|
-
<RichTextEditor
|
|
1499
|
-
label="Notes"
|
|
1500
|
-
isReadOnly
|
|
1501
|
-
toolbar={<RichTextEditorToolbar />}
|
|
1502
|
-
/>,
|
|
1503
|
-
);
|
|
1504
|
-
expect(screen.getByRole('button', {name: 'Bold'})).toBeDisabled();
|
|
1505
|
-
});
|
|
1506
|
-
|
|
1507
|
-
it('renders a theme-registered icon override for a richtext:* key', () => {
|
|
1508
|
-
registerIcons({
|
|
1509
|
-
'richtext:bold': <span data-testid="themed-bold">B!</span>,
|
|
1510
|
-
});
|
|
1511
|
-
try {
|
|
1512
|
-
render(
|
|
1513
|
-
<RichTextEditor label="Notes" toolbar={<RichTextEditorToolbar />} />,
|
|
1514
|
-
);
|
|
1515
|
-
// The Bold control uses the theme's registered glyph instead of the
|
|
1516
|
-
// bundled inline default.
|
|
1517
|
-
const bold = screen.getByRole('button', {name: 'Bold'});
|
|
1518
|
-
expect(
|
|
1519
|
-
bold.querySelector('[data-testid="themed-bold"]'),
|
|
1520
|
-
).toBeInTheDocument();
|
|
1521
|
-
} finally {
|
|
1522
|
-
resetIcons();
|
|
1523
|
-
}
|
|
1524
|
-
});
|
|
1525
|
-
|
|
1526
|
-
it('draws the glyphs of the theme it renders in', () => {
|
|
1527
|
-
const theme = defineTheme({
|
|
1528
|
-
name: 'richtext-toolbar-themed-icons',
|
|
1529
|
-
icons: {
|
|
1530
|
-
'richtext:bold': <svg data-testid="theme-bold" />,
|
|
1531
|
-
'richtext:undo': <svg data-testid="theme-undo" />,
|
|
1532
|
-
},
|
|
1533
|
-
});
|
|
1534
|
-
render(
|
|
1535
|
-
<Theme theme={theme}>
|
|
1536
|
-
<RichTextEditor label="Notes" toolbar={<RichTextEditorToolbar />} />
|
|
1537
|
-
</Theme>,
|
|
1538
|
-
);
|
|
1539
|
-
expect(
|
|
1540
|
-
screen
|
|
1541
|
-
.getByRole('button', {name: 'Bold'})
|
|
1542
|
-
.querySelector('[data-testid="theme-bold"]'),
|
|
1543
|
-
).toBeInTheDocument();
|
|
1544
|
-
expect(
|
|
1545
|
-
screen
|
|
1546
|
-
.getByRole('button', {name: 'Undo'})
|
|
1547
|
-
.querySelector('[data-testid="theme-undo"]'),
|
|
1548
|
-
).toBeInTheDocument();
|
|
1549
|
-
});
|
|
1550
|
-
|
|
1551
|
-
it('wraps only undo and redo in the RTL mirror', () => {
|
|
1552
|
-
const {container} = render(
|
|
1553
|
-
<RichTextEditor label="Notes" toolbar={<RichTextEditorToolbar />} />,
|
|
1554
|
-
);
|
|
1555
|
-
// The atomic class only; dev builds add a debug name with a dot in it.
|
|
1556
|
-
const mirrorClass = (stylex.props(rtlStyles.mirror).className ?? '')
|
|
1557
|
-
.split(' ')
|
|
1558
|
-
.find(name => !name.includes('__'));
|
|
1559
|
-
expect(mirrorClass).toBeDefined();
|
|
1560
|
-
const mirrored = [
|
|
1561
|
-
...container.getElementsByClassName(mirrorClass ?? ''),
|
|
1562
|
-
].map(element => element.closest('button')?.getAttribute('aria-label'));
|
|
1563
|
-
expect(mirrored).toEqual(['Undo', 'Redo']);
|
|
1564
|
-
});
|
|
1565
|
-
});
|
|
1566
|
-
|
|
1567
|
-
describe('linkUtils', () => {
|
|
1568
|
-
describe('sanitizeUrl', () => {
|
|
1569
|
-
it('passes through http/https URLs', () => {
|
|
1570
|
-
expect(sanitizeUrl('https://example.com')).toBe('https://example.com/');
|
|
1571
|
-
expect(sanitizeUrl('http://example.com/x')).toBe('http://example.com/x');
|
|
1572
|
-
});
|
|
1573
|
-
|
|
1574
|
-
it('defaults a scheme-less host to https', () => {
|
|
1575
|
-
expect(sanitizeUrl('example.com')).toBe('https://example.com/');
|
|
1576
|
-
});
|
|
1577
|
-
|
|
1578
|
-
it('preserves mailto and tel schemes', () => {
|
|
1579
|
-
expect(sanitizeUrl('mailto:a@b.com')).toBe('mailto:a@b.com');
|
|
1580
|
-
expect(sanitizeUrl('tel:+15551234')).toBe('tel:+15551234');
|
|
1581
|
-
});
|
|
1582
|
-
|
|
1583
|
-
it('rejects javascript: and other unsafe schemes as about:blank', () => {
|
|
1584
|
-
expect(sanitizeUrl('javascript:alert(1)')).toBe('about:blank');
|
|
1585
|
-
expect(sanitizeUrl('data:text/html,<script>')).toBe('about:blank');
|
|
1586
|
-
expect(sanitizeUrl('vbscript:msgbox')).toBe('about:blank');
|
|
1587
|
-
});
|
|
1588
|
-
|
|
1589
|
-
it('rejects empty/whitespace input as about:blank', () => {
|
|
1590
|
-
expect(sanitizeUrl('')).toBe('about:blank');
|
|
1591
|
-
expect(sanitizeUrl(' ')).toBe('about:blank');
|
|
1592
|
-
});
|
|
1593
|
-
});
|
|
1594
|
-
|
|
1595
|
-
describe('validateUrl', () => {
|
|
1596
|
-
it('accepts safe URLs (with or without scheme)', () => {
|
|
1597
|
-
expect(validateUrl('https://example.com')).toBe(true);
|
|
1598
|
-
expect(validateUrl('example.com')).toBe(true);
|
|
1599
|
-
expect(validateUrl('mailto:a@b.com')).toBe(true);
|
|
1600
|
-
});
|
|
1601
|
-
|
|
1602
|
-
it('rejects unsafe schemes and empty input', () => {
|
|
1603
|
-
expect(validateUrl('javascript:alert(1)')).toBe(false);
|
|
1604
|
-
expect(validateUrl('')).toBe(false);
|
|
1605
|
-
});
|
|
1606
|
-
});
|
|
1607
|
-
});
|
|
1608
|
-
|
|
1609
|
-
describe('RichTextEditorToolbar — links', () => {
|
|
1610
|
-
it('renders a Link button by default', () => {
|
|
1611
|
-
render(
|
|
1612
|
-
<RichTextEditor label="Notes" toolbar={<RichTextEditorToolbar />} />,
|
|
1613
|
-
);
|
|
1614
|
-
expect(screen.getByRole('button', {name: 'Link'})).toBeInTheDocument();
|
|
1615
|
-
});
|
|
1616
|
-
|
|
1617
|
-
it('opens an Astryx Dialog instead of the browser prompt by default', async () => {
|
|
1618
|
-
const browserPrompt = vi.spyOn(window, 'prompt').mockReturnValue(null);
|
|
1619
|
-
try {
|
|
1620
|
-
render(
|
|
1621
|
-
<RichTextEditor label="Notes" toolbar={<RichTextEditorToolbar />} />,
|
|
1622
|
-
);
|
|
1623
|
-
|
|
1624
|
-
fireEvent.click(screen.getByRole('button', {name: 'Link'}));
|
|
1625
|
-
|
|
1626
|
-
expect(browserPrompt).not.toHaveBeenCalled();
|
|
1627
|
-
await waitFor(() =>
|
|
1628
|
-
expect(
|
|
1629
|
-
screen.getByRole('dialog', {name: 'Insert link', ...h}),
|
|
1630
|
-
).toBeInTheDocument(),
|
|
1631
|
-
);
|
|
1632
|
-
const dialog = screen.getByRole('dialog', {
|
|
1633
|
-
name: 'Insert link',
|
|
1634
|
-
...h,
|
|
1635
|
-
});
|
|
1636
|
-
expect(dialog.querySelector('.astryx-layout-header')).toBeInTheDocument();
|
|
1637
|
-
expect(
|
|
1638
|
-
dialog.querySelector('.astryx-layout-content'),
|
|
1639
|
-
).toBeInTheDocument();
|
|
1640
|
-
expect(dialog.querySelector('.astryx-layout-footer')).toBeInTheDocument();
|
|
1641
|
-
expect(
|
|
1642
|
-
screen.getByRole('textbox', {name: 'URL', ...h}),
|
|
1643
|
-
).toBeInTheDocument();
|
|
1644
|
-
expect(
|
|
1645
|
-
screen.getByRole('button', {name: 'Add link', ...h}),
|
|
1646
|
-
).toBeInTheDocument();
|
|
1647
|
-
} finally {
|
|
1648
|
-
browserPrompt.mockRestore();
|
|
1649
|
-
}
|
|
1650
|
-
});
|
|
1651
|
-
|
|
1652
|
-
it('omits the Link button when hasLink is false', () => {
|
|
1653
|
-
render(
|
|
1654
|
-
<RichTextEditor
|
|
1655
|
-
label="Notes"
|
|
1656
|
-
toolbar={<RichTextEditorToolbar hasLink={false} />}
|
|
1657
|
-
/>,
|
|
1658
|
-
);
|
|
1659
|
-
expect(
|
|
1660
|
-
screen.queryByRole('button', {name: 'Link'}),
|
|
1661
|
-
).not.toBeInTheDocument();
|
|
1662
|
-
});
|
|
1663
|
-
|
|
1664
|
-
it('opens link insertion on Cmd/Ctrl+K but ignores Cmd/Ctrl+Shift+K', async () => {
|
|
1665
|
-
const promptForUrl = vi.fn(() => null);
|
|
1666
|
-
render(
|
|
1667
|
-
<RichTextEditor
|
|
1668
|
-
label="Notes"
|
|
1669
|
-
toolbar={<RichTextEditorToolbar promptForUrl={promptForUrl} />}
|
|
1670
|
-
/>,
|
|
1671
|
-
);
|
|
1672
|
-
const textbox = screen.getByRole('textbox');
|
|
1673
|
-
|
|
1674
|
-
// Cmd+K (mac) / Ctrl+K (others) — matches isExactShortcutMatch, so the
|
|
1675
|
-
// link flow runs and asks for a URL. Fire both modifier variants so the
|
|
1676
|
-
// test is platform-agnostic.
|
|
1677
|
-
fireEvent.keyDown(textbox, {key: 'k', metaKey: true});
|
|
1678
|
-
fireEvent.keyDown(textbox, {key: 'k', ctrlKey: true});
|
|
1679
|
-
expect(promptForUrl).toHaveBeenCalled();
|
|
1680
|
-
|
|
1681
|
-
// Adding Shift must NOT trigger it (exact-match semantics, matching the
|
|
1682
|
-
// Lexical playground).
|
|
1683
|
-
promptForUrl.mockClear();
|
|
1684
|
-
fireEvent.keyDown(textbox, {key: 'k', metaKey: true, shiftKey: true});
|
|
1685
|
-
fireEvent.keyDown(textbox, {key: 'k', ctrlKey: true, shiftKey: true});
|
|
1686
|
-
expect(promptForUrl).not.toHaveBeenCalled();
|
|
1687
|
-
});
|
|
1688
|
-
|
|
1689
|
-
it('creates a sanitized link over the selected text via promptForUrl', async () => {
|
|
1690
|
-
let editor!: LexicalEditor;
|
|
1691
|
-
const promptForUrl = vi.fn(() => 'example.com');
|
|
1692
|
-
render(
|
|
1693
|
-
<RichTextEditor
|
|
1694
|
-
label="Notes"
|
|
1695
|
-
toolbar={<RichTextEditorToolbar promptForUrl={promptForUrl} />}
|
|
1696
|
-
plugins={<CaptureEditor onReady={e => (editor = e)} />}
|
|
1697
|
-
/>,
|
|
1698
|
-
);
|
|
1699
|
-
await waitFor(() => expect(editor).toBeDefined());
|
|
1700
|
-
|
|
1701
|
-
// Seed "hello" and select all of it so the toggle wraps a real range.
|
|
1702
|
-
// Seed + select in a single update so the RangeSelection is live when the
|
|
1703
|
-
// toolbar reads it.
|
|
1704
|
-
editor.update(() => {
|
|
1705
|
-
const root = $getRoot();
|
|
1706
|
-
root.clear();
|
|
1707
|
-
const paragraph = $createParagraphNode();
|
|
1708
|
-
const textNode = $createTextNode('hello');
|
|
1709
|
-
paragraph.append(textNode);
|
|
1710
|
-
root.append(paragraph);
|
|
1711
|
-
textNode.select(0, 5);
|
|
1712
|
-
});
|
|
1713
|
-
|
|
1714
|
-
fireEvent.click(screen.getByRole('button', {name: 'Link'}));
|
|
1715
|
-
|
|
1716
|
-
expect(promptForUrl).toHaveBeenCalled();
|
|
1717
|
-
// The selection text is passed to the prompt (empty string is acceptable
|
|
1718
|
-
// if jsdom drops the range; the important contract is the sanitized href).
|
|
1719
|
-
// The link node exists with the sanitized (https, scheme-added) href.
|
|
1720
|
-
await waitFor(() => {
|
|
1721
|
-
let href: string | null = null;
|
|
1722
|
-
editor.getEditorState().read(() => {
|
|
1723
|
-
const root = $getRoot();
|
|
1724
|
-
const paragraph = root.getFirstChild();
|
|
1725
|
-
// Find the first LinkNode-like descendant (has getURL()).
|
|
1726
|
-
const descendants = $isElementNode(paragraph)
|
|
1727
|
-
? paragraph.getChildren()
|
|
1728
|
-
: [];
|
|
1729
|
-
for (const child of descendants) {
|
|
1730
|
-
if ('getURL' in child) {
|
|
1731
|
-
href = (child as {getURL(): string}).getURL();
|
|
1732
|
-
break;
|
|
1733
|
-
}
|
|
1734
|
-
}
|
|
1735
|
-
});
|
|
1736
|
-
expect(href).toBe('https://example.com/');
|
|
1737
|
-
});
|
|
1738
|
-
});
|
|
1739
|
-
|
|
1740
|
-
it('does not create a link when promptForUrl returns an unsafe scheme', async () => {
|
|
1741
|
-
let editor!: LexicalEditor;
|
|
1742
|
-
const promptForUrl = vi.fn(() => 'javascript:alert(1)');
|
|
1743
|
-
render(
|
|
1744
|
-
<RichTextEditor
|
|
1745
|
-
label="Notes"
|
|
1746
|
-
toolbar={<RichTextEditorToolbar promptForUrl={promptForUrl} />}
|
|
1747
|
-
plugins={<CaptureEditor onReady={e => (editor = e)} />}
|
|
1748
|
-
/>,
|
|
1749
|
-
);
|
|
1750
|
-
await waitFor(() => expect(editor).toBeDefined());
|
|
1751
|
-
editor.update(() => {
|
|
1752
|
-
const root = $getRoot();
|
|
1753
|
-
root.clear();
|
|
1754
|
-
const paragraph = $createParagraphNode();
|
|
1755
|
-
const textNode = $createTextNode('hello');
|
|
1756
|
-
paragraph.append(textNode);
|
|
1757
|
-
root.append(paragraph);
|
|
1758
|
-
textNode.select(0, 5);
|
|
1759
|
-
});
|
|
1760
|
-
|
|
1761
|
-
fireEvent.click(screen.getByRole('button', {name: 'Link'}));
|
|
1762
|
-
expect(promptForUrl).toHaveBeenCalled();
|
|
1763
|
-
|
|
1764
|
-
// No link node was created — no descendant exposes getURL().
|
|
1765
|
-
let hasLinkNode = false;
|
|
1766
|
-
editor.getEditorState().read(() => {
|
|
1767
|
-
const root = $getRoot();
|
|
1768
|
-
const paragraph = root.getFirstChild();
|
|
1769
|
-
const descendants = $isElementNode(paragraph)
|
|
1770
|
-
? paragraph.getChildren()
|
|
1771
|
-
: [];
|
|
1772
|
-
for (const child of descendants) {
|
|
1773
|
-
if ('getURL' in child) {
|
|
1774
|
-
hasLinkNode = true;
|
|
1775
|
-
}
|
|
1776
|
-
}
|
|
1777
|
-
});
|
|
1778
|
-
expect(hasLinkNode).toBe(false);
|
|
1779
|
-
});
|
|
1780
|
-
});
|
|
1781
|
-
|
|
1782
|
-
describe('RichTextEditorAutoLinkPlugin', () => {
|
|
1783
|
-
it('renders without crashing inside the editor', () => {
|
|
1784
|
-
render(
|
|
1785
|
-
<RichTextEditor
|
|
1786
|
-
label="Notes"
|
|
1787
|
-
plugins={<RichTextEditorAutoLinkPlugin />}
|
|
1788
|
-
/>,
|
|
1789
|
-
);
|
|
1790
|
-
expect(screen.getByRole('textbox')).toBeInTheDocument();
|
|
1791
|
-
});
|
|
1792
|
-
|
|
1793
|
-
it('exposes URL + email default matchers that open in a new tab', () => {
|
|
1794
|
-
expect(DEFAULT_LINK_MATCHERS).toHaveLength(2);
|
|
1795
|
-
const urlMatch = DEFAULT_LINK_MATCHERS[0]('see https://example.com now');
|
|
1796
|
-
expect(urlMatch).not.toBeNull();
|
|
1797
|
-
expect(urlMatch?.url).toBe('https://example.com/');
|
|
1798
|
-
expect(urlMatch?.attributes).toEqual(NEW_TAB_LINK_ATTRIBUTES);
|
|
1799
|
-
|
|
1800
|
-
const emailMatch = DEFAULT_LINK_MATCHERS[1]('ping a@b.com please');
|
|
1801
|
-
expect(emailMatch).not.toBeNull();
|
|
1802
|
-
expect(emailMatch?.url).toBe('mailto:a@b.com');
|
|
1803
|
-
expect(emailMatch?.attributes).toEqual(NEW_TAB_LINK_ATTRIBUTES);
|
|
1804
|
-
});
|
|
1805
|
-
|
|
1806
|
-
it('auto-links a typed URL as an AutoLinkNode', async () => {
|
|
1807
|
-
let editor!: LexicalEditor;
|
|
1808
|
-
render(
|
|
1809
|
-
<RichTextEditor
|
|
1810
|
-
label="Notes"
|
|
1811
|
-
plugins={
|
|
1812
|
-
<>
|
|
1813
|
-
<RichTextEditorAutoLinkPlugin />
|
|
1814
|
-
<CaptureEditor onReady={e => (editor = e)} />
|
|
1815
|
-
</>
|
|
1816
|
-
}
|
|
1817
|
-
/>,
|
|
1818
|
-
);
|
|
1819
|
-
await waitFor(() => expect(editor).toBeDefined());
|
|
1820
|
-
|
|
1821
|
-
// A URL followed by a separator triggers the AutoLink transform.
|
|
1822
|
-
editor.update(() => {
|
|
1823
|
-
const root = $getRoot();
|
|
1824
|
-
root.clear();
|
|
1825
|
-
const paragraph = $createParagraphNode();
|
|
1826
|
-
paragraph.append($createTextNode('visit https://example.com '));
|
|
1827
|
-
root.append(paragraph);
|
|
1828
|
-
});
|
|
1829
|
-
|
|
1830
|
-
await waitFor(() => {
|
|
1831
|
-
let hasAutoLink = false;
|
|
1832
|
-
editor.getEditorState().read(() => {
|
|
1833
|
-
const root = $getRoot();
|
|
1834
|
-
const paragraph = root.getFirstChild();
|
|
1835
|
-
const children = $isElementNode(paragraph)
|
|
1836
|
-
? paragraph.getChildren()
|
|
1837
|
-
: [];
|
|
1838
|
-
children.forEach(child => {
|
|
1839
|
-
if (child.getType() === 'autolink') {
|
|
1840
|
-
hasAutoLink = true;
|
|
1841
|
-
}
|
|
1842
|
-
});
|
|
1843
|
-
});
|
|
1844
|
-
expect(hasAutoLink).toBe(true);
|
|
1845
|
-
});
|
|
1846
|
-
});
|
|
1847
|
-
});
|
|
1848
|
-
|
|
1849
|
-
describe('RichTextEditorToolbar — new-tab links', () => {
|
|
1850
|
-
it('bakes target=_blank + rel into the created link node by default', async () => {
|
|
1851
|
-
let editor!: LexicalEditor;
|
|
1852
|
-
const promptForUrl = vi.fn(() => 'example.com');
|
|
1853
|
-
render(
|
|
1854
|
-
<RichTextEditor
|
|
1855
|
-
label="Notes"
|
|
1856
|
-
toolbar={<RichTextEditorToolbar promptForUrl={promptForUrl} />}
|
|
1857
|
-
plugins={<CaptureEditor onReady={e => (editor = e)} />}
|
|
1858
|
-
/>,
|
|
1859
|
-
);
|
|
1860
|
-
await waitFor(() => expect(editor).toBeDefined());
|
|
1861
|
-
editor.update(() => {
|
|
1862
|
-
const root = $getRoot();
|
|
1863
|
-
root.clear();
|
|
1864
|
-
const paragraph = $createParagraphNode();
|
|
1865
|
-
const textNode = $createTextNode('hello');
|
|
1866
|
-
paragraph.append(textNode);
|
|
1867
|
-
root.append(paragraph);
|
|
1868
|
-
textNode.select(0, 5);
|
|
1869
|
-
});
|
|
1870
|
-
|
|
1871
|
-
fireEvent.click(screen.getByRole('button', {name: 'Link'}));
|
|
1872
|
-
|
|
1873
|
-
// The attributes live on the NODE (not patched onto the DOM), so they are
|
|
1874
|
-
// readable from editor state and will serialize.
|
|
1875
|
-
await waitFor(() => {
|
|
1876
|
-
let target: string | null = null;
|
|
1877
|
-
let rel: string | null = null;
|
|
1878
|
-
editor.getEditorState().read(() => {
|
|
1879
|
-
const root = $getRoot();
|
|
1880
|
-
const paragraph = root.getFirstChild();
|
|
1881
|
-
const children = $isElementNode(paragraph)
|
|
1882
|
-
? paragraph.getChildren()
|
|
1883
|
-
: [];
|
|
1884
|
-
for (const child of children) {
|
|
1885
|
-
if ('getTarget' in child) {
|
|
1886
|
-
const linkNode = child as unknown as {
|
|
1887
|
-
getTarget(): string | null;
|
|
1888
|
-
getRel(): string | null;
|
|
1889
|
-
};
|
|
1890
|
-
target = linkNode.getTarget();
|
|
1891
|
-
rel = linkNode.getRel();
|
|
1892
|
-
}
|
|
1893
|
-
}
|
|
1894
|
-
});
|
|
1895
|
-
expect(target).toBe('_blank');
|
|
1896
|
-
expect(rel).toBe('noopener noreferrer');
|
|
1897
|
-
});
|
|
1898
|
-
});
|
|
1899
|
-
|
|
1900
|
-
it('passes base props and the ref through to the Astryx Toolbar', () => {
|
|
1901
|
-
const ref = createRef<HTMLDivElement>();
|
|
1902
|
-
const onKeyDown = vi.fn();
|
|
1903
|
-
render(
|
|
1904
|
-
<RichTextEditor
|
|
1905
|
-
label="Notes"
|
|
1906
|
-
toolbar={
|
|
1907
|
-
<RichTextEditorToolbar
|
|
1908
|
-
ref={ref}
|
|
1909
|
-
className="product-toolbar"
|
|
1910
|
-
style={{marginBlockEnd: 3}}
|
|
1911
|
-
data-testid="toolbar-root"
|
|
1912
|
-
aria-describedby="toolbar-help"
|
|
1913
|
-
onKeyDown={onKeyDown}
|
|
1914
|
-
/>
|
|
1915
|
-
}
|
|
1916
|
-
/>,
|
|
1917
|
-
);
|
|
1918
|
-
// Base props land where core Toolbar puts them: the ref and styling on
|
|
1919
|
-
// its root, every other base prop on the role="toolbar" element inside.
|
|
1920
|
-
const toolbar = screen.getByRole('toolbar');
|
|
1921
|
-
const root = ref.current;
|
|
1922
|
-
expect(root).not.toBeNull();
|
|
1923
|
-
expect(root?.contains(toolbar)).toBe(true);
|
|
1924
|
-
expect(root).toHaveClass('product-toolbar');
|
|
1925
|
-
expect(root?.style.marginBlockEnd).toBe('3px');
|
|
1926
|
-
expect(screen.getByTestId('toolbar-root')).toBe(toolbar);
|
|
1927
|
-
expect(toolbar).toHaveAttribute('aria-describedby', 'toolbar-help');
|
|
1928
|
-
// The toolbar's own label and role survive the spread.
|
|
1929
|
-
expect(toolbar).toHaveAccessibleName('Text formatting');
|
|
1930
|
-
fireEvent.keyDown(toolbar, {key: 'x'});
|
|
1931
|
-
expect(onKeyDown).toHaveBeenCalledTimes(1);
|
|
1932
|
-
});
|
|
1933
|
-
|
|
1934
|
-
it('omits new-tab attributes when hasNewTabLinks is false', async () => {
|
|
1935
|
-
let editor!: LexicalEditor;
|
|
1936
|
-
const promptForUrl = vi.fn(() => 'example.com');
|
|
1937
|
-
render(
|
|
1938
|
-
<RichTextEditor
|
|
1939
|
-
label="Notes"
|
|
1940
|
-
toolbar={
|
|
1941
|
-
<RichTextEditorToolbar
|
|
1942
|
-
promptForUrl={promptForUrl}
|
|
1943
|
-
hasNewTabLinks={false}
|
|
1944
|
-
/>
|
|
1945
|
-
}
|
|
1946
|
-
plugins={<CaptureEditor onReady={e => (editor = e)} />}
|
|
1947
|
-
/>,
|
|
1948
|
-
);
|
|
1949
|
-
await waitFor(() => expect(editor).toBeDefined());
|
|
1950
|
-
editor.update(() => {
|
|
1951
|
-
const root = $getRoot();
|
|
1952
|
-
root.clear();
|
|
1953
|
-
const paragraph = $createParagraphNode();
|
|
1954
|
-
const textNode = $createTextNode('hello');
|
|
1955
|
-
paragraph.append(textNode);
|
|
1956
|
-
root.append(paragraph);
|
|
1957
|
-
textNode.select(0, 5);
|
|
1958
|
-
});
|
|
1959
|
-
|
|
1960
|
-
fireEvent.click(screen.getByRole('button', {name: 'Link'}));
|
|
1961
|
-
|
|
1962
|
-
await waitFor(() => {
|
|
1963
|
-
let target: string | null | undefined = undefined;
|
|
1964
|
-
let sawLink = false;
|
|
1965
|
-
editor.getEditorState().read(() => {
|
|
1966
|
-
const root = $getRoot();
|
|
1967
|
-
const paragraph = root.getFirstChild();
|
|
1968
|
-
const children = $isElementNode(paragraph)
|
|
1969
|
-
? paragraph.getChildren()
|
|
1970
|
-
: [];
|
|
1971
|
-
for (const child of children) {
|
|
1972
|
-
if ('getTarget' in child) {
|
|
1973
|
-
sawLink = true;
|
|
1974
|
-
target = (
|
|
1975
|
-
child as unknown as {getTarget(): string | null}
|
|
1976
|
-
).getTarget();
|
|
1977
|
-
}
|
|
1978
|
-
}
|
|
1979
|
-
});
|
|
1980
|
-
expect(sawLink).toBe(true);
|
|
1981
|
-
expect(target).toBeNull();
|
|
1982
|
-
});
|
|
1983
|
-
});
|
|
1984
|
-
});
|
|
1985
|
-
|
|
1986
|
-
describe('editable state follows prop changes', () => {
|
|
1987
|
-
it('disables editing and announces it when isDisabled turns on after mount', () => {
|
|
1988
|
-
const {rerender} = render(<RichTextEditor label="Notes" />);
|
|
1989
|
-
expect(screen.getByRole('textbox')).toHaveAttribute(
|
|
1990
|
-
'contenteditable',
|
|
1991
|
-
'true',
|
|
1992
|
-
);
|
|
1993
|
-
rerender(<RichTextEditor label="Notes" isDisabled />);
|
|
1994
|
-
const textbox = screen.getByRole('textbox');
|
|
1995
|
-
// The surface must actually stop accepting input, not only announce it.
|
|
1996
|
-
expect(textbox).toHaveAttribute('contenteditable', 'false');
|
|
1997
|
-
expect(textbox).toHaveAttribute('aria-disabled', 'true');
|
|
1998
|
-
expect(textbox).not.toHaveAttribute('aria-readonly');
|
|
1999
|
-
});
|
|
2000
|
-
|
|
2001
|
-
it('becomes editable when isReadOnly turns off after mount', () => {
|
|
2002
|
-
const {rerender} = render(<RichTextEditor label="Notes" isReadOnly />);
|
|
2003
|
-
expect(screen.getByRole('textbox')).toHaveAttribute(
|
|
2004
|
-
'contenteditable',
|
|
2005
|
-
'false',
|
|
2006
|
-
);
|
|
2007
|
-
rerender(<RichTextEditor label="Notes" />);
|
|
2008
|
-
const textbox = screen.getByRole('textbox');
|
|
2009
|
-
expect(textbox).toHaveAttribute('contenteditable', 'true');
|
|
2010
|
-
expect(textbox).not.toHaveAttribute('aria-readonly');
|
|
2011
|
-
});
|
|
2012
|
-
|
|
2013
|
-
it("keeps Lexical's read-only announcement when a plugin disables editing", () => {
|
|
2014
|
-
let captured: LexicalEditor | null = null;
|
|
2015
|
-
render(
|
|
2016
|
-
<RichTextEditor
|
|
2017
|
-
label="Notes"
|
|
2018
|
-
plugins={<CaptureEditor onReady={editor => (captured = editor)} />}
|
|
2019
|
-
/>,
|
|
2020
|
-
);
|
|
2021
|
-
act(() => {
|
|
2022
|
-
(captured as unknown as LexicalEditor).setEditable(false);
|
|
2023
|
-
});
|
|
2024
|
-
const textbox = screen.getByRole('textbox');
|
|
2025
|
-
expect(textbox).toHaveAttribute('contenteditable', 'false');
|
|
2026
|
-
// Neither prop is set, so the component must not clobber Lexical's own
|
|
2027
|
-
// state-derived aria-readonly with undefined.
|
|
2028
|
-
expect(textbox).toHaveAttribute('aria-readonly', 'true');
|
|
2029
|
-
});
|
|
2030
|
-
|
|
2031
|
-
it('keeps a plugin that disables editing during mount in charge', () => {
|
|
2032
|
-
// The prop sync renders after `plugins`, so its mount effect runs after
|
|
2033
|
-
// theirs; it must only react to prop changes, never re-assert the mount
|
|
2034
|
-
// value over a plugin that already locked the editor.
|
|
2035
|
-
function DisableOnMount() {
|
|
2036
|
-
const [editor] = useLexicalComposerContext();
|
|
2037
|
-
useEffect(() => {
|
|
2038
|
-
editor.setEditable(false);
|
|
2039
|
-
}, [editor]);
|
|
2040
|
-
return null;
|
|
2041
|
-
}
|
|
2042
|
-
let captured: LexicalEditor | null = null;
|
|
2043
|
-
render(
|
|
2044
|
-
<RichTextEditor
|
|
2045
|
-
label="Notes"
|
|
2046
|
-
plugins={
|
|
2047
|
-
<>
|
|
2048
|
-
<DisableOnMount />
|
|
2049
|
-
<CaptureEditor onReady={editor => (captured = editor)} />
|
|
2050
|
-
</>
|
|
2051
|
-
}
|
|
2052
|
-
/>,
|
|
2053
|
-
);
|
|
2054
|
-
expect((captured as unknown as LexicalEditor).isEditable()).toBe(false);
|
|
2055
|
-
expect(screen.getByRole('textbox')).toHaveAttribute(
|
|
2056
|
-
'contenteditable',
|
|
2057
|
-
'false',
|
|
2058
|
-
);
|
|
2059
|
-
});
|
|
2060
|
-
});
|
|
2061
|
-
|
|
2062
|
-
describe('isReadOnly + isDisabled combined', () => {
|
|
2063
|
-
it('lets disabled win everywhere when both flags are set', () => {
|
|
2064
|
-
const {container} = render(
|
|
2065
|
-
<RichTextEditor label="Notes" isReadOnly isDisabled />,
|
|
2066
|
-
);
|
|
2067
|
-
const textbox = screen.getByRole('textbox');
|
|
2068
|
-
expect(textbox).toHaveAttribute('contenteditable', 'false');
|
|
2069
|
-
// Disabled semantics take precedence: announced disabled, never
|
|
2070
|
-
// read-only, and out of the tab order.
|
|
2071
|
-
expect(textbox).toHaveAttribute('aria-disabled', 'true');
|
|
2072
|
-
expect(textbox).not.toHaveAttribute('aria-readonly');
|
|
2073
|
-
expect(textbox).not.toHaveAttribute('tabindex', '0');
|
|
2074
|
-
const wrapper = container.querySelector('.astryx-rich-text-editor');
|
|
2075
|
-
expect(wrapper).not.toBeNull();
|
|
2076
|
-
expect(getComputedStyle(wrapper as Element).opacity).toBe('0.5');
|
|
2077
|
-
});
|
|
2078
|
-
|
|
2079
|
-
it('swaps disabled semantics for read-only semantics on rerender', () => {
|
|
2080
|
-
const {container, rerender} = render(
|
|
2081
|
-
<RichTextEditor label="Notes" isDisabled />,
|
|
2082
|
-
);
|
|
2083
|
-
expect(screen.getByRole('textbox')).toHaveAttribute(
|
|
2084
|
-
'aria-disabled',
|
|
2085
|
-
'true',
|
|
2086
|
-
);
|
|
2087
|
-
|
|
2088
|
-
rerender(<RichTextEditor label="Notes" isReadOnly />);
|
|
2089
|
-
const textbox = screen.getByRole('textbox');
|
|
2090
|
-
// Still non-editable, but the announcement and reachability flip from
|
|
2091
|
-
// disabled to read-only.
|
|
2092
|
-
expect(textbox).toHaveAttribute('contenteditable', 'false');
|
|
2093
|
-
expect(textbox).not.toHaveAttribute('aria-disabled');
|
|
2094
|
-
expect(textbox).toHaveAttribute('aria-readonly', 'true');
|
|
2095
|
-
expect(textbox).toHaveAttribute('tabindex', '0');
|
|
2096
|
-
// jsdom computes unset opacity as '' rather than '1', so assert the
|
|
2097
|
-
// dimmed rule no longer applies.
|
|
2098
|
-
const wrapper = container.querySelector('.astryx-rich-text-editor');
|
|
2099
|
-
expect(wrapper).not.toBeNull();
|
|
2100
|
-
expect(getComputedStyle(wrapper as Element).opacity).not.toBe('0.5');
|
|
2101
|
-
});
|
|
2102
|
-
});
|
|
2103
|
-
|
|
2104
|
-
describe('imperative ref across editable prop toggles', () => {
|
|
2105
|
-
stubRangeRects();
|
|
2106
|
-
|
|
2107
|
-
it('unlocks focus() once isDisabled is removed', async () => {
|
|
2108
|
-
const ref = createRef<RichTextEditorRef>();
|
|
2109
|
-
const {rerender} = render(
|
|
2110
|
-
<RichTextEditor ref={ref} label="Notes" isDisabled />,
|
|
2111
|
-
);
|
|
2112
|
-
const disabledTextbox = screen.getByRole('textbox');
|
|
2113
|
-
expect(disabledTextbox).toHaveAttribute('contenteditable', 'false');
|
|
2114
|
-
|
|
2115
|
-
// While disabled, focus() is gated: no caret is placed in the editor.
|
|
2116
|
-
ref.current?.focus();
|
|
2117
|
-
await new Promise(resolve => setTimeout(resolve, 0));
|
|
2118
|
-
expect(document.activeElement).toBe(document.body);
|
|
2119
|
-
expect(
|
|
2120
|
-
disabledTextbox.contains(window.getSelection()?.anchorNode ?? null),
|
|
2121
|
-
).toBe(false);
|
|
2122
|
-
|
|
2123
|
-
rerender(<RichTextEditor ref={ref} label="Notes" />);
|
|
2124
|
-
const textbox = screen.getByRole('textbox');
|
|
2125
|
-
// The setEditable sync re-enables the surface, not just its styling.
|
|
2126
|
-
expect(textbox).toHaveAttribute('contenteditable', 'true');
|
|
2127
|
-
|
|
2128
|
-
// Lexical focuses by placing the DOM selection inside the
|
|
2129
|
-
// contenteditable; jsdom does not move document.activeElement with it,
|
|
2130
|
-
// so assert the observable outcome: the caret lands in the textbox.
|
|
2131
|
-
ref.current?.focus();
|
|
2132
|
-
await waitFor(() => {
|
|
2133
|
-
const anchorNode = window.getSelection()?.anchorNode ?? null;
|
|
2134
|
-
expect(anchorNode).not.toBeNull();
|
|
2135
|
-
expect(textbox.contains(anchorNode)).toBe(true);
|
|
2136
|
-
});
|
|
2137
|
-
});
|
|
2138
|
-
});
|
|
2139
|
-
|
|
2140
|
-
describe('read-only keyboard traversal (WCAG 2.1.2)', () => {
|
|
2141
|
-
it('lets Tab leave a read-only editor without the Escape step', async () => {
|
|
2142
|
-
const user = userEvent.setup();
|
|
2143
|
-
render(
|
|
2144
|
-
<>
|
|
2145
|
-
<button type="button">before</button>
|
|
2146
|
-
<RichTextEditor label="Notes" isReadOnly />
|
|
2147
|
-
<button type="button">after</button>
|
|
2148
|
-
</>,
|
|
2149
|
-
);
|
|
2150
|
-
|
|
2151
|
-
screen.getByRole('button', {name: 'before'}).focus();
|
|
2152
|
-
await user.tab();
|
|
2153
|
-
expect(document.activeElement).toBe(screen.getByRole('textbox'));
|
|
2154
|
-
|
|
2155
|
-
// A non-editable editor renders no Tab-escape hint, so there must be no
|
|
2156
|
-
// trap to escape: a bare Tab moves on.
|
|
2157
|
-
await user.tab();
|
|
2158
|
-
expect(document.activeElement).toBe(
|
|
2159
|
-
screen.getByRole('button', {name: 'after'}),
|
|
2160
|
-
);
|
|
2161
|
-
});
|
|
2162
|
-
|
|
2163
|
-
it('skips a disabled editor entirely', async () => {
|
|
2164
|
-
const user = userEvent.setup();
|
|
2165
|
-
render(
|
|
2166
|
-
<>
|
|
2167
|
-
<button type="button">before</button>
|
|
2168
|
-
<RichTextEditor label="Notes" isDisabled />
|
|
2169
|
-
<button type="button">after</button>
|
|
2170
|
-
</>,
|
|
2171
|
-
);
|
|
2172
|
-
screen.getByRole('button', {name: 'before'}).focus();
|
|
2173
|
-
await user.tab();
|
|
2174
|
-
expect(document.activeElement).toBe(
|
|
2175
|
-
screen.getByRole('button', {name: 'after'}),
|
|
2176
|
-
);
|
|
2177
|
-
});
|
|
2178
|
-
});
|
|
2179
|
-
|
|
2180
|
-
describe('toolbar follows the editable props after mount', () => {
|
|
2181
|
-
// The extension's `editable` is applied only when the composer builds the
|
|
2182
|
-
// editor, so without the setEditable sync the toolbar would stay at its
|
|
2183
|
-
// mount state while the wrapper styling and ARIA followed the props.
|
|
2184
|
-
it('re-enables and re-disables the formatting controls on rerender', () => {
|
|
2185
|
-
const {rerender} = render(
|
|
2186
|
-
<RichTextEditor
|
|
2187
|
-
label="Notes"
|
|
2188
|
-
isDisabled
|
|
2189
|
-
toolbar={<RichTextEditorToolbar />}
|
|
2190
|
-
/>,
|
|
2191
|
-
);
|
|
2192
|
-
expect(screen.getByRole('button', {name: 'Bold'})).toBeDisabled();
|
|
2193
|
-
|
|
2194
|
-
rerender(
|
|
2195
|
-
<RichTextEditor label="Notes" toolbar={<RichTextEditorToolbar />} />,
|
|
2196
|
-
);
|
|
2197
|
-
expect(screen.getByRole('button', {name: 'Bold'})).toBeEnabled();
|
|
2198
|
-
|
|
2199
|
-
rerender(
|
|
2200
|
-
<RichTextEditor
|
|
2201
|
-
label="Notes"
|
|
2202
|
-
isReadOnly
|
|
2203
|
-
toolbar={<RichTextEditorToolbar />}
|
|
2204
|
-
/>,
|
|
2205
|
-
);
|
|
2206
|
-
expect(screen.getByRole('button', {name: 'Bold'})).toBeDisabled();
|
|
2207
|
-
});
|
|
2208
|
-
});
|
|
2209
|
-
|
|
2210
|
-
describe('RichTextEditor translation', () => {
|
|
2211
|
-
const FRENCH = {
|
|
2212
|
-
fr: {
|
|
2213
|
-
'@astryx.richTextEditor.toolbar.label': 'Mise en forme',
|
|
2214
|
-
'@astryx.richTextEditor.toolbar.bold': 'Gras',
|
|
2215
|
-
'@astryx.richTextEditor.toolbar.undo': 'Annuler',
|
|
2216
|
-
'@astryx.richTextEditor.toolbar.blockFormat': 'Format du bloc',
|
|
2217
|
-
'@astryx.richTextEditor.tabEscapeHint':
|
|
2218
|
-
'Appuyez sur Échap puis Tab pour quitter l’éditeur.',
|
|
2219
|
-
},
|
|
2220
|
-
};
|
|
2221
|
-
|
|
2222
|
-
it('routes the toolbar and the Tab escape hint through the active locale', () => {
|
|
2223
|
-
render(
|
|
2224
|
-
<InternationalizationProvider locale="fr" overrides={FRENCH}>
|
|
2225
|
-
<RichTextEditor label="Notes" toolbar={<RichTextEditorToolbar />} />
|
|
2226
|
-
</InternationalizationProvider>,
|
|
2227
|
-
);
|
|
2228
|
-
|
|
2229
|
-
expect(
|
|
2230
|
-
screen.getByRole('toolbar', {name: 'Mise en forme'}),
|
|
2231
|
-
).toBeInTheDocument();
|
|
2232
|
-
expect(screen.getByRole('button', {name: 'Gras'})).toBeInTheDocument();
|
|
2233
|
-
expect(screen.getByRole('button', {name: 'Annuler'})).toBeInTheDocument();
|
|
2234
|
-
expect(screen.getByText('Format du bloc')).toBeInTheDocument();
|
|
2235
|
-
expect(screen.getByRole('textbox')).toHaveAccessibleDescription(
|
|
2236
|
-
'Appuyez sur Échap puis Tab pour quitter l’éditeur.',
|
|
2237
|
-
);
|
|
2238
|
-
});
|
|
2239
|
-
|
|
2240
|
-
it('keeps caller-provided text over the translated defaults', () => {
|
|
2241
|
-
render(
|
|
2242
|
-
<InternationalizationProvider locale="fr" overrides={FRENCH}>
|
|
2243
|
-
<RichTextEditor
|
|
2244
|
-
label="Notes"
|
|
2245
|
-
tabEscapeHint="Custom hint"
|
|
2246
|
-
toolbar={<RichTextEditorToolbar label="Custom toolbar" />}
|
|
2247
|
-
/>
|
|
2248
|
-
</InternationalizationProvider>,
|
|
2249
|
-
);
|
|
2250
|
-
|
|
2251
|
-
expect(
|
|
2252
|
-
screen.getByRole('toolbar', {name: 'Custom toolbar'}),
|
|
2253
|
-
).toBeInTheDocument();
|
|
2254
|
-
expect(screen.getByRole('textbox')).toHaveAccessibleDescription(
|
|
2255
|
-
'Custom hint',
|
|
2256
|
-
);
|
|
2257
|
-
});
|
|
2258
|
-
});
|