Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(i18n): allow to store the new entry in the draft after a pre save #7227

Merged
merged 9 commits into from
Aug 2, 2024
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
Loading