8
0

utils.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. 'use strict';
  6. import amdUtils from '/tests/_utils/amd.js';
  7. const utils = {
  8. /**
  9. * Defines CKEditor plugin which is a mock of an editor creator.
  10. *
  11. * If `proto` is not set or it does not define `create()` and `destroy()` methods,
  12. * then they will be set to Sinon spies. Therefore the shortest usage is:
  13. *
  14. * testUtils.defineEditorCreatorMock( 'test1' );
  15. *
  16. * The mocked creator is available under:
  17. *
  18. * editor.plugins.get( 'creator-thename' );
  19. *
  20. * @param {String} creatorName Name of the creator.
  21. * @param {Object} [proto] Prototype of the creator. Properties from the proto param will
  22. * be copied to the prototype of the creator.
  23. */
  24. defineEditorCreatorMock( creatorName, proto ) {
  25. amdUtils.define( 'creator-' + creatorName, [ 'core/creator' ], ( Creator ) => {
  26. class TestCreator extends Creator {}
  27. if ( proto ) {
  28. for ( let propName in proto ) {
  29. TestCreator.prototype[ propName ] = proto[ propName ];
  30. }
  31. }
  32. if ( !TestCreator.prototype.create ) {
  33. TestCreator.prototype.create = sinon.spy().named( creatorName + '-create' );
  34. }
  35. if ( !TestCreator.prototype.destroy ) {
  36. TestCreator.prototype.destroy = sinon.spy().named( creatorName + '-destroy' );
  37. }
  38. return TestCreator;
  39. } );
  40. },
  41. /**
  42. * Returns the number of elements return by the iterator.
  43. *
  44. * testUtils.getIteratorCount( [ 1, 2, 3, 4, 5 ] ); // 5;
  45. *
  46. * @param {Iterable.<*>} iterator Any iterator.
  47. * @returns {Number} Number of elements returned by that iterator.
  48. */
  49. getIteratorCount( iterator ) {
  50. let count = 0;
  51. for ( let _ of iterator ) { // jshint ignore:line
  52. count++;
  53. }
  54. return count;
  55. }
  56. };
  57. export default utils;