diff --git a/editor/effects.js b/editor/effects.js index d21cea89577674..5b665b13a19f5a 100644 --- a/editor/effects.js +++ b/editor/effects.js @@ -22,6 +22,7 @@ import { autosave, queueAutosave, savePost, + editPost, } from './actions'; import { getCurrentPost, @@ -30,6 +31,7 @@ import { getPostEdits, isCurrentPostPublished, isEditedPostDirty, + isEditedPostNew, isEditedPostSaveable, } from './selectors'; @@ -219,7 +221,7 @@ export default { ) ); }, AUTOSAVE( action, store ) { - const { getState } = store; + const { getState, dispatch } = store; const state = getState(); if ( ! isEditedPostSaveable( state ) || ! isEditedPostDirty( state ) ) { return; @@ -234,7 +236,12 @@ export default { return; } - return savePost(); + // Change status from auto-draft to draft + if ( isEditedPostNew( state ) ) { + dispatch( editPost( { status: 'draft' } ) ); + } + + dispatch( savePost() ); }, QUEUE_AUTOSAVE: debounce( ( action, store ) => { store.dispatch( autosave() ); diff --git a/editor/test/effects.js b/editor/test/effects.js index 570feee943da2b..ec2cef6215aee4 100644 --- a/editor/test/effects.js +++ b/editor/test/effects.js @@ -11,7 +11,7 @@ import { getBlockTypes, unregisterBlockType, registerBlockType, createBlock } fr /** * Internal dependencies */ -import { mergeBlocks, focusBlock, replaceBlocks } from '../actions'; +import { mergeBlocks, focusBlock, replaceBlocks, editPost, savePost } from '../actions'; import effects from '../effects'; import * as selectors from '../selectors'; @@ -153,42 +153,60 @@ describe( 'effects', () => { describe( '.AUTOSAVE', () => { const handler = effects.AUTOSAVE; - const store = { getState: () => {} }; + const dispatch = jest.fn(); + const store = { getState: () => {}, dispatch }; it( 'should do nothing for unsaveable', () => { selectors.isEditedPostSaveable.mockReturnValue( false ); selectors.isEditedPostDirty.mockReturnValue( true ); selectors.isCurrentPostPublished.mockReturnValue( false ); + selectors.isEditedPostNew.mockReturnValue( true ); - expect( handler( {}, store ) ).toBeUndefined(); + expect( dispatch ).not.toHaveBeenCalled(); } ); it( 'should do nothing for clean', () => { selectors.isEditedPostSaveable.mockReturnValue( true ); selectors.isEditedPostDirty.mockReturnValue( false ); selectors.isCurrentPostPublished.mockReturnValue( false ); + selectors.isEditedPostNew.mockReturnValue( true ); - expect( handler( {}, store ) ).toBeUndefined(); + expect( dispatch ).not.toHaveBeenCalled(); } ); it( 'should return autosave action for saveable, dirty, published post', () => { selectors.isEditedPostSaveable.mockReturnValue( true ); selectors.isEditedPostDirty.mockReturnValue( true ); selectors.isCurrentPostPublished.mockReturnValue( true ); + selectors.isEditedPostNew.mockReturnValue( true ); - expect( handler( {}, store ) ).toBeUndefined(); // TODO: Publish autosave - // expect( handler( {}, store ) ).toEqual( { } ); + expect( dispatch ).not.toHaveBeenCalled(); + } ); + + it( 'should set auto-draft to draft before save', () => { + selectors.isEditedPostSaveable.mockReturnValue( true ); + selectors.isEditedPostDirty.mockReturnValue( true ); + selectors.isCurrentPostPublished.mockReturnValue( false ); + selectors.isEditedPostNew.mockReturnValue( true ); + + handler( {}, store ); + + expect( dispatch ).toHaveBeenCalledTimes( 2 ); + expect( dispatch ).toHaveBeenCalledWith( editPost( { status: 'draft' } ) ); + expect( dispatch ).toHaveBeenCalledWith( savePost() ); } ); it( 'should return update action for saveable, dirty draft', () => { selectors.isEditedPostSaveable.mockReturnValue( true ); selectors.isEditedPostDirty.mockReturnValue( true ); selectors.isCurrentPostPublished.mockReturnValue( false ); + selectors.isEditedPostNew.mockReturnValue( false ); - expect( handler( {}, store ) ).toEqual( { - type: 'REQUEST_POST_UPDATE', - } ); + handler( {}, store ); + + expect( dispatch ).toHaveBeenCalledTimes( 1 ); + expect( dispatch ).toHaveBeenCalledWith( savePost() ); } ); } ); } );