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

Fix update block attributes shallow clone comparison #1995

Merged
merged 1 commit into from
Jul 24, 2017
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
4 changes: 2 additions & 2 deletions editor/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ export const editor = combineUndoableReducers( {
const nextAttributes = reduce( action.attributes, ( result, value, key ) => {
if ( value !== result[ key ] ) {
// Avoid mutating original block by creating shallow clone
if ( result === state[ action.uid ] ) {
result = { ...state[ action.uid ] };
if ( result === state[ action.uid ].attributes ) {
result = { ...result };
}

result[ key ] = value;
Expand Down
36 changes: 30 additions & 6 deletions editor/test/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -509,13 +509,13 @@ describe( 'state', () => {

describe( 'blocksByUid', () => {
it( 'should return with attribute block updates', () => {
const original = editor( undefined, {
const original = deepFreeze( editor( undefined, {
type: 'RESET_BLOCKS',
blocks: [ {
uid: 'kumquat',
attributes: {},
} ],
} );
} ) );
const state = editor( original, {
type: 'UPDATE_BLOCK_ATTRIBUTES',
uid: 'kumquat',
Expand All @@ -527,11 +527,35 @@ describe( 'state', () => {
expect( state.blocksByUid.kumquat.attributes.updated ).toBe( true );
} );

it( 'should accumulate attribute block updates', () => {
const original = deepFreeze( editor( undefined, {
type: 'RESET_BLOCKS',
blocks: [ {
uid: 'kumquat',
attributes: {
updated: true,
},
} ],
} ) );
const state = editor( original, {
type: 'UPDATE_BLOCK_ATTRIBUTES',
uid: 'kumquat',
attributes: {
moreUpdated: true,
},
} );

expect( state.blocksByUid.kumquat.attributes ).toEqual( {
updated: true,
moreUpdated: true,
} );
} );

it( 'should ignore updates to non-existant block', () => {
const original = editor( undefined, {
const original = deepFreeze( editor( undefined, {
type: 'RESET_BLOCKS',
blocks: [],
} );
} ) );
const state = editor( original, {
type: 'UPDATE_BLOCK_ATTRIBUTES',
uid: 'kumquat',
Expand All @@ -544,15 +568,15 @@ describe( 'state', () => {
} );

it( 'should return with same reference if no changes in updates', () => {
const original = editor( undefined, {
const original = deepFreeze( editor( undefined, {
type: 'RESET_BLOCKS',
blocks: [ {
uid: 'kumquat',
attributes: {
updated: true,
},
} ],
} );
} ) );
const state = editor( original, {
type: 'UPDATE_BLOCK_ATTRIBUTES',
uid: 'kumquat',
Expand Down