diff --git a/packages/editor/src/store/selectors.js b/packages/editor/src/store/selectors.js index ae62492e79042d..a2bbf0f47770f1 100644 --- a/packages/editor/src/store/selectors.js +++ b/packages/editor/src/store/selectors.js @@ -289,17 +289,23 @@ export function getCurrentPostAttribute( state, attributeName ) { * * @return {*} Post attribute value. */ -const getNestedEditedPostProperty = ( state, attributeName ) => { - const edits = getPostEdits( state ); - if ( ! edits.hasOwnProperty( attributeName ) ) { - return getCurrentPostAttribute( state, attributeName ); - } +const getNestedEditedPostProperty = createSelector( + ( state, attributeName ) => { + const edits = getPostEdits( state ); + if ( ! edits.hasOwnProperty( attributeName ) ) { + return getCurrentPostAttribute( state, attributeName ); + } - return { - ...getCurrentPostAttribute( state, attributeName ), - ...edits[ attributeName ], - }; -}; + return { + ...getCurrentPostAttribute( state, attributeName ), + ...edits[ attributeName ], + }; + }, + ( state, attributeName ) => [ + getCurrentPostAttribute( state, attributeName ), + getPostEdits( state )[ attributeName ], + ] +); /** * Returns a single attribute of the post being edited, preferring the unsaved diff --git a/packages/editor/src/store/test/selectors.js b/packages/editor/src/store/test/selectors.js index ea8b3fd97b87d1..c18377c4e385e7 100644 --- a/packages/editor/src/store/test/selectors.js +++ b/packages/editor/src/store/test/selectors.js @@ -725,6 +725,31 @@ describe( 'selectors', () => { b: 2, } ); } ); + + it( 'should return the same value for mergeable properties when called multiple times', () => { + const state = { + currentPost: { + meta: { + a: 1, + b: 1, + }, + }, + editor: { + present: { + edits: { + meta: { + b: 2, + }, + }, + }, + }, + initialEdits: {}, + }; + + expect( getEditedPostAttribute( state, 'meta' ) ).toBe( + getEditedPostAttribute( state, 'meta' ) + ); + } ); } ); describe( 'getCurrentPostLastRevisionId', () => {