Skip to content

Commit

Permalink
[Automated Testing]: Increase test coverage for pattern transforms (#…
Browse files Browse the repository at this point in the history
…31689)

* [Automated Testing]: Increase test coverage for pattern transforms

* Update packages/block-editor/src/store/test/selectors.js

Co-authored-by: Miguel Fonseca <[email protected]>

Co-authored-by: Miguel Fonseca <[email protected]>
  • Loading branch information
ntsekouras and mcsf authored May 14, 2021
1 parent b684490 commit d21299e
Show file tree
Hide file tree
Showing 2 changed files with 149 additions and 1 deletion.
1 change: 0 additions & 1 deletion packages/block-editor/src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -1884,7 +1884,6 @@ export const __experimentalGetPatternsByBlockTypes = createSelector(
*
* @return {WPBlockPattern[]} Items that are eligible for a pattern transformation.
*/
// TODO tests
export const __experimentalGetPatternTransformItems = createSelector(
( state, blocks, rootClientId = null ) => {
if ( ! blocks ) return EMPTY_ARRAY;
Expand Down
149 changes: 149 additions & 0 deletions packages/block-editor/src/store/test/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ const {
__experimentalGetPatternsByBlockTypes,
__unstableGetClientIdWithClientIdsTree,
__unstableGetClientIdsTree,
__experimentalGetPatternTransformItems,
wasBlockJustInserted,
} = selectors;

Expand Down Expand Up @@ -3485,6 +3486,154 @@ describe( 'selectors', () => {
);
} );
} );
describe( '__experimentalGetPatternTransformItems', () => {
const state = {
blocks: {
byClientId: {
block1: { name: 'core/test-block-a' },
block2: { name: 'core/test-block-b' },
},
controlledInnerBlocks: { 'block2-clientId': true },
},
blockListSettings: {
block1: {
allowedBlocks: [ 'core/test-block-b' ],
},
},
settings: {
__experimentalBlockPatterns: [
{
name: 'pattern-a',
blockTypes: [ 'test/block-a' ],
title: 'pattern a',
content:
'<!-- wp:test-block-a --><!-- /wp:test-block-a -->',
},
{
name: 'pattern-b',
blockTypes: [ 'test/block-b' ],
title: 'pattern b',
content:
'<!-- wp:test-block-b --><!-- /wp:test-block-b -->',
},
{
name: 'pattern-c',
title: 'pattern c',
blockTypes: [ 'test/block-a' ],
content:
'<!-- wp:test-block-b --><!-- /wp:test-block-b -->',
},
{
name: 'pattern-mix',
title: 'pattern mix',
blockTypes: [
'core/test-block-a',
'core/test-block-b',
],
content:
'<!-- wp:test-block-b --><!-- /wp:test-block-b -->',
},
],
},
};
describe( 'should return empty array', () => {
it( 'when no blocks are selected', () => {
expect(
__experimentalGetPatternTransformItems( state )
).toEqual( [] );
} );
it( 'when a selected block has inner blocks', () => {
const blocks = [
{ name: 'core/test-block-a', innerBlocks: [] },
{
name: 'core/test-block-b',
innerBlocks: [ { name: 'some inner block' } ],
},
];
expect(
__experimentalGetPatternTransformItems( state, blocks )
).toEqual( [] );
} );
it( 'when a selected block has controlled inner blocks', () => {
const blocks = [
{ name: 'core/test-block-a', innerBlocks: [] },
{
name: 'core/test-block-b',
clientId: 'block2-clientId',
innerBlocks: [],
},
];
expect(
__experimentalGetPatternTransformItems( state, blocks )
).toEqual( [] );
} );
it( 'when no patterns are available based on the selected blocks', () => {
const blocks = [
{ name: 'block-with-no-patterns', innerBlocks: [] },
];
expect(
__experimentalGetPatternTransformItems( state, blocks )
).toEqual( [] );
} );
} );
describe( 'should return proper results', () => {
it( 'when a single block is selected', () => {
const blocks = [
{ name: 'core/test-block-b', innerBlocks: [] },
];
const patterns = __experimentalGetPatternTransformItems(
state,
blocks
);
expect( patterns ).toHaveLength( 1 );
expect( patterns[ 0 ] ).toEqual(
expect.objectContaining( {
name: 'pattern-mix',
} )
);
} );
it( 'when different multiple blocks are selected', () => {
const blocks = [
{ name: 'core/test-block-b', innerBlocks: [] },
{ name: 'test/block-b', innerBlocks: [] },
{ name: 'some other block', innerBlocks: [] },
];
const patterns = __experimentalGetPatternTransformItems(
state,
blocks
);
expect( patterns ).toHaveLength( 2 );
expect( patterns ).toEqual(
expect.arrayContaining( [
expect.objectContaining( {
name: 'pattern-mix',
} ),
expect.objectContaining( {
name: 'pattern-b',
} ),
] )
);
} );
it( 'when multiple blocks are selected containing multiple times the same block', () => {
const blocks = [
{ name: 'core/test-block-b', innerBlocks: [] },
{ name: 'some other block', innerBlocks: [] },
{ name: 'core/test-block-a', innerBlocks: [] },
{ name: 'core/test-block-b', innerBlocks: [] },
];
const patterns = __experimentalGetPatternTransformItems(
state,
blocks
);
expect( patterns ).toHaveLength( 1 );
expect( patterns[ 0 ] ).toEqual(
expect.objectContaining( {
name: 'pattern-mix',
} )
);
} );
} );
} );

describe( 'wasBlockJustInserted', () => {
it( 'should return true if the client id passed to wasBlockJustInserted is found within the state', () => {
Expand Down

0 comments on commit d21299e

Please sign in to comment.