utils.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. 'use strict';
  6. /* global console:false */
  7. import moduleUtils from '/tests/_utils/module.js';
  8. import EmitterMixin from '/ckeditor5/core/emittermixin.js';
  9. const utils = {
  10. /**
  11. * Defines CKEditor plugin which is a mock of an editor creator.
  12. *
  13. * The mocked creator is available under:
  14. *
  15. * editor.plugins.get( 'creator-thename' );
  16. *
  17. * @param {String} creatorName Name of the creator.
  18. * @param {Object} [proto] Prototype of the creator. Properties from the proto param will
  19. * be copied to the prototype of the creator.
  20. */
  21. defineEditorCreatorMock( creatorName, proto ) {
  22. moduleUtils.define( 'creator-' + creatorName, [ 'core/creator' ], ( Creator ) => {
  23. class TestCreator extends Creator {}
  24. if ( proto ) {
  25. for ( let propName in proto ) {
  26. TestCreator.prototype[ propName ] = proto[ propName ];
  27. }
  28. }
  29. return TestCreator;
  30. } );
  31. },
  32. /**
  33. * Returns the number of elements return by the iterator.
  34. *
  35. * testUtils.getIteratorCount( [ 1, 2, 3, 4, 5 ] ); // 5;
  36. *
  37. * @param {Iterable.<*>} iterator Any iterator.
  38. * @returns {Number} Number of elements returned by that iterator.
  39. */
  40. getIteratorCount( iterator ) {
  41. let count = 0;
  42. for ( let _ of iterator ) { // jshint ignore:line
  43. count++;
  44. }
  45. return count;
  46. },
  47. /**
  48. * Creates an instance inheriting from {@link core.EmitterMixin} with one additional method `observe()`.
  49. * It allows observing changes to attributes in objects being {@link core.Observable observable}.
  50. *
  51. * The `observe()` method accepts:
  52. *
  53. * * `{String} observableName` – Identifier for the observable object. E.g. `"Editable"` when
  54. * you observe one of editor's editables. This name will be displayed on the console.
  55. * * `{core.Observable observable} – The object to observe.
  56. *
  57. * Typical usage:
  58. *
  59. * const observer = utils.createObserver();
  60. * observer.observe( 'Editable', editor.editables.current );
  61. *
  62. * // Stop listening (method from the EmitterMixin):
  63. * observer.stopListening();
  64. *
  65. * @returns {Emitter} The observer.
  66. */
  67. createObserver() {
  68. const observer = Object.create( EmitterMixin, {
  69. observe: {
  70. value: function observe( observableName, observable ) {
  71. observer.listenTo( observable, 'change', ( evt, propertyName, value, oldValue ) => {
  72. console.log( `[Change in ${ observableName }] ${ propertyName } = '${ value }' (was '${ oldValue }')` );
  73. } );
  74. return observer;
  75. }
  76. }
  77. } );
  78. return observer;
  79. },
  80. /**
  81. * Checkes wether observable properties are properly bound to each other.
  82. *
  83. * Syntax given that observable `A` is bound to observables [`B`, `C`, ...]:
  84. *
  85. * assertBinding( A,
  86. * { initial `A` attributes },
  87. * [
  88. * [ B, { new `B` attributes } ],
  89. * [ C, { new `C` attributes } ],
  90. * ...
  91. * ],
  92. * { `A` attributes after [`B`, 'C', ...] changed }
  93. * );
  94. */
  95. assertBinding( observable, stateBefore, data, stateAfter ) {
  96. let key, pair;
  97. for ( key in stateBefore ) {
  98. expect( observable[ key ] ).to.be.equal( stateBefore[ key ] );
  99. }
  100. // Change attributes of bound observables.
  101. for ( pair of data ) {
  102. for ( key in pair[ 1 ] ) {
  103. pair[ 0 ][ key ] = pair[ 1 ][ key ];
  104. }
  105. }
  106. for ( key in stateAfter ) {
  107. expect( observable[ key ] ).to.be.equal( stateAfter[ key ] );
  108. }
  109. }
  110. };
  111. export default utils;