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

State: Set new post status to draft in initialization #5089

Merged
merged 1 commit into from
Feb 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 3 additions & 14 deletions editor/components/post-saved-state/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,14 @@ import { Dashicon, Button } from '@wordpress/components';
*/
import './style.scss';
import PostSwitchToDraftButton from '../post-switch-to-draft-button';
import { editPost, savePost } from '../../store/actions';
import { savePost } from '../../store/actions';
import {
isEditedPostNew,
isCurrentPostPublished,
isEditedPostDirty,
isSavingPost,
isEditedPostSaveable,
getCurrentPost,
getEditedPostAttribute,
hasMetaBoxes,
} from '../../store/selectors';

Expand All @@ -33,7 +32,7 @@ import {
* @param {Object} Props Component Props.
* @return {WPElement} WordPress Element.
*/
export function PostSavedState( { hasActiveMetaboxes, isNew, isPublished, isDirty, isSaving, isSaveable, status, onStatusChange, onSave } ) {
export function PostSavedState( { hasActiveMetaboxes, isNew, isPublished, isDirty, isSaving, isSaveable, onSave } ) {
const className = 'editor-post-saved-state';

if ( isSaving ) {
Expand Down Expand Up @@ -61,16 +60,8 @@ export function PostSavedState( { hasActiveMetaboxes, isNew, isPublished, isDirt
);
}

const onClick = () => {
if ( 'auto-draft' === status ) {
onStatusChange( 'draft' );
}

onSave();
};

return (
<Button className={ classnames( className, 'button-link' ) } onClick={ onClick }>
<Button className={ classnames( className, 'button-link' ) } onClick={ onSave }>
<span className="editor-post-saved-state__mobile">{ __( 'Save' ) }</span>
<span className="editor-post-saved-state__desktop">{ __( 'Save Draft' ) }</span>
</Button>
Expand All @@ -85,11 +76,9 @@ export default connect(
isDirty: isEditedPostDirty( state ),
isSaving: isSavingPost( state ),
isSaveable: isEditedPostSaveable( state ),
status: getEditedPostAttribute( state, 'status' ),
hasActiveMetaboxes: hasMetaBoxes( state ),
} ),
{
onStatusChange: ( status ) => editPost( { status } ),
onSave: savePost,
}
)( PostSavedState );
24 changes: 0 additions & 24 deletions editor/components/post-saved-state/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,44 +52,20 @@ describe( 'PostSavedState', () => {
expect( wrapper.childAt( 1 ).text() ).toBe( 'Saved' );
} );

it( 'should edit auto-draft post to draft before save', () => {
const statusSpy = jest.fn();
const saveSpy = jest.fn();
const wrapper = shallow(
<PostSavedState
isNew={ false }
isDirty={ true }
isSaving={ false }
isSaveable={ true }
onStatusChange={ statusSpy }
onSave={ saveSpy }
status="auto-draft" />
);

expect( wrapper.name() ).toBe( 'Button' );
expect( wrapper.childAt( 0 ).text() ).toBe( 'Save' );
wrapper.simulate( 'click' );
expect( statusSpy ).toHaveBeenCalledWith( 'draft' );
expect( saveSpy ).toHaveBeenCalled();
} );

it( 'should return Save button if edits to be saved', () => {
const statusSpy = jest.fn();
const saveSpy = jest.fn();
const wrapper = shallow(
<PostSavedState
isNew={ false }
isDirty={ true }
isSaving={ false }
isSaveable={ true }
onStatusChange={ statusSpy }
onSave={ saveSpy } />
);

expect( wrapper.name() ).toBe( 'Button' );
expect( wrapper.childAt( 0 ).text() ).toBe( 'Save' );
wrapper.simulate( 'click' );
expect( statusSpy ).not.toHaveBeenCalled();
expect( saveSpy ).toHaveBeenCalled();
} );
} );
7 changes: 1 addition & 6 deletions editor/store/effects.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ import {
createErrorNotice,
removeNotice,
savePost,
editPost,
requestMetaBoxUpdates,
metaBoxUpdatesSuccess,
updateReusableBlock,
Expand Down Expand Up @@ -285,11 +284,6 @@ export default {
return;
}

// Change status from auto-draft to draft
if ( isEditedPostNew( state ) ) {
dispatch( editPost( { status: 'draft' } ) );
}

dispatch( savePost() );
},
SETUP_EDITOR( action ) {
Expand Down Expand Up @@ -319,6 +313,7 @@ export default {
if ( post.status === 'auto-draft' ) {
effects.push( setupNewPost( {
title: post.title.raw,
status: 'draft',
} ) );
}

Expand Down
22 changes: 5 additions & 17 deletions editor/store/test/effects.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import {
resetBlocks,
mergeBlocks,
replaceBlocks,
editPost,
savePost,
requestMetaBoxUpdates,
updateReusableBlock,
Expand Down Expand Up @@ -265,8 +264,7 @@ describe( 'effects', () => {

handler( {}, store );

expect( dispatch ).toHaveBeenCalledTimes( 2 );
expect( dispatch ).toHaveBeenCalledWith( editPost( { status: 'draft' } ) );
expect( dispatch ).toHaveBeenCalledTimes( 1 );
expect( dispatch ).toHaveBeenCalledWith( savePost() );
} );

Expand All @@ -280,19 +278,6 @@ describe( 'effects', () => {
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 );
Expand Down Expand Up @@ -497,7 +482,10 @@ describe( 'effects', () => {

expect( result ).toEqual( [
resetPost( post ),
setupNewPost( { title: 'A History of Pork' } ),
setupNewPost( {
title: 'A History of Pork',
status: 'draft',
} ),
] );
} );
} );
Expand Down
8 changes: 6 additions & 2 deletions editor/store/test/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,9 @@ describe( 'selectors', () => {
},
editor: {
present: {
edits: {},
edits: {
status: 'draft',
},
},
},
};
Expand All @@ -269,7 +271,9 @@ describe( 'selectors', () => {
},
editor: {
present: {
edits: {},
edits: {
status: 'draft',
},
},
},
};
Expand Down