8
0

utils.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. /* globals document */
  6. import View from '../../src/view';
  7. /**
  8. * Test utils for CKEditor UI.
  9. */
  10. const utils = {
  11. /**
  12. * Returns a view for a given region/DOM selector pairs, which {@link module:ui/view~View#element}
  13. * is `document.body`. It is useful for manual tests which engage various UI components and/or
  14. * UI {@link module:ui/view~View} 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 view.
  20. * const view = testUtils.createTestUIView( {
  21. * 'some-collection': '#collection'
  22. * } );
  23. *
  24. * // Then use it to organize and initialize children.
  25. * view.add( 'some-collection', childControllerInstance );
  26. *
  27. * @param {Object} regions An object literal with `regionName: [DOM Selector|callback]` pairs.
  28. *
  29. * See {@link module:ui/view~View#createCollection}.
  30. */
  31. createTestUIView( regions ) {
  32. const TestUIView = class extends View {
  33. constructor() {
  34. super();
  35. this.element = document.body;
  36. for ( const name in regions ) {
  37. const regionCollection = this[ name ] = this.createCollection();
  38. const callbackOrSelector = regions[ name ];
  39. regionCollection.setParent(
  40. typeof callbackOrSelector == 'string' ?
  41. document.querySelector( callbackOrSelector ) :
  42. callbackOrSelector( this.element )
  43. );
  44. }
  45. }
  46. };
  47. const view = new TestUIView();
  48. view.render();
  49. return view;
  50. }
  51. };
  52. export default utils;