@@ -22,7 +22,6 @@ import {
2222 CRDT_RECORD_METADATA_MAP_KEY as RECORD_METADATA_MAP_KEY ,
2323 CRDT_RECORD_METADATA_SAVED_AT_KEY as SAVED_AT_KEY ,
2424 CRDT_RECORD_METADATA_SAVED_BY_KEY as SAVED_BY_KEY ,
25- WORDPRESS_META_KEY_FOR_CRDT_DOC_PERSISTENCE ,
2625} from '../config' ;
2726import { createPersistedCRDTDoc } from '../persistence' ;
2827import { getProviderCreators } from '../providers' ;
@@ -269,33 +268,16 @@ describe( 'SyncManager', () => {
269268 mockSyncConfig . applyChangesToCRDTDoc
270269 ) . toHaveBeenCalledWith ( expect . any ( Y . Doc ) , mockRecord ) ;
271270
272- // Changes should be correctly applied.
273- const mockCall =
274- mockSyncConfig . applyChangesToCRDTDoc . mock . calls [ 0 ] ;
275- const targetDoc = mockCall [ 0 ] as Y . Doc ;
276- const appliedChanges = mockCall [ 1 ] as ObjectData ;
277- expect (
278- targetDoc . getMap ( CRDT_RECORD_MAP_KEY ) . get ( 'title' )
279- ) . toBeUndefined ( ) ;
280- expect ( appliedChanges . title ) . toStrictEqual ( 'Test Post' ) ;
281-
282271 // getChangesFromCRDTDoc should not be called since there was no persisted doc.
283272 expect (
284273 mockSyncConfig . getChangesFromCRDTDoc
285274 ) . not . toHaveBeenCalled ( ) ;
286275
287276 // Verify a save operation occurred.
288- expect ( mockHandlers . editRecord ) . toHaveBeenCalledTimes ( 1 ) ;
289- expect ( mockHandlers . editRecord ) . toHaveBeenCalledWith ( {
290- meta : {
291- [ WORDPRESS_META_KEY_FOR_CRDT_DOC_PERSISTENCE ] :
292- expect . any ( String ) ,
293- } ,
294- } ) ;
295277 expect ( mockHandlers . saveRecord ) . toHaveBeenCalledTimes ( 1 ) ;
296278 } ) ;
297279
298- it ( 'applies a valid persisted CRDT doc without applying the current record ' , async ( ) => {
280+ it ( 'accepts a valid persisted CRDT doc without applying changes ' , async ( ) => {
299281 const record = createRecordWithPersistedCRDTDoc ( mockRecord ) ;
300282
301283 mockSyncConfig = {
@@ -313,7 +295,7 @@ describe( 'SyncManager', () => {
313295 mockHandlers
314296 ) ;
315297
316- // Current record should NOT be applied since the persisted doc is valid.
298+ // Changes should NOT be applied since the persisted doc is valid.
317299 expect (
318300 mockSyncConfig . applyChangesToCRDTDoc
319301 ) . not . toHaveBeenCalled ( ) ;
@@ -331,9 +313,10 @@ describe( 'SyncManager', () => {
331313 expect ( mockHandlers . saveRecord ) . not . toHaveBeenCalled ( ) ;
332314 } ) ;
333315
334- it ( 'applies an invalid persisted CRDT doc, then applies the current record ' , async ( ) => {
316+ it ( 'applies an invalid CRDT doc, then applies changes ' , async ( ) => {
335317 const record = createRecordWithPersistedCRDTDoc ( mockRecord , {
336- title : 'Title from persisted CRDT doc' ,
318+ ...mockRecord ,
319+ title : 'Invalidated title from persisted CRDT doc' ,
337320 } ) ;
338321
339322 mockSyncConfig = {
@@ -351,23 +334,17 @@ describe( 'SyncManager', () => {
351334 mockHandlers
352335 ) ;
353336
354- // Current record should be applied since the persisted doc is invalid.
337+ // Changes should be applied for the invalidated properties.
338+ const expectedChanges = {
339+ title : mockRecord . title ,
340+ } ;
341+
355342 expect (
356343 mockSyncConfig . applyChangesToCRDTDoc
357344 ) . toHaveBeenCalledTimes ( 1 ) ;
358345 expect (
359346 mockSyncConfig . applyChangesToCRDTDoc
360- ) . toHaveBeenCalledWith ( expect . any ( Y . Doc ) , record ) ;
361-
362- // Changes should be correctly applied.
363- const mockCall =
364- mockSyncConfig . applyChangesToCRDTDoc . mock . calls [ 0 ] ;
365- const targetDoc = mockCall [ 0 ] as Y . Doc ;
366- const appliedChanges = mockCall [ 1 ] as ObjectData ;
367- expect (
368- targetDoc . getMap ( CRDT_RECORD_MAP_KEY ) . get ( 'title' )
369- ) . toStrictEqual ( 'Title from persisted CRDT doc' ) ;
370- expect ( appliedChanges . title ) . toStrictEqual ( 'Test Post' ) ;
347+ ) . toHaveBeenCalledWith ( expect . any ( Y . Doc ) , expectedChanges ) ;
371348
372349 // getChangesFromCRDTDoc should be called with the persisted doc and record.
373350 expect (
@@ -378,18 +355,12 @@ describe( 'SyncManager', () => {
378355 ) . toHaveBeenCalledWith ( expect . any ( Y . Doc ) , record ) ;
379356
380357 // Verify a save operation occurred.
381- expect ( mockHandlers . editRecord ) . toHaveBeenCalledTimes ( 1 ) ;
382- expect ( mockHandlers . editRecord ) . toHaveBeenCalledWith ( {
383- meta : {
384- [ WORDPRESS_META_KEY_FOR_CRDT_DOC_PERSISTENCE ] :
385- expect . any ( String ) ,
386- } ,
387- } ) ;
388358 expect ( mockHandlers . saveRecord ) . toHaveBeenCalledTimes ( 1 ) ;
389359 } ) ;
390360
391361 it ( 'ignores a persisted CRDT doc when CRDT persistence is not supported' , async ( ) => {
392362 const record = createRecordWithPersistedCRDTDoc ( mockRecord , {
363+ ...mockRecord ,
393364 title : 'Persisted Title' ,
394365 } ) ;
395366
@@ -411,27 +382,13 @@ describe( 'SyncManager', () => {
411382 mockSyncConfig . applyChangesToCRDTDoc
412383 ) . toHaveBeenCalledWith ( expect . any ( Y . Doc ) , record ) ;
413384
414- // Changes should be correctly applied.
415- const mockCall =
416- mockSyncConfig . applyChangesToCRDTDoc . mock . calls [ 0 ] ;
417- const targetDoc = mockCall [ 0 ] as Y . Doc ;
418- const appliedChanges = mockCall [ 1 ] as ObjectData ;
419- expect (
420- targetDoc . getMap ( CRDT_RECORD_MAP_KEY ) . get ( 'title' )
421- ) . toBeUndefined ( ) ;
422- expect ( appliedChanges . title ) . toStrictEqual ( 'Test Post' ) ;
423-
424385 // getChangesFromCRDTDoc should not be called since the persisted doc is igored.
425386 expect (
426387 mockSyncConfig . getChangesFromCRDTDoc
427388 ) . not . toHaveBeenCalled ( ) ;
428389
429- // Verify a save operation occurred.
430- expect ( mockHandlers . editRecord ) . toHaveBeenCalledTimes ( 1 ) ;
431- expect ( mockHandlers . editRecord ) . toHaveBeenCalledWith ( {
432- meta : { } ,
433- } ) ;
434- expect ( mockHandlers . saveRecord ) . toHaveBeenCalledTimes ( 1 ) ;
390+ // Verify no save operation occurred because persistence is not supported.
391+ expect ( mockHandlers . saveRecord ) . not . toHaveBeenCalled ( ) ;
435392 } ) ;
436393 } ) ;
437394 } ) ;
0 commit comments