This commit is contained in:
KhaiNguyen
2020-02-13 10:39:37 +07:00
commit 59401cb805
12867 changed files with 4646216 additions and 0 deletions

View File

@@ -0,0 +1,19 @@
/**
* Internal dependencies
*/
import { registeredBlocks } from './registered-blocks-init';
/**
* Retrieves the inner blocks registered as a child of a specific one.
*
* @export
* @param {string} main Name of the parent block to retrieve children of.
*
* @returns {Object}
*/
export function getRegisteredInnerBlocks( main ) {
return typeof registeredBlocks[ main ] === 'object' &&
Object.keys( registeredBlocks[ main ] ).length > 0
? registeredBlocks[ main ]
: {};
}

View File

@@ -0,0 +1,2 @@
export { getRegisteredInnerBlocks } from './get-registered-inner-blocks';
export { registerInnerBlock } from './register-inner-block';

View File

@@ -0,0 +1,43 @@
/**
* Internal dependencies
*/
import { registeredBlocks } from './registered-blocks-init';
/**
* Asserts that an option is of the given type. Otherwise, throws an error.
*
* @throws Will throw an error if the type of the option doesn't match the expected type.
* @param {Object} options Object containing the option to validate.
* @param {string} optionName Name of the option to validate.
* @param {string} expectedType Type expected for the option.
*/
const assertOption = ( options, optionName, expectedType ) => {
if ( typeof options[ optionName ] !== expectedType ) {
throw new Error(
`Incorrect value for the ${ optionName } argument when registering an inner block. It must be a ${ expectedType }.`
);
}
};
/**
* Registers an inner block that can be added as a child of another block.
*
* @export
* @param {Object} options Options to use when registering the block.
* @param {string} options.main Name of the parent block.
* @param {string} options.blockName Name of the child block being registered.
* @param {Function} options.component React component used to render the child block.
*/
export function registerInnerBlock( options ) {
assertOption( options, 'main', 'string' );
assertOption( options, 'blockName', 'string' );
assertOption( options, 'component', 'function' );
const { main, blockName, component } = options;
if ( ! registeredBlocks[ main ] ) {
registeredBlocks[ main ] = {};
}
registeredBlocks[ main ][ blockName ] = component;
}

View File

@@ -0,0 +1,3 @@
const registeredBlocks = {};
export { registeredBlocks };

View File

@@ -0,0 +1,43 @@
/**
* Internal dependencies
*/
import { getRegisteredInnerBlocks, registerInnerBlock } from '../index';
describe( 'blocks registry', () => {
const main = '@woocommerce/all-products';
const blockName = '@woocommerce-extension/price-level';
const component = () => {};
describe( 'registerInnerBlock', () => {
const invokeTest = ( args ) => () => {
return registerInnerBlock( args );
};
it( 'throws an error when registered block is missing `main`', () => {
expect( invokeTest( { main: null } ) ).toThrowError( /main/ );
} );
it( 'throws an error when registered block is missing `blockName`', () => {
expect( invokeTest( { main, blockName: null } ) ).toThrowError(
/blockName/
);
} );
it( 'throws an error when registered block is missing `component`', () => {
expect(
invokeTest( { main, blockName, component: null } )
).toThrowError( /component/ );
} );
} );
describe( 'getRegisteredInnerBlocks', () => {
it( 'gets an empty object when parent has no inner blocks', () => {
expect(
getRegisteredInnerBlocks( '@woocommerce/all-products' )
).toEqual( {} );
} );
it( 'gets a block that was successfully registered', () => {
registerInnerBlock( { main, blockName, component } );
expect(
getRegisteredInnerBlocks( '@woocommerce/all-products' )
).toEqual( { [ blockName ]: component } );
} );
} );
} );