Skip to content

Commit

Permalink
Validate block settings for edit() and save()
Browse files Browse the repository at this point in the history
Fixes #362. Validates that a block has a `save()` and `edit()`.  This is
probably a temporary solution.  The API should probably be cleaned up a
bit so everything is not quite as coupled and tests can be done
differently.
  • Loading branch information
BE-Webdesign committed Apr 25, 2017
1 parent 60e13a4 commit 76360c0
Show file tree
Hide file tree
Showing 7 changed files with 251 additions and 39 deletions.
38 changes: 38 additions & 0 deletions blocks/api/registration.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ export function registerBlock( slug, settings ) {
);
return;
}
if ( true !== validateBlockSettings( settings ) ) {
console.error(
'Block not registered.'
);
// Return the block even though it was not registered.
return Object.assign( { slug }, settings );
}
const block = Object.assign( { slug }, settings );
blocks[ slug ] = block;
return block;
Expand Down Expand Up @@ -104,3 +111,34 @@ export function getBlockSettings( slug ) {
export function getBlocks() {
return Object.values( blocks );
}

/**
* Validates the block settings.
*
* @param {Object} settings Block settings.
* @return {Boolean} Whether the block settings are valid.
*/
export function validateBlockSettings( settings ) {
if ( ! settings ) {
console.error(
'Block settings must specify a save() and edit() method for each block.'
);
return false;
}

if ( ! settings.save || 'function' !== typeof settings.save ) {
console.error(
'Block settings must specify a save() method for each block.'
);
return false;
}

if ( ! settings.edit || 'function' !== typeof settings.edit ) {
console.error(
'Block settings must specify a edit() method for each block.'
);
return false;
}

return true;
}
29 changes: 23 additions & 6 deletions blocks/api/test/factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { expect } from 'chai';
*/
import { createBlock, switchToBlockType } from '../factory';
import { getBlocks, unregisterBlock, setUnknownTypeHandler, registerBlock } from '../registration';
import { edit, save } from './function-refs';

describe( 'block factory', () => {
afterEach( () => {
Expand Down Expand Up @@ -43,9 +44,14 @@ describe( 'block factory', () => {
};
}
} ]
}
},
edit: edit,
save: save
} );
registerBlock( 'core/text-block', {
edit: edit,
save: save
} );
registerBlock( 'core/text-block', {} );

const block = {
uid: 1,
Expand All @@ -67,7 +73,10 @@ describe( 'block factory', () => {
} );

it( 'should switch the blockType of a block using the "transform to"', () => {
registerBlock( 'core/updated-text-block', {} );
registerBlock( 'core/updated-text-block', {
edit: edit,
save: save
} );
registerBlock( 'core/text-block', {
transforms: {
to: [ {
Expand All @@ -78,7 +87,9 @@ describe( 'block factory', () => {
};
}
} ]
}
},
edit: edit,
save: save
} );

const block = {
Expand All @@ -101,8 +112,14 @@ describe( 'block factory', () => {
} );

it( 'should return null if no transformation is found', () => {
registerBlock( 'core/updated-text-block', {} );
registerBlock( 'core/text-block', {} );
registerBlock( 'core/updated-text-block', {
edit: edit,
save: save
} );
registerBlock( 'core/text-block', {
edit: edit,
save: save
} );

const block = {
uid: 1,
Expand Down
13 changes: 13 additions & 0 deletions blocks/api/test/function-refs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* Function references used so that Chai doesn't complain about functions
* not being equal.
*
* These are methods that should be assigned to a valid block.
*/
export const edit = function edit() {
'tacos';
};

export const save = function save() {
'delicious';
};
51 changes: 41 additions & 10 deletions blocks/api/test/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ import {
getBlocks,
setUnknownTypeHandler,
} from '../registration';
import {
edit,
save
} from './function-refs.js';

describe( 'block parser', () => {
afterEach( () => {
Expand Down Expand Up @@ -87,7 +91,10 @@ describe( 'block parser', () => {

describe( 'createBlockWithFallback', () => {
it( 'should create the requested block if it exists', () => {
registerBlock( 'core/test-block', {} );
registerBlock( 'core/test-block', {
edit: edit,
save: save
} );

const block = createBlockWithFallback(
'core/test-block',
Expand All @@ -99,15 +106,21 @@ describe( 'block parser', () => {
} );

it( 'should create the requested block with no attributes if it exists', () => {
registerBlock( 'core/test-block', {} );
registerBlock( 'core/test-block', {
edit: edit,
save: save
} );

const block = createBlockWithFallback( 'core/test-block', 'content' );
expect( block.blockType ).to.eql( 'core/test-block' );
expect( block.attributes ).to.eql( {} );
} );

it( 'should fall back to the unknown type handler for unknown blocks if present', () => {
registerBlock( 'core/unknown-block', {} );
registerBlock( 'core/unknown-block', {
edit: edit,
save: save
} );
setUnknownTypeHandler( 'core/unknown-block' );

const block = createBlockWithFallback(
Expand All @@ -120,7 +133,10 @@ describe( 'block parser', () => {
} );

it( 'should fall back to the unknown type handler if block type not specified', () => {
registerBlock( 'core/unknown-block', {} );
registerBlock( 'core/unknown-block', {
edit: edit,
save: save
} );
setUnknownTypeHandler( 'core/unknown-block' );

const block = createBlockWithFallback( null, 'content' );
Expand All @@ -142,7 +158,9 @@ describe( 'block parser', () => {
return {
content: rawContent,
};
}
},
edit: edit,
save: save
} );

const parsed = parse(
Expand All @@ -166,7 +184,9 @@ describe( 'block parser', () => {
return {
content: rawContent + ' & Chicken'
};
}
},
edit: edit,
save: save
} );

const parsed = parse(
Expand All @@ -184,8 +204,14 @@ describe( 'block parser', () => {
} );

it( 'should parse the post content, using unknown block handler', () => {
registerBlock( 'core/test-block', {} );
registerBlock( 'core/unknown-block', {} );
registerBlock( 'core/test-block', {
edit: edit,
save: save
} );
registerBlock( 'core/unknown-block', {
edit: edit,
save: save
} );

setUnknownTypeHandler( 'core/unknown-block' );

Expand All @@ -204,14 +230,19 @@ describe( 'block parser', () => {
} );

it( 'should parse the post content, including raw HTML at each end', () => {
registerBlock( 'core/test-block', {} );
registerBlock( 'core/test-block', {
edit: edit,
save: save
} );
registerBlock( 'core/unknown-block', {
// Currently this is the only way to test block content parsing?
attributes: function( rawContent ) {
return {
content: rawContent,
};
}
},
edit: edit,
save: save
} );

setUnknownTypeHandler( 'core/unknown-block' );
Expand Down
Loading

0 comments on commit 76360c0

Please sign in to comment.