utils.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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 View from '/ckeditor5/ui/view.js';
  7. import Controller from '/ckeditor5/ui/controller.js';
  8. import ControllerCollection from '/ckeditor5/ui/controllercollection.js';
  9. /**
  10. * Test utils for CKEditor UI.
  11. */
  12. const utils = {
  13. /**
  14. * Returns UI controller for given region/DOM selector pairs, which {@link ui.Controller#view}
  15. * is `document.body`. It is useful for manual tests which engage various UI components and/or
  16. * UI {@link ui.Controller} instances, where initialization and the process of insertion into
  17. * DOM could be problematic i.e. because of the number of instances.
  18. *
  19. * Usage:
  20. *
  21. * // Get the controller.
  22. * const controller = testUtils.createTestUIController();
  23. *
  24. * // Then use it to organize and initialize children.
  25. * controller.add( 'some-collection', childControllerInstance );
  26. *
  27. * @param {Object} regions An object literal with `regionName: DOM Selector pairs`.
  28. * See {@link ui.View#register}.
  29. */
  30. createTestUIController( regions ) {
  31. const TestUIView = class extends View {
  32. constructor() {
  33. super();
  34. this.element = document.body;
  35. for ( let r in regions ) {
  36. this.register( r, regions[ r ] );
  37. }
  38. }
  39. };
  40. const controller = new Controller( null, new TestUIView() );
  41. for ( let r in regions ) {
  42. controller.collections.add( new ControllerCollection( r ) );
  43. }
  44. return controller.init().then( () => {
  45. return controller;
  46. } );
  47. }
  48. };
  49. export default utils;