tools.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /**
  2. * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* globals bender, sinon */
  6. 'use strict';
  7. ( function() {
  8. bender.tools.core = {
  9. /**
  10. * Defines CKEditor plugin which is a mock of an editor creator.
  11. *
  12. * If `proto` is not set or it does not define `create()` and `destroy()` methods,
  13. * then they will be set to Sinon spies. Therefore the shortest usage is:
  14. *
  15. * bender.tools.defineEditorCreatorMock( 'test1' );
  16. *
  17. * The mocked creator is available under:
  18. *
  19. * editor.plugins.get( 'creator-thename' );
  20. *
  21. * @param {String} creatorName Name of the creator.
  22. * @param {Object} [proto] Prototype of the creator. Properties from the proto param will
  23. * be copied to the prototype of the creator.
  24. */
  25. defineEditorCreatorMock: function( creatorName, proto ) {
  26. CKEDITOR.define( 'plugin!creator-' + creatorName, [ 'creator' ], function( Creator ) {
  27. return mockCreator( Creator );
  28. } );
  29. function mockCreator( Creator ) {
  30. class TestCreator extends Creator {}
  31. if ( proto ) {
  32. for ( var propName in proto ) {
  33. TestCreator.prototype[ propName ] = proto[ propName ];
  34. }
  35. }
  36. if ( !TestCreator.prototype.create ) {
  37. TestCreator.prototype.create = sinon.spy().named( creatorName + '-create' );
  38. }
  39. if ( !TestCreator.prototype.destroy ) {
  40. TestCreator.prototype.destroy = sinon.spy().named( creatorName + '-destroy' );
  41. }
  42. return TestCreator;
  43. }
  44. }
  45. };
  46. } )();