classiceditorui.js 5.1 KB

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