inlineeditorui.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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 { keyCodes } from '@ckeditor/ckeditor5-utils/src/keyboard';
  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#isVisible to editor.ui#focusTracker', () => {
  45. ui.focusTracker.isFocused = false;
  46. expect( view.panel.isVisible ).to.be.false;
  47. ui.focusTracker.isFocused = true;
  48. expect( view.panel.isVisible ).to.be.true;
  49. } );
  50. // https://github.com/ckeditor/ckeditor5-editor-inline/issues/4
  51. it( 'pin() is called on editor.editable.view#render', () => {
  52. const spy = sinon.spy( view.panel, 'pin' );
  53. view.panel.hide();
  54. editor.editing.view.fire( 'render' );
  55. sinon.assert.notCalled( spy );
  56. view.panel.show();
  57. editor.editing.view.fire( 'render' );
  58. sinon.assert.calledOnce( spy );
  59. sinon.assert.calledWithExactly( spy, {
  60. target: view.editableElement,
  61. positions: sinon.match.array
  62. } );
  63. } );
  64. } );
  65. describe( 'editable', () => {
  66. it( 'registers view.editable#element in editor focus tracker', () => {
  67. ui.focusTracker.isFocused = false;
  68. view.editable.element.dispatchEvent( new Event( 'focus' ) );
  69. expect( ui.focusTracker.isFocused ).to.true;
  70. } );
  71. it( 'sets view.editable#name', () => {
  72. expect( view.editable.name ).to.equal( editable.rootName );
  73. } );
  74. it( 'binds view.editable#isFocused', () => {
  75. utils.assertBinding(
  76. view.editable,
  77. { isFocused: false },
  78. [
  79. [ ui.focusTracker, { isFocused: true } ]
  80. ],
  81. { isFocused: true }
  82. );
  83. } );
  84. it( 'binds view.editable#isReadOnly', () => {
  85. utils.assertBinding(
  86. view.editable,
  87. { isReadOnly: false },
  88. [
  89. [ editable, { isReadOnly: true } ]
  90. ],
  91. { isReadOnly: true }
  92. );
  93. } );
  94. } );
  95. } );
  96. describe( 'init()', () => {
  97. afterEach( () => {
  98. return ui.destroy();
  99. } );
  100. it( 'returns a promise', () => {
  101. const promise = ui.init().then( () => {
  102. expect( promise ).to.be.instanceof( Promise );
  103. } );
  104. return promise;
  105. } );
  106. it( 'initializes the #view', () => {
  107. const spy = sinon.spy( view, 'init' );
  108. return ui.init().then( () => {
  109. sinon.assert.calledOnce( spy );
  110. } );
  111. } );
  112. it( 'fills view.toolbar#items with editor config', () => {
  113. const spy = testUtils.sinon.spy( view.toolbar, 'fillFromConfig' );
  114. return ui.init().then( () => {
  115. sinon.assert.calledWithExactly( spy, editor.config.get( 'toolbar' ), ui.componentFactory );
  116. } );
  117. } );
  118. it( 'initializes keyboard navigation between view#toolbar and view#editable', () => {
  119. const spy = testUtils.sinon.spy( view.toolbar, 'focus' );
  120. return ui.init().then( () => {
  121. ui.focusTracker.isFocused = true;
  122. ui.view.toolbar.focusTracker.isFocused = false;
  123. editor.keystrokes.press( {
  124. keyCode: keyCodes.f10,
  125. altKey: true,
  126. preventDefault: sinon.spy(),
  127. stopPropagation: sinon.spy()
  128. } );
  129. sinon.assert.calledOnce( spy );
  130. } );
  131. } );
  132. } );
  133. describe( 'destroy()', () => {
  134. it( 'returns a promise', () => {
  135. return ui.init().then( () => {
  136. const promise = ui.destroy().then( () => {
  137. expect( promise ).to.be.instanceof( Promise );
  138. } );
  139. return promise;
  140. } );
  141. } );
  142. it( 'destroys the #view', () => {
  143. const spy = sinon.spy( view, 'destroy' );
  144. return ui.init()
  145. .then( () => ui.destroy() )
  146. .then( () => {
  147. sinon.assert.calledOnce( spy );
  148. } );
  149. } );
  150. } );
  151. } );
  152. function viewCreator( name ) {
  153. return locale => {
  154. const view = new View( locale );
  155. view.name = name;
  156. view.element = document.createElement( 'a' );
  157. return view;
  158. };
  159. }