Skip to content

Commit

Permalink
Lodash: Refactor away from _.unionBy() (#43735)
Browse files Browse the repository at this point in the history
* Lodash: Refactor away from _.unionBy()

* Fix a few tests
  • Loading branch information
tyxla authored Sep 1, 2022
1 parent 01cebaa commit c800e19
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 12 deletions.
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ module.exports = {
'toString',
'trim',
'truncate',
'unionBy',
'uniq',
'uniqBy',
'uniqueId',
Expand Down
4 changes: 4 additions & 0 deletions packages/block-library/src/block/test/edit.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ describe( 'Reusable block', () => {
response = [ reusableBlockMock1, reusableBlockMock2 ];
} else if ( path.startsWith( '/wp/v2/blocks/1' ) ) {
response = reusableBlockMock1;
} else if (
path.startsWith( '/wp/v2/block-patterns/categories' )
) {
response = [];
}
return Promise.resolve( response );
} );
Expand Down
17 changes: 17 additions & 0 deletions packages/block-library/src/embed/test/index.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,10 @@ const mockEmbedResponses = ( mockedResponses ) => {
] );
}

if ( path.startsWith( '/wp/v2/block-patterns/categories' ) ) {
return Promise.resolve( [] );
}

const matchedEmbedResponse = mockedResponses.find(
( mockedResponse ) =>
path ===
Expand Down Expand Up @@ -698,6 +702,9 @@ describe( 'Embed block', () => {
response = RICH_TEXT_EMBED_SUCCESS_RESPONSE;
}
}
if ( path.startsWith( '/wp/v2/block-patterns/categories' ) ) {
response = [];
}
return Promise.resolve( response );
} );

Expand All @@ -724,6 +731,9 @@ describe( 'Embed block', () => {
it( 'converts to link if preview request failed', async () => {
// Return bad response for requests to oembed endpoint.
fetchRequest.mockImplementation( ( { path } ) => {
if ( path.startsWith( '/wp/v2/block-patterns/categories' ) ) {
return Promise.resolve( [] );
}
const isEmbedRequest = path.startsWith( '/oembed/1.0/proxy' );
return Promise.resolve(
isEmbedRequest ? MOCK_BAD_WORDPRESS_RESPONSE : {}
Expand Down Expand Up @@ -761,6 +771,10 @@ describe( 'Embed block', () => {
response = MOCK_BAD_WORDPRESS_RESPONSE;
} else if ( matchesPath( successURL ) ) {
response = RICH_TEXT_EMBED_SUCCESS_RESPONSE;
} else if (
path.startsWith( '/wp/v2/block-patterns/categories' )
) {
response = [];
}

return Promise.resolve( response );
Expand Down Expand Up @@ -1048,6 +1062,9 @@ describe( 'Embed block', () => {
it( 'displays cannot embed on the placeholder if preview data is null', async () => {
// Return null response for requests to oembed endpoint.
fetchRequest.mockImplementation( ( { path } ) => {
if ( path.startsWith( '/wp/v2/block-patterns/categories' ) ) {
return Promise.resolve( [] );
}
const isEmbedRequest = path.startsWith( '/oembed/1.0/proxy' );
return Promise.resolve( isEmbedRequest ? EMBED_NULL_RESPONSE : {} );
} );
Expand Down
20 changes: 14 additions & 6 deletions packages/edit-site/src/components/block-editor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
* External dependencies
*/
import classnames from 'classnames';
import { unionBy } from 'lodash';

/**
* WordPress dependencies
Expand Down Expand Up @@ -80,16 +79,25 @@ export default function BlockEditor( { setIsInserterOpen } ) {
);

const blockPatterns = useMemo(
() => unionBy( settingsBlockPatterns, restBlockPatterns, 'name' ),
() =>
[
...( settingsBlockPatterns || [] ),
...( restBlockPatterns || [] ),
].filter(
( x, index, arr ) =>
index === arr.findIndex( ( y ) => x.name === y.name )
),
[ settingsBlockPatterns, restBlockPatterns ]
);

const blockPatternCategories = useMemo(
() =>
unionBy(
settingsBlockPatternCategories,
restBlockPatternCategories,
'name'
[
...( settingsBlockPatternCategories || [] ),
...( restBlockPatternCategories || [] ),
].filter(
( x, index, arr ) =>
index === arr.findIndex( ( y ) => x.name === y.name )
),
[ settingsBlockPatternCategories, restBlockPatternCategories ]
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* External dependencies
*/
import { pick, unionBy } from 'lodash';
import { pick } from 'lodash';

/**
* WordPress dependencies
Expand Down Expand Up @@ -77,16 +77,25 @@ function useBlockEditorSettings( settings, hasTemplate ) {
);

const blockPatterns = useMemo(
() => unionBy( settingsBlockPatterns, restBlockPatterns, 'name' ),
() =>
[
...( settingsBlockPatterns || [] ),
...( restBlockPatterns || [] ),
].filter(
( x, index, arr ) =>
index === arr.findIndex( ( y ) => x.name === y.name )
),
[ settingsBlockPatterns, restBlockPatterns ]
);

const blockPatternCategories = useMemo(
() =>
unionBy(
settingsBlockPatternCategories,
restBlockPatternCategories,
'name'
[
...( settingsBlockPatternCategories || [] ),
...( restBlockPatternCategories || [] ),
].filter(
( x, index, arr ) =>
index === arr.findIndex( ( y ) => x.name === y.name )
),
[ settingsBlockPatternCategories, restBlockPatternCategories ]
);
Expand Down

0 comments on commit c800e19

Please sign in to comment.