Skip to content

Commit

Permalink
Editor: Remove redundant artificial dirtying
Browse files Browse the repository at this point in the history
  • Loading branch information
aduth committed Nov 6, 2018
1 parent 062a4a3 commit 804a206
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 54 deletions.
12 changes: 1 addition & 11 deletions packages/editor/src/store/effects/posts.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,18 +171,8 @@ export const requestPostUpdate = async ( action, store ) => {
* @param {Object} action action object.
* @param {Object} store Redux Store.
*/
export const requestPostUpdateSuccess = ( action, store ) => {
export const requestPostUpdateSuccess = ( action ) => {
const { previousPost, post, isAutosave, postType } = action;
const { dispatch, getState } = store;

// TEMPORARY: If edits remain after a save completes, the user must be
// prompted about unsaved changes. This should be refactored as part of
// the `isEditedPostDirty` selector instead.
//
// See: https://github.com/WordPress/gutenberg/issues/7409
if ( Object.keys( getPostEdits( getState() ) ).length ) {
dispatch( { type: 'DIRTY_ARTIFICIALLY' } );
}

// Autosaves are neither shown a notice nor redirected.
if ( isAutosave ) {
Expand Down
3 changes: 0 additions & 3 deletions packages/editor/src/store/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -257,9 +257,6 @@ export const editor = flow( [

return state;

case 'DIRTY_ARTIFICIALLY':
return { ...state };

case 'UPDATE_POST':
case 'RESET_POST':
const getCanonicalValue = action.type === 'UPDATE_POST' ?
Expand Down
44 changes: 4 additions & 40 deletions packages/editor/src/store/test/effects.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import actions, {
resetBlocks,
selectBlock,
setTemplateValidity,
editPost,
} from '../actions';
import effects, { validateBlocksToTemplate } from '../effects';
import { SAVE_POST_NOTICE_ID } from '../effects/posts';
Expand Down Expand Up @@ -222,16 +221,6 @@ describe( 'effects', () => {
describe( '.REQUEST_POST_UPDATE_SUCCESS', () => {
const handler = effects.REQUEST_POST_UPDATE_SUCCESS;

function createGetState( hasLingeringEdits = false ) {
let state = reducer( undefined, {} );
if ( hasLingeringEdits ) {
state = reducer( state, editPost( { edited: true } ) );
}

const getState = () => state;
return getState;
}

const defaultPost = {
id: 1,
title: {
Expand Down Expand Up @@ -259,14 +248,11 @@ describe( 'effects', () => {
} );

it( 'should dispatch notices when publishing or scheduling a post', () => {
const dispatch = jest.fn();
const store = { dispatch, getState: createGetState() };

const previousPost = getDraftPost();
const post = getPublishedPost();
const postType = getPostType();

handler( { post, previousPost, postType }, store );
handler( { post, previousPost, postType } );

expect( dataDispatch( 'core/notices' ).createSuccessNotice ).toHaveBeenCalledWith(
'Post published.',
Expand All @@ -280,14 +266,11 @@ describe( 'effects', () => {
} );

it( 'should dispatch notices when reverting a published post to a draft', () => {
const dispatch = jest.fn();
const store = { dispatch, getState: createGetState() };

const previousPost = getPublishedPost();
const post = getDraftPost();
const postType = getPostType();

handler( { post, previousPost, postType }, store );
handler( { post, previousPost, postType } );

expect( dataDispatch( 'core/notices' ).createSuccessNotice ).toHaveBeenCalledWith(
'Post reverted to draft.',
Expand All @@ -299,14 +282,11 @@ describe( 'effects', () => {
} );

it( 'should dispatch notices when just updating a published post again', () => {
const dispatch = jest.fn();
const store = { dispatch, getState: createGetState() };

const previousPost = getPublishedPost();
const post = getPublishedPost();
const postType = getPostType();

handler( { post, previousPost, postType }, store );
handler( { post, previousPost, postType } );

expect( dataDispatch( 'core/notices' ).createSuccessNotice ).toHaveBeenCalledWith(
'Post updated.',
Expand All @@ -320,29 +300,13 @@ describe( 'effects', () => {
} );

it( 'should do nothing if the updated post was autosaved', () => {
const dispatch = jest.fn();
const store = { dispatch, getState: createGetState() };

const previousPost = getPublishedPost();
const post = { ...getPublishedPost(), id: defaultPost.id + 1 };

handler( { post, previousPost, isAutosave: true }, store );
handler( { post, previousPost, isAutosave: true } );

expect( dataDispatch( 'core/notices' ).createSuccessNotice ).not.toHaveBeenCalled();
} );

it( 'should dispatch dirtying action if edits linger after autosave', () => {
const dispatch = jest.fn();
const store = { dispatch, getState: createGetState( true ) };

const previousPost = getPublishedPost();
const post = { ...getPublishedPost(), id: defaultPost.id + 1 };

handler( { post, previousPost, isAutosave: true }, store );

expect( dispatch ).toHaveBeenCalledTimes( 1 );
expect( dispatch ).toHaveBeenCalledWith( { type: 'DIRTY_ARTIFICIALLY' } );
} );
} );

describe( '.REQUEST_POST_UPDATE_FAILURE', () => {
Expand Down

0 comments on commit 804a206

Please sign in to comment.