Skip to content

Commit

Permalink
Set status from auto-draft to draft before save
Browse files Browse the repository at this point in the history
  • Loading branch information
aduth committed Jul 26, 2017
1 parent 995f4cd commit 56d6dfa
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 11 deletions.
11 changes: 9 additions & 2 deletions editor/effects.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
autosave,
queueAutosave,
savePost,
editPost,
} from './actions';
import {
getCurrentPost,
Expand All @@ -30,6 +31,7 @@ import {
getPostEdits,
isCurrentPostPublished,
isEditedPostDirty,
isEditedPostNew,
isEditedPostSaveable,
} from './selectors';

Expand Down Expand Up @@ -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;
Expand All @@ -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() );
Expand Down
36 changes: 27 additions & 9 deletions editor/test/effects.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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() );
} );
} );
} );

0 comments on commit 56d6dfa

Please sign in to comment.