creator.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. } );
  31. describe( 'destroy', () => {
  32. it( 'calls super.destroy', () => {
  33. const pluginSpy = testUtils.sinon.spy( Plugin.prototype, 'destroy' );
  34. editor.ui = {
  35. destroy() {}
  36. };
  37. creator.destroy();
  38. expect( pluginSpy.called ).to.be.true;
  39. } );
  40. it( 'should destroy the UI', () => {
  41. const uiSpy = sinon.stub().returns( new Promise( () => {} ) );
  42. editor.ui = {
  43. destroy: uiSpy
  44. };
  45. creator.destroy();
  46. expect( uiSpy.called ).to.be.true;
  47. expect( editor.ui ).to.be.null;
  48. } );
  49. it( 'should wait until UI is destroyed', () => {
  50. let resolved = false;
  51. let resolve;
  52. const uiSpy = sinon.stub().returns(
  53. new Promise( ( r ) => {
  54. resolve = r;
  55. } )
  56. );
  57. editor.ui = {
  58. destroy: uiSpy
  59. };
  60. // Is there an easier method to verify whether the promise chain isn't broken? ;/
  61. setTimeout( () => {
  62. resolved = true;
  63. resolve( 'foo' );
  64. } );
  65. return creator.destroy()
  66. .then( () => {
  67. expect( resolved ).to.be.true;
  68. } );
  69. } );
  70. it( 'should restore the replaced element', () => {
  71. const spy = testUtils.sinon.stub( creator, '_restoreElement' );
  72. const element = document.createElement( 'div' );
  73. editor.ui = {
  74. destroy() {}
  75. };
  76. creator._replaceElement( element );
  77. creator.destroy();
  78. expect( spy.calledOnce ).to.be.true;
  79. } );
  80. } );
  81. describe( 'updateEditorElement', () => {
  82. it( 'should pass data to the element', () => {
  83. editor.editable = {
  84. getData() {
  85. return 'foo';
  86. }
  87. };
  88. creator.updateEditorElement();
  89. expect( editor.element.innerHTML ).to.equal( 'foo' );
  90. } );
  91. } );
  92. describe( 'loadDataFromEditorElement', () => {
  93. it( 'should pass data to the element', () => {
  94. editor.editable = {
  95. setData: sinon.spy()
  96. };
  97. editor.element.innerHTML = 'foo';
  98. creator.loadDataFromEditorElement();
  99. expect( editor.editable.setData.args[ 0 ][ 0 ] ).to.equal( 'foo' );
  100. } );
  101. } );
  102. describe( 'getDataFromElement', () => {
  103. [ 'textarea', 'template', 'div' ].forEach( ( elementName ) => {
  104. it( 'should return the content of a ' + elementName, function() {
  105. const data = Creator.getDataFromElement( document.getElementById( 'getData-' + elementName ) );
  106. expect( data ).to.equal( '<b>foo</b>' );
  107. } );
  108. } );
  109. } );
  110. describe( 'setDataInElement', () => {
  111. [ 'textarea', 'template', 'div' ].forEach( ( elementName ) => {
  112. it( 'should set the content of a ' + elementName, () => {
  113. const el = document.createElement( elementName );
  114. const expectedData = '<b>foo</b>';
  115. Creator.setDataInElement( el, expectedData );
  116. const actualData = Creator.getDataFromElement( el );
  117. expect( actualData ).to.equal( actualData );
  118. } );
  119. } );
  120. } );
  121. describe( '_replaceElement', () => {
  122. it( 'should use editor ui element when arg not provided', () => {
  123. editor.ui = {
  124. view: {
  125. element: document.createElement( 'div' )
  126. }
  127. };
  128. creator._replaceElement();
  129. expect( editor.element.nextSibling ).to.equal( editor.ui.view.element );
  130. } );
  131. } );
  132. describe( '_restoreElement', () => {
  133. it( 'should remove the replacement element', () => {
  134. const element = document.createElement( 'div' );
  135. creator._replaceElement( element );
  136. expect( editor.element.nextSibling ).to.equal( element );
  137. creator._restoreElement();
  138. expect( element.parentNode ).to.be.null;
  139. } );
  140. } );
  141. } );