inlineeditorui.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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 InlineEditorUI from '../src/inlineeditorui';
  9. import InlineEditorUIView from '../src/inlineeditoruiview';
  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( 'InlineEditorUI', () => {
  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. } );
  24. view = new InlineEditorUIView( editor.locale );
  25. ui = new InlineEditorUI( editor, view );
  26. editable = editor.editing.view.getRoot();
  27. ui.componentFactory.add( 'foo', viewCreator( 'foo' ) );
  28. ui.componentFactory.add( 'bar', viewCreator( 'bar' ) );
  29. } );
  30. describe( 'constructor()', () => {
  31. it( 'sets #editor', () => {
  32. expect( ui.editor ).to.equal( editor );
  33. } );
  34. it( 'sets #view', () => {
  35. expect( ui.view ).to.equal( view );
  36. } );
  37. it( 'creates #componentFactory factory', () => {
  38. expect( ui.componentFactory ).to.be.instanceOf( ComponentFactory );
  39. } );
  40. it( 'creates #focusTracker', () => {
  41. expect( ui.focusTracker ).to.be.instanceOf( FocusTracker );
  42. } );
  43. describe( 'panel', () => {
  44. it( 'binds view.panel#isActive to editor.ui#focusTracker', () => {
  45. ui.focusTracker.isFocused = false;
  46. expect( view.panel.isActive ).to.be.false;
  47. ui.focusTracker.isFocused = true;
  48. expect( view.panel.isActive ).to.be.true;
  49. } );
  50. it( 'sets view.panel#targetElement', () => {
  51. expect( view.panel.targetElement ).to.equal( view.editableElement );
  52. } );
  53. } );
  54. describe( 'editable', () => {
  55. it( 'registers view.editable#element in editor focus tracker', () => {
  56. ui.focusTracker.isFocused = false;
  57. view.editable.element.dispatchEvent( new Event( 'focus' ) );
  58. expect( ui.focusTracker.isFocused ).to.true;
  59. } );
  60. it( 'sets view.editable#name', () => {
  61. expect( view.editable.name ).to.equal( editable.rootName );
  62. } );
  63. it( 'binds view.editable#isFocused', () => {
  64. utils.assertBinding(
  65. view.editable,
  66. { isFocused: false },
  67. [
  68. [ ui.focusTracker, { isFocused: true } ]
  69. ],
  70. { isFocused: true }
  71. );
  72. } );
  73. it( 'binds view.editable#isReadOnly', () => {
  74. utils.assertBinding(
  75. view.editable,
  76. { isReadOnly: false },
  77. [
  78. [ editable, { isReadOnly: true } ]
  79. ],
  80. { isReadOnly: true }
  81. );
  82. } );
  83. } );
  84. } );
  85. describe( 'init()', () => {
  86. afterEach( () => {
  87. return ui.destroy();
  88. } );
  89. it( 'returns a promise', () => {
  90. const promise = ui.init().then( () => {
  91. expect( promise ).to.be.instanceof( Promise );
  92. } );
  93. return promise;
  94. } );
  95. it( 'initializes the #view', () => {
  96. const spy = sinon.spy( view, 'init' );
  97. return ui.init().then( () => {
  98. sinon.assert.calledOnce( spy );
  99. } );
  100. } );
  101. it( 'fills view.toolbar#items with editor config', () => {
  102. const spy = testUtils.sinon.spy( toolbarUtils, 'getItemsFromConfig' );
  103. return ui.init().then( () => {
  104. sinon.assert.calledWithExactly( spy,
  105. editor.config.get( 'toolbar' ),
  106. view.toolbar.items,
  107. ui.componentFactory
  108. );
  109. } );
  110. } );
  111. it( 'initializes keyboard navigation between view#toolbar and view#editable', () => {
  112. const spy = testUtils.sinon.spy( toolbarUtils, 'enableToolbarKeyboardFocus' );
  113. return ui.init().then( () => {
  114. sinon.assert.calledWithExactly( spy, {
  115. origin: editor.editing.view,
  116. originFocusTracker: ui.focusTracker,
  117. originKeystrokeHandler: editor.keystrokes,
  118. toolbar: view.toolbar
  119. } );
  120. } );
  121. } );
  122. } );
  123. describe( 'destroy()', () => {
  124. it( 'returns a promise', () => {
  125. return ui.init().then( () => {
  126. const promise = ui.destroy().then( () => {
  127. expect( promise ).to.be.instanceof( Promise );
  128. } );
  129. return promise;
  130. } );
  131. } );
  132. it( 'destroys the #view', () => {
  133. const spy = sinon.spy( view, 'destroy' );
  134. return ui.init()
  135. .then( () => ui.destroy() )
  136. .then( () => {
  137. sinon.assert.calledOnce( spy );
  138. } );
  139. } );
  140. } );
  141. } );
  142. function viewCreator( name ) {
  143. return ( locale ) => {
  144. const view = new View( locale );
  145. view.name = name;
  146. view.element = document.createElement( 'a' );
  147. return view;
  148. };
  149. }