@@ -19,7 +19,8 @@ import turnOffDefaultErrorCatching from './_utils/turnoffdefaulterrorcatching.js
19
19
describe ( 'useMultiRootEditor' , ( ) => {
20
20
const rootsContent = {
21
21
intro : '<h2>Sample</h2><p>This is an instance of the.</p>' ,
22
- content : '<p>It is the custom content</p>'
22
+ content : '<p>It is the custom content</p>' ,
23
+ footer : '<p>Footer content</p>'
23
24
} ;
24
25
25
26
const rootsAttributes = {
@@ -30,6 +31,10 @@ describe( 'useMultiRootEditor', () => {
30
31
content : {
31
32
row : '1' ,
32
33
order : 20
34
+ } ,
35
+ footer : {
36
+ row : '2' ,
37
+ order : 30
33
38
}
34
39
} ;
35
40
@@ -232,7 +237,7 @@ describe( 'useMultiRootEditor', () => {
232
237
const { data, editableElements } = result . current ;
233
238
234
239
expect ( data ) . to . deep . equal ( rootsContent ) ;
235
- expect ( editableElements . length ) . to . equal ( 2 ) ;
240
+ expect ( editableElements . length ) . to . equal ( 3 ) ;
236
241
} ) ;
237
242
238
243
it ( 'should update the editor data when the state has been changed' , async ( ) => {
@@ -254,8 +259,64 @@ describe( 'useMultiRootEditor', () => {
254
259
255
260
expect ( spy ) . toHaveBeenCalledOnce ( ) ;
256
261
expect ( data . intro ) . to . equal ( '<p>New data</p>' ) ;
257
- expect ( editableElements . length ) . to . equal ( 2 ) ;
262
+ expect ( data . content ) . to . equal ( rootsContent . content ) ;
263
+ expect ( data . footer ) . to . equal ( rootsContent . footer ) ;
264
+ expect ( spy . mock . calls [ 0 ] [ 0 ] ) . to . deep . equal ( { 'intro' : 'New data' } ) ;
265
+ expect ( editableElements . length ) . to . equal ( 3 ) ;
258
266
expect ( editor ! . getFullData ( ) . intro ) . to . equal ( '<p>New data</p>' ) ;
267
+ expect ( editor ! . getFullData ( ) . content ) . to . equal ( rootsContent . content ) ;
268
+ expect ( editor ! . getFullData ( ) . footer ) . to . equal ( rootsContent . footer ) ;
269
+ } ) ;
270
+ } ) ;
271
+
272
+ it ( 'should update only editor roots which content have been changed' , async ( ) => {
273
+ const { result } = renderHook ( ( ) => useMultiRootEditor ( editorProps ) ) ;
274
+
275
+ await waitFor ( ( ) => {
276
+ expect ( result . current . editor ) . to . be . instanceof ( TestMultiRootEditor ) ;
277
+ } ) ;
278
+
279
+ const { editor, setData } = result . current ;
280
+ const spy = vi . spyOn ( editor ! . data , 'set' ) ;
281
+
282
+ act ( ( ) => {
283
+ setData ( { ...rootsContent , 'intro' : '<h2>Sample</h2>' , 'footer' : 'Text...' } ) ;
284
+ } ) ;
285
+
286
+ await waitFor ( ( ) => {
287
+ const { data, editableElements } = result . current ;
288
+
289
+ expect ( spy ) . toHaveBeenCalledOnce ( ) ;
290
+ expect ( data . intro ) . to . equal ( '<h2>Sample</h2>' ) ;
291
+ expect ( data . footer ) . to . equal ( '<p>Text...</p>' ) ;
292
+ expect ( spy . mock . calls [ 0 ] [ 0 ] ) . to . deep . equal ( { 'intro' : '<h2>Sample</h2>' , 'footer' : 'Text...' } ) ;
293
+ expect ( editableElements . length ) . to . equal ( 3 ) ;
294
+ expect ( editor ! . getFullData ( ) . intro ) . to . equal ( '<h2>Sample</h2>' ) ;
295
+ expect ( editor ! . getFullData ( ) . footer ) . to . equal ( '<p>Text...</p>' ) ;
296
+ } ) ;
297
+ } ) ;
298
+
299
+ it ( 'should not update editor roots when no content have been changed' , async ( ) => {
300
+ const { result } = renderHook ( ( ) => useMultiRootEditor ( editorProps ) ) ;
301
+
302
+ await waitFor ( ( ) => {
303
+ expect ( result . current . editor ) . to . be . instanceof ( TestMultiRootEditor ) ;
304
+ } ) ;
305
+
306
+ const { editor, setData } = result . current ;
307
+ const spy = vi . spyOn ( editor ! . data , 'set' ) ;
308
+
309
+ act ( ( ) => {
310
+ setData ( { ...rootsContent , 'intro' : '<h2>Sample</h2><p>This is an instance of the.</p>' } ) ;
311
+ } ) ;
312
+
313
+ await waitFor ( ( ) => {
314
+ const { data, editableElements } = result . current ;
315
+
316
+ expect ( spy . mock . calls . length ) . to . equal ( 0 ) ;
317
+ expect ( data ) . to . deep . equal ( rootsContent ) ;
318
+ expect ( editableElements . length ) . to . equal ( 3 ) ;
319
+ expect ( editor ! . getFullData ( ) ) . to . deep . equal ( rootsContent ) ;
259
320
} ) ;
260
321
} ) ;
261
322
@@ -281,7 +342,7 @@ describe( 'useMultiRootEditor', () => {
281
342
282
343
expect ( spy ) . toHaveBeenCalledOnce ( ) ;
283
344
expect ( data . intro ) . to . be . undefined ;
284
- expect ( editableElements . length ) . to . equal ( 1 ) ;
345
+ expect ( editableElements . length ) . to . equal ( 2 ) ;
285
346
expect ( editor ! . getFullData ( ) . intro ) . to . be . undefined ;
286
347
} ) ;
287
348
} ) ;
@@ -306,7 +367,7 @@ describe( 'useMultiRootEditor', () => {
306
367
307
368
expect ( spy ) . toHaveBeenCalledOnce ( ) ;
308
369
expect ( data . outro ) . to . be . equal ( '<p>New data</p>' ) ;
309
- expect ( editableElements . length ) . to . equal ( 3 ) ;
370
+ expect ( editableElements . length ) . to . equal ( 4 ) ;
310
371
expect ( editor ! . getFullData ( ) . outro ) . to . be . equal ( '<p>New data</p>' ) ;
311
372
} ) ;
312
373
} ) ;
@@ -328,7 +389,7 @@ describe( 'useMultiRootEditor', () => {
328
389
const { data, editableElements } = result . current ;
329
390
330
391
expect ( data . intro ) . to . equal ( '<p>New data</p>' ) ;
331
- expect ( editableElements . length ) . to . equal ( 2 ) ;
392
+ expect ( editableElements . length ) . to . equal ( 3 ) ;
332
393
expect ( editor ! . getFullData ( ) . intro ) . to . equal ( '<p>New data</p>' ) ;
333
394
} ) ;
334
395
} ) ;
@@ -354,7 +415,7 @@ describe( 'useMultiRootEditor', () => {
354
415
expect ( spy . mock . calls . length ) . to . equal ( editableElements . length ) ;
355
416
expect ( data . outro ) . to . equal ( '' ) ;
356
417
expect ( attributes . outro ) . to . deep . equal ( { order : null , row : null } ) ;
357
- expect ( editableElements . length ) . to . equal ( 3 ) ;
418
+ expect ( editableElements . length ) . to . equal ( 4 ) ;
358
419
expect ( editor ! . getFullData ( ) . outro ) . to . equal ( '' ) ;
359
420
} ) ;
360
421
@@ -396,7 +457,7 @@ describe( 'useMultiRootEditor', () => {
396
457
397
458
const { editor } = result . current ;
398
459
399
- expect ( result . current . editableElements . length ) . to . equal ( 2 ) ;
460
+ expect ( result . current . editableElements . length ) . to . equal ( 3 ) ;
400
461
401
462
act ( ( ) => {
402
463
editor ! . detachRoot ( 'intro' ) ;
@@ -406,7 +467,7 @@ describe( 'useMultiRootEditor', () => {
406
467
const { data, editableElements } = result . current ;
407
468
408
469
expect ( data . intro ) . to . be . undefined ;
409
- expect ( editableElements . length ) . to . equal ( 1 ) ;
470
+ expect ( editableElements . length ) . to . equal ( 2 ) ;
410
471
expect ( editor ! . getFullData ( ) . intro ) . to . be . undefined ;
411
472
} ) ;
412
473
} ) ;
@@ -718,7 +779,7 @@ describe( 'useMultiRootEditor', () => {
718
779
const { data, editableElements } = result . current ;
719
780
720
781
expect ( data . intro ) . to . equal ( rootsContent . intro ) ;
721
- expect ( editableElements . length ) . to . equal ( 2 ) ;
782
+ expect ( editableElements . length ) . to . equal ( 3 ) ;
722
783
expect ( editor ! . getFullData ( ) . intro ) . to . equal ( '<p>New data</p>' ) ;
723
784
expect ( getDataSpy ) . not . toHaveBeenCalled ( ) ;
724
785
} ) ;
@@ -747,7 +808,7 @@ describe( 'useMultiRootEditor', () => {
747
808
expect ( spy . mock . calls . length ) . to . equal ( editableElements . length ) ;
748
809
expect ( data . outro ) . to . be . undefined ;
749
810
expect ( attributes . outro ) . to . be . undefined ;
750
- expect ( editableElements . length ) . to . equal ( 3 ) ;
811
+ expect ( editableElements . length ) . to . equal ( 4 ) ;
751
812
expect ( editor ! . getFullData ( ) . outro ) . to . equal ( '' ) ;
752
813
} ) ;
753
814
@@ -763,7 +824,7 @@ describe( 'useMultiRootEditor', () => {
763
824
764
825
const { editor } = result . current ;
765
826
766
- expect ( result . current . editableElements . length ) . to . equal ( 2 ) ;
827
+ expect ( result . current . editableElements . length ) . to . equal ( 3 ) ;
767
828
768
829
act ( ( ) => {
769
830
editor ! . detachRoot ( 'intro' ) ;
@@ -773,7 +834,7 @@ describe( 'useMultiRootEditor', () => {
773
834
const { data, editableElements } = result . current ;
774
835
775
836
expect ( data . intro ) . to . be . equal ( rootsContent . intro ) ;
776
- expect ( editableElements . length ) . to . equal ( 1 ) ;
837
+ expect ( editableElements . length ) . to . equal ( 2 ) ;
777
838
expect ( editor ! . getFullData ( ) . intro ) . to . be . undefined ;
778
839
} ) ;
779
840
} ) ;
@@ -1029,7 +1090,7 @@ describe( 'useMultiRootEditor', () => {
1029
1090
1030
1091
await waitFor ( ( ) => {
1031
1092
expect ( renderedEditor . current ) . not . to . be . null ;
1032
- expect ( container . getElementsByClassName ( 'ck-editor__editable' ) . length ) . to . equal ( 2 ) ;
1093
+ expect ( container . getElementsByClassName ( 'ck-editor__editable' ) . length ) . to . equal ( 3 ) ;
1033
1094
} ) ;
1034
1095
1035
1096
renderedEditor . current ! . model . document . roots . remove ( 'intro' ) ;
@@ -1104,7 +1165,7 @@ describe( 'useMultiRootEditor', () => {
1104
1165
const { container } = render ( < Component /> ) ;
1105
1166
1106
1167
await waitFor ( ( ) => {
1107
- expect ( container . getElementsByClassName ( 'ck-editor__editable' ) . length ) . to . equal ( 2 ) ;
1168
+ expect ( container . getElementsByClassName ( 'ck-editor__editable' ) . length ) . to . equal ( 3 ) ;
1108
1169
} ) ;
1109
1170
} ) ;
1110
1171
0 commit comments