decouplededitoruiview.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /**
  2. * @license Copyright (c) 2003-2019, 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. } );
  39. describe( '#editable', () => {
  40. it( 'is created', () => {
  41. expect( view.editable ).to.be.instanceof( InlineEditableUIView );
  42. } );
  43. it( 'is given a locale object', () => {
  44. expect( view.editable.locale ).to.equal( locale );
  45. } );
  46. it( 'is not rendered', () => {
  47. expect( view.editable.isRendered ).to.be.false;
  48. } );
  49. it( 'can be created out of an existing DOM element', () => {
  50. const editableElement = document.createElement( 'div' );
  51. const testView = new DecoupledEditorUIView( locale, editingView, editableElement );
  52. testView.editable.name = editingViewRoot.rootName;
  53. testView.render();
  54. expect( testView.editable.element ).to.equal( editableElement );
  55. testView.destroy();
  56. } );
  57. } );
  58. } );
  59. describe( 'render()', () => {
  60. beforeEach( () => {
  61. view.render();
  62. } );
  63. afterEach( () => {
  64. view.destroy();
  65. } );
  66. describe( '#toolbar', () => {
  67. it( 'is rendered but gets no parent', () => {
  68. expect( view.isRendered ).to.be.true;
  69. expect( view.toolbar.element.parentElement ).to.be.null;
  70. } );
  71. it( 'gets the CSS classes', () => {
  72. expect( view.toolbar.element.classList.contains( 'ck-reset_all' ) ).to.be.true;
  73. expect( view.toolbar.element.classList.contains( 'ck-rounded-corners' ) ).to.be.true;
  74. } );
  75. it( 'gets the "dir" attribute corresponding to Locale#uiLanguageDirection', () => {
  76. expect( view.toolbar.element.getAttribute( 'dir' ) ).to.equal( 'ltr' );
  77. } );
  78. } );
  79. describe( '#editable', () => {
  80. it( 'is rendered but gets no parent', () => {
  81. expect( view.isRendered ).to.be.true;
  82. expect( view.editable.element.parentElement ).to.be.null;
  83. } );
  84. } );
  85. } );
  86. describe( 'destroy', () => {
  87. beforeEach( () => {
  88. view.render();
  89. } );
  90. it( 'destroys #toolbar and #editable', () => {
  91. const toolbarSpy = sinon.spy( view.toolbar, 'destroy' );
  92. const editableSpy = sinon.spy( view.editable, 'destroy' );
  93. view.destroy();
  94. sinon.assert.calledOnce( toolbarSpy );
  95. sinon.assert.calledOnce( editableSpy );
  96. } );
  97. it( 'does not touch the toolbar#element and editable#element by default', () => {
  98. document.body.appendChild( view.toolbar.element );
  99. document.body.appendChild( view.editable.element );
  100. view.destroy();
  101. expect( view.toolbar.element.parentElement ).to.equal( document.body );
  102. expect( view.editable.element.parentElement ).to.equal( document.body );
  103. view.toolbar.element.remove();
  104. view.editable.element.remove();
  105. } );
  106. } );
  107. } );