Skip to content

Commit

Permalink
Merge pull request #127 from ansorensen/on-error
Browse files Browse the repository at this point in the history
Feature: Introduced support for `onError` callback that is being called if an error occurred during the editor's initialization. Closes #123.

Thanks to @ansorensen.
  • Loading branch information
pomek authored Jan 8, 2020
2 parents b8dd780 + c6029b2 commit becf9f7
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
5 changes: 4 additions & 1 deletion src/ckeditor.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
} );
}

Expand Down Expand Up @@ -132,6 +134,7 @@ CKEditor.propTypes = {
onInit: PropTypes.func,
onFocus: PropTypes.func,
onBlur: PropTypes.func,
onError: PropTypes.func,
disabled: PropTypes.bool
};

Expand Down
19 changes: 18 additions & 1 deletion tests/ckeditor.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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' );

Expand Down Expand Up @@ -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( <CKEditor editor={Editor} onError={errorHandler}/> );

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 ) {
Expand Down

0 comments on commit becf9f7

Please sign in to comment.