Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions packages/core-data/src/resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -260,13 +260,18 @@ export const getEntityRecord =
return;
}

// Trigger a save to persist the CRDT document. The entity's
// pre-persist hooks will create the persisted CRDT document
// and apply it to the record's meta.
// Trigger a minimal save to persist the CRDT document. The
// entity's pre-persist hooks will create the persisted CRDT
// document and apply it to the record's meta.
const entityIdKey =
entityConfig.key || DEFAULT_ENTITY_KEY;
dispatch.saveEntityRecord(
kind,
name,
editedRecord,
{
[ entityIdKey ]:
editedRecord[ entityIdKey ],
},
{ __unstableSkipSyncUpdate: true }
);
} );
Expand Down
20 changes: 13 additions & 7 deletions packages/core-data/src/test/resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,9 +234,14 @@ describe( 'getEntityRecord', () => {
expect( dispatch.saveEntityRecord ).not.toHaveBeenCalled();
} );

it( 'persistCRDTDoc fetches edited record and saves full entity record', async () => {
it( 'persistCRDTDoc saves only the entity ID and omits REST-invalid fields', async () => {
const POST_RECORD = { id: 1, title: 'Test Post', meta: {} };
const EDITED_RECORD = { id: 1, title: 'Edited Post', meta: {} };
const EDITED_RECORD = {
id: 1,
title: 'Edited Post',
ping_status: '',
meta: { _crdt_document: 'doc2' },
};
const POST_RESPONSE = {
json: () => Promise.resolve( POST_RECORD ),
};
Expand Down Expand Up @@ -283,11 +288,12 @@ describe( 'getEntityRecord', () => {
resolveSelectWithSync.getEditedEntityRecord
).toHaveBeenCalledWith( 'postType', 'post', 1 );

// Should have called saveEntityRecord (not saveEditedEntityRecord).
// Should only send the entity ID. The pre-persist hook creates the
// persisted CRDT meta without round-tripping the full edited record.
expect( dispatch.saveEntityRecord ).toHaveBeenCalledWith(
'postType',
'post',
EDITED_RECORD,
{ id: 1 },
{ __unstableSkipSyncUpdate: true }
);
} );
Expand Down Expand Up @@ -335,11 +341,11 @@ describe( 'getEntityRecord', () => {
handlers.persistCRDTDoc();
await resolveSelectWithSync.getEditedEntityRecord();

// Should save the record even with no edits (the whole point of the fix).
// Should save only the entity ID even with no edits.
expect( dispatch.saveEntityRecord ).toHaveBeenCalledWith(
'postType',
'post',
POST_RECORD,
{ id: 1 },
{ __unstableSkipSyncUpdate: true }
);
} );
Expand Down Expand Up @@ -437,7 +443,7 @@ describe( 'getEntityRecord', () => {
expect( dispatch.saveEntityRecord ).toHaveBeenCalledWith(
'postType',
'post',
EDITED_RECORD,
{ id: 1 },
{ __unstableSkipSyncUpdate: true }
);
expect( syncManager.update ).toHaveBeenCalledWith(
Expand Down
Loading