forked from WordPress/gutenberg
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Dont trigger invalid block on empty HTML content (WordPress#9546)
* Dont trigger invalid block on empty HTML content If a block’s HTML is set to the empty string we reset the block to it’s default HTML and mark it as valid. This is more useful than the block becoming invalid and needing additional work to resolve * Only update HTML content if block is reset
- Loading branch information
1 parent
5ea0283
commit 4c579ed
Showing
2 changed files
with
125 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
packages/editor/src/components/block-list/test/block-html.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { shallow } from 'enzyme'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { createBlock } from '@wordpress/blocks'; | ||
import { registerCoreBlocks } from '@wordpress/block-library'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { BlockHTML } from '../block-html'; | ||
|
||
describe( 'BlockHTML', () => { | ||
beforeAll( () => { | ||
registerCoreBlocks(); | ||
} ); | ||
|
||
it( 'renders HTML editor', () => { | ||
const wrapper = shallow( <BlockHTML block={ { isValid: true } } /> ); | ||
|
||
expect( wrapper.name() ).toBe( 'TextareaAutosize' ); | ||
} ); | ||
|
||
describe( 'block content', () => { | ||
it( 'use originalContent for an invalid block', () => { | ||
const wrapper = shallow( <BlockHTML block={ { isValid: false, originalContent: 'test' } } /> ); | ||
|
||
expect( wrapper.state( 'html' ) ).toBe( 'test' ); | ||
} ); | ||
|
||
it( 'use block content for a valid block', () => { | ||
const block = createBlock( 'core/paragraph', { | ||
content: 'test-block', | ||
isValid: true, | ||
} ); | ||
|
||
const wrapper = shallow( <BlockHTML block={ block } /> ); | ||
|
||
expect( wrapper.state( 'html' ) ).toBe( '<p>test-block</p>' ); | ||
} ); | ||
|
||
it( 'onChange updates block html', () => { | ||
const wrapper = shallow( <BlockHTML block={ { isValid: true } } /> ); | ||
|
||
wrapper.instance().onChange( { target: { value: 'update' } } ); | ||
|
||
expect( wrapper.state( 'html' ) ).toBe( 'update' ); | ||
} ); | ||
} ); | ||
|
||
describe( 'onBlur', () => { | ||
const onChange = jest.fn(); | ||
|
||
beforeEach( () => { | ||
onChange.mockClear(); | ||
} ); | ||
|
||
it( 'onBlur updates valid HTML string in block', () => { | ||
const block = createBlock( 'core/paragraph', { | ||
content: 'test-block', | ||
isValid: true, | ||
} ); | ||
const wrapper = shallow( <BlockHTML block={ block } onChange={ onChange } /> ); | ||
|
||
wrapper.instance().onChange( { target: { value: '<p>update</p>' } } ); | ||
wrapper.instance().onBlur(); | ||
|
||
expect( onChange ).toHaveBeenCalledTimes( 1 ); | ||
expect( onChange.mock.calls[ 0 ][ 2 ] ).toBe( '<p>update</p>' ); | ||
expect( onChange.mock.calls[ 0 ][ 3 ] ).toBe( true ); | ||
} ); | ||
|
||
it( 'onBlur updates invalid HTML string in block', () => { | ||
const block = createBlock( 'core/paragraph', { | ||
content: 'test-block', | ||
isValid: true, | ||
} ); | ||
const wrapper = shallow( <BlockHTML block={ block } onChange={ onChange } /> ); | ||
|
||
wrapper.instance().onChange( { target: { value: '<p>update' } } ); | ||
wrapper.instance().onBlur(); | ||
|
||
expect( console ).toHaveErrored(); | ||
expect( console ).toHaveWarned(); | ||
expect( onChange ).toHaveBeenCalledTimes( 1 ); | ||
expect( onChange.mock.calls[ 0 ][ 2 ] ).toBe( '<p>update' ); | ||
expect( onChange.mock.calls[ 0 ][ 3 ] ).toBe( false ); | ||
} ); | ||
|
||
it( 'onBlur resets block to default content if supplied empty string', () => { | ||
const block = createBlock( 'core/paragraph', { | ||
content: 'test-block', | ||
isValid: true, | ||
} ); | ||
const wrapper = shallow( <BlockHTML block={ block } onChange={ onChange } /> ); | ||
|
||
wrapper.instance().onChange( { target: { value: '' } } ); | ||
wrapper.instance().onBlur(); | ||
|
||
expect( onChange ).toHaveBeenCalledTimes( 1 ); | ||
expect( onChange.mock.calls[ 0 ][ 2 ] ).toBe( '<p></p>' ); | ||
expect( onChange.mock.calls[ 0 ][ 3 ] ).toBe( true ); | ||
expect( wrapper.state( 'html' ) ).toBe( '<p></p>' ); | ||
} ); | ||
} ); | ||
} ); |