diff --git a/src/ckeditor.jsx b/src/ckeditor.jsx index 13dd2e6a..157ea81d 100644 --- a/src/ckeditor.jsx +++ b/src/ckeditor.jsx @@ -92,7 +92,9 @@ export default class CKEditor extends React.Component { } ); } ) .catch( error => { - console.error( error ); + const onErrorCallback = this.props.onError || console.error; + + onErrorCallback( error ); } ); } @@ -132,6 +134,7 @@ CKEditor.propTypes = { onInit: PropTypes.func, onFocus: PropTypes.func, onBlur: PropTypes.func, + onError: PropTypes.func, disabled: PropTypes.bool }; diff --git a/tests/ckeditor.jsx b/tests/ckeditor.jsx index 87920c50..c97535c2 100644 --- a/tests/ckeditor.jsx +++ b/tests/ckeditor.jsx @@ -102,7 +102,7 @@ describe( 'CKEditor Component', () => { } ); } ); - it( 'displays an error if something went wrong', done => { + it( 'displays an error if something went wrong and "onError" callback was not specified', done => { const error = new Error( 'Something went wrong.' ); const consoleErrorStub = sandbox.stub( console, 'error' ); @@ -366,6 +366,23 @@ describe( 'CKEditor Component', () => { } ); } ); + describe( '#onError', () => { + it( 'calls the callback if specified when an error occurs', done => { + const error = new Error( 'Error was thrown.' ); + const errorHandler = sandbox.spy(); + + sandbox.stub( Editor, 'create' ).rejects( error ); + wrapper = mount( ); + + setTimeout( () => { + expect( errorHandler.calledOnce ).to.equal( true ); + + expect( errorHandler.firstCall.args[ 0 ] ).to.equal( error ); + done(); + } ); + } ); + } ); + describe( '#disabled', () => { it( 'switches the editor to read-only mode if [disabled={true}]', done => { const onInit = function( editor ) {