Skip to content
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Add tests for createBlockWithFallback
  • Loading branch information
nylen committed Apr 18, 2017
commit 455498557de3e7f86806ecc2814b4c266655aeaa
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