From df296eb0c87a64a7cc1278ab1fd957a93c500a67 Mon Sep 17 00:00:00 2001 From: ntsekouras Date: Mon, 14 Dec 2020 15:02:18 +0200 Subject: [PATCH] getBlockTransformItems tests --- .../block-editor/src/store/test/selectors.js | 217 ++++++++++++++++++ 1 file changed, 217 insertions(+) diff --git a/packages/block-editor/src/store/test/selectors.js b/packages/block-editor/src/store/test/selectors.js index d11e53a9e74853..a0803a0e5b2c20 100644 --- a/packages/block-editor/src/store/test/selectors.js +++ b/packages/block-editor/src/store/test/selectors.js @@ -61,6 +61,7 @@ const { canInsertBlockType, canInsertBlocks, getInserterItems, + getBlockTransformItems, isValidTemplate, getTemplate, getTemplateLock, @@ -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 = {