Skip to content

Commit

Permalink
Editable: Add unit tests to verify Editable.Value
Browse files Browse the repository at this point in the history
  • Loading branch information
gziolo committed Oct 20, 2017
1 parent ef415ca commit eebaeca
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
4 changes: 4 additions & 0 deletions blocks/editable/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -666,6 +666,10 @@ Editable.contextTypes = {
Editable.Value = ( { value } ) => valueToReact( value );

function valueToReact( value ) {
if ( ! Array.isArray( value ) ) {
return value;
}

return value.map( ( element, i ) => {
if ( typeof element === 'string' ) {
return element;
Expand Down
48 changes: 48 additions & 0 deletions blocks/editable/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,52 @@ describe( 'Editable', () => {
} );
/* eslint-enable no-console */
} );

describe( 'Editable.Value', () => {
const Component = ( { value } ) => (
<div>
<Editable.Value value={ value } />
</div>
);

it( 'should render value containing string', () => {
const value = [ 'Hello, Dolly!' ];
const wrapper = shallow( <Component value={ value } /> );

expect( wrapper.html() ).toBe( '<div>Hello, Dolly!</div>' );
} );

it( 'should render value containing a single DOM node', () => {
const value = [
[ 'h1', {}, 'This is a header' ],
];
const wrapper = shallow( <Component value={ value } /> );

expect( wrapper.html() ).toBe( '<div><h1>This is a header</h1></div>' );
} );

it( 'should render value with deeply nested DOM nodes', () => {
const value = [
'This is a ',
[ 'strong', {}, 'paragraph', ],
' with a ',
[ 'a', { href: 'https://w.org/' }, 'link with ', [
'b',
{},
'bold ',
[
'i',
{},
'and italics',
],
] ],
'.',
];
const wrapper = shallow( <Component value={ value } /> );

expect( wrapper.html() ).toBe(
'<div>This is a <strong>paragraph</strong> with a <a href=\"https://w.org/\">link with <b>bold <i>and italics</i></b></a>.</div>'
);
} );
} );
} );

0 comments on commit eebaeca

Please sign in to comment.