From 9e7aa0c500f2bd394ca711846ec88c2b4fbf7ec5 Mon Sep 17 00:00:00 2001 From: Bartholomew Date: Wed, 25 Dec 2019 10:47:02 +0100 Subject: [PATCH] fix: duplicate and new entry action (#3003) * fix: duplicate and new entry action * test(e2e): flush clock when duplicating entry to handle debounced fields Co-authored-by: Erez Rokah --- cypress/utils/steps.js | 38 ++++++++++--------- .../src/actions/editorialWorkflow.ts | 2 +- .../netlify-cms-core/src/actions/entries.ts | 14 ++++++- .../src/components/Editor/Editor.js | 4 +- 4 files changed, 37 insertions(+), 21 deletions(-) diff --git a/cypress/utils/steps.js b/cypress/utils/steps.js index c6de5b60..53b95355 100644 --- a/cypress/utils/steps.js +++ b/cypress/utils/steps.js @@ -184,22 +184,7 @@ function selectDropdownItem(label, item) { }); } -function populateEntry(entry) { - const keys = Object.keys(entry); - for (let key of keys) { - const value = entry[key]; - if (key === 'body') { - cy.getMarkdownEditor() - .click() - .clear() - .type(value); - } else { - cy.get(`[id^="${key}-field"]`) - .clear() - .type(value); - } - } - +function flushClockAndSave() { cy.clock().then(clock => { // some input fields are de-bounced thus require advancing the clock if (clock) { @@ -218,6 +203,25 @@ function populateEntry(entry) { }); } +function populateEntry(entry) { + const keys = Object.keys(entry); + for (let key of keys) { + const value = entry[key]; + if (key === 'body') { + cy.getMarkdownEditor() + .click() + .clear() + .type(value); + } else { + cy.get(`[id^="${key}-field"]`) + .clear() + .type(value); + } + } + + flushClockAndSave(); +} + function newPost() { cy.contains('a', 'New Post').click(); } @@ -257,7 +261,7 @@ function unpublishEntry(entry) { function duplicateEntry(entry) { selectDropdownItem('Published', 'Duplicate'); cy.url().should('contain', '/#/collections/posts/new'); - cy.contains('button', 'Save').click(); + flushClockAndSave(); updateWorkflowStatusInEditor(editorStatus.ready); publishEntryInEditor(publishTypes.publishNow); exitEditor(); diff --git a/packages/netlify-cms-core/src/actions/editorialWorkflow.ts b/packages/netlify-cms-core/src/actions/editorialWorkflow.ts index f2644632..13b29669 100644 --- a/packages/netlify-cms-core/src/actions/editorialWorkflow.ts +++ b/packages/netlify-cms-core/src/actions/editorialWorkflow.ts @@ -529,7 +529,7 @@ export function publishUnpublishedEntry(collection: string, slug: string) { ); dispatch(unpublishedEntryPublished(collection, slug, transactionID)); - dispatch(loadEntry(collections.get(collection), slug)); + return dispatch(loadEntry(collections.get(collection), slug)); }) .catch((error: Error) => { dispatch( diff --git a/packages/netlify-cms-core/src/actions/entries.ts b/packages/netlify-cms-core/src/actions/entries.ts index f5ccd659..9f29662b 100644 --- a/packages/netlify-cms-core/src/actions/entries.ts +++ b/packages/netlify-cms-core/src/actions/entries.ts @@ -24,6 +24,7 @@ import { import { ThunkDispatch } from 'redux-thunk'; import { AnyAction, Dispatch } from 'redux'; import { waitForMediaLibraryToLoad } from './mediaLibrary'; +import { waitUntil } from './waitUntil'; const { notifSend } = notifActions; @@ -215,7 +216,7 @@ export function createDraftFromEntry(entry: EntryMap, metadata?: Map) => { + dispatch( + waitUntil({ + predicate: ({ type }) => type === DRAFT_CREATE_EMPTY, + run: () => dispatch(draftDuplicateEntry(entry)), + }), + ); + }; +} + export function retrieveLocalBackup(collection: Collection, slug: string) { return async (dispatch: ThunkDispatch, getState: () => State) => { const state = getState(); diff --git a/packages/netlify-cms-core/src/components/Editor/Editor.js b/packages/netlify-cms-core/src/components/Editor/Editor.js index c21df953..11379756 100644 --- a/packages/netlify-cms-core/src/components/Editor/Editor.js +++ b/packages/netlify-cms-core/src/components/Editor/Editor.js @@ -318,10 +318,10 @@ export class Editor extends React.Component { return navigateToCollection(collection.get('name')); }; - handleDuplicateEntry = async () => { + handleDuplicateEntry = () => { const { createDraftDuplicateFromEntry, collection, entryDraft } = this.props; - await navigateToNewEntry(collection.get('name')); + navigateToNewEntry(collection.get('name')); createDraftDuplicateFromEntry(entryDraft.get('entry')); };