creator.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. 'use strict';
  6. import testUtils from '/tests/_utils/utils.js';
  7. import Creator from '/ckeditor5/core/creator.js';
  8. import Editor from '/ckeditor5/core/editor.js';
  9. import Plugin from '/ckeditor5/core/plugin.js';
  10. testUtils.createSinonSandbox();
  11. describe( 'Creator', () => {
  12. let creator, editor;
  13. beforeEach( () => {
  14. const editorElement = document.createElement( 'div' );
  15. document.body.appendChild( editorElement );
  16. editor = new Editor( editorElement );
  17. creator = new Creator( editor );
  18. } );
  19. describe( 'create', () => {
  20. it( 'should init the UI', () => {
  21. const promise = new Promise( () => {} );
  22. editor.ui = {
  23. init: sinon.stub().returns( promise )
  24. };
  25. const ret = creator.create();
  26. expect( ret ).to.equal( promise );
  27. expect( editor.ui.init.called ).to.be.true;
  28. } );
  29. } );
  30. describe( 'destroy', () => {
  31. it( 'calls super.destroy', () => {
  32. const pluginSpy = testUtils.sinon.spy( Plugin.prototype, 'destroy' );
  33. editor.ui = {
  34. destroy() {}
  35. };
  36. creator.destroy();
  37. expect( pluginSpy.called ).to.be.true;
  38. } );
  39. it( 'should destroy the UI', () => {
  40. const uiSpy = sinon.stub().returns( new Promise( () => {} ) );
  41. editor.ui = {
  42. destroy: uiSpy
  43. };
  44. creator.destroy();
  45. expect( uiSpy.called ).to.be.true;
  46. expect( editor.ui ).to.be.null;
  47. } );
  48. it( 'should wait until UI is destroyed', () => {
  49. let resolved = false;
  50. let resolve;
  51. const uiSpy = sinon.stub().returns(
  52. new Promise( ( r ) => {
  53. resolve = r;
  54. } )
  55. );
  56. editor.ui = {
  57. destroy: uiSpy
  58. };
  59. // Is there an easier method to verify whether the promise chain isn't broken? ;/
  60. setTimeout( () => {
  61. resolved = true;
  62. resolve( 'foo' );
  63. } );
  64. return creator.destroy()
  65. .then( () => {
  66. expect( resolved ).to.be.true;
  67. } );
  68. } );
  69. it( 'should restore the replaced element', () => {
  70. const spy = testUtils.sinon.stub( creator, '_restoreElement' );
  71. const element = document.createElement( 'div' );
  72. editor.ui = {
  73. destroy() {}
  74. };
  75. creator._replaceElement( element );
  76. creator.destroy();
  77. expect( spy.calledOnce ).to.be.true;
  78. } );
  79. } );
  80. describe( 'updateEditorElement', () => {
  81. it( 'should pass data to the element', () => {
  82. editor.editable = {
  83. getData() {
  84. return 'foo';
  85. }
  86. };
  87. creator.updateEditorElement();
  88. expect( editor.element.innerHTML ).to.equal( 'foo' );
  89. } );
  90. } );
  91. describe( 'loadDataFromEditorElement', () => {
  92. it( 'should pass data to the element', () => {
  93. editor.editable = {
  94. setData: sinon.spy()
  95. };
  96. editor.element.innerHTML = 'foo';
  97. creator.loadDataFromEditorElement();
  98. expect( editor.editable.setData.args[ 0 ][ 0 ] ).to.equal( 'foo' );
  99. } );
  100. } );
  101. describe( 'getDataFromElement', () => {
  102. [ 'textarea', 'template', 'div' ].forEach( ( elementName ) => {
  103. it( 'should return the content of a ' + elementName, function() {
  104. const data = Creator.getDataFromElement( document.getElementById( 'getData-' + elementName ) );
  105. expect( data ).to.equal( '<b>foo</b>' );
  106. } );
  107. } );
  108. } );
  109. describe( 'setDataInElement', () => {
  110. [ 'textarea', 'template', 'div' ].forEach( ( elementName ) => {
  111. it( 'should set the content of a ' + elementName, () => {
  112. const el = document.createElement( elementName );
  113. const expectedData = '<b>foo</b>';
  114. Creator.setDataInElement( el, expectedData );
  115. const actualData = Creator.getDataFromElement( el );
  116. expect( actualData ).to.equal( actualData );
  117. } );
  118. } );
  119. } );
  120. describe( '_replaceElement', () => {
  121. it( 'should use editor ui element when arg not provided', () => {
  122. editor.ui = {
  123. view: {
  124. element: document.createElement( 'div' )
  125. }
  126. };
  127. creator._replaceElement();
  128. expect( editor.element.nextSibling ).to.equal( editor.ui.view.element );
  129. } );
  130. } );
  131. describe( '_restoreElement', () => {
  132. it( 'should remove the replacement element', () => {
  133. const element = document.createElement( 'div' );
  134. creator._replaceElement( element );
  135. expect( editor.element.nextSibling ).to.equal( element );
  136. creator._restoreElement();
  137. expect( element.parentNode ).to.be.null;
  138. } );
  139. } );
  140. } );