Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Find tests in nested directories #355

Merged
merged 6 commits into from
Mar 30, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 1 addition & 81 deletions blocks/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
/* eslint-disable no-console */

/**
* External dependencies
*/
Expand All @@ -9,82 +7,4 @@ export { query };
export { default as Editable } from './components/editable';
export { default as parse } from './parser';
export { getCategories } from './categories';

/**
* Block settings keyed by block slug.
*
* @var {Object} blocks
*/
const blocks = {};

/**
* Registers a new block provided a unique slug and an object defining its
* behavior. Once registered, the block is made available as an option to any
* editor interface where blocks are implemented.
*
* @param {string} slug Block slug
* @param {Object} settings Block settings
* @return {?WPBlock} The block, if it has been successfully
* registered; otherwise `undefined`.
*/
export function registerBlock( slug, settings ) {
if ( typeof slug !== 'string' ) {
console.error(
'Block slugs must be strings.'
);
return;
}
if ( ! /^[a-z0-9-]+\/[a-z0-9-]+$/.test( slug ) ) {
console.error(
'Block slugs must contain a namespace prefix. Example: my-plugin/my-custom-block'
);
return;
}
if ( blocks[ slug ] ) {
console.error(
'Block "' + slug + '" is already registered.'
);
return;
}
const block = Object.assign( { slug }, settings );
blocks[ slug ] = block;
return block;
}

/**
* Unregisters a block.
*
* @param {string} slug Block slug
* @return {?WPBlock} The previous block value, if it has been
* successfully unregistered; otherwise `undefined`.
*/
export function unregisterBlock( slug ) {
if ( ! blocks[ slug ] ) {
console.error(
'Block "' + slug + '" is not registered.'
);
return;
}
const oldBlock = blocks[ slug ];
delete blocks[ slug ];
return oldBlock;
}

/**
* Returns settings associated with a registered block.
*
* @param {string} slug Block slug
* @return {?Object} Block settings
*/
export function getBlockSettings( slug ) {
return blocks[ slug ];
}

/**
* Returns all registered blocks.
*
* @return {Array} Block settings
*/
export function getBlocks() {
return Object.values( blocks );
}
export { registerBlock, unregisterBlock, getBlockSettings, getBlocks } from './registration';
2 changes: 1 addition & 1 deletion blocks/parser/index.js → blocks/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import * as query from 'hpq';
* Internal dependencies
*/
import { parse as grammarParse } from './post.pegjs';
import { getBlockSettings } from '../';
import { getBlockSettings } from './registration';

/**
* Returns the block attributes of a registered block node given its settings.
Expand Down
File renamed without changes.
80 changes: 80 additions & 0 deletions blocks/registration.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/* eslint-disable no-console */

/**
* Block settings keyed by block slug.
*
* @var {Object} blocks
*/
const blocks = {};

/**
* Registers a new block provided a unique slug and an object defining its
* behavior. Once registered, the block is made available as an option to any
* editor interface where blocks are implemented.
*
* @param {string} slug Block slug
* @param {Object} settings Block settings
* @return {?WPBlock} The block, if it has been successfully
* registered; otherwise `undefined`.
*/
export function registerBlock( slug, settings ) {
if ( typeof slug !== 'string' ) {
console.error(
'Block slugs must be strings.'
);
return;
}
if ( ! /^[a-z0-9-]+\/[a-z0-9-]+$/.test( slug ) ) {
console.error(
'Block slugs must contain a namespace prefix. Example: my-plugin/my-custom-block'
);
return;
}
if ( blocks[ slug ] ) {
console.error(
'Block "' + slug + '" is already registered.'
);
return;
}
const block = Object.assign( { slug }, settings );
blocks[ slug ] = block;
return block;
}

/**
* Unregisters a block.
*
* @param {string} slug Block slug
* @return {?WPBlock} The previous block value, if it has been
* successfully unregistered; otherwise `undefined`.
*/
export function unregisterBlock( slug ) {
if ( ! blocks[ slug ] ) {
console.error(
'Block "' + slug + '" is not registered.'
);
return;
}
const oldBlock = blocks[ slug ];
delete blocks[ slug ];
return oldBlock;
}

/**
* Returns settings associated with a registered block.
*
* @param {string} slug Block slug
* @return {?Object} Block settings
*/
export function getBlockSettings( slug ) {
return blocks[ slug ];
}

/**
* Returns all registered blocks.
*
* @return {Array} Block settings
*/
export function getBlocks() {
return Object.values( blocks );
}
12 changes: 9 additions & 3 deletions blocks/parser/test/index.js → blocks/test/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,16 @@ import { text } from 'hpq';
/**
* Internal dependencies
*/
import { default as parse, getBlockAttributes } from '../';
import * as blocks from '../../';
import { default as parse, getBlockAttributes } from '../parser';
import { getBlocks, unregisterBlock, registerBlock } from '../registration';

describe( 'block parser', () => {
beforeEach( () => {
getBlocks().forEach( ( block ) => {
unregisterBlock( block.slug );
} );
} );

describe( 'getBlockAttributes()', () => {
it( 'should merge attributes from function implementation', () => {
const blockSettings = {
Expand Down Expand Up @@ -63,7 +69,7 @@ describe( 'block parser', () => {
};
}
};
blocks.registerBlock( 'core/test-block', blockSettings );
registerBlock( 'core/test-block', blockSettings );

const postContent = '<!-- wp:core/test-block -->Ribs<!-- /wp:core/test-block -->' +
'<!-- wp:core/unknown-block -->Ribs<!-- /wp:core/unknown-block -->';
Expand Down
48 changes: 24 additions & 24 deletions blocks/test/index.js → blocks/test/registration.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ import sinon from 'sinon';
/**
* Internal dependencies
*/
import * as blocks from '../';
import { getBlocks, unregisterBlock, registerBlock, getBlockSettings } from '../registration';

describe( 'blocks', () => {
// Reset block state before each test.
beforeEach( () => {
blocks.getBlocks().forEach( block => {
blocks.unregisterBlock( block.slug );
getBlocks().forEach( block => {
unregisterBlock( block.slug );
} );
sinon.stub( console, 'error' );
} );
Expand All @@ -26,41 +26,41 @@ describe( 'blocks', () => {

describe( 'registerBlock()', () => {
it( 'should reject numbers', () => {
const block = blocks.registerBlock( 999 );
const block = registerBlock( 999 );
expect( console.error ).to.have.been.calledWith( 'Block slugs must be strings.' );
expect( block ).to.be.undefined();
} );

it( 'should reject blocks without a namespace', () => {
const block = blocks.registerBlock( 'doing-it-wrong' );
const block = registerBlock( 'doing-it-wrong' );
expect( console.error ).to.have.been.calledWith( 'Block slugs must contain a namespace prefix. Example: my-plugin/my-custom-block' );
expect( block ).to.be.undefined();
} );

it( 'should reject blocks with invalid characters', () => {
const block = blocks.registerBlock( 'still/_doing_it_wrong' );
const block = registerBlock( 'still/_doing_it_wrong' );
expect( console.error ).to.have.been.calledWith( 'Block slugs must contain a namespace prefix. Example: my-plugin/my-custom-block' );
expect( block ).to.be.undefined();
} );

it( 'should accept valid block names', () => {
const block = blocks.registerBlock( 'my-plugin/fancy-block-4' );
const block = registerBlock( 'my-plugin/fancy-block-4' );
expect( console.error ).to.not.have.been.called();
expect( block ).to.eql( { slug: 'my-plugin/fancy-block-4' } );
} );

it( 'should prohibit registering the same block twice', () => {
blocks.registerBlock( 'core/test-block' );
const block = blocks.registerBlock( 'core/test-block' );
registerBlock( 'core/test-block' );
const block = registerBlock( 'core/test-block' );
expect( console.error ).to.have.been.calledWith( 'Block "core/test-block" is already registered.' );
expect( block ).to.be.undefined();
} );

it( 'should store a copy of block settings', () => {
const blockSettings = { settingName: 'settingValue' };
blocks.registerBlock( 'core/test-block-with-settings', blockSettings );
registerBlock( 'core/test-block-with-settings', blockSettings );
blockSettings.mutated = true;
expect( blocks.getBlockSettings( 'core/test-block-with-settings' ) ).to.eql( {
expect( getBlockSettings( 'core/test-block-with-settings' ) ).to.eql( {
slug: 'core/test-block-with-settings',
settingName: 'settingValue',
} );
Expand All @@ -69,35 +69,35 @@ describe( 'blocks', () => {

describe( 'unregisterBlock()', () => {
it( 'should fail if a block is not registered', () => {
const oldBlock = blocks.unregisterBlock( 'core/test-block' );
const oldBlock = unregisterBlock( 'core/test-block' );
expect( console.error ).to.have.been.calledWith( 'Block "core/test-block" is not registered.' );
expect( oldBlock ).to.be.undefined();
} );

it( 'should unregister existing blocks', () => {
blocks.registerBlock( 'core/test-block' );
expect( blocks.getBlocks() ).to.eql( [
registerBlock( 'core/test-block' );
expect( getBlocks() ).to.eql( [
{ slug: 'core/test-block' },
] );
const oldBlock = blocks.unregisterBlock( 'core/test-block' );
const oldBlock = unregisterBlock( 'core/test-block' );
expect( console.error ).to.not.have.been.called();
expect( oldBlock ).to.eql( { slug: 'core/test-block' } );
expect( blocks.getBlocks() ).to.eql( [] );
expect( getBlocks() ).to.eql( [] );
} );
} );

describe( 'getBlockSettings()', () => {
it( 'should return { slug } for blocks with no settings', () => {
blocks.registerBlock( 'core/test-block' );
expect( blocks.getBlockSettings( 'core/test-block' ) ).to.eql( {
registerBlock( 'core/test-block' );
expect( getBlockSettings( 'core/test-block' ) ).to.eql( {
slug: 'core/test-block',
} );
} );

it( 'should return all block settings', () => {
const blockSettings = { settingName: 'settingValue' };
blocks.registerBlock( 'core/test-block-with-settings', blockSettings );
expect( blocks.getBlockSettings( 'core/test-block-with-settings' ) ).to.eql( {
registerBlock( 'core/test-block-with-settings', blockSettings );
expect( getBlockSettings( 'core/test-block-with-settings' ) ).to.eql( {
slug: 'core/test-block-with-settings',
settingName: 'settingValue',
} );
Expand All @@ -106,14 +106,14 @@ describe( 'blocks', () => {

describe( 'getBlocks()', () => {
it( 'should return an empty array at first', () => {
expect( blocks.getBlocks() ).to.eql( [] );
expect( getBlocks() ).to.eql( [] );
} );

it( 'should return all registered blocks', () => {
blocks.registerBlock( 'core/test-block' );
registerBlock( 'core/test-block' );
const blockSettings = { settingName: 'settingValue' };
blocks.registerBlock( 'core/test-block-with-settings', blockSettings );
expect( blocks.getBlocks() ).to.eql( [
registerBlock( 'core/test-block-with-settings', blockSettings );
expect( getBlocks() ).to.eql( [
{ slug: 'core/test-block' },
{ slug: 'core/test-block-with-settings', settingName: 'settingValue' },
] );
Expand Down
2 changes: 1 addition & 1 deletion webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ switch ( process.env.NODE_ENV ) {
'./element/index.js',
'./blocks/index.js',
'./editor/index.js',
...glob.sync( `./{${ Object.keys( config.entry ).join() }}/test/*.js` )
...glob.sync( `./{${ Object.keys( config.entry ).join() }}/**/test/*.js` )
];
config.externals = [ require( 'webpack-node-externals' )() ];
config.output = {
Expand Down