utils.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. /* global console:false */
  6. import EmitterMixin from '../../src/emittermixin';
  7. import CKEditorError from '../../src/ckeditorerror';
  8. import areConnectedThroughProperties from '../../src/areconnectedthroughproperties';
  9. /**
  10. * Creates an instance inheriting from {@link utils.EmitterMixin} with one additional method `observe()`.
  11. * It allows observing changes to attributes in objects being {@link utils.Observable observable}.
  12. *
  13. * The `observe()` method accepts:
  14. *
  15. * * `{String} observableName` – Identifier for the observable object. E.g. `"Editable"` when
  16. * you observe one of editor's editables. This name will be displayed on the console.
  17. * * `{utils.Observable observable} – The object to observe.
  18. * * `{Array.<String>} filterNames` – Array of property names to be observed.
  19. *
  20. * Typical usage:
  21. *
  22. * const observer = utils.createObserver();
  23. * observer.observe( 'Editable', editor.editables.current );
  24. *
  25. * // Stop listening (method from the EmitterMixin):
  26. * observer.stopListening();
  27. *
  28. * @returns {Emitter} The observer.
  29. */
  30. export function createObserver() {
  31. const observer = Object.create( EmitterMixin, {
  32. observe: {
  33. value: function observe( observableName, observable, filterNames ) {
  34. observer.listenTo( observable, 'change', ( evt, propertyName, value, oldValue ) => {
  35. if ( !filterNames || filterNames.includes( propertyName ) ) {
  36. console.log( `[Change in ${ observableName }] ${ propertyName } = '${ value }' (was '${ oldValue }')` );
  37. }
  38. } );
  39. return observer;
  40. }
  41. }
  42. } );
  43. return observer;
  44. }
  45. /**
  46. * Checks whether observable properties are properly bound to each other.
  47. *
  48. * Syntax given that observable `A` is bound to observables [`B`, `C`, ...]:
  49. *
  50. * assertBinding( A,
  51. * { initial `A` attributes },
  52. * [
  53. * [ B, { new `B` attributes } ],
  54. * [ C, { new `C` attributes } ],
  55. * ...
  56. * ],
  57. * { `A` attributes after [`B`, 'C', ...] changed }
  58. * );
  59. */
  60. export function assertBinding( observable, stateBefore, data, stateAfter ) {
  61. let key, boundObservable, attrs;
  62. for ( key in stateBefore ) {
  63. expect( observable[ key ] ).to.be.equal( stateBefore[ key ] );
  64. }
  65. // Change attributes of bound observables.
  66. for ( [ boundObservable, attrs ] of data ) {
  67. for ( key in attrs ) {
  68. if ( !boundObservable.hasOwnProperty( key ) ) {
  69. boundObservable.set( key, attrs[ key ] );
  70. } else {
  71. boundObservable[ key ] = attrs[ key ];
  72. }
  73. }
  74. }
  75. for ( key in stateAfter ) {
  76. expect( observable[ key ] ).to.be.equal( stateAfter[ key ] );
  77. }
  78. }
  79. /**
  80. * An assertion util to test whether the given function throws error that has correct message,
  81. * data and whether the context of the error and the `editorThatShouldBeFindableFromContext`
  82. * have common props (So the watchdog will be able to find the correct editor instance and restart it).
  83. *
  84. * @param {Function} fn Tested function that should throw a `CKEditorError`.
  85. * @param {RegExp|String} message Expected message of the error.
  86. * @param {*} editorThatShouldBeFindableFromContext An editor instance that should be findable from the error context.
  87. * @param {Object} [data] Error data.
  88. */
  89. export function expectToThrowCKEditorError( fn, message, editorThatShouldBeFindableFromContext, data ) {
  90. let err = null;
  91. try {
  92. fn();
  93. } catch ( _err ) {
  94. err = _err;
  95. assertCKEditorError( err, message, editorThatShouldBeFindableFromContext, data );
  96. }
  97. expect( err ).to.not.equal( null, 'Function did not throw any error' );
  98. }
  99. /**
  100. * An assertion util to test whether a given error has correct message, data and whether the context of the
  101. * error and the `editorThatShouldBeFindableFromContext` have common props (So the watchdog will be able to
  102. * find the correct editor instance and restart it).
  103. *
  104. * @param {module:utils/ckeditorerror~CKEditorError} err The tested error.
  105. * @param {RegExp|String} message Expected message of the error.
  106. * @param {*} [editorThatShouldBeFindableFromContext] An editor instance that should be findable from the error context.
  107. * @param {Object} [data] Error data.
  108. */
  109. export function assertCKEditorError( err, message, editorThatShouldBeFindableFromContext, data ) {
  110. if ( typeof message === 'string' ) {
  111. message = new RegExp( message );
  112. }
  113. expect( message ).to.be.a( 'regexp', 'Error message should be a string or a regexp.' );
  114. expect( err ).to.be.instanceOf( CKEditorError );
  115. expect( err.message ).to.match( message, 'Error message does not match the provided one.' );
  116. // TODO: The `editorThatShouldBeFindableFromContext` is optional but should be required in the future.
  117. if ( editorThatShouldBeFindableFromContext === null ) {
  118. expect( err.context ).to.equal( null, 'Error context was expected to be `null`' );
  119. } else if ( editorThatShouldBeFindableFromContext !== undefined ) {
  120. expect(
  121. areConnectedThroughProperties( editorThatShouldBeFindableFromContext, err.context ),
  122. 'Editor cannot be find from the error context'
  123. );
  124. }
  125. if ( data ) {
  126. expect( err.data ).to.deep.equal( data );
  127. }
  128. }