utils.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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 moduleUtils from '/tests/ckeditor5/_utils/module.js';
  7. /**
  8. * General test utils for CKEditor.
  9. */
  10. const utils = {
  11. /**
  12. * Creates Sinon sandbox in {@link bender#sinon} and plugs `afterEach()` callback which
  13. * restores all spies and stubs created in this sandbox.
  14. *
  15. * See https://github.com/ckeditor/ckeditor5-design/issues/72 and http://sinonjs.org/docs/#sinon-sandbox
  16. *
  17. * Usage:
  18. *
  19. * // Directly in the test file:
  20. * testUtils.createSinonSandbox();
  21. *
  22. * // Then inside tests you can use bender.sinon:
  23. * it( 'does something', () => {
  24. * testUtils.sinon.spy( obj, 'method' );
  25. * } );
  26. */
  27. createSinonSandbox() {
  28. before( () => {
  29. utils.sinon = sinon.sandbox.create();
  30. } );
  31. afterEach( () => {
  32. utils.sinon.restore();
  33. } );
  34. },
  35. /**
  36. * Defines CKEditor plugin which is a mock of an editor creator.
  37. *
  38. * The mocked creator is available under:
  39. *
  40. * editor.plugins.get( 'creator-thename' );
  41. *
  42. * @param {String} creatorName Name of the creator.
  43. * @param {Object} [proto] Prototype of the creator. Properties from the proto param will
  44. * be copied to the prototype of the creator.
  45. */
  46. defineEditorCreatorMock( creatorName, proto ) {
  47. moduleUtils.define( `creator-${ creatorName }/creator-${ creatorName }`, [ 'creator/creator' ], ( Creator ) => {
  48. class TestCreator extends Creator {}
  49. if ( proto ) {
  50. for ( let propName in proto ) {
  51. TestCreator.prototype[ propName ] = proto[ propName ];
  52. }
  53. }
  54. return TestCreator;
  55. } );
  56. }
  57. };
  58. export default utils;