8
0

utils.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. export function expectToThrowCKEditorError( fn, message, editorThatShouldBeFindableFromContext ) {
  80. let err = null;
  81. try {
  82. fn();
  83. } catch ( _err ) {
  84. err = _err;
  85. assertCKEditorError( err, message, editorThatShouldBeFindableFromContext );
  86. }
  87. expect( err ).to.not.equal( null, 'Function did not throw any error' );
  88. }
  89. export function assertCKEditorError( err, message, editorThatShouldBeFindableFromContext ) {
  90. expect( err ).to.be.instanceOf( CKEditorError );
  91. expect( err.message ).to.match( message, 'Error message does not match the provided one.' );
  92. if ( editorThatShouldBeFindableFromContext === null ) {
  93. expect( err.context ).to.equal( null, 'Error context was expected to be `null`' );
  94. } else {
  95. expect(
  96. areConnectedThroughProperties( editorThatShouldBeFindableFromContext, err.context ),
  97. 'Editor cannot be find from the error context'
  98. );
  99. }
  100. }