baseeditor.js 4.4 KB

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