Skip to content

Commit

Permalink
Lodash: Remove _.merge() from getMappedColumnWidths() (#48032)
Browse files Browse the repository at this point in the history
  • Loading branch information
tyxla authored Feb 20, 2023
1 parent 7f4bc29 commit 4aff318
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 8 deletions.
54 changes: 54 additions & 0 deletions packages/block-library/src/columns/test/utils.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/**
* External dependencies
*/
import deepFreeze from 'deep-freeze';

/**
* Internal dependencies
*/
Expand Down Expand Up @@ -280,4 +285,53 @@ describe( 'getMappedColumnWidths', () => {
{ clientId: 'b', attributes: { width: '35%' } },
] );
} );

it( 'always returns new objects and does not mutate input blocks', () => {
const blocks = [
deepFreeze( { clientId: 'a', attributes: { width: 30 } } ),
deepFreeze( { clientId: 'b', attributes: { width: 40 } } ),
];
const widths = {
a: 25,
b: 35,
};

const result = getMappedColumnWidths( blocks, widths );

expect( blocks[ 0 ] ).not.toBe( result[ 0 ] );
expect( blocks[ 1 ] ).not.toBe( result[ 1 ] );
} );

it( 'merges to block attributes if original blocks do not have any attributes', () => {
const blocks = [ { clientId: 'a' }, { clientId: 'b' } ];
const widths = {
a: 25,
b: 35,
};

const result = getMappedColumnWidths( blocks, widths );

expect( result ).toEqual( [
{ clientId: 'a', attributes: { width: '25%' } },
{ clientId: 'b', attributes: { width: '35%' } },
] );
} );

it( 'merges to block attributes if original blocks do not have a width attribute', () => {
const blocks = [
{ clientId: 'a', attributes: { align: 'left' } },
{ clientId: 'b', attributes: { align: 'right' } },
];
const widths = {
a: 25,
b: 35,
};

const result = getMappedColumnWidths( blocks, widths );

expect( result ).toEqual( [
{ clientId: 'a', attributes: { align: 'left', width: '25%' } },
{ clientId: 'b', attributes: { align: 'right', width: '35%' } },
] );
} );
} );
16 changes: 8 additions & 8 deletions packages/block-library/src/columns/utils.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* External dependencies
*/
import { merge, mapValues } from 'lodash';
import { mapValues } from 'lodash';

/**
* Returns a column width attribute value rounded to standard precision.
Expand Down Expand Up @@ -121,13 +121,13 @@ export function hasExplicitPercentColumnWidths( blocks ) {
* @return {WPBlock[]} blocks Mapped block objects.
*/
export function getMappedColumnWidths( blocks, widths ) {
return blocks.map( ( block ) =>
merge( {}, block, {
attributes: {
width: `${ widths[ block.clientId ] }%`,
},
} )
);
return blocks.map( ( block ) => ( {
...block,
attributes: {
...block.attributes,
width: `${ widths[ block.clientId ] }%`,
},
} ) );
}

/**
Expand Down

1 comment on commit 4aff318

@github-actions
Copy link

Choose a reason for hiding this comment

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

Flaky tests detected in 4aff318.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/4221208435
📝 Reported issues:

Please sign in to comment.