Skip to content

Commit

Permalink
Add tests for createBlockWithFallback
Browse files Browse the repository at this point in the history
  • Loading branch information
nylen committed Apr 18, 2017
1 parent f900617 commit 4554985
Showing 1 changed file with 61 additions and 2 deletions.
63 changes: 61 additions & 2 deletions blocks/api/test/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,18 @@ import { text } from 'hpq';
/**
* Internal dependencies
*/
import { default as parse, getBlockAttributes, parseBlockAttributes } from '../parser';
import { getBlocks, unregisterBlock, setUnknownTypeHandler, registerBlock } from '../registration';
import {
getBlockAttributes,
parseBlockAttributes,
createBlockWithFallback,
default as parse,
} from '../parser';
import {
registerBlock,
unregisterBlock,
getBlocks,
setUnknownTypeHandler,
} from '../registration';

describe( 'block parser', () => {
afterEach( () => {
Expand Down Expand Up @@ -75,6 +85,55 @@ describe( 'block parser', () => {
} );
} );

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

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

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

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', {} );
setUnknownTypeHandler( 'core/unknown-block' );

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

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

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

it( 'should not create a block if no unknown type handler', () => {
const block = createBlockWithFallback( 'core/test-block', 'content' );
expect( block ).to.be.undefined();
} );
} );

describe( 'parse()', () => {
it( 'should parse the post content, including block attributes', () => {
registerBlock( 'core/test-block', {
Expand Down

0 comments on commit 4554985

Please sign in to comment.