utils.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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 ( const 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. callbackOrSelector( this.element )
  40. );
  41. }
  42. }
  43. };
  44. const view = new TestUIView();
  45. view.init();
  46. return view;
  47. }
  48. };
  49. export default utils;