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

Fix canInsertBlockType selector returning true for blocks that don't allow inner blocks #24514

Merged
merged 2 commits into from
Aug 12, 2020
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: 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