Skip to content

Commit

Permalink
Fix canInsertBlockType selector returning true for blocks that don't …
Browse files Browse the repository at this point in the history
…allow inner blocks (#24514)

* Fix canInsertBlockType returns incorrect value for block that does not support inner blocks

* Update unit tests
  • Loading branch information
talldan authored Aug 12, 2020
1 parent be99466 commit 48ff9a1
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
7 changes: 7 additions & 0 deletions packages/block-editor/src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -1164,6 +1164,13 @@ const canInsertBlockTypeUnmemoized = (
}

const parentBlockListSettings = getBlockListSettings( state, rootClientId );

// The parent block doesn't have settings indicating it doesn't support
// inner blocks, return false.
if ( rootClientId && parentBlockListSettings === undefined ) {
return false;
}

const parentAllowedBlocks = get( parentBlockListSettings, [
'allowedBlocks',
] );
Expand Down
33 changes: 30 additions & 3 deletions packages/block-editor/src/store/test/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -2160,7 +2160,7 @@ describe( 'selectors', () => {
).toBe( false );
} );

it( 'should allow blocks that restrict parent to be inserted into an allowed parent', () => {
it( 'should allow blocks to be inserted into an allowed parent', () => {
const state = {
blocks: {
byClientId: {
Expand All @@ -2170,7 +2170,29 @@ describe( 'selectors', () => {
block1: {},
},
},
blockListSettings: {},
blockListSettings: {
block1: {},
},
settings: {},
};
expect(
canInsertBlockType( state, 'core/test-block-c', 'block1' )
).toBe( true );
} );

it( 'should deny blocks from being inserted into a block that does not allow inner blocks', () => {
const state = {
blocks: {
byClientId: {
block1: { name: 'core/test-block-b' },
},
attributes: {
block1: {},
},
},
blockListSettings: {
block1: {},
},
settings: {},
};
expect(
Expand Down Expand Up @@ -2441,12 +2463,16 @@ describe( 'selectors', () => {
preferences: {
insertUsage: {},
},
blockListSettings: {},
blockListSettings: {
block3: {},
block4: {},
},
};

const stateSecondBlockRestricted = {
...state,
blockListSettings: {
...state.blockListSettings,
block4: {
allowedBlocks: [ 'core/test-block-b' ],
},
Expand All @@ -2472,6 +2498,7 @@ describe( 'selectors', () => {
stateSecondBlockRestricted,
'block4'
);
expect( secondBlockFirstCall ).not.toBe( secondBlockSecondCall );
expect( secondBlockFirstCall.map( ( item ) => item.id ) ).toEqual( [
'core/test-block-a',
'core/test-block-b',
Expand Down

0 comments on commit 48ff9a1

Please sign in to comment.