From 4e4c259a919c539e3cbdc020d7083574474b5505 Mon Sep 17 00:00:00 2001 From: Grzegorz Ziolkowski Date: Fri, 19 Apr 2019 15:45:57 +0200 Subject: [PATCH 1/2] Fix 13776: Format is already registered to handle class name --- packages/rich-text/src/store/selectors.js | 4 +- .../rich-text/src/store/test/selectors.js | 52 ++++++++++++++++--- 2 files changed, 46 insertions(+), 10 deletions(-) diff --git a/packages/rich-text/src/store/selectors.js b/packages/rich-text/src/store/selectors.js index 518bb7d90800a5..c5f970c6a956eb 100644 --- a/packages/rich-text/src/store/selectors.js +++ b/packages/rich-text/src/store/selectors.js @@ -40,8 +40,8 @@ export function getFormatType( state, name ) { * @return {?Object} Format type. */ export function getFormatTypeForBareElement( state, bareElementTagName ) { - return find( getFormatTypes( state ), ( { tagName } ) => { - return bareElementTagName === tagName; + return find( getFormatTypes( state ), ( { className, tagName } ) => { + return className === null && bareElementTagName === tagName; } ); } diff --git a/packages/rich-text/src/store/test/selectors.js b/packages/rich-text/src/store/test/selectors.js index ca49185a6e6b1a..f9d1cfa76dc02a 100644 --- a/packages/rich-text/src/store/test/selectors.js +++ b/packages/rich-text/src/store/test/selectors.js @@ -6,33 +6,69 @@ import deepFreeze from 'deep-freeze'; /** * Internal dependencies */ -import { getFormatTypes, getFormatType } from '../selectors'; +import { + getFormatTypes, + getFormatType, + getFormatTypeForBareElement, + getFormatTypeForClassName, +} from '../selectors'; describe( 'selectors', () => { + const formatType = { + name: 'core/test-format', + className: null, + tagName: 'format', + }; + const formatTypeBareTag = { + name: 'core/test-format-bare-tag', + className: null, + tagName: 'strong', + }; + const formatTypeClassName = { + name: 'core/test-format-class-name', + className: 'class-name', + tagName: 'strong', + }; const defaultState = deepFreeze( { formatTypes: { - 'core/test-format': { name: 'core/test-format' }, - 'core/test-format-2': { name: 'core/test-format-2' }, + 'core/test-format': formatType, + 'core/test-format-bare-tag': formatTypeBareTag, + 'core/test-format-class-name': formatTypeClassName, }, } ); describe( 'getFormatTypes', () => { it( 'should get format types', () => { const expected = [ - { name: 'core/test-format' }, - { name: 'core/test-format-2' }, + formatType, + formatTypeBareTag, + formatTypeClassName, ]; - expect( getFormatTypes( defaultState ) ).toEqual( expected ); } ); } ); describe( 'getFormatType', () => { it( 'should get a format type', () => { - const expected = { name: 'core/test-format' }; const result = getFormatType( defaultState, 'core/test-format' ); - expect( result ).toEqual( expected ); + expect( result ).toEqual( formatType ); + } ); + } ); + + describe( 'getFormatTypeForBareElement', () => { + it( 'should get a format type', () => { + const result = getFormatTypeForBareElement( defaultState, 'strong' ); + + expect( result ).toEqual( formatTypeBareTag ); + } ); + } ); + + describe( 'getFormatTypeForClassName', () => { + it( 'should get a format type', () => { + const result = getFormatTypeForClassName( defaultState, 'class-name' ); + + expect( result ).toEqual( formatTypeClassName ); } ); } ); } ); From 0f9e0f9a74644139143fe2b8de1adc2e24de40cc Mon Sep 17 00:00:00 2001 From: iseulde Date: Fri, 19 Apr 2019 16:48:10 +0200 Subject: [PATCH 2/2] Adjust test order --- packages/rich-text/src/store/test/selectors.js | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/packages/rich-text/src/store/test/selectors.js b/packages/rich-text/src/store/test/selectors.js index f9d1cfa76dc02a..745f077c5f3f7c 100644 --- a/packages/rich-text/src/store/test/selectors.js +++ b/packages/rich-text/src/store/test/selectors.js @@ -19,21 +19,23 @@ describe( 'selectors', () => { className: null, tagName: 'format', }; - const formatTypeBareTag = { - name: 'core/test-format-bare-tag', - className: null, - tagName: 'strong', - }; const formatTypeClassName = { name: 'core/test-format-class-name', className: 'class-name', tagName: 'strong', }; + const formatTypeBareTag = { + name: 'core/test-format-bare-tag', + className: null, + tagName: 'strong', + }; const defaultState = deepFreeze( { formatTypes: { 'core/test-format': formatType, - 'core/test-format-bare-tag': formatTypeBareTag, 'core/test-format-class-name': formatTypeClassName, + // Order matters: we need to ensure that + // `core/test-format-class-name` is not considered bare. + 'core/test-format-bare-tag': formatTypeBareTag, }, } ); @@ -41,8 +43,8 @@ describe( 'selectors', () => { it( 'should get format types', () => { const expected = [ formatType, - formatTypeBareTag, formatTypeClassName, + formatTypeBareTag, ]; expect( getFormatTypes( defaultState ) ).toEqual( expected ); } );