Skip to content

Commit

Permalink
getBlockTransformItems tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ntsekouras committed Dec 14, 2020
1 parent a8e1818 commit df296eb
Showing 1 changed file with 217 additions and 0 deletions.
217 changes: 217 additions & 0 deletions packages/block-editor/src/store/test/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ const {
canInsertBlockType,
canInsertBlocks,
getInserterItems,
getBlockTransformItems,
isValidTemplate,
getTemplate,
getTemplateLock,
Expand Down Expand Up @@ -2675,6 +2676,222 @@ describe( 'selectors', () => {
} );
} );

describe( 'getBlockTransformItems', () => {
beforeAll( () => {
registerBlockType( 'core/with-tranforms-a', {
category: 'text',
title: 'Tranforms a',
edit: () => {},
save: () => {},
transforms: {
to: [
{
type: 'block',
blocks: [ 'core/with-tranforms-b' ],
transform: () => {},
},
{
type: 'block',
blocks: [ 'core/with-tranforms-c' ],
transform: () => {},
},
{
type: 'block',
blocks: [
'core/with-tranforms-b',
'core/with-tranforms-c',
],
transform: () => {},
isMultiBlock: true,
},
],
},
} );
registerBlockType( 'core/with-tranforms-b', {
category: 'text',
title: 'Tranforms b',
edit: () => {},
save: () => {},
transforms: {
to: [
{
type: 'block',
blocks: [ 'core/with-tranforms-a' ],
transform: () => {},
},
],
},
} );
registerBlockType( 'core/with-tranforms-c', {
category: 'text',
title: 'Tranforms c',
edit: () => {},
save: () => {},
transforms: {
to: [
{
type: 'block',
blocks: [ 'core/with-tranforms-a' ],
transform: () => {},
},
],
},
supports: { multiple: false },
} );
} );
afterAll( () => {
[
'core/with-tranforms-a',
'core/with-tranforms-b',
'core/with-tranforms-c',
].forEach( unregisterBlockType );
} );
it( 'should properly return block type items', () => {
const state = {
blocks: {
byClientId: {},
attributes: {},
order: {},
parents: {},
cache: {},
},
settings: {},
preferences: {},
blockListSettings: {},
};
const blocks = [ { name: 'core/with-tranforms-a' } ];
const items = getBlockTransformItems( state, blocks );
expect( items ).toHaveLength( 2 );
const returnedProps = Object.keys( items[ 0 ] );
// Verify we have only the wanted props.
expect( returnedProps ).toHaveLength( 6 );
expect( returnedProps ).toEqual(
expect.arrayContaining( [
'id',
'name',
'title',
'icon',
'frecency',
'isDisabled',
] )
);
expect( items ).toEqual(
expect.arrayContaining( [
expect.objectContaining( {
name: 'core/with-tranforms-b',
} ),
expect.objectContaining( {
name: 'core/with-tranforms-c',
} ),
] )
);
} );
it( 'should return only eligible blocks for transformation - `allowedBlocks`', () => {
const state = {
blocks: {
byClientId: {
block1: { name: 'core/with-tranforms-b' },
block2: { name: 'core/with-tranforms-a' },
},
attributes: {
block1: {},
block2: {},
},
order: {},
parents: {
block1: '',
block2: 'block1',
},
cache: {},
controlledInnerBlocks: {},
},
settings: {},
preferences: {},
blockListSettings: {
block1: {
allowedBlocks: [ 'core/with-tranforms-c' ],
},
block2: {},
},
};
const blocks = [
{ clientId: 'block2', name: 'core/with-tranforms-a' },
];
const items = getBlockTransformItems( state, blocks, 'block1' );
expect( items ).toHaveLength( 1 );
expect( items[ 0 ].name ).toEqual( 'core/with-tranforms-c' );
} );
it( 'should take into account the usage of blocks settings `multiple` - if multiple blocks of the same type are allowed', () => {
const state = {
blocks: {
byClientId: {
block1: {
clientId: 'block1',
name: 'core/with-tranforms-c',
},
},
attributes: {
block1: { attribute: {} },
},
order: {
'': [ 'block1' ],
},
cache: {
block1: {},
},
controlledInnerBlocks: {},
},
preferences: {
insertUsage: {},
},
blockListSettings: {},
settings: {},
};
const blocks = [ { name: 'core/with-tranforms-a' } ];
const items = getBlockTransformItems( state, blocks );
expect( items ).toHaveLength( 2 );
expect( items ).toEqual(
expect.arrayContaining( [
expect.objectContaining( {
name: 'core/with-tranforms-b',
isDisabled: false,
} ),
expect.objectContaining( {
name: 'core/with-tranforms-c',
isDisabled: true,
} ),
] )
);
} );
it( 'should set frecency', () => {
const state = {
blocks: {
byClientId: {},
attributes: {},
order: {},
parents: {},
cache: {},
},
preferences: {
insertUsage: {
'core/with-tranforms-a': { count: 10, time: 1000 },
},
},
blockListSettings: {},
settings: {},
};
const blocks = [ { name: 'core/with-tranforms-c' } ];
const items = getBlockTransformItems( state, blocks );
expect( items ).toHaveLength( 1 );
expect( items[ 0 ] ).toEqual(
expect.objectContaining( {
name: 'core/with-tranforms-a',
frecency: 2.5,
} )
);
} );
} );

describe( 'isValidTemplate', () => {
it( 'should return true if template is valid', () => {
const state = {
Expand Down

0 comments on commit df296eb

Please sign in to comment.