8
0

baseeditor.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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( 'destroy', () => {
  32. it( 'should fire "destroy"', () => {
  33. const editor = new Editor();
  34. let spy = sinon.spy();
  35. editor.on( 'destroy', spy );
  36. return editor.destroy().then( () => {
  37. expect( spy.calledOnce ).to.be.true;
  38. } );
  39. } );
  40. // Note: Tests for destroying creators are in creator/creator.js.
  41. // When destroying creator will be generalized to destroying plugins,
  42. // move that code here.
  43. } );
  44. describe( 'execute', () => {
  45. it( 'should execute specified command', () => {
  46. const editor = new Editor();
  47. let command = new Command( editor );
  48. sinon.spy( command, '_execute' );
  49. editor.commands.set( 'commandName', command );
  50. editor.execute( 'commandName' );
  51. expect( command._execute.calledOnce ).to.be.true;
  52. } );
  53. it( 'should throw an error if specified command has not been added', () => {
  54. const editor = new Editor();
  55. expect( () => {
  56. editor.execute( 'command' );
  57. } ).to.throw( CKEditorError, /^editor-command-not-found:/ );
  58. } );
  59. } );
  60. describe( 'setData', () => {
  61. let editor;
  62. beforeEach( () => {
  63. editor = new Editor();
  64. editor.document = new Document();
  65. editor.data = {
  66. set: sinon.spy()
  67. };
  68. } );
  69. it( 'should set data of the first root', () => {
  70. editor.document.createRoot( 'firstRoot', 'div' );
  71. editor.setData( 'foo' );
  72. expect( editor.data.set.calledOnce ).to.be.true;
  73. expect( editor.data.set.calledWithExactly( 'firstRoot', 'foo' ) ).to.be.true;
  74. } );
  75. it( 'should set data of the specified root', () => {
  76. editor.setData( 'foo', 'someRoot' );
  77. expect( editor.data.set.calledOnce ).to.be.true;
  78. expect( editor.data.set.calledWithExactly( 'someRoot', 'foo' ) ).to.be.true;
  79. } );
  80. it( 'should throw when no roots', () => {
  81. expect( () => {
  82. editor.setData( 'foo' );
  83. } ).to.throw( CKEditorError, /^editor-no-editable-roots:/ );
  84. } );
  85. it( 'should throw when more than one root and no root name given', () => {
  86. editor.document.createRoot( 'firstRoot', 'div' );
  87. editor.document.createRoot( 'secondRoot', 'div' );
  88. expect( () => {
  89. editor.setData( 'foo' );
  90. } ).to.throw( CKEditorError, /^editor-editable-root-name-missing:/ );
  91. } );
  92. it( 'should throw when no data controller', () => {
  93. expect( () => {
  94. editor.data = null;
  95. editor.setData( 'foo' );
  96. } ).to.throw( CKEditorError, /^editor-no-datacontroller:/ );
  97. } );
  98. } );
  99. describe( 'getData', () => {
  100. let editor;
  101. beforeEach( () => {
  102. editor = new Editor();
  103. editor.document = new Document();
  104. editor.data = {
  105. get( rootName ) {
  106. return `data for ${ rootName }`;
  107. }
  108. };
  109. } );
  110. it( 'should get data from the first root', () => {
  111. editor.document.createRoot( 'firstRoot', 'div' );
  112. expect( editor.getData() ).to.equal( 'data for firstRoot' );
  113. } );
  114. it( 'should get data from the specified root', () => {
  115. expect( editor.getData( 'someRoot' ) ).to.equal( 'data for someRoot' );
  116. } );
  117. it( 'should throw when no roots', () => {
  118. expect( () => {
  119. editor.getData();
  120. } ).to.throw( CKEditorError, /^editor-no-editable-roots:/ );
  121. } );
  122. it( 'should throw when more than one root and no root name given', () => {
  123. editor.document.createRoot( 'firstRoot', 'div' );
  124. editor.document.createRoot( 'secondRoot', 'div' );
  125. expect( () => {
  126. editor.getData();
  127. } ).to.throw( CKEditorError, /^editor-editable-root-name-missing:/ );
  128. } );
  129. it( 'should throw when no data controller', () => {
  130. expect( () => {
  131. editor.data = null;
  132. editor.getData();
  133. } ).to.throw( CKEditorError, /^editor-no-datacontroller:/ );
  134. } );
  135. } );
  136. } );