utils.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. import View from '/ckeditor5/ui/view.js';
  8. import Controller from '/ckeditor5/ui/controller.js';
  9. import ControllerCollection from '/ckeditor5/ui/controllercollection.js';
  10. /**
  11. * General test utils for CKEditor.
  12. */
  13. const utils = {
  14. /**
  15. * Creates Sinon sandbox in {@link bender#sinon} and plugs `afterEach()` callback which
  16. * restores all spies and stubs created in this sandbox.
  17. *
  18. * See https://github.com/ckeditor/ckeditor5-design/issues/72 and http://sinonjs.org/docs/#sinon-sandbox
  19. *
  20. * Usage:
  21. *
  22. * // Directly in the test file:
  23. * testUtils.createSinonSandbox();
  24. *
  25. * // Then inside tests you can use bender.sinon:
  26. * it( 'does something', () => {
  27. * testUtils.sinon.spy( obj, 'method' );
  28. * } );
  29. */
  30. createSinonSandbox() {
  31. before( () => {
  32. utils.sinon = sinon.sandbox.create();
  33. } );
  34. afterEach( () => {
  35. utils.sinon.restore();
  36. } );
  37. },
  38. /**
  39. * Defines CKEditor plugin which is a mock of an editor creator.
  40. *
  41. * The mocked creator is available under:
  42. *
  43. * editor.plugins.get( 'creator-thename' );
  44. *
  45. * @param {String} creatorName Name of the creator.
  46. * @param {Object} [proto] Prototype of the creator. Properties from the proto param will
  47. * be copied to the prototype of the creator.
  48. */
  49. defineEditorCreatorMock( creatorName, proto ) {
  50. moduleUtils.define( `creator-${ creatorName }/creator-${ creatorName }`, [ 'creator/creator' ], ( Creator ) => {
  51. class TestCreator extends Creator {}
  52. if ( proto ) {
  53. for ( let propName in proto ) {
  54. TestCreator.prototype[ propName ] = proto[ propName ];
  55. }
  56. }
  57. return TestCreator;
  58. } );
  59. },
  60. /**
  61. * Returns UI controller for given region/DOM selector pairs, which {@link ui.Controller#view}
  62. * is `document.body`. It is useful for manual tests which engage various UI components and/or
  63. * UI {@link ui.Controller} instances, where initialization and the process of insertion into
  64. * DOM could be problematic i.e. because of the number of instances.
  65. *
  66. * Usage:
  67. *
  68. * // Get the controller.
  69. * const controller = testUtils.createTestUIController();
  70. *
  71. * // Then use it to organize and initialize children.
  72. * controller.add( 'some-collection', childControllerInstance );
  73. *
  74. * @param {Object} regions An object literal with `regionName: DOM Selector pairs`.
  75. * See {@link ui.View#register}.
  76. */
  77. createTestUIController( regions ) {
  78. const TestUIView = class extends View {
  79. constructor() {
  80. super();
  81. this.element = document.body;
  82. for ( let r in regions ) {
  83. this.register( r, regions[ r ] );
  84. }
  85. }
  86. };
  87. const controller = new Controller( null, new TestUIView() );
  88. for ( let r in regions ) {
  89. controller.collections.add( new ControllerCollection( r ) );
  90. }
  91. return controller.init().then( () => {
  92. return controller;
  93. } );
  94. }
  95. };
  96. export default utils;