diff --git a/packages/block-editor/src/store/selectors.js b/packages/block-editor/src/store/selectors.js index 564f6bd1a8970..aa4f2d3c87b22 100644 --- a/packages/block-editor/src/store/selectors.js +++ b/packages/block-editor/src/store/selectors.js @@ -1884,7 +1884,6 @@ export const __experimentalGetPatternsByBlockTypes = createSelector( * * @return {WPBlockPattern[]} Items that are eligible for a pattern transformation. */ -// TODO tests export const __experimentalGetPatternTransformItems = createSelector( ( state, blocks, rootClientId = null ) => { if ( ! blocks ) return EMPTY_ARRAY; diff --git a/packages/block-editor/src/store/test/selectors.js b/packages/block-editor/src/store/test/selectors.js index 5339ffb49943c..0e67cc0f1d551 100644 --- a/packages/block-editor/src/store/test/selectors.js +++ b/packages/block-editor/src/store/test/selectors.js @@ -75,6 +75,7 @@ const { __experimentalGetPatternsByBlockTypes, __unstableGetClientIdWithClientIdsTree, __unstableGetClientIdsTree, + __experimentalGetPatternTransformItems, wasBlockJustInserted, } = selectors; @@ -3485,6 +3486,154 @@ describe( 'selectors', () => { ); } ); } ); + describe( '__experimentalGetPatternTransformItems', () => { + const state = { + blocks: { + byClientId: { + block1: { name: 'core/test-block-a' }, + block2: { name: 'core/test-block-b' }, + }, + controlledInnerBlocks: { 'block2-clientId': true }, + }, + blockListSettings: { + block1: { + allowedBlocks: [ 'core/test-block-b' ], + }, + }, + settings: { + __experimentalBlockPatterns: [ + { + name: 'pattern-a', + blockTypes: [ 'test/block-a' ], + title: 'pattern a', + content: + '', + }, + { + name: 'pattern-b', + blockTypes: [ 'test/block-b' ], + title: 'pattern b', + content: + '', + }, + { + name: 'pattern-c', + title: 'pattern c', + blockTypes: [ 'test/block-a' ], + content: + '', + }, + { + name: 'pattern-mix', + title: 'pattern mix', + blockTypes: [ + 'core/test-block-a', + 'core/test-block-b', + ], + content: + '', + }, + ], + }, + }; + describe( 'should return empty array', () => { + it( 'when no blocks are selected', () => { + expect( + __experimentalGetPatternTransformItems( state ) + ).toEqual( [] ); + } ); + it( 'when a selected block has inner blocks', () => { + const blocks = [ + { name: 'core/test-block-a', innerBlocks: [] }, + { + name: 'core/test-block-b', + innerBlocks: [ { name: 'some inner block' } ], + }, + ]; + expect( + __experimentalGetPatternTransformItems( state, blocks ) + ).toEqual( [] ); + } ); + it( 'when a selected block has controlled inner blocks', () => { + const blocks = [ + { name: 'core/test-block-a', innerBlocks: [] }, + { + name: 'core/test-block-b', + clientId: 'block2-clientId', + innerBlocks: [], + }, + ]; + expect( + __experimentalGetPatternTransformItems( state, blocks ) + ).toEqual( [] ); + } ); + it( 'when no patterns are available based on the selected blocks', () => { + const blocks = [ + { name: 'block-with-no-patterns', innerBlocks: [] }, + ]; + expect( + __experimentalGetPatternTransformItems( state, blocks ) + ).toEqual( [] ); + } ); + } ); + describe( 'should return proper results', () => { + it( 'when a single block is selected', () => { + const blocks = [ + { name: 'core/test-block-b', innerBlocks: [] }, + ]; + const patterns = __experimentalGetPatternTransformItems( + state, + blocks + ); + expect( patterns ).toHaveLength( 1 ); + expect( patterns[ 0 ] ).toEqual( + expect.objectContaining( { + name: 'pattern-mix', + } ) + ); + } ); + it( 'when different multiple blocks are selected', () => { + const blocks = [ + { name: 'core/test-block-b', innerBlocks: [] }, + { name: 'test/block-b', innerBlocks: [] }, + { name: 'some other block', innerBlocks: [] }, + ]; + const patterns = __experimentalGetPatternTransformItems( + state, + blocks + ); + expect( patterns ).toHaveLength( 2 ); + expect( patterns ).toEqual( + expect.arrayContaining( [ + expect.objectContaining( { + name: 'pattern-mix', + } ), + expect.objectContaining( { + name: 'pattern-b', + } ), + ] ) + ); + } ); + it( 'when multiple blocks are selected containing multiple times the same block', () => { + const blocks = [ + { name: 'core/test-block-b', innerBlocks: [] }, + { name: 'some other block', innerBlocks: [] }, + { name: 'core/test-block-a', innerBlocks: [] }, + { name: 'core/test-block-b', innerBlocks: [] }, + ]; + const patterns = __experimentalGetPatternTransformItems( + state, + blocks + ); + expect( patterns ).toHaveLength( 1 ); + expect( patterns[ 0 ] ).toEqual( + expect.objectContaining( { + name: 'pattern-mix', + } ) + ); + } ); + } ); + } ); describe( 'wasBlockJustInserted', () => { it( 'should return true if the client id passed to wasBlockJustInserted is found within the state', () => {