@contentful/field-editor-rich-text 4.18.0 → 4.19.1

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.
@@ -0,0 +1,607 @@
1
+ import { BLOCKS, INLINES } from '@contentful/rich-text-types';
2
+ import { block, document, inline, text } from '../nodeFactory';
3
+ import { toSlateDoc } from '../toSlateDoc';
4
+ describe('toSlateDoc', ()=>{
5
+ const cases = [
6
+ {
7
+ title: 'undefined documents',
8
+ input: undefined,
9
+ expected: [
10
+ {
11
+ type: 'paragraph',
12
+ children: [
13
+ {
14
+ text: ''
15
+ }
16
+ ],
17
+ data: {},
18
+ isVoid: false
19
+ }
20
+ ]
21
+ },
22
+ {
23
+ title: 'empty documents',
24
+ input: document(),
25
+ expected: [
26
+ {
27
+ type: 'paragraph',
28
+ children: [
29
+ {
30
+ text: ''
31
+ }
32
+ ],
33
+ data: {},
34
+ isVoid: false
35
+ }
36
+ ]
37
+ },
38
+ {
39
+ title: 'empty hyperlinks',
40
+ input: document(block(BLOCKS.PARAGRAPH, {}, inline(INLINES.HYPERLINK), inline(INLINES.HYPERLINK, {}, text('')))),
41
+ expected: [
42
+ {
43
+ type: 'paragraph',
44
+ children: [
45
+ {
46
+ text: ''
47
+ }
48
+ ],
49
+ data: {},
50
+ isVoid: false
51
+ }
52
+ ]
53
+ },
54
+ {
55
+ title: 'adjacent text nodes with identical marks',
56
+ input: document(block(BLOCKS.PARAGRAPH, {}, text('This '), text(''), text('should be one '), text('text node.'), text(' but this is unique.', [
57
+ {
58
+ type: 'bold'
59
+ }
60
+ ]), text(' and this should be merged ', [
61
+ {
62
+ type: 'italic'
63
+ }
64
+ ]), text('with this.', [
65
+ {
66
+ type: 'italic'
67
+ }
68
+ ]))),
69
+ expected: [
70
+ {
71
+ type: 'paragraph',
72
+ children: [
73
+ {
74
+ text: 'This should be one text node.',
75
+ data: {}
76
+ },
77
+ {
78
+ text: ' but this is unique.',
79
+ bold: true,
80
+ data: {}
81
+ },
82
+ {
83
+ text: ' and this should be merged with this.',
84
+ italic: true,
85
+ data: {}
86
+ }
87
+ ],
88
+ data: {},
89
+ isVoid: false
90
+ }
91
+ ]
92
+ },
93
+ {
94
+ title: 'inlines without surrounding text',
95
+ input: document(block(BLOCKS.PARAGRAPH, {}, inline(INLINES.EMBEDDED_ENTRY), inline(INLINES.EMBEDDED_RESOURCE))),
96
+ expected: [
97
+ {
98
+ type: 'paragraph',
99
+ children: [
100
+ {
101
+ text: ''
102
+ },
103
+ {
104
+ type: INLINES.EMBEDDED_ENTRY,
105
+ data: {},
106
+ children: [
107
+ {
108
+ text: ''
109
+ }
110
+ ],
111
+ isVoid: true
112
+ },
113
+ {
114
+ text: ''
115
+ },
116
+ {
117
+ type: INLINES.EMBEDDED_RESOURCE,
118
+ data: {},
119
+ children: [
120
+ {
121
+ text: ''
122
+ }
123
+ ],
124
+ isVoid: true
125
+ },
126
+ {
127
+ text: ''
128
+ }
129
+ ],
130
+ data: {},
131
+ isVoid: false
132
+ }
133
+ ]
134
+ },
135
+ {
136
+ title: 'uneven tables',
137
+ input: document(block('table', {}, block('table-row', {}, block('table-header-cell', {}, block('paragraph', {}, text('cell #1')))), block('table-row', {}, block('table-cell', {}, block('paragraph', {}, text('cell #1'))), block('table-cell', {}, block('paragraph', {}, text('cell #2'))), block('table-cell', {}, block('paragraph', {}, text('cell #3')))), block('table-row', {}, block('table-cell', {}, block('paragraph', {}, text('cell #1'))), block('table-cell', {}, block('paragraph', {}, text('cell #2')))))),
138
+ expected: [
139
+ {
140
+ type: 'table',
141
+ data: {},
142
+ children: [
143
+ {
144
+ type: 'table-row',
145
+ data: {},
146
+ children: [
147
+ {
148
+ type: 'table-header-cell',
149
+ data: {},
150
+ children: [
151
+ {
152
+ type: 'paragraph',
153
+ data: {},
154
+ children: [
155
+ {
156
+ text: 'cell #1',
157
+ data: {}
158
+ }
159
+ ],
160
+ isVoid: false
161
+ }
162
+ ],
163
+ isVoid: false
164
+ },
165
+ {
166
+ type: 'table-header-cell',
167
+ data: {},
168
+ children: [
169
+ {
170
+ type: 'paragraph',
171
+ data: {},
172
+ children: [
173
+ {
174
+ text: ''
175
+ }
176
+ ],
177
+ isVoid: false
178
+ }
179
+ ],
180
+ isVoid: false
181
+ },
182
+ {
183
+ type: 'table-header-cell',
184
+ data: {},
185
+ children: [
186
+ {
187
+ type: 'paragraph',
188
+ data: {},
189
+ children: [
190
+ {
191
+ text: ''
192
+ }
193
+ ],
194
+ isVoid: false
195
+ }
196
+ ],
197
+ isVoid: false
198
+ }
199
+ ],
200
+ isVoid: false
201
+ },
202
+ {
203
+ type: 'table-row',
204
+ data: {},
205
+ children: [
206
+ {
207
+ type: 'table-cell',
208
+ data: {},
209
+ children: [
210
+ {
211
+ type: 'paragraph',
212
+ data: {},
213
+ children: [
214
+ {
215
+ text: 'cell #1',
216
+ data: {}
217
+ }
218
+ ],
219
+ isVoid: false
220
+ }
221
+ ],
222
+ isVoid: false
223
+ },
224
+ {
225
+ type: 'table-cell',
226
+ data: {},
227
+ children: [
228
+ {
229
+ type: 'paragraph',
230
+ data: {},
231
+ children: [
232
+ {
233
+ text: 'cell #2',
234
+ data: {}
235
+ }
236
+ ],
237
+ isVoid: false
238
+ }
239
+ ],
240
+ isVoid: false
241
+ },
242
+ {
243
+ type: 'table-cell',
244
+ data: {},
245
+ children: [
246
+ {
247
+ type: 'paragraph',
248
+ data: {},
249
+ children: [
250
+ {
251
+ text: 'cell #3',
252
+ data: {}
253
+ }
254
+ ],
255
+ isVoid: false
256
+ }
257
+ ],
258
+ isVoid: false
259
+ }
260
+ ],
261
+ isVoid: false
262
+ },
263
+ {
264
+ type: 'table-row',
265
+ data: {},
266
+ children: [
267
+ {
268
+ type: 'table-cell',
269
+ data: {},
270
+ children: [
271
+ {
272
+ type: 'paragraph',
273
+ data: {},
274
+ children: [
275
+ {
276
+ text: 'cell #1',
277
+ data: {}
278
+ }
279
+ ],
280
+ isVoid: false
281
+ }
282
+ ],
283
+ isVoid: false
284
+ },
285
+ {
286
+ type: 'table-cell',
287
+ data: {},
288
+ children: [
289
+ {
290
+ type: 'paragraph',
291
+ data: {},
292
+ children: [
293
+ {
294
+ text: 'cell #2',
295
+ data: {}
296
+ }
297
+ ],
298
+ isVoid: false
299
+ }
300
+ ],
301
+ isVoid: false
302
+ },
303
+ {
304
+ type: 'table-cell',
305
+ data: {},
306
+ children: [
307
+ {
308
+ type: 'paragraph',
309
+ data: {},
310
+ children: [
311
+ {
312
+ text: ''
313
+ }
314
+ ],
315
+ isVoid: false
316
+ }
317
+ ],
318
+ isVoid: false
319
+ }
320
+ ],
321
+ isVoid: false
322
+ }
323
+ ],
324
+ isVoid: false
325
+ },
326
+ {
327
+ type: 'paragraph',
328
+ data: {},
329
+ children: [
330
+ {
331
+ text: ''
332
+ }
333
+ ],
334
+ isVoid: false
335
+ }
336
+ ]
337
+ },
338
+ {
339
+ title: 'empty blockquotes',
340
+ input: document(block(BLOCKS.QUOTE, {})),
341
+ expected: [
342
+ {
343
+ type: 'blockquote',
344
+ data: {},
345
+ children: [
346
+ {
347
+ type: 'paragraph',
348
+ data: {},
349
+ children: [
350
+ {
351
+ text: ''
352
+ }
353
+ ],
354
+ isVoid: false
355
+ }
356
+ ],
357
+ isVoid: false
358
+ },
359
+ {
360
+ type: 'paragraph',
361
+ data: {},
362
+ children: [
363
+ {
364
+ text: ''
365
+ }
366
+ ],
367
+ isVoid: false
368
+ }
369
+ ]
370
+ },
371
+ {
372
+ title: 'empty list items',
373
+ input: document(block(BLOCKS.LIST_ITEM, {})),
374
+ expected: [
375
+ {
376
+ type: 'list-item',
377
+ data: {},
378
+ children: [
379
+ {
380
+ type: 'paragraph',
381
+ data: {},
382
+ children: [
383
+ {
384
+ text: ''
385
+ }
386
+ ],
387
+ isVoid: false
388
+ }
389
+ ],
390
+ isVoid: false
391
+ },
392
+ {
393
+ type: 'paragraph',
394
+ data: {},
395
+ children: [
396
+ {
397
+ text: ''
398
+ }
399
+ ],
400
+ isVoid: false
401
+ }
402
+ ]
403
+ },
404
+ {
405
+ title: 'empty lists',
406
+ input: document(block(BLOCKS.UL_LIST, {}), block(BLOCKS.OL_LIST, {})),
407
+ expected: [
408
+ {
409
+ type: 'unordered-list',
410
+ data: {},
411
+ children: [
412
+ {
413
+ type: 'list-item',
414
+ data: {},
415
+ children: [
416
+ {
417
+ type: 'paragraph',
418
+ data: {},
419
+ children: [
420
+ {
421
+ text: ''
422
+ }
423
+ ],
424
+ isVoid: false
425
+ }
426
+ ],
427
+ isVoid: false
428
+ }
429
+ ],
430
+ isVoid: false
431
+ },
432
+ {
433
+ type: 'ordered-list',
434
+ data: {},
435
+ children: [
436
+ {
437
+ type: 'list-item',
438
+ data: {},
439
+ children: [
440
+ {
441
+ type: 'paragraph',
442
+ data: {},
443
+ children: [
444
+ {
445
+ text: ''
446
+ }
447
+ ],
448
+ isVoid: false
449
+ }
450
+ ],
451
+ isVoid: false
452
+ }
453
+ ],
454
+ isVoid: false
455
+ },
456
+ {
457
+ type: 'paragraph',
458
+ data: {},
459
+ children: [
460
+ {
461
+ text: ''
462
+ }
463
+ ],
464
+ isVoid: false
465
+ }
466
+ ]
467
+ },
468
+ {
469
+ title: 'list of embedded entry blocks',
470
+ input: document(block(BLOCKS.EMBEDDED_ENTRY, {
471
+ target: {
472
+ sys: {
473
+ id: '4jo9APjyJs31WugGbV0E42',
474
+ type: 'Link',
475
+ linkType: 'Entry'
476
+ }
477
+ }
478
+ }), block(BLOCKS.EMBEDDED_ENTRY, {
479
+ target: {
480
+ sys: {
481
+ id: '2bwp9MdklBd4WVLGm1Fngy',
482
+ type: 'Link',
483
+ linkType: 'Entry'
484
+ }
485
+ }
486
+ }), block(BLOCKS.EMBEDDED_ENTRY, {
487
+ target: {
488
+ sys: {
489
+ id: '73qAHseh6G40k7ndmON5OD',
490
+ type: 'Link',
491
+ linkType: 'Entry'
492
+ }
493
+ }
494
+ }), block(BLOCKS.EMBEDDED_ENTRY, {
495
+ target: {
496
+ sys: {
497
+ id: '7FYYiCprd5VwItrHH3nNJj',
498
+ type: 'Link',
499
+ linkType: 'Entry'
500
+ }
501
+ }
502
+ }), block(BLOCKS.PARAGRAPH, {}, text('')), block(BLOCKS.PARAGRAPH, {}, text(''))),
503
+ expected: [
504
+ {
505
+ type: 'embedded-entry-block',
506
+ children: [
507
+ {
508
+ text: ''
509
+ }
510
+ ],
511
+ data: {
512
+ target: {
513
+ sys: {
514
+ id: '4jo9APjyJs31WugGbV0E42',
515
+ type: 'Link',
516
+ linkType: 'Entry'
517
+ }
518
+ }
519
+ },
520
+ isVoid: true
521
+ },
522
+ {
523
+ type: 'embedded-entry-block',
524
+ children: [
525
+ {
526
+ text: ''
527
+ }
528
+ ],
529
+ data: {
530
+ target: {
531
+ sys: {
532
+ id: '2bwp9MdklBd4WVLGm1Fngy',
533
+ type: 'Link',
534
+ linkType: 'Entry'
535
+ }
536
+ }
537
+ },
538
+ isVoid: true
539
+ },
540
+ {
541
+ type: 'embedded-entry-block',
542
+ children: [
543
+ {
544
+ text: ''
545
+ }
546
+ ],
547
+ data: {
548
+ target: {
549
+ sys: {
550
+ id: '73qAHseh6G40k7ndmON5OD',
551
+ type: 'Link',
552
+ linkType: 'Entry'
553
+ }
554
+ }
555
+ },
556
+ isVoid: true
557
+ },
558
+ {
559
+ type: 'embedded-entry-block',
560
+ children: [
561
+ {
562
+ text: ''
563
+ }
564
+ ],
565
+ data: {
566
+ target: {
567
+ sys: {
568
+ id: '7FYYiCprd5VwItrHH3nNJj',
569
+ type: 'Link',
570
+ linkType: 'Entry'
571
+ }
572
+ }
573
+ },
574
+ isVoid: true
575
+ },
576
+ {
577
+ type: 'paragraph',
578
+ children: [
579
+ {
580
+ text: '',
581
+ data: {}
582
+ }
583
+ ],
584
+ data: {},
585
+ isVoid: false
586
+ },
587
+ {
588
+ type: 'paragraph',
589
+ children: [
590
+ {
591
+ text: '',
592
+ data: {}
593
+ }
594
+ ],
595
+ data: {},
596
+ isVoid: false
597
+ }
598
+ ]
599
+ }
600
+ ];
601
+ cases.forEach(({ title, input, expected })=>{
602
+ it(`should normalize ${title}`, ()=>{
603
+ const out = toSlateDoc(input);
604
+ expect(out).toEqual(expected);
605
+ });
606
+ });
607
+ });