From 141927ede8de0cceb9105a0abd80db80ca9090f2 Mon Sep 17 00:00:00 2001 From: ntsekouras Date: Mon, 9 Nov 2020 12:35:47 +0200 Subject: [PATCH] change getBlockVariations + add tests --- packages/blocks/src/store/selectors.js | 12 +- packages/blocks/src/store/test/selectors.js | 139 ++++++++++++++------ 2 files changed, 107 insertions(+), 44 deletions(-) diff --git a/packages/blocks/src/store/selectors.js b/packages/blocks/src/store/selectors.js index 21e88311b522f..08d11b361269f 100644 --- a/packages/blocks/src/store/selectors.js +++ b/packages/blocks/src/store/selectors.js @@ -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 ); } /** diff --git a/packages/blocks/src/store/test/selectors.js b/packages/blocks/src/store/test/selectors.js index fde5d92253622..3f19b1d47988e 100644 --- a/packages/blocks/src/store/test/selectors.js +++ b/packages/blocks/src/store/test/selectors.js @@ -11,6 +11,7 @@ import deepFreeze from 'deep-freeze'; import { getBlockSupport, getChildBlockNames, + getBlockVariations, getDefaultBlockVariation, getGroupingBlockName, isMatchingSearchTerm, @@ -220,7 +221,7 @@ describe( 'selectors', () => { } ); } ); - describe( 'getDefaultBlockVariation', () => { + describe( 'Testing block variations selectors', () => { const blockName = 'block/name'; const createBlockVariationsState = ( variations ) => { return deepFreeze( { @@ -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 ); + } ); } ); } );