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

[Automated Testing]: Increase test coverage for pattern transforms #31689

Merged
merged 2 commits into from
May 14, 2021
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
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