ckeditorerror.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import { default as CKEditorError, DOCUMENTATION_URL } from '../src/ckeditorerror';
  6. describe( 'CKEditorError', () => {
  7. it( 'inherits from Error', () => {
  8. const error = new CKEditorError( 'foo' );
  9. expect( error ).to.be.an.instanceOf( Error );
  10. expect( error ).to.be.an.instanceOf( CKEditorError );
  11. } );
  12. it( 'sets the name', () => {
  13. const error = new CKEditorError( 'foo' );
  14. expect( error ).to.have.property( 'name', 'CKEditorError' );
  15. } );
  16. it( 'sets the message', () => {
  17. const error = new CKEditorError( 'foo' );
  18. expect( error ).to.have.property( 'message', 'foo' );
  19. expect( error.data ).to.be.undefined;
  20. } );
  21. it( 'sets the message and data', () => {
  22. const data = { bar: 1 };
  23. const error = new CKEditorError( 'foo', data );
  24. expect( error ).to.have.property( 'message', 'foo {"bar":1}' );
  25. expect( error ).to.have.property( 'data', data );
  26. } );
  27. it( 'appends stringified data to the message', () => {
  28. class Foo {
  29. constructor() {
  30. this.x = 1;
  31. }
  32. }
  33. const data = {
  34. bar: 'a',
  35. bom: new Foo(),
  36. bim: 10
  37. };
  38. const error = new CKEditorError( 'foo', data );
  39. expect( error ).to.have.property( 'message', 'foo {"bar":"a","bom":{"x":1},"bim":10}' );
  40. expect( error ).to.have.property( 'data', data );
  41. } );
  42. it( 'contains a link which leads to the documentation', () => {
  43. const error = new CKEditorError( 'model-schema-no-item: Specified item cannot be found.' );
  44. const errorMessage = 'model-schema-no-item: Specified item cannot be found. ' +
  45. `Read more: ${ DOCUMENTATION_URL }#error-model-schema-no-item\n`;
  46. expect( error ).to.have.property( 'message', errorMessage );
  47. } );
  48. it( 'link to documentation is added before the additional data message', () => {
  49. const error = new CKEditorError( 'model-schema-no-item: Specified item cannot be found.', { foo: 1, bar: 2 } );
  50. const errorMessage = 'model-schema-no-item: Specified item cannot be found. ' +
  51. `Read more: ${ DOCUMENTATION_URL }#error-model-schema-no-item\n ` +
  52. '{"foo":1,"bar":2}';
  53. expect( error ).to.have.property( 'message', errorMessage );
  54. } );
  55. describe( 'isCKEditorError', () => {
  56. it( 'checks if error is an instance of CKEditorError', () => {
  57. const ckeditorError = new CKEditorError( 'foo' );
  58. const regularError = new Error( 'foo' );
  59. expect( CKEditorError.isCKEditorError( ckeditorError ) ).to.be.true;
  60. expect( CKEditorError.isCKEditorError( regularError ) ).to.be.false;
  61. } );
  62. } );
  63. } );