Skip to content

Commit

Permalink
Try: removing the multi-block type check to make __experimentalConver…
Browse files Browse the repository at this point in the history
…t more useful (#22577)

* Try: removing the multi-block type check to make __experimentalConvert more useful

* Remove unneeded isBlockSelectionOfSameType tests

* Add additional test cases for __experimentalConvert to catch * and block matches

* fix whitespace for linter
  • Loading branch information
gwwar authored Jun 5, 2020
1 parent a698810 commit 495ff0e
Show file tree
Hide file tree
Showing 2 changed files with 177 additions and 72 deletions.
28 changes: 0 additions & 28 deletions packages/blocks/src/api/factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -284,23 +284,6 @@ export const isWildcardBlockTransform = ( t ) =>
export const isContainerGroupBlock = ( name ) =>
name === getGroupingBlockName();

/**
* Determines whether the provided Blocks are of the same type
* (eg: all `core/paragraph`).
*
* @param {Array} blocksArray the Block definitions
*
* @return {boolean} whether or not the given Blocks pass the criteria
*/
export const isBlockSelectionOfSameType = ( blocksArray = [] ) => {
if ( ! blocksArray.length ) {
return false;
}
const sourceName = blocksArray[ 0 ].name;

return every( blocksArray, [ 'name', sourceName ] );
};

/**
* Returns an array of block types that the set of blocks received as argument
* can be transformed into.
Expand Down Expand Up @@ -407,17 +390,6 @@ export function switchToBlockType( blocks, name ) {
const firstBlock = blocksArray[ 0 ];
const sourceName = firstBlock.name;

// Unless it's a Grouping Block then for multi block selections
// check that all Blocks are of the same type otherwise
// we can't run a conversion
if (
! isContainerGroupBlock( name ) &&
isMultiBlock &&
! isBlockSelectionOfSameType( blocksArray )
) {
return null;
}

// Find the right transformation by giving priority to the "to"
// transformation.
const transformationsFrom = getBlockTransforms( 'from', name );
Expand Down
221 changes: 177 additions & 44 deletions packages/blocks/src/api/test/factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import {
findTransform,
isWildcardBlockTransform,
isContainerGroupBlock,
isBlockSelectionOfSameType,
} from '../factory';
import {
getBlockType,
Expand Down Expand Up @@ -1559,6 +1558,183 @@ describe( 'block factory', () => {
);
} );

it( 'should call "__experimentalConvert" with mixed block types and wildcard', () => {
const convertSpy = jest.fn( ( blocks ) => {
const groupInnerBlocks = blocks.map(
( { name, attributes, innerBlocks } ) => {
return createBlock( name, attributes, innerBlocks );
}
);

return createBlock(
'core/test-group-block',
{},
groupInnerBlocks
);
} );
const transformSpy = jest.fn();

registerBlockType( 'core/test-group-block', {
attributes: {
value: {
type: 'string',
},
},
transforms: {
from: [
{
type: 'block',
blocks: [ '*' ],
isMultiBlock: true,
__experimentalConvert: convertSpy,
transform: transformSpy,
},
],
},
save: noop,
category: 'text',
title: 'Test Group Block',
} );

registerBlockType( 'core/text-block', defaultBlockSettings );
registerBlockType( 'core/image-block', defaultBlockSettings );

const numOfBlocksToGroup = 4;
const blocks = times( numOfBlocksToGroup, ( index ) => {
return createBlock(
index % 2 ? 'core/text-block' : 'core/image-block',
{
value: `block-value-${ index + 1 }`,
}
);
} );

const transformedBlocks = switchToBlockType(
blocks,
'core/test-group-block'
);

expect( transformedBlocks ).toHaveLength( 1 );
expect( convertSpy.mock.calls ).toHaveLength( 1 );
expect( transformSpy.mock.calls ).toHaveLength( 0 );
} );

it( 'should call "__experimentalConvert" with same block types', () => {
const convertSpy = jest.fn( ( blocks ) => {
const groupInnerBlocks = blocks.map(
( { name, attributes, innerBlocks } ) => {
return createBlock( name, attributes, innerBlocks );
}
);

return createBlock(
'core/test-group-block',
{},
groupInnerBlocks
);
} );
const transformSpy = jest.fn();

registerBlockType( 'core/test-group-block', {
attributes: {
value: {
type: 'string',
},
},
transforms: {
from: [
{
type: 'block',
blocks: [ 'core/text-block' ],
isMultiBlock: true,
__experimentalConvert: convertSpy,
transform: transformSpy,
},
],
},
save: noop,
category: 'text',
title: 'Test Group Block',
} );

registerBlockType( 'core/text-block', defaultBlockSettings );
registerBlockType( 'core/image-block', defaultBlockSettings );

const numOfBlocksToGroup = 4;
const blocks = times( numOfBlocksToGroup, ( index ) => {
return createBlock( 'core/text-block', {
value: `block-value-${ index + 1 }`,
} );
} );

const transformedBlocks = switchToBlockType(
blocks,
'core/test-group-block'
);

expect( transformedBlocks ).toHaveLength( 1 );
expect( convertSpy.mock.calls ).toHaveLength( 1 );
expect( transformSpy.mock.calls ).toHaveLength( 0 );
} );

it( 'should not call "__experimentalConvert" with non-matching block types', () => {
const convertSpy = jest.fn( ( blocks ) => {
const groupInnerBlocks = blocks.map(
( { name, attributes, innerBlocks } ) => {
return createBlock( name, attributes, innerBlocks );
}
);

return createBlock(
'core/test-group-block',
{},
groupInnerBlocks
);
} );
const transformSpy = jest.fn();

registerBlockType( 'core/test-group-block', {
attributes: {
value: {
type: 'string',
},
},
transforms: {
from: [
{
type: 'block',
blocks: [ 'core/image-block' ],
isMultiBlock: true,
__experimentalConvert: convertSpy,
transform: transformSpy,
},
],
},
save: noop,
category: 'text',
title: 'Test Group Block',
} );

registerBlockType( 'core/text-block', defaultBlockSettings );
registerBlockType( 'core/image-block', defaultBlockSettings );

const numOfBlocksToGroup = 4;
const blocks = times( numOfBlocksToGroup, ( index ) => {
return createBlock( 'core/text-block', {
value: `block-value-${ index + 1 }`,
} );
} );

const transformedBlocks = switchToBlockType(
blocks,
'core/test-group-block'
);

expect( transformedBlocks ).toEqual( null );
expect( convertSpy.mock.calls ).toHaveLength( 0 );
expect( transformSpy.mock.calls ).toHaveLength( 0 );
} );

it( 'should prefer "__experimentalConvert" method over "transform" method when running a transformation', () => {
const convertSpy = jest.fn( ( blocks ) => {
const groupInnerBlocks = blocks.map(
Expand Down Expand Up @@ -1828,47 +2004,4 @@ describe( 'block factory', () => {
expect( isContainerGroupBlock( 'core/group' ) ).toBe( false );
} );
} );

describe( 'isBlockSelectionOfSameType', () => {
it( 'should return false when all blocks do not match the name of the first block', () => {
const blocks = [
{
name: 'core/test-block',
},
{
name: 'core/test-block',
},
{
name: 'core/test-block',
},
{
name: 'core/another-block',
},
{
name: 'core/test-block',
},
];

expect( isBlockSelectionOfSameType( blocks ) ).toBe( false );
} );

it( 'should return true when all blocks match the name of the first block', () => {
const blocks = [
{
name: 'core/test-block',
},
{
name: 'core/test-block',
},
{
name: 'core/test-block',
},
{
name: 'core/test-block',
},
];

expect( isBlockSelectionOfSameType( blocks ) ).toBe( true );
} );
} );
} );

0 comments on commit 495ff0e

Please sign in to comment.