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

Block categories - ensure that categories are unique by slug. #62954

Merged
merged 3 commits into from
Aug 2, 2024
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
7 changes: 6 additions & 1 deletion packages/blocks/src/store/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,12 @@ export const groupingBlockName = createBlockNameSetterReducer(
export function categories( state = DEFAULT_CATEGORIES, action ) {
switch ( action.type ) {
case 'SET_CATEGORIES':
return action.categories || [];
// Ensure, that categories are unique by slug.
const uniqueCategories = new Map();
( action.categories || [] ).forEach( ( category ) => {
uniqueCategories.set( category.slug, category );
} );
return [ ...uniqueCategories.values() ];
case 'UPDATE_CATEGORY': {
if (
! action.category ||
Expand Down
16 changes: 16 additions & 0 deletions packages/blocks/src/store/test/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,22 @@ describe( 'categories', () => {
expect( state ).toEqual( [ { slug: 'wings', title: 'Wings' } ] );
} );

it( 'should ensure, that categories are unique by slug', () => {
const original = deepFreeze( [
{ slug: 'chicken', title: 'Chicken' },
] );

const state = categories( original, {
type: 'SET_CATEGORIES',
categories: [ { slug: 'chicken', title: 'Another chicken' } ],
} );

expect( state ).toEqual( [
{ slug: 'chicken', title: 'Another chicken' },
] );
expect( state.length ).toBe( 1 );
} );

it( 'should add the category icon', () => {
const original = deepFreeze( [
{
Expand Down
Loading