Skip to content

Commit

Permalink
Blocks API: Improve validation after block gets filters applied (#14529)
Browse files Browse the repository at this point in the history
* Block API: Improve validation after block gets filters applied

* Update CHANGELOG.md

* Update CHANGELOG.md

* Apply suggestions from code review

Co-Authored-By: gziolo <[email protected]>

* Update CHANGELOG.md
  • Loading branch information
gziolo authored Mar 28, 2019
1 parent a3ef292 commit 406baf1
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 7 deletions.
6 changes: 6 additions & 0 deletions packages/blocks/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## x.x.x (Unreleased)

### New Feature

- Added a default implementation for `save` setting in `registerBlockType` which saves no markup in the post content.

## 6.1.0 (2019-03-06)

### New Feature
Expand Down
17 changes: 14 additions & 3 deletions packages/blocks/src/api/registration.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
/**
* External dependencies
*/
import { get, isFunction, some } from 'lodash';
import {
get,
isFunction,
isPlainObject,
some,
} from 'lodash';

/**
* WordPress dependencies
Expand Down Expand Up @@ -91,10 +96,16 @@ export function registerBlockType( name, settings ) {
}

settings = applyFilters( 'blocks.registerBlockType', settings, name );
if ( ! isPlainObject( settings ) ) {
console.error(
'Block settings must be a valid object.'
);
return;
}

if ( ! settings || ! isFunction( settings.save ) ) {
if ( ! isFunction( settings.save ) ) {
console.error(
'The "save" property must be specified and must be a valid function.'
'The "save" property must be a valid function.'
);
return;
}
Expand Down
17 changes: 13 additions & 4 deletions packages/blocks/src/api/test/registration.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/**
* External dependencies
*/
import { noop, omit } from 'lodash';
import { noop } from 'lodash';

/**
* WordPress dependencies
Expand Down Expand Up @@ -106,6 +106,15 @@ describe( 'blocks', () => {
expect( block ).toBeUndefined();
} );

it( 'should reject blocks with invalid save function', () => {
const block = registerBlockType( 'my-plugin/fancy-block-5', {
...defaultBlockSettings,
save: 'invalid',
} );
expect( console ).toHaveErroredWith( 'The "save" property must be a valid function.' );
expect( block ).toBeUndefined();
} );

it( 'should reject blocks with an invalid edit function', () => {
const blockType = { save: noop, edit: 'not-a-function', category: 'common', title: 'block title' },
block = registerBlockType( 'my-plugin/fancy-block-6', blockType );
Expand Down Expand Up @@ -318,12 +327,12 @@ describe( 'blocks', () => {
expect( block ).toBeUndefined();
} );

it( 'should reject valid blocks when they become invalid after executing filter which removes save property', () => {
it( 'should reject blocks which become invalid after executing filter which does not return a plain object', () => {
addFilter( 'blocks.registerBlockType', 'core/blocks/without-save', ( settings ) => {
return omit( settings, 'save' );
return [ settings ];
} );
const block = registerBlockType( 'my-plugin/fancy-block-13', defaultBlockSettings );
expect( console ).toHaveErroredWith( 'The "save" property must be specified and must be a valid function.' );
expect( console ).toHaveErroredWith( 'Block settings must be a valid object.' );
expect( block ).toBeUndefined();
} );
} );
Expand Down

0 comments on commit 406baf1

Please sign in to comment.