Skip to content

Commit

Permalink
Merge pull request #2344 from WordPress/add/editable-proptypes
Browse files Browse the repository at this point in the history
Editable: Add prop types to warn on non-array value
  • Loading branch information
aduth authored Aug 11, 2017
2 parents 898a793 + ef5eb3f commit 9b1c7fd
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
14 changes: 13 additions & 1 deletion blocks/editable/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,18 @@ export default class Editable extends Component {
constructor( props ) {
super( ...arguments );

const { value } = props;
if ( 'production' !== process.env.NODE_ENV && undefined !== value &&
! Array.isArray( value ) ) {
// eslint-disable-next-line no-console
console.error(
`Invalid value of type ${ typeof value } passed to Editable ` +
'(expected array). Attribute values should be sourced using ' +
'the `children` source when used with Editable.\n\n' +
'See: http://gutenberg-devdoc.surge.sh/reference/attribute-sources/#children'
);
}

this.onInit = this.onInit.bind( this );
this.getSettings = this.getSettings.bind( this );
this.onSetup = this.onSetup.bind( this );
Expand All @@ -72,7 +84,7 @@ export default class Editable extends Component {
this.state = {
formats: {},
bookmark: null,
empty: ! props.value || ! props.value.length,
empty: ! value || ! value.length,
};
}

Expand Down
43 changes: 43 additions & 0 deletions blocks/editable/test/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* External dependencies
*/
import { shallow } from 'enzyme';

/**
* Internal dependencies
*/
import Editable from '../';

describe( 'Editable', () => {
describe( '.propTypes', () => {
/* eslint-disable no-console */
let consoleError;
beforeEach( () => {
consoleError = console.error;
console.error = jest.fn();
} );

afterEach( () => {
console.error = consoleError;
} );

it( 'should warn when rendered with string value', () => {
shallow( <Editable value="Uh oh!" /> );

expect( console.error ).toHaveBeenCalled();
} );

it( 'should not warn when rendered with undefined value', () => {
shallow( <Editable /> );

expect( console.error ).not.toHaveBeenCalled();
} );

it( 'should not warn when rendered with array value', () => {
shallow( <Editable value={ [ 'Oh, good' ] } /> );

expect( console.error ).not.toHaveBeenCalled();
} );
/* eslint-enable no-console */
} );
} );

0 comments on commit 9b1c7fd

Please sign in to comment.