classiceditoruiview.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 ClassicEditorUIView from 'ckeditor5/editor-classic/classiceditoruiview.js';
  8. import ClassicTestEditor from 'tests/core/_utils/classictesteditor.js';
  9. import View from 'ckeditor5/ui/view.js';
  10. import StickyToolbarView from 'ckeditor5/ui/toolbar/sticky/stickytoolbarview.js';
  11. import InlineEditableUIView from 'ckeditor5/ui/editableui/inline/inlineeditableuiview.js';
  12. import testUtils from 'tests/utils/_utils/utils.js';
  13. describe( 'ClassicEditorUIView', () => {
  14. let editorElement, editor, editable, view;
  15. beforeEach( () => {
  16. editorElement = document.createElement( 'div' );
  17. document.body.appendChild( editorElement );
  18. editor = new ClassicTestEditor( editorElement, {
  19. toolbar: [ 'foo', 'bar' ]
  20. } );
  21. editable = editor.editing.view.createRoot( document.createElement( 'div' ) );
  22. editor.ui = view = new ClassicEditorUIView( editor, editor.locale );
  23. function viewCreator( name ) {
  24. return ( locale ) => {
  25. const view = new View( locale );
  26. view.name = name;
  27. return view;
  28. };
  29. }
  30. view.featureComponents.add( 'foo', viewCreator( 'foo' ) );
  31. view.featureComponents.add( 'bar', viewCreator( 'bar' ) );
  32. } );
  33. describe( 'constructor()', () => {
  34. describe( 'toolbar', () => {
  35. it( 'creates toolbar', () => {
  36. expect( view.toolbar ).to.be.instanceof( StickyToolbarView );
  37. } );
  38. it( 'binds view.toolbar#isFocused to editor#focusTracker', () => {
  39. editor.focusTracker.isFocused = false;
  40. expect( view.toolbar.isActive ).to.false;
  41. editor.focusTracker.isFocused = true;
  42. expect( view.toolbar.isActive ).to.true;
  43. } );
  44. } );
  45. describe( 'editable', () => {
  46. it( 'creates view#editable', () => {
  47. expect( view.editable ).to.be.instanceof( InlineEditableUIView );
  48. } );
  49. it( 'registers editable#element in editor focus tracker', () => {
  50. return view.init()
  51. .then( () => {
  52. editor.focusTracker.isFocused = false;
  53. view.editable.element.dispatchEvent( new Event( 'focus' ) );
  54. expect( editor.focusTracker.isFocused ).to.true;
  55. } );
  56. } );
  57. it( 'sets view.editable#name', () => {
  58. expect( view.editable.name ).to.equal( editable.rootName );
  59. } );
  60. it( 'binds view.editable#isFocused', () => {
  61. testUtils.assertBinding(
  62. view.editable,
  63. { isFocused: false },
  64. [
  65. [ editable, { isFocused: true } ]
  66. ],
  67. { isFocused: true }
  68. );
  69. } );
  70. it( 'binds view.editable#isReadOnly', () => {
  71. testUtils.assertBinding(
  72. view.editable,
  73. { isReadOnly: false },
  74. [
  75. [ editable, { isReadOnly: true } ]
  76. ],
  77. { isReadOnly: true }
  78. );
  79. } );
  80. } );
  81. } );
  82. describe( 'editableElement', () => {
  83. it( 'returns editable\'s view element', () => {
  84. document.body.appendChild( view.element );
  85. return view.init()
  86. .then( () => {
  87. expect( view.editableElement.getAttribute( 'contentEditable' ) ).to.equal( 'true' );
  88. } );
  89. } );
  90. } );
  91. describe( 'init', () => {
  92. it( 'returns a promise', () => {
  93. document.body.appendChild( view.element );
  94. expect( view.init() ).to.be.instanceof( Promise );
  95. } );
  96. it( 'sets view.toolbar#limiterElement', () => {
  97. return view.init().then( () => {
  98. expect( view.toolbar.limiterElement ).to.equal( view.element );
  99. } );
  100. } );
  101. it( 'fills view.toolbar#items with editor config', () => {
  102. return view.init().then( () => {
  103. expect( view.toolbar.items ).to.have.length( 2 );
  104. expect( view.toolbar.items.get( 0 ).name ).to.equal( 'foo' );
  105. expect( view.toolbar.items.get( 1 ).name ).to.equal( 'bar' );
  106. } );
  107. } );
  108. } );
  109. } );