diff --git a/packages/blocks/src/store/reducer.js b/packages/blocks/src/store/reducer.js index c00810c534d55d..ed7b4e74b0998c 100644 --- a/packages/blocks/src/store/reducer.js +++ b/packages/blocks/src/store/reducer.js @@ -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 || diff --git a/packages/blocks/src/store/test/reducer.js b/packages/blocks/src/store/test/reducer.js index babaaad4e0e0d7..1a295f1efc30d7 100644 --- a/packages/blocks/src/store/test/reducer.js +++ b/packages/blocks/src/store/test/reducer.js @@ -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( [ {