balloontoolbareditorui.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 BalloonToolbarEditorUI from '../src/balloontoolbareditorui';
  8. import BalloonToolbarEditorUIView from '../src/balloontoolbareditoruiview';
  9. import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor';
  10. import ContextualToolbar from '@ckeditor/ckeditor5-ui/src/toolbar/contextual/contextualtoolbar';
  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( 'BalloonToolbarEditorUI', () => {
  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. plugins: [ ContextualToolbar ]
  23. } );
  24. return editor.initPlugins()
  25. .then( () => {
  26. view = new BalloonToolbarEditorUIView( editor.locale );
  27. ui = new BalloonToolbarEditorUI( editor, view );
  28. editable = editor.editing.view.getRoot();
  29. } );
  30. } );
  31. describe( 'constructor()', () => {
  32. it( 'sets #editor', () => {
  33. expect( ui.editor ).to.equal( editor );
  34. } );
  35. it( 'sets #view', () => {
  36. expect( ui.view ).to.equal( view );
  37. } );
  38. it( 'creates #componentFactory factory', () => {
  39. expect( ui.componentFactory ).to.be.instanceOf( ComponentFactory );
  40. } );
  41. it( 'creates #focusTracker', () => {
  42. expect( ui.focusTracker ).to.be.instanceOf( FocusTracker );
  43. } );
  44. describe( 'editable', () => {
  45. it( 'registers view.editable#element in editor focus tracker', () => {
  46. ui.focusTracker.isFocused = false;
  47. view.editable.element.dispatchEvent( new Event( 'focus' ) );
  48. expect( ui.focusTracker.isFocused ).to.true;
  49. } );
  50. it( 'sets view.editable#name', () => {
  51. expect( view.editable.name ).to.equal( editable.rootName );
  52. } );
  53. it( 'binds view.editable#isFocused', () => {
  54. utils.assertBinding(
  55. view.editable,
  56. { isFocused: false },
  57. [
  58. [ ui.focusTracker, { isFocused: true } ]
  59. ],
  60. { isFocused: true }
  61. );
  62. } );
  63. it( 'binds view.editable#isReadOnly', () => {
  64. utils.assertBinding(
  65. view.editable,
  66. { isReadOnly: false },
  67. [
  68. [ editable, { isReadOnly: true } ]
  69. ],
  70. { isReadOnly: true }
  71. );
  72. } );
  73. } );
  74. } );
  75. describe( 'init()', () => {
  76. afterEach( () => {
  77. return ui.destroy();
  78. } );
  79. it( 'returns a promise', () => {
  80. const promise = ui.init().then( () => {
  81. expect( promise ).to.be.instanceof( Promise );
  82. } );
  83. return promise;
  84. } );
  85. it( 'initializes the #view', () => {
  86. const spy = sinon.spy( view, 'init' );
  87. return ui.init().then( () => {
  88. sinon.assert.calledOnce( spy );
  89. } );
  90. } );
  91. it( 'initializes keyboard navigation between view#toolbar and view#editable', () => {
  92. const toolbar = editor.plugins.get( 'ui/contextualtoolbar' );
  93. const spy = testUtils.sinon.spy( toolbar.toolbarView, 'focus' );
  94. return ui.init().then( () => {
  95. ui.focusTracker.isFocused = true;
  96. toolbar.toolbarView.focusTracker.isFocused = false;
  97. editor.keystrokes.press( {
  98. keyCode: keyCodes.f10,
  99. altKey: true,
  100. preventDefault: sinon.spy(),
  101. stopPropagation: sinon.spy()
  102. } );
  103. sinon.assert.calledOnce( spy );
  104. } );
  105. } );
  106. } );
  107. describe( 'destroy()', () => {
  108. it( 'returns a promise', () => {
  109. return ui.init().then( () => {
  110. const promise = ui.destroy().then( () => {
  111. expect( promise ).to.be.instanceof( Promise );
  112. } );
  113. return promise;
  114. } );
  115. } );
  116. it( 'destroys the #view', () => {
  117. const spy = sinon.spy( view, 'destroy' );
  118. return ui.init()
  119. .then( () => ui.destroy() )
  120. .then( () => {
  121. sinon.assert.calledOnce( spy );
  122. } );
  123. } );
  124. } );
  125. } );