From 1115dd0475b38c653a642423077bc21c18368692 Mon Sep 17 00:00:00 2001 From: "jm.fiaschi" Date: Fri, 2 Aug 2024 12:59:51 +0200 Subject: [PATCH] fix(i18n): allow to store the new entry in the draft after a pre save (#7227) * fix(i18n): allow to store the new entry in the draft after a pre save * feat(persistEntry): test preSave that return data or entry map * fix(linter): expected property shorthand object-shorthand * fix: format backend spec, update caniuse-lite --------- Co-authored-by: Martin Jagodic --- package-lock.json | 6 +- .../src/__tests__/backend.spec.js | 75 +++++++++++++++++++ packages/decap-cms-core/src/backend.ts | 10 ++- 3 files changed, 86 insertions(+), 5 deletions(-) diff --git a/package-lock.json b/package-lock.json index 99943a78922a..763499462cfb 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10264,9 +10264,9 @@ } }, "node_modules/caniuse-lite": { - "version": "1.0.30001587", - "resolved": "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001587.tgz", - "integrity": "sha512-HMFNotUmLXn71BQxg8cijvqxnIAofforZOwGsxyXJ0qugTdspUF4sPSJ2vhgprHCB996tIDzEq1ubumPDV8ULA==", + "version": "1.0.30001646", + "resolved": "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001646.tgz", + "integrity": "sha512-dRg00gudiBDDTmUhClSdv3hqRfpbOnU28IpI1T6PBTLWa+kOj0681C8uML3PifYfREuBrVjDGhL3adYpBT6spw==", "dev": true, "funding": [ { diff --git a/packages/decap-cms-core/src/__tests__/backend.spec.js b/packages/decap-cms-core/src/__tests__/backend.spec.js index 9b69519ff1dc..c673c19a01c4 100644 --- a/packages/decap-cms-core/src/__tests__/backend.spec.js +++ b/packages/decap-cms-core/src/__tests__/backend.spec.js @@ -318,6 +318,81 @@ describe('Backend', () => { }); }); + describe('persistEntry', () => { + it('should update the draft with the new entry returned by preSave event', async () => { + const implementation = { + init: jest.fn(() => implementation), + persistEntry: jest.fn(() => implementation), + }; + + const config = { + backend: { + commit_messages: 'commit-messages', + }, + }; + const collection = Map({ + name: 'posts', + }); + const entry = Map({ + data: 'old_data', + }); + const newEntry = Map({ + data: 'new_data', + }); + const entryDraft = Map({ + entry, + }); + const user = { login: 'login', name: 'name' }; + const backend = new Backend(implementation, { config, backendName: 'github' }); + + backend.currentUser = jest.fn().mockResolvedValue(user); + backend.entryToRaw = jest.fn().mockReturnValue('content'); + backend.invokePreSaveEvent = jest.fn().mockReturnValueOnce(newEntry); + + await backend.persistEntry({ config, collection, entryDraft }); + + expect(backend.entryToRaw).toHaveBeenCalledTimes(1); + expect(backend.entryToRaw).toHaveBeenCalledWith(collection, newEntry); + }); + + it('should update the draft with the new data returned by preSave event', async () => { + const implementation = { + init: jest.fn(() => implementation), + persistEntry: jest.fn(() => implementation), + }; + + const config = { + backend: { + commit_messages: 'commit-messages', + }, + }; + const collection = Map({ + name: 'posts', + }); + const entry = Map({ + data: Map({}), + }); + const newData = Map({}); + const newEntry = Map({ + data: newData, + }); + const entryDraft = Map({ + entry, + }); + const user = { login: 'login', name: 'name' }; + const backend = new Backend(implementation, { config, backendName: 'github' }); + + backend.currentUser = jest.fn().mockResolvedValue(user); + backend.entryToRaw = jest.fn().mockReturnValue('content'); + backend.invokePreSaveEvent = jest.fn().mockReturnValueOnce(newData); + + await backend.persistEntry({ config, collection, entryDraft }); + + expect(backend.entryToRaw).toHaveBeenCalledTimes(1); + expect(backend.entryToRaw).toHaveBeenCalledWith(collection, newEntry); + }); + }); + describe('persistMedia', () => { it('should persist media', async () => { const persistMediaResult = {}; diff --git a/packages/decap-cms-core/src/backend.ts b/packages/decap-cms-core/src/backend.ts index 49e668d042ef..bcb8881de045 100644 --- a/packages/decap-cms-core/src/backend.ts +++ b/packages/decap-cms-core/src/backend.ts @@ -1091,8 +1091,14 @@ export class Backend { unpublished = false, status, }: PersistArgs) { - const modifiedData = await this.invokePreSaveEvent(draft.get('entry')); - const entryDraft = (modifiedData && draft.setIn(['entry', 'data'], modifiedData)) || draft; + const updatedEntity = await this.invokePreSaveEvent(draft.get('entry')); + + let entryDraft; + if (updatedEntity.get('data') === undefined) { + entryDraft = (updatedEntity && draft.setIn(['entry', 'data'], updatedEntity)) || draft; + } else { + entryDraft = (updatedEntity && draft.setIn(['entry'], updatedEntity)) || draft; + } const newEntry = entryDraft.getIn(['entry', 'newRecord']) || false;