8
0

creator.js 4.4 KB

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