Skip to content

Commit

Permalink
change getBlockVariations + add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ntsekouras committed Nov 9, 2020
1 parent 135dbc2 commit 141927e
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 44 deletions.
12 changes: 9 additions & 3 deletions packages/blocks/src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,15 @@ export function getBlockVariations( state, blockName, scope ) {
if ( ! variations || ! scope ) {
return variations;
}
return variations.filter( ( variation ) => {
return ! variation.scope || variation.scope.includes( scope );
} );
// There are cases like `transform` where we want strict filtering of
// block variations and not return all the variations that do not have
// the `scope` set.
const variationFilterCb =
scope === 'transform'
? ( variation ) => variation.scope?.includes( scope )
: ( variation ) =>
! variation.scope || variation.scope.includes( scope );
return variations.filter( variationFilterCb );
}

/**
Expand Down
139 changes: 98 additions & 41 deletions packages/blocks/src/store/test/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import deepFreeze from 'deep-freeze';
import {
getBlockSupport,
getChildBlockNames,
getBlockVariations,
getDefaultBlockVariation,
getGroupingBlockName,
isMatchingSearchTerm,
Expand Down Expand Up @@ -220,7 +221,7 @@ describe( 'selectors', () => {
} );
} );

describe( 'getDefaultBlockVariation', () => {
describe( 'Testing block variations selectors', () => {
const blockName = 'block/name';
const createBlockVariationsState = ( variations ) => {
return deepFreeze( {
Expand All @@ -238,55 +239,111 @@ describe( 'selectors', () => {
const thirdBlockVariation = {
name: 'third-block-variation',
};
describe( 'getBlockVariations', () => {
it( 'should return undefined if no variations exists', () => {
expect(
getBlockVariations( { blockVariations: {} }, blockName )
).toBeUndefined();
} );
it( 'should return all variations if scope is not provided', () => {
const variations = [
firstBlockVariation,
secondBlockVariation,
];
const state = createBlockVariationsState( variations );
expect( getBlockVariations( state, blockName ) ).toEqual(
variations
);
} );
it( 'should strict filter the variations if `transform` is passed', () => {
const variations = [
{ ...firstBlockVariation, scope: [ 'transform' ] },
secondBlockVariation,
thirdBlockVariation,
];
const state = createBlockVariationsState( variations );
expect(
getBlockVariations( state, blockName, 'transform' )
).toEqual( [
expect.objectContaining( {
name: firstBlockVariation.name,
} ),
] );
} );
// Testing the assumption that no scope means all scopes, except the `transform` scope.
it( 'should return variations with scope not set at all or explicitly set', () => {
const variations = [
{ ...firstBlockVariation, scope: [ 'inserter' ] },
{ name: 'only-block', scope: [ 'block' ] },
{
name: 'multiple-scopes-with-block',
scope: [ 'transform', 'block' ],
},
{ name: 'no-scope' },
];
const state = createBlockVariationsState( variations );
const expectedVariationNames = [
'only-block',
'multiple-scopes-with-block',
'no-scope',
];
const result = getBlockVariations( state, blockName, 'block' );
expect( result ).toHaveLength( 3 );
expect( result.map( ( { name } ) => name ) ).toEqual(
expect.arrayContaining( expectedVariationNames )
);
} );
} );
describe( 'getDefaultBlockVariation', () => {
it( 'should return the default variation when set', () => {
const defaultBlockVariation = {
...secondBlockVariation,
isDefault: true,
};
const state = createBlockVariationsState( [
firstBlockVariation,
defaultBlockVariation,
thirdBlockVariation,
] );

it( 'should return the default variation when set', () => {
const defaultBlockVariation = {
...secondBlockVariation,
isDefault: true,
};
const state = createBlockVariationsState( [
firstBlockVariation,
defaultBlockVariation,
thirdBlockVariation,
] );

const result = getDefaultBlockVariation( state, blockName );
const result = getDefaultBlockVariation( state, blockName );

expect( result ).toEqual( defaultBlockVariation );
} );
expect( result ).toEqual( defaultBlockVariation );
} );

it( 'should return the last variation when multiple default variations added', () => {
const defaultBlockVariation = {
...thirdBlockVariation,
isDefault: true,
};
const state = createBlockVariationsState( [
{
...firstBlockVariation,
isDefault: true,
},
{
...secondBlockVariation,
it( 'should return the last variation when multiple default variations added', () => {
const defaultBlockVariation = {
...thirdBlockVariation,
isDefault: true,
},
defaultBlockVariation,
] );
};
const state = createBlockVariationsState( [
{
...firstBlockVariation,
isDefault: true,
},
{
...secondBlockVariation,
isDefault: true,
},
defaultBlockVariation,
] );

const result = getDefaultBlockVariation( state, blockName );
const result = getDefaultBlockVariation( state, blockName );

expect( result ).toEqual( defaultBlockVariation );
} );
expect( result ).toEqual( defaultBlockVariation );
} );

it( 'should return the first variation when no default variation set', () => {
const state = createBlockVariationsState( [
firstBlockVariation,
secondBlockVariation,
thirdBlockVariation,
] );
it( 'should return the first variation when no default variation set', () => {
const state = createBlockVariationsState( [
firstBlockVariation,
secondBlockVariation,
thirdBlockVariation,
] );

const result = getDefaultBlockVariation( state, blockName );
const result = getDefaultBlockVariation( state, blockName );

expect( result ).toEqual( firstBlockVariation );
expect( result ).toEqual( firstBlockVariation );
} );
} );
} );

Expand Down

0 comments on commit 141927e

Please sign in to comment.