classiceditorui.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* globals document, Event */
  6. /* bender-tags: editor, browser-only */
  7. import ComponentFactory from 'ckeditor5-ui/src/componentfactory';
  8. import FocusTracker from 'ckeditor5-utils/src/focustracker';
  9. import ClassicEditorUI from 'ckeditor5-editor-classic/src/classiceditorui';
  10. import ClassicEditorUIView from 'ckeditor5-editor-classic/src/classiceditoruiview';
  11. import ClassicTestEditor from 'ckeditor5-core/tests/_utils/classictesteditor';
  12. import View from 'ckeditor5-ui/src/view';
  13. import testUtils from 'ckeditor5-core/tests/_utils/utils';
  14. import utils from '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. [ editable, { 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. return ui.init().then( () => {
  112. expect( view.toolbar.items ).to.have.length( 2 );
  113. expect( view.toolbar.items.get( 0 ).name ).to.equal( 'foo' );
  114. expect( view.toolbar.items.get( 1 ).name ).to.equal( 'bar' );
  115. } );
  116. } );
  117. } );
  118. describe( 'destroy()', () => {
  119. beforeEach( () => {
  120. document.body.appendChild( view.element );
  121. } );
  122. it( 'returns a promise', () => {
  123. return ui.init().then( () => {
  124. const promise = ui.destroy().then( () => {
  125. expect( promise ).to.be.instanceof( Promise );
  126. } );
  127. return promise;
  128. } );
  129. } );
  130. it( 'destroys the #view', () => {
  131. const spy = sinon.spy( view, 'destroy' );
  132. return ui.init()
  133. .then( () => ui.destroy() )
  134. .then( () => {
  135. sinon.assert.calledOnce( spy );
  136. } );
  137. } );
  138. } );
  139. } );
  140. function viewCreator( name ) {
  141. return ( locale ) => {
  142. const view = new View( locale );
  143. view.name = name;
  144. return view;
  145. };
  146. }