diff --git a/packages/blocks/src/store/test/selectors.js b/packages/blocks/src/store/test/selectors.js index ea24fcd7d855b..2f37ed006042c 100644 --- a/packages/blocks/src/store/test/selectors.js +++ b/packages/blocks/src/store/test/selectors.js @@ -1,7 +1,10 @@ /** * Internal dependencies */ -import { getChildBlockNames } from '../selectors'; +import { + getChildBlockNames, + isMatchingSearchTerm, +} from '../selectors'; describe( 'selectors', () => { describe( 'getChildBlockNames', () => { @@ -134,4 +137,66 @@ describe( 'selectors', () => { expect( getChildBlockNames( state, 'parent2' ) ).toEqual( [ 'child2' ] ); } ); } ); + + describe( 'isMatchingSearchTerm', () => { + const name = 'core/paragraph'; + const blockType = { + title: 'Paragraph', + category: 'common', + keywords: [ 'text' ], + }; + + const state = { + blockTypes: { + [ name ]: blockType, + }, + }; + + describe.each( [ + [ 'name', name ], + [ 'block type', blockType ], + ] )( 'by %s', ( label, nameOrType ) => { + it( 'should return false if not match', () => { + const result = isMatchingSearchTerm( state, nameOrType, 'Quote' ); + + expect( result ).toBe( false ); + } ); + + it( 'should return true if match by title', () => { + const result = isMatchingSearchTerm( state, nameOrType, 'Paragraph' ); + + expect( result ).toBe( true ); + } ); + + it( 'should return true if match ignoring case', () => { + const result = isMatchingSearchTerm( state, nameOrType, 'PARAGRAPH' ); + + expect( result ).toBe( true ); + } ); + + it( 'should return true if match ignoring diacritics', () => { + const result = isMatchingSearchTerm( state, nameOrType, 'PÁRAGRAPH' ); + + expect( result ).toBe( true ); + } ); + + it( 'should return true if match ignoring whitespace', () => { + const result = isMatchingSearchTerm( state, nameOrType, ' PARAGRAPH ' ); + + expect( result ).toBe( true ); + } ); + + it( 'should return true if match using the keywords', () => { + const result = isMatchingSearchTerm( state, nameOrType, 'TEXT' ); + + expect( result ).toBe( true ); + } ); + + it( 'should return true if match using the categories', () => { + const result = isMatchingSearchTerm( state, nameOrType, 'COMMON' ); + + expect( result ).toBe( true ); + } ); + } ); + } ); } );