8
0

utils.js 1.6 KB

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