8
0

utils.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 amdUtils from '/tests/_utils/amd.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. * If `proto` is not set or it does not define `create()` and `destroy()` methods,
  14. * then they will be set to Sinon spies. Therefore the shortest usage is:
  15. *
  16. * testUtils.defineEditorCreatorMock( 'test1' );
  17. *
  18. * The mocked creator is available under:
  19. *
  20. * editor.plugins.get( 'creator-thename' );
  21. *
  22. * @param {String} creatorName Name of the creator.
  23. * @param {Object} [proto] Prototype of the creator. Properties from the proto param will
  24. * be copied to the prototype of the creator.
  25. */
  26. defineEditorCreatorMock( creatorName, proto ) {
  27. amdUtils.define( 'creator-' + creatorName, [ 'core/creator' ], ( Creator ) => {
  28. class TestCreator extends Creator {}
  29. if ( proto ) {
  30. for ( let propName in proto ) {
  31. TestCreator.prototype[ propName ] = proto[ propName ];
  32. }
  33. }
  34. if ( !TestCreator.prototype.create ) {
  35. TestCreator.prototype.create = sinon.spy().named( creatorName + '-create' );
  36. }
  37. if ( !TestCreator.prototype.destroy ) {
  38. TestCreator.prototype.destroy = sinon.spy().named( creatorName + '-destroy' );
  39. }
  40. return TestCreator;
  41. } );
  42. },
  43. /**
  44. * Returns the number of elements return by the iterator.
  45. *
  46. * testUtils.getIteratorCount( [ 1, 2, 3, 4, 5 ] ); // 5;
  47. *
  48. * @param {Iterable.<*>} iterator Any iterator.
  49. * @returns {Number} Number of elements returned by that iterator.
  50. */
  51. getIteratorCount( iterator ) {
  52. let count = 0;
  53. for ( let _ of iterator ) { // jshint ignore:line
  54. count++;
  55. }
  56. return count;
  57. },
  58. /**
  59. * Creates an instance inheriting from {@link core.EmitterMixin} with one additional method `observe()`.
  60. * It allows observing changes to attributes in objects being {@link core.Observable observable}.
  61. *
  62. * The `observe()` method accepts:
  63. *
  64. * * `{String} observableName` – Identifier for the observable object. E.g. `"Editable"` when
  65. * you observe one of editor's editables. This name will be displayed on the console.
  66. * * `{core.Observable observable} – The object to observe.
  67. *
  68. * Typical usage:
  69. *
  70. * const observer = utils.createObserver();
  71. * observer.observe( 'Editable', editor.editables.current );
  72. *
  73. * // Stop listening (method from the EmitterMixin):
  74. * observer.stopListening();
  75. *
  76. * @returns {Emitter} The observer.
  77. */
  78. createObserver() {
  79. const observer = Object.create( EmitterMixin, {
  80. observe: {
  81. value: function observe( observableName, observable ) {
  82. observer.listenTo( observable, 'change', ( evt, propertyName, value, oldValue ) => {
  83. console.log( `[Change in ${ observableName }] ${ propertyName } = '${ value }' (was '${ oldValue }')` );
  84. } );
  85. return observer;
  86. }
  87. }
  88. } );
  89. return observer;
  90. }
  91. };
  92. export default utils;