-
Notifications
You must be signed in to change notification settings - Fork 84
/
Copy pathtext-editor.test.tsx
570 lines (485 loc) · 18 KB
/
text-editor.test.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
import { render, screen, within } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import React from "react";
import TextEditor, { createEmpty, createFromHTML } from ".";
import { COMPONENT_PREFIX } from "./__internal__/constants";
/**
* Mock the OnChangePlugin whilst testing the full editor. This is to prevent
* the editor from attempting to repeatedly create update listeners when the
* tests are run, which causes errors to be thrown by Jest.
*
* The onChange prop is tested in the OnChangePlugin tests.
*/
jest.mock("./__internal__/plugins/OnChange/on-change.plugin", () => {
return jest.fn().mockReturnValue(null);
});
// Reusable JSON object for testing the default state
const initialValue = {
root: {
children: [
{
children: [
{
detail: 0,
format: 0,
mode: "normal",
style: "",
text: "Sample text",
type: "text",
version: 1,
},
],
direction: "ltr",
format: "",
indent: 0,
type: "paragraph",
version: 1,
textFormat: 0,
textStyle: "",
},
],
direction: "ltr",
format: "",
indent: 0,
type: "root",
version: 1,
},
};
test("rendering and basic functionality", async () => {
const user = userEvent.setup();
const mockCancel = jest.fn();
const mockSave = jest.fn();
const value = JSON.stringify(initialValue);
// render the TextEditor component
render(
<TextEditor
labelText="Example"
onCancel={() => mockCancel()}
onSave={() => mockSave()}
value={value}
characterLimit={20}
/>,
);
// expect the editor to be rendered with the default value
expect(screen.getByText("Sample text")).toBeInTheDocument();
// Click the editor space and send a few key presses
const editor = screen.getByRole(`textbox`);
await user.click(editor);
await user.keyboard(" abc");
// expect the edited value to be visible
expect(screen.getByText("Sample text abc")).toBeInTheDocument();
// expect the label to be rendered
expect(screen.getByText("Example")).toBeInTheDocument();
// expect the toolbar to be rendered
expect(screen.getByTestId(`${COMPONENT_PREFIX}-toolbar`)).toBeInTheDocument();
// expect the character counter to be rendered
const characterCounter = screen.getByTestId(
`${COMPONENT_PREFIX}-character-limit`,
);
expect(characterCounter).toBeInTheDocument();
expect(characterCounter).toHaveTextContent("5 characters remaining");
await user.click(editor);
await user.keyboard("defghijklmnopqrstuvwxyz");
expect(characterCounter).toHaveTextContent("0 characters remaining");
// highlight all of the text and click the bold button
await user.tripleClick(editor);
const boldButton = screen.getByTestId(`${COMPONENT_PREFIX}-bold-button`);
await user.click(boldButton);
// expect the text to be bold
expect(
screen.getByText("Sample text abcdefghijklmnopqrstuvwxyz"),
).toHaveStyle("font-weight: bold");
// click the bold button again and expect the text to be normal
await user.click(boldButton);
expect(
screen.getByText("Sample text abcdefghijklmnopqrstuvwxyz"),
).not.toHaveStyle("font-weight: bold");
// click the italic button
const italicButton = screen.getByTestId(`${COMPONENT_PREFIX}-italic-button`);
await user.click(italicButton);
// expect the text to be italic
expect(
screen.getByText("Sample text abcdefghijklmnopqrstuvwxyz"),
).toHaveStyle("font-style: italic");
// click the italic button again and expect the text to be normal
await user.click(italicButton);
expect(
screen.getByText("Sample text abcdefghijklmnopqrstuvwxyz"),
).not.toHaveStyle("font-style: italic");
// click the ordered list button
const olButton = screen.getByTestId(
`${COMPONENT_PREFIX}-ordered-list-button`,
);
await user.click(olButton);
// Variable to store list for checks
let list;
// expect the text to be in an ordered list
list = screen.getByRole("list");
expect(list).toBeInstanceOf(HTMLOListElement);
expect(
within(list).getByText("Sample text abcdefghijklmnopqrstuvwxyz"),
).toBeInTheDocument();
// click the ordered list button again and expect the text to be normal
await user.click(olButton);
expect(list).not.toBeInTheDocument();
// click the unordered list button
const ulButton = screen.getByTestId(
`${COMPONENT_PREFIX}-unordered-list-button`,
);
await user.click(ulButton);
// expect the text to be in an unordered list
list = screen.getByRole("list");
expect(list).toBeInstanceOf(HTMLUListElement);
expect(
within(list).getByText("Sample text abcdefghijklmnopqrstuvwxyz"),
).toBeInTheDocument();
// click the unordered list button again and expect the text to be normal
await user.click(ulButton);
expect(list).not.toBeInTheDocument();
// get both command buttons
const cancelButton = screen.getByText("Cancel");
const saveButton = screen.getByText("Save");
// click each button and expect the respective callback to be called
await user.click(saveButton);
expect(mockSave).toHaveBeenCalledTimes(1);
await user.click(cancelButton);
expect(mockCancel).toHaveBeenCalledTimes(1);
// expect the text to have been reset to the default value because of the above Cancel click
expect(screen.getByText("Sample text")).toBeInTheDocument();
});
test("input hint renders correctly when inputHint prop is provided", () => {
// render the TextEditor component with an input hint
render(<TextEditor inputHint="This is an input hint" labelText="Example" />);
// expect the input hint to be rendered
expect(screen.getByText("This is an input hint")).toBeInTheDocument();
});
test("character limit renders correctly when characterLimit prop is provided", () => {
// render the TextEditor component with a character limit
render(<TextEditor characterLimit={100} labelText="Example" />);
// expect the character counter to be rendered
const characterCounter = screen.getByTestId(
`${COMPONENT_PREFIX}-character-limit`,
);
expect(characterCounter).toBeInTheDocument();
expect(characterCounter).toHaveTextContent("100 characters remaining");
});
test("character limit is not rendered when characterLimit prop is provided with a value of 0", () => {
// render the TextEditor component with a character limit
render(<TextEditor characterLimit={0} labelText="Example" />);
// expect the character counter to be rendered
const characterCounter = screen.queryByTestId(
`${COMPONENT_PREFIX}-character-limit`,
);
expect(characterCounter).not.toBeInTheDocument();
});
test("required prop renders correctly when required prop is provided", () => {
// render the TextEditor component with the required prop
render(<TextEditor labelText="Example" required />);
const label = screen.getByText("Example");
// expect the required indicator to be rendered
expect(label).toHaveStyleRule("content", '"*"', {
modifier: "::after",
});
});
test("optional prop renders correctly when optional prop is provided", () => {
// render the TextEditor component with the optional prop
render(<TextEditor labelText="Example" isOptional />);
const label = screen.getByTestId("label-container");
// expect the optional indicator to be rendered
expect(label).toHaveStyleRule("content", '"(optional)"', {
modifier: "::after",
});
});
test("placeholder prop renders correctly when placeholder prop is provided", () => {
// render the TextEditor component with a placeholder
render(
<TextEditor labelText="Example" placeholder="This is a nice placeholder" />,
);
// expect the placeholder to be rendered
expect(screen.getByText("This is a nice placeholder")).toBeInTheDocument();
});
test("rows prop renders correctly when rows prop is provided", () => {
// render the TextEditor component with a rows prop
render(<TextEditor labelText="Example" rows={20} />);
// expect the editor to have the correct number of rows
const editor = screen.getByTestId(`${COMPONENT_PREFIX}-editable`);
expect(editor).toHaveStyle("min-height: 420px");
});
test("validation renders correctly when error prop is provided", () => {
// render the TextEditor component with an error
render(<TextEditor error="This is an error" labelText="Example" />);
const errorMessage = screen.getByText("This is an error");
// expect the error message to be rendered
expect(errorMessage).toBeInTheDocument();
expect(errorMessage).toHaveStyle("color: var(--colorsSemanticNegative500)");
});
test("validation renders correctly when warning prop is provided", () => {
// render the TextEditor component with an error
render(<TextEditor warning="This is a warning" labelText="Example" />);
const warningMessage = screen.getByText("This is a warning");
// expect the warning message to be rendered
expect(warningMessage).toBeInTheDocument();
expect(warningMessage).toHaveStyle("color: var(--colorsSemanticCaution500)");
});
test("serialisation of editor", async () => {
const user = userEvent.setup();
const mockSave = jest.fn();
render(
<TextEditor
labelText="Text Editor"
onSave={(values) => mockSave(values)}
value={JSON.stringify(initialValue)}
/>,
);
const saveButton = screen.getByText("Save");
// click the save button and expect the callback to be called
await user.click(saveButton);
expect(mockSave).toHaveBeenCalledTimes(1);
});
test("valid data is parsed when HTML is passed into the createFromHTML function", async () => {
const html = `<p dir="ltr"><span style="white-space: pre-wrap;">This is a HTML example.</span></p><ol><li value="1"><span style="white-space: pre-wrap;">Look, it has lists!</span></li></ol>`;
const value = createFromHTML(html);
expect(JSON.parse(value)).toEqual({
root: {
children: [
{
children: [
{
detail: 0,
format: 0,
mode: "normal",
style: "",
text: "This is a HTML example.",
type: "text",
version: 1,
},
],
direction: null,
format: "",
indent: 0,
textFormat: 0,
textStyle: "",
type: "paragraph",
version: 1,
},
{
children: [
{
children: [
{
detail: 0,
format: 0,
mode: "normal",
style: "",
text: "Look, it has lists!",
type: "text",
version: 1,
},
],
direction: null,
format: "",
indent: 0,
type: "listitem",
value: 1,
version: 1,
},
],
direction: null,
format: "",
indent: 0,
listType: "number",
start: 1,
tag: "ol",
type: "list",
version: 1,
},
],
direction: null,
format: "",
indent: 0,
type: "root",
version: 1,
},
});
});
test("valid state is created when the CreateEmpty function is called", async () => {
const value = createEmpty();
expect(JSON.parse(value)).toEqual({
root: {
children: [
{
children: [],
direction: null,
format: "",
indent: 0,
type: "paragraph",
version: 1,
},
],
direction: null,
format: "",
indent: 0,
type: "root",
version: 1,
},
});
render(<TextEditor labelText="Text Editor" value={value} />);
expect(screen.getByRole("textbox")).toBeInTheDocument();
expect(screen.getByRole("textbox")).toHaveTextContent("");
});
test("previews are rendered correctly if provided", () => {
const previews = [<div key="preview-1">Preview 1</div>];
render(<TextEditor previews={previews} labelText="Example" />);
const preview = screen.getByText("Preview 1");
// expect the preview to be rendered
expect(preview).toBeInTheDocument();
});
test("no previews are rendered if the prop is not provided", () => {
render(<TextEditor labelText="Example" />);
const preview = screen.queryByText("Preview 1");
// expect the preview not to be rendered
expect(preview).not.toBeInTheDocument();
});
test("should reset the content to the default if the Cancel button is pressed", async () => {
const user = userEvent.setup();
const mockCancel = jest.fn();
const mockSave = jest.fn();
const value = JSON.stringify(initialValue);
// render the TextEditor component
render(
<TextEditor
labelText="Example"
onCancel={() => mockCancel()}
onSave={() => mockSave()}
value={value}
/>,
);
// Click the editor space and send a few key presses
const editor = screen.getByRole(`textbox`);
await user.click(editor);
await user.keyboard(" abc");
// expect the edited value to be visible
expect(screen.getByText("Sample text abc")).toBeInTheDocument();
// Click the cancel button
const cancelButton = screen.getByText("Cancel");
await user.click(cancelButton);
// expect the editor to be rendered with the default value
expect(screen.getByText("Sample text")).toBeInTheDocument();
});
test("readOnly prop renders correctly when readOnly prop is provided", () => {
// render the TextEditor component with the readOnly prop
render(<TextEditor labelText="Example" readOnly />);
// expect the editor to be read-only
const editor = screen.getByTestId(`${COMPONENT_PREFIX}-editable`);
expect(editor).toHaveAttribute("contenteditable", "false");
});
test("should toggle the list type when a list is active and the alternate list type is clicked", async () => {
const user = userEvent.setup();
const value = createFromHTML(
`<ul><li value="1"><span style="white-space: pre-wrap;">Example List</span></li></ul>`,
);
// render the TextEditor component
render(<TextEditor labelText="Example" namespace="test" value={value} />);
const olButton = screen.getByTestId(`test-ordered-list-button`);
const ulButton = screen.getByTestId(`test-unordered-list-button`);
expect(olButton).toBeInTheDocument();
expect(ulButton).toBeInTheDocument();
expect(screen.getByRole("list")).toBeInTheDocument();
expect(screen.getByRole("list").tagName).toBe("UL");
const listText = screen.getByText("Example List");
await user.click(listText);
await user.click(olButton);
expect(screen.getByRole("list").tagName).toBe("OL");
await user.click(listText);
await user.click(ulButton);
expect(screen.getByRole("list").tagName).toBe("UL");
});
test("should toggle the an indiviual list item's type when a list is active and the alternate list type is clicked", async () => {
const user = userEvent.setup();
const value = createFromHTML(
`<ul><li value="1"><span style="white-space: pre-wrap;">Example List</span></li><li value="2"><span style="white-space: pre-wrap;">Change Me</span></li><li value="3"><span style="white-space: pre-wrap;">Example List</span></li></ul>`,
);
// render the TextEditor component
render(<TextEditor labelText="Example" namespace="test" value={value} />);
const olButton = screen.getByTestId(`test-ordered-list-button`);
const ulButton = screen.getByTestId(`test-unordered-list-button`);
expect(olButton).toBeInTheDocument();
expect(ulButton).toBeInTheDocument();
expect(screen.getByRole("list")).toBeInTheDocument();
expect(screen.getByRole("list").tagName).toBe("UL");
const listText = screen.getByText("Change Me");
await user.click(listText);
await user.click(olButton);
expect(screen.queryAllByRole("list").length).toBe(3);
await user.click(listText);
await user.click(ulButton);
expect(screen.queryAllByRole("list").length).toBe(1);
});
describe("shortcut keys", () => {
it("should toggle bold text when the bold shortcut is pressed", async () => {
const user = userEvent.setup();
const mockCancel = jest.fn();
const mockSave = jest.fn();
const value = JSON.stringify(initialValue);
// render the TextEditor component
render(
<TextEditor
labelText="Example"
onCancel={() => mockCancel()}
onSave={() => mockSave()}
value={value}
characterLimit={20}
/>,
);
// Click the editor space and send a few key presses
const editor = screen.getByRole(`textbox`);
await user.click(editor);
await user.keyboard(" not bold");
// expect the edited value to be visible
expect(screen.getByText("Sample text not bold")).toBeInTheDocument();
await user.tripleClick(editor);
await user.keyboard(`{Control>}b{/Control>}`);
// expect the text to be bold
expect(screen.getByText("Sample text not bold")).toHaveStyle(
"font-weight: bold",
);
await user.keyboard(`{Control>}b{/Control>}`);
// expect the text to be normal
expect(screen.getByText("Sample text not bold")).not.toHaveStyle(
"font-weight: bold",
);
});
it("should toggle italic text when the italic shortcut is pressed", async () => {
const user = userEvent.setup();
const mockCancel = jest.fn();
const mockSave = jest.fn();
const value = JSON.stringify(initialValue);
// render the TextEditor component
render(
<TextEditor
labelText="Example"
onCancel={() => mockCancel()}
onSave={() => mockSave()}
value={value}
characterLimit={20}
/>,
);
// Click the editor space and send a few key presses
const editor = screen.getByRole(`textbox`);
await user.click(editor);
await user.keyboard(" not italic");
// expect the edited value to be visible
expect(screen.getByText("Sample text not italic")).toBeInTheDocument();
await user.tripleClick(editor);
await user.keyboard(`{Control>}i{/Control>}`);
// expect the text to be bold
expect(screen.getByText("Sample text not italic")).toHaveStyle(
"font-style: italic",
);
await user.keyboard(`{Control>}i{/Control>}`);
// expect the text to be normal
expect(screen.getByText("Sample text not italic")).not.toHaveStyle(
"font-style: italic",
);
});
});