utils.js 1.5 KB

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