8
0

baseeditor.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* bender-tags: editor */
  6. 'use strict';
  7. import Editor from '/ckeditor5/editor.js';
  8. import EditorConfig from '/ckeditor5/editorconfig.js';
  9. import Command from '/ckeditor5/command/command.js';
  10. import Locale from '/ckeditor5/utils/locale.js';
  11. import CKEditorError from '/ckeditor5/utils/ckeditorerror.js';
  12. import Document from '/ckeditor5/engine/treemodel/document.js';
  13. describe( 'Editor', () => {
  14. describe( 'config', () => {
  15. it( 'should be an instance of EditorConfig', () => {
  16. const editor = new Editor();
  17. expect( editor.config ).to.be.an.instanceof( EditorConfig );
  18. } );
  19. } );
  20. describe( 'locale', () => {
  21. it( 'is instantiated and t() is exposed', () => {
  22. const editor = new Editor();
  23. expect( editor.locale ).to.be.instanceof( Locale );
  24. expect( editor.t ).to.equal( editor.locale.t );
  25. } );
  26. it( 'is configured with the config.lang', () => {
  27. const editor = new Editor( null, { lang: 'pl' } );
  28. expect( editor.locale.lang ).to.equal( 'pl' );
  29. } );
  30. } );
  31. describe( 'firstElement', () => {
  32. it( 'should be set to first element', () => {
  33. const editor = new Editor( { foo: 'a', bar: 'b' } );
  34. expect( editor.firstElement ).to.equal( 'a' );
  35. } );
  36. it( 'should be set to null if there are no elements', () => {
  37. const editor = new Editor();
  38. expect( editor.firstElement ).to.be.null;
  39. } );
  40. } );
  41. describe( 'firstElementName', () => {
  42. it( 'should be set to first element name', () => {
  43. const editor = new Editor( { foo: 'a', bar: 'b' } );
  44. expect( editor.firstElementName ).to.equal( 'foo' );
  45. } );
  46. it( 'should be set to null if there are no elements', () => {
  47. const editor = new Editor();
  48. expect( editor.firstElementName ).to.be.null;
  49. } );
  50. } );
  51. describe( 'destroy', () => {
  52. it( 'should fire "destroy"', () => {
  53. const editor = new Editor();
  54. let spy = sinon.spy();
  55. editor.on( 'destroy', spy );
  56. return editor.destroy().then( () => {
  57. expect( spy.calledOnce ).to.be.true;
  58. } );
  59. } );
  60. // Note: Tests for destroying creators are in creator/creator.js.
  61. // When destroying creator will be generalized to destroying plugins,
  62. // move that code here.
  63. } );
  64. describe( 'execute', () => {
  65. it( 'should execute specified command', () => {
  66. const editor = new Editor();
  67. let command = new Command( editor );
  68. sinon.spy( command, '_execute' );
  69. editor.commands.set( 'commandName', command );
  70. editor.execute( 'commandName' );
  71. expect( command._execute.calledOnce ).to.be.true;
  72. } );
  73. it( 'should throw an error if specified command has not been added', () => {
  74. const editor = new Editor();
  75. expect( () => {
  76. editor.execute( 'command' );
  77. } ).to.throw( CKEditorError, /^editor-command-not-found:/ );
  78. } );
  79. } );
  80. describe( 'setData', () => {
  81. let editor;
  82. beforeEach( () => {
  83. editor = new Editor();
  84. editor.document = new Document();
  85. editor.data = {
  86. set: sinon.spy()
  87. };
  88. } );
  89. it( 'should set data of the first root', () => {
  90. editor.document.createRoot( 'firstRoot', 'div' );
  91. editor.setData( 'foo' );
  92. expect( editor.data.set.calledOnce ).to.be.true;
  93. expect( editor.data.set.calledWithExactly( 'firstRoot', 'foo' ) ).to.be.true;
  94. } );
  95. it( 'should set data of the specified root', () => {
  96. editor.setData( 'foo', 'someRoot' );
  97. expect( editor.data.set.calledOnce ).to.be.true;
  98. expect( editor.data.set.calledWithExactly( 'someRoot', 'foo' ) ).to.be.true;
  99. } );
  100. it( 'should throw when no roots', () => {
  101. expect( () => {
  102. editor.setData( 'foo' );
  103. } ).to.throw( CKEditorError, /^editor-no-editable-roots:/ );
  104. } );
  105. it( 'should throw when more than one root and no root name given', () => {
  106. editor.document.createRoot( 'firstRoot', 'div' );
  107. editor.document.createRoot( 'secondRoot', 'div' );
  108. expect( () => {
  109. editor.setData( 'foo' );
  110. } ).to.throw( CKEditorError, /^editor-editable-root-name-missing:/ );
  111. } );
  112. it( 'should throw when no data controller', () => {
  113. expect( () => {
  114. editor.data = null;
  115. editor.setData( 'foo' );
  116. } ).to.throw( CKEditorError, /^editor-no-datacontroller:/ );
  117. } );
  118. } );
  119. describe( 'getData', () => {
  120. let editor;
  121. beforeEach( () => {
  122. editor = new Editor();
  123. editor.document = new Document();
  124. editor.data = {
  125. get( rootName ) {
  126. return `data for ${ rootName }`;
  127. }
  128. };
  129. } );
  130. it( 'should get data from the first root', () => {
  131. editor.document.createRoot( 'firstRoot', 'div' );
  132. expect( editor.getData() ).to.equal( 'data for firstRoot' );
  133. } );
  134. it( 'should get data from the specified root', () => {
  135. expect( editor.getData( 'someRoot' ) ).to.equal( 'data for someRoot' );
  136. } );
  137. it( 'should throw when no roots', () => {
  138. expect( () => {
  139. editor.getData();
  140. } ).to.throw( CKEditorError, /^editor-no-editable-roots:/ );
  141. } );
  142. it( 'should throw when more than one root and no root name given', () => {
  143. editor.document.createRoot( 'firstRoot', 'div' );
  144. editor.document.createRoot( 'secondRoot', 'div' );
  145. expect( () => {
  146. editor.getData();
  147. } ).to.throw( CKEditorError, /^editor-editable-root-name-missing:/ );
  148. } );
  149. it( 'should throw when no data controller', () => {
  150. expect( () => {
  151. editor.data = null;
  152. editor.getData();
  153. } ).to.throw( CKEditorError, /^editor-no-datacontroller:/ );
  154. } );
  155. } );
  156. } );