8
0

classiceditorui.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* globals document, Event */
  6. import ComponentFactory from '@ckeditor/ckeditor5-ui/src/componentfactory';
  7. import FocusTracker from '@ckeditor/ckeditor5-utils/src/focustracker';
  8. import ClassicEditorUI from '../src/classiceditorui';
  9. import ClassicEditorUIView from '../src/classiceditoruiview';
  10. import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor';
  11. import View from '@ckeditor/ckeditor5-ui/src/view';
  12. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  13. import utils from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
  14. testUtils.createSinonSandbox();
  15. describe( 'ClassicEditorUI', () => {
  16. let editorElement, editor, editable, view, ui;
  17. beforeEach( () => {
  18. editorElement = document.createElement( 'div' );
  19. document.body.appendChild( editorElement );
  20. editor = new ClassicTestEditor( editorElement, {
  21. toolbar: [ 'foo', 'bar' ],
  22. ui: {
  23. width: 100,
  24. height: 200
  25. }
  26. } );
  27. view = new ClassicEditorUIView( editor.locale );
  28. ui = new ClassicEditorUI( editor, view );
  29. editable = editor.editing.view.getRoot();
  30. ui.componentFactory.add( 'foo', viewCreator( 'foo' ) );
  31. ui.componentFactory.add( 'bar', viewCreator( 'bar' ) );
  32. } );
  33. describe( 'constructor()', () => {
  34. it( 'sets #editor', () => {
  35. expect( ui.editor ).to.equal( editor );
  36. } );
  37. it( 'sets #view', () => {
  38. expect( ui.view ).to.equal( view );
  39. } );
  40. it( 'creates #componentFactory factory', () => {
  41. expect( ui.componentFactory ).to.be.instanceOf( ComponentFactory );
  42. } );
  43. it( 'creates #focusTracker', () => {
  44. expect( ui.focusTracker ).to.be.instanceOf( FocusTracker );
  45. } );
  46. it( 'sets view#width and view#height', () => {
  47. expect( view.width ).to.equal( 100 );
  48. expect( view.height ).to.equal( 200 );
  49. } );
  50. describe( 'toolbar', () => {
  51. it( 'binds view.toolbar#isFocused to editor#focusTracker', () => {
  52. ui.focusTracker.isFocused = false;
  53. expect( view.toolbar.isActive ).to.be.false;
  54. ui.focusTracker.isFocused = true;
  55. expect( view.toolbar.isActive ).to.be.true;
  56. } );
  57. it( 'sets view.toolbar#limiterElement', () => {
  58. expect( view.toolbar.limiterElement ).to.equal( view.element );
  59. } );
  60. } );
  61. describe( 'editable', () => {
  62. it( 'registers view.editable#element in editor focus tracker', () => {
  63. ui.focusTracker.isFocused = false;
  64. view.editable.element.dispatchEvent( new Event( 'focus' ) );
  65. expect( ui.focusTracker.isFocused ).to.true;
  66. } );
  67. it( 'sets view.editable#name', () => {
  68. expect( view.editable.name ).to.equal( editable.rootName );
  69. } );
  70. it( 'binds view.editable#isFocused', () => {
  71. utils.assertBinding(
  72. view.editable,
  73. { isFocused: false },
  74. [
  75. [ editable, { isFocused: true } ]
  76. ],
  77. { isFocused: true }
  78. );
  79. } );
  80. it( 'binds view.editable#isReadOnly', () => {
  81. utils.assertBinding(
  82. view.editable,
  83. { isReadOnly: false },
  84. [
  85. [ editable, { isReadOnly: true } ]
  86. ],
  87. { isReadOnly: true }
  88. );
  89. } );
  90. } );
  91. } );
  92. describe( 'init()', () => {
  93. afterEach( () => {
  94. return ui.destroy();
  95. } );
  96. it( 'returns a promise', () => {
  97. document.body.appendChild( view.element );
  98. const promise = ui.init().then( () => {
  99. expect( promise ).to.be.instanceof( Promise );
  100. } );
  101. return promise;
  102. } );
  103. it( 'initializes the #view', () => {
  104. const spy = sinon.spy( view, 'init' );
  105. return ui.init().then( () => {
  106. sinon.assert.calledOnce( spy );
  107. } );
  108. } );
  109. it( 'fills view.toolbar#items with editor config', () => {
  110. return ui.init().then( () => {
  111. expect( view.toolbar.items ).to.have.length( 2 );
  112. expect( view.toolbar.items.get( 0 ).name ).to.equal( 'foo' );
  113. expect( view.toolbar.items.get( 1 ).name ).to.equal( 'bar' );
  114. } );
  115. } );
  116. } );
  117. describe( 'destroy()', () => {
  118. beforeEach( () => {
  119. document.body.appendChild( view.element );
  120. } );
  121. it( 'returns a promise', () => {
  122. return ui.init().then( () => {
  123. const promise = ui.destroy().then( () => {
  124. expect( promise ).to.be.instanceof( Promise );
  125. } );
  126. return promise;
  127. } );
  128. } );
  129. it( 'destroys the #view', () => {
  130. const spy = sinon.spy( view, 'destroy' );
  131. return ui.init()
  132. .then( () => ui.destroy() )
  133. .then( () => {
  134. sinon.assert.calledOnce( spy );
  135. } );
  136. } );
  137. } );
  138. } );
  139. function viewCreator( name ) {
  140. return ( locale ) => {
  141. const view = new View( locale );
  142. view.name = name;
  143. return view;
  144. };
  145. }