ckeditorerror.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /**
  2. * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  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', null );
  14. expect( error ).to.have.property( 'name', 'CKEditorError' );
  15. } );
  16. it( 'sets the message', () => {
  17. const error = new CKEditorError( 'foo', null );
  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', null, data );
  24. expect( error ).to.have.property( 'message', 'foo {"bar":1}' );
  25. expect( error ).to.have.property( 'data', data );
  26. } );
  27. it( 'sets the context of the error', () => {
  28. const data = { bar: 1 };
  29. const editor = {};
  30. const error = new CKEditorError( 'foo', editor, data );
  31. expect( error.context ).to.equal( editor );
  32. } );
  33. it( 'appends stringified data to the message', () => {
  34. class Foo {
  35. constructor() {
  36. this.x = 1;
  37. }
  38. }
  39. const data = {
  40. bar: 'a',
  41. bom: new Foo(),
  42. bim: 10
  43. };
  44. const error = new CKEditorError( 'foo', null, data );
  45. expect( error ).to.have.property( 'message', 'foo {"bar":"a","bom":{"x":1},"bim":10}' );
  46. expect( error ).to.have.property( 'data', data );
  47. } );
  48. it( 'contains a link which leads to the documentation', () => {
  49. const error = new CKEditorError( 'model-schema-no-item: Specified item cannot be found.', null );
  50. const errorMessage = 'model-schema-no-item: Specified item cannot be found. ' +
  51. `Read more: ${ DOCUMENTATION_URL }#error-model-schema-no-item\n`;
  52. expect( error ).to.have.property( 'message', errorMessage );
  53. } );
  54. it( 'link to documentation is added before the additional data message', () => {
  55. const error = new CKEditorError( 'model-schema-no-item: Specified item cannot be found.', null, { foo: 1, bar: 2 } );
  56. const errorMessage = 'model-schema-no-item: Specified item cannot be found. ' +
  57. `Read more: ${ DOCUMENTATION_URL }#error-model-schema-no-item\n ` +
  58. '{"foo":1,"bar":2}';
  59. expect( error ).to.have.property( 'message', errorMessage );
  60. } );
  61. describe( 'is()', () => {
  62. it( 'checks if error is an instance of CKEditorError', () => {
  63. const ckeditorError = new CKEditorError( 'foo', null );
  64. const regularError = new Error( 'foo' );
  65. expect( ( !!ckeditorError.is && ckeditorError.is( 'CKEditorError' ) ) ).to.be.true;
  66. expect( ( !!regularError.is && regularError.is( 'CKEditorError' ) ) ).to.be.false;
  67. } );
  68. } );
  69. } );