Skip to content

Commit

Permalink
Merge pull request #1013 from wordpress-mobile/issue/update_on_split_…
Browse files Browse the repository at this point in the history
…uses

Update code to use the new OnSplit prop on RichText.
  • Loading branch information
SergioEstevao authored May 22, 2019
2 parents 96a75be + 94c7a38 commit d4f9614
Show file tree
Hide file tree
Showing 3 changed files with 2 additions and 64 deletions.
2 changes: 1 addition & 1 deletion gutenberg
Submodule gutenberg updated 125 files
63 changes: 0 additions & 63 deletions src/__tests__/paragraph.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import Paragraph from '../../gutenberg/packages/block-library/src/paragraph/edit
/**
* WordPress dependencies
*/
import { createBlock } from '@wordpress/blocks';
jest.mock( '@wordpress/blocks' );

const getTestComponentWithContent = ( content ) => {
Expand All @@ -25,71 +24,9 @@ const getTestComponentWithContent = ( content ) => {
);
};

const getTestInstanceWithContent = ( content ) => {
return getTestComponentWithContent( content ).instance();
};

describe( 'Paragraph block', () => {
it( 'renders without crashing', () => {
const component = getTestComponentWithContent( '' );
expect( component.exists() ).toBe( true );
} );

it( 'splits empty block on Enter', () => {
// Given
const instance = getTestInstanceWithContent( '' );

const blocks = [ ];
const before = '';
const after = '';

// Mock implemenattion of `createBlock` to test against `insertBlocksAfter`.
const newBlock = { content: after };
createBlock.mockImplementation( () => newBlock );

// When
instance.splitBlock( before, after, ...blocks );

// Then

// Should ask for creating a new paragraph block.
expect( createBlock ).toHaveBeenCalledTimes( 1 );
expect( createBlock ).toHaveBeenCalledWith( 'core/paragraph', { content: after } );

// New block is inserted after the current block.
expect( instance.props.insertBlocksAfter ).toHaveBeenCalledTimes( 1 );
expect( instance.props.insertBlocksAfter ).toHaveBeenCalledWith( [ newBlock ] );
} );

it( 'splits block on Enter with content on the middle', () => {
// Given
const before = 'Some text ';
const after = 'to split';
const blocks = [ ];

const newBlock = { content: after };
createBlock.mockImplementation( () => newBlock );

const instance = getTestInstanceWithContent( before + after );

// When
instance.splitBlock( before, after, ...blocks );

// Then

// Do NOT remove current block
expect( instance.props.onReplace ).toHaveBeenCalledTimes( 0 );

// Should ask for creating a new paragraph block with the second half of the text.
expect( createBlock ).toHaveBeenCalledTimes( 1 );
expect( createBlock ).toHaveBeenCalledWith( 'core/paragraph', { content: after } );

// Insert new block with the second half of the text
expect( instance.props.insertBlocksAfter ).toHaveBeenCalledTimes( 1 );
expect( instance.props.insertBlocksAfter ).toHaveBeenCalledWith( [ newBlock ] );

// Replace current block content with first half of the text.
expect( instance.props.setAttributes ).toHaveBeenCalledTimes( 1 );
expect( instance.props.setAttributes ).toHaveBeenCalledWith( { content: before } );
} );
} );
1 change: 1 addition & 0 deletions symlinked-packages/@wordpress/data-controls

0 comments on commit d4f9614

Please sign in to comment.