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

Global styles: Fix error when updating padding input in global styles #49465

Merged
merged 2 commits into from
Mar 30, 2023
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
2 changes: 1 addition & 1 deletion packages/block-editor/src/utils/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ function normalizePath( path ) {
* @return {*} Cloned object, or original literal non-object value.
*/
function cloneObject( object ) {
if ( typeof object === 'object' ) {
if ( object && typeof object === 'object' ) {
Copy link
Contributor

@talldan talldan Mar 30, 2023

Choose a reason for hiding this comment

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

Is there a test case that can be added to the tests for immutableSet?

BTW, cloneObject wasn't introduced in #49365, it was already in the codebase. I think it might be from the refactor away from lodash, so it might be good to check what lodash does in this situation and mirror that.

Copy link
Contributor

@talldan talldan Mar 30, 2023

Choose a reason for hiding this comment

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

It looks like this is what happens:

_.set( null, [ 'path' ], 'value' ); // returns `null`
_.set( undefined, [ 'path' ], 'value' ); // returns `undefined`

So I think the fix is good in that it still returns the original value.

Ignore this, the bug is different, and immutableSet does work differently to lodash set.

I've added some tests that catch the issue, which was specifically happening when using immutableSet on objects that have a nested null property. (that was mentioned in the PR description, but I read it too quickly 😄 )

return {
...Object.fromEntries(
Object.entries( object ).map( ( [ key, value ] ) => [
Expand Down
38 changes: 38 additions & 0 deletions packages/block-editor/src/utils/test/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,44 @@ describe( 'immutableSet', () => {
} );
} );
} );

describe( 'for nested falsey values', () => {
it( 'overwrites undefined values', () => {
const result = immutableSet( { test: undefined }, 'test', 1 );

expect( result ).toEqual( { test: 1 } );
} );

it( 'overwrites null values', () => {
const result = immutableSet( { test: null }, 'test', 1 );

expect( result ).toEqual( { test: 1 } );
} );

it( 'overwrites false values', () => {
const result = immutableSet( { test: false }, 'test', 1 );

expect( result ).toEqual( { test: 1 } );
} );

it( 'overwrites `0` values', () => {
const result = immutableSet( { test: 0 }, 'test', 1 );

expect( result ).toEqual( { test: 1 } );
} );

it( 'overwrites empty string values', () => {
const result = immutableSet( { test: '' }, 'test', 1 );

expect( result ).toEqual( { test: 1 } );
} );

it( 'overwrites NaN values', () => {
const result = immutableSet( { test: NaN }, 'test', 1 );

expect( result ).toEqual( { test: 1 } );
} );
} );
} );

describe( 'does not mutate the original object', () => {
Expand Down