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

Don't error if autosave runs and there are no changes to save #7347

Merged
merged 2 commits into from
Jun 18, 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
9 changes: 8 additions & 1 deletion editor/store/effects.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,14 @@ export default {
}
},
REQUEST_POST_UPDATE_FAILURE( action, store ) {
const { post, edits } = action;
const { post, edits, error } = action;

if ( error && 'rest_autosave_no_changes' === error.code ) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In what situations will we expect error to be empty such that we'd want to check its truthiness? If it's expected, we should have a separate unit test for this circumstance as well.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure here. While REQUEST_POST_UPDATE does include the error in the dispatched action, REQUEST_POST_UPDATE_FAILURE didn't use it until this change, and several tests didn't include an error either, so I felt it was safer to include this check until someone who knows things better could say for certain if the error would always be there or not.

// Autosave requested a new autosave, but there were no changes. This shouldn't
// result in an error notice for the user.
return;
}

const { dispatch } = store;

const publishStatus = [ 'publish', 'private', 'future' ];
Expand Down
29 changes: 29 additions & 0 deletions editor/store/test/effects.js
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,35 @@ describe( 'effects', () => {
expect( dispatch ).toHaveBeenCalledWith( createErrorNotice( 'Publishing failed', { id: 'SAVE_POST_NOTICE_ID' } ) );
} );

it( 'should not dispatch a notice when there were no changes for autosave to save.', () => {
const handler = effects.REQUEST_POST_UPDATE_FAILURE;
const dispatch = jest.fn();
const store = { getState: () => {}, dispatch };

const action = {
post: {
id: 1,
title: {
raw: 'A History of Pork',
},
content: {
raw: '',
},
status: 'draft',
},
edits: {
status: 'publish',
},
error: {
code: 'rest_autosave_no_changes',
},
};

handler( action, store );

expect( dispatch ).toHaveBeenCalledTimes( 0 );
} );

it( 'should dispatch a notice on failure when trying to update a draft.', () => {
const handler = effects.REQUEST_POST_UPDATE_FAILURE;
const dispatch = jest.fn();
Expand Down
2 changes: 1 addition & 1 deletion lib/class-wp-rest-autosaves-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ public function create_post_autosave( $post_data ) {

if ( ! $autosave_is_different ) {
wp_delete_post_revision( $old_autosave->ID );
return new WP_Error( 'rest_autosave_no_changes', __( 'There is nothing to save. The autosave and the post content are the same.', 'gutenberg' ) );
return new WP_Error( 'rest_autosave_no_changes', __( 'There is nothing to save. The autosave and the post content are the same.', 'gutenberg' ), array( 'status' => 400 ) );
}

/**
Expand Down