From eab4da1305d1f1b75c745aeb0a17a2af48b888bf Mon Sep 17 00:00:00 2001 From: Andrew Serong <14988353+andrewserong@users.noreply.github.com> Date: Thu, 30 Mar 2023 20:45:16 +1100 Subject: [PATCH] Global styles: Fix error when updating padding input in global styles (#49465) * Fix padding input in global styles * Add tests --------- Co-authored-by: Daniel Richards --- packages/block-editor/src/utils/object.js | 2 +- .../block-editor/src/utils/test/object.js | 38 +++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/packages/block-editor/src/utils/object.js b/packages/block-editor/src/utils/object.js index 5c6afbc7ba4ff..5fe357e5e9f61 100644 --- a/packages/block-editor/src/utils/object.js +++ b/packages/block-editor/src/utils/object.js @@ -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' ) { return { ...Object.fromEntries( Object.entries( object ).map( ( [ key, value ] ) => [ diff --git a/packages/block-editor/src/utils/test/object.js b/packages/block-editor/src/utils/test/object.js index be5360064ac08..14562400741b5 100644 --- a/packages/block-editor/src/utils/test/object.js +++ b/packages/block-editor/src/utils/test/object.js @@ -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', () => {