Skip to content

Commit

Permalink
Block HTML Mode: Adding unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
youknowriad committed Sep 29, 2017
1 parent 2503c12 commit 3d65fd6
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 0 deletions.
28 changes: 28 additions & 0 deletions blocks/api/test/serializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import serialize, {
serializeAttributes,
getCommentDelimitedContent,
serializeBlock,
getBlockContent,
} from '../serializer';
import {
getBlockTypes,
Expand Down Expand Up @@ -402,4 +403,31 @@ describe( 'block serializer', () => {
);
} );
} );

describe( 'getBlockContent', () => {
it( 'should return the block\'s serialized inner HTML', () => {
const blockType = {
attributes: {
content: {
type: 'string',
source: text(),
},
},
save( { attributes } ) {
return attributes.content;
},
category: 'common',
title: 'block title',
};
registerBlockType( 'core/chicken', blockType );
const block = {
name: 'core/chicken',
attributes: {
content: '<p>chicken </p>',
},
isValid: true,
};
expect( getBlockContent( block ) ).toBe( '<p>chicken </p>' );
} );
} );
} );
50 changes: 50 additions & 0 deletions editor/test/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
notices,
showInsertionPoint,
userData,
blocksMode,
} from '../reducer';

describe( 'state', () => {
Expand Down Expand Up @@ -121,6 +122,33 @@ describe( 'state', () => {
expect( state.blockOrder ).toEqual( [ 'wings' ] );
} );

it( 'should update the block', () => {
const original = editor( undefined, {
type: 'RESET_BLOCKS',
blocks: [ {
uid: 'chicken',
name: 'core/test-block',
attributes: {},
isValid: false,
} ],
} );
const state = editor( deepFreeze( original ), {
type: 'UPDATE_BLOCK',
uid: 'chicken',
updates: {
attributes: { content: 'ribs' },
isValid: true,
},
} );

expect( state.blocksByUid.chicken ).toEqual( {
uid: 'chicken',
name: 'core/test-block',
attributes: { content: 'ribs' },
isValid: true,
} );
} );

it( 'should move the block up', () => {
const original = editor( undefined, {
type: 'RESET_BLOCKS',
Expand Down Expand Up @@ -968,4 +996,26 @@ describe( 'state', () => {
expect( initial.recentlyUsedBlocks ).toHaveLength( 8 );
} );
} );

describe( 'blocksMode', () => {
it( 'should set mode to html if not set', () => {
const action = {
type: 'TOGGLE_BLOCK_MODE',
uid: 'chicken',
};
const value = blocksMode( deepFreeze( {} ), action );

expect( value ).toEqual( { chicken: 'html' } );
} );

it( 'should toggle mode to visual if set as html', () => {
const action = {
type: 'TOGGLE_BLOCK_MODE',
uid: 'chicken',
};
const value = blocksMode( deepFreeze( { chicken: 'html' } ), action );

expect( value ).toEqual( { chicken: 'visual' } );
} );
} );
} );
21 changes: 21 additions & 0 deletions editor/test/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ import {
isFirstMultiSelectedBlock,
isBlockHovered,
getBlockFocus,
getBlockMode,
isTyping,
getBlockInsertionPoint,
isBlockInsertionPointVisible,
Expand Down Expand Up @@ -1504,6 +1505,26 @@ describe( 'selectors', () => {
} );
} );

describe( 'geteBlockMode', () => {
it( 'should return "visual" if unset', () => {
const state = {
blocksMode: {},
};

expect( getBlockMode( state, 123 ) ).toEqual( 'visual' );
} );

it( 'should return the block mode', () => {
const state = {
blocksMode: {
123: 'html',
},
};

expect( getBlockMode( state, 123 ) ).toEqual( 'html' );
} );
} );

describe( 'isTyping', () => {
it( 'should return the isTyping flag if the block is selected', () => {
const state = {
Expand Down

0 comments on commit 3d65fd6

Please sign in to comment.