8
0

decouplededitoruiview.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 DecoupledEditorUIView from '../src/decouplededitoruiview';
  7. import EditingView from '@ckeditor/ckeditor5-engine/src/view/view';
  8. import ToolbarView from '@ckeditor/ckeditor5-ui/src/toolbar/toolbarview';
  9. import InlineEditableUIView from '@ckeditor/ckeditor5-ui/src/editableui/inline/inlineeditableuiview';
  10. import Locale from '@ckeditor/ckeditor5-utils/src/locale';
  11. import createRoot from '@ckeditor/ckeditor5-engine/tests/view/_utils/createroot.js';
  12. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  13. describe( 'DecoupledEditorUIView', () => {
  14. let locale, view, editingView, editingViewRoot;
  15. testUtils.createSinonSandbox();
  16. beforeEach( () => {
  17. locale = new Locale();
  18. editingView = new EditingView();
  19. editingViewRoot = createRoot( editingView.document );
  20. view = new DecoupledEditorUIView( locale, editingView );
  21. view.editable.name = editingViewRoot.rootName;
  22. } );
  23. describe( 'constructor()', () => {
  24. it( 'is virtual', () => {
  25. expect( view.template ).to.be.undefined;
  26. expect( view.element ).to.be.null;
  27. } );
  28. describe( '#toolbar', () => {
  29. it( 'is created', () => {
  30. expect( view.toolbar ).to.be.instanceof( ToolbarView );
  31. } );
  32. it( 'is given a locale object', () => {
  33. expect( view.toolbar.locale ).to.equal( locale );
  34. } );
  35. it( 'is not rendered', () => {
  36. expect( view.toolbar.isRendered ).to.be.false;
  37. } );
  38. describe( 'automatic items grouping', () => {
  39. it( 'should be disabled by default', () => {
  40. expect( view.toolbar.options.shouldGroupWhenFull ).to.be.undefined;
  41. } );
  42. it( 'should be controlled via options.shouldToolbarGroupWhenFull', () => {
  43. const locale = new Locale();
  44. const editingView = new EditingView();
  45. const editingViewRoot = createRoot( editingView.document );
  46. const view = new DecoupledEditorUIView( locale, editingView, {
  47. shouldToolbarGroupWhenFull: true
  48. } );
  49. view.editable.name = editingViewRoot.rootName;
  50. view.render();
  51. expect( view.toolbar.options.shouldGroupWhenFull ).to.be.true;
  52. return view.destroy();
  53. } );
  54. } );
  55. } );
  56. describe( '#editable', () => {
  57. it( 'is created', () => {
  58. expect( view.editable ).to.be.instanceof( InlineEditableUIView );
  59. } );
  60. it( 'is given a locale object', () => {
  61. expect( view.editable.locale ).to.equal( locale );
  62. } );
  63. it( 'is not rendered', () => {
  64. expect( view.editable.isRendered ).to.be.false;
  65. } );
  66. it( 'can be created out of an existing DOM element', () => {
  67. const editableElement = document.createElement( 'div' );
  68. const testView = new DecoupledEditorUIView( locale, editingView, {
  69. editableElement
  70. } );
  71. testView.editable.name = editingViewRoot.rootName;
  72. testView.render();
  73. expect( testView.editable.element ).to.equal( editableElement );
  74. testView.destroy();
  75. } );
  76. } );
  77. } );
  78. describe( 'render()', () => {
  79. beforeEach( () => {
  80. view.render();
  81. } );
  82. afterEach( () => {
  83. view.destroy();
  84. } );
  85. describe( '#toolbar', () => {
  86. it( 'is rendered but gets no parent', () => {
  87. expect( view.isRendered ).to.be.true;
  88. expect( view.toolbar.element.parentElement ).to.be.null;
  89. } );
  90. it( 'gets the CSS classes', () => {
  91. expect( view.toolbar.element.classList.contains( 'ck-reset_all' ) ).to.be.true;
  92. expect( view.toolbar.element.classList.contains( 'ck-rounded-corners' ) ).to.be.true;
  93. } );
  94. it( 'gets the "dir" attribute corresponding to Locale#uiLanguageDirection', () => {
  95. expect( view.toolbar.element.getAttribute( 'dir' ) ).to.equal( 'ltr' );
  96. } );
  97. } );
  98. describe( '#editable', () => {
  99. it( 'is rendered but gets no parent', () => {
  100. expect( view.isRendered ).to.be.true;
  101. expect( view.editable.element.parentElement ).to.be.null;
  102. } );
  103. } );
  104. } );
  105. describe( 'destroy', () => {
  106. beforeEach( () => {
  107. view.render();
  108. } );
  109. it( 'destroys #toolbar and #editable', () => {
  110. const toolbarSpy = sinon.spy( view.toolbar, 'destroy' );
  111. const editableSpy = sinon.spy( view.editable, 'destroy' );
  112. view.destroy();
  113. sinon.assert.calledOnce( toolbarSpy );
  114. sinon.assert.calledOnce( editableSpy );
  115. } );
  116. it( 'does not touch the toolbar#element and editable#element by default', () => {
  117. document.body.appendChild( view.toolbar.element );
  118. document.body.appendChild( view.editable.element );
  119. view.destroy();
  120. expect( view.toolbar.element.parentElement ).to.equal( document.body );
  121. expect( view.editable.element.parentElement ).to.equal( document.body );
  122. view.toolbar.element.remove();
  123. view.editable.element.remove();
  124. } );
  125. } );
  126. } );