Skip to content

Commit

Permalink
fix(i18n): allow to store the new entry in the draft after a pre save (
Browse files Browse the repository at this point in the history
…#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 <[email protected]>
  • Loading branch information
jmfiaschi and martinjagodic authored Aug 2, 2024
1 parent fae3e05 commit dc8b684
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 5 deletions.
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

75 changes: 75 additions & 0 deletions packages/decap-cms-core/src/__tests__/backend.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {};
Expand Down
10 changes: 8 additions & 2 deletions packages/decap-cms-core/src/backend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down

0 comments on commit dc8b684

Please sign in to comment.