creator.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. 'use strict';
  6. /* bender-tags: editor, creator */
  7. import amdUtils from '/tests/_utils/amd.js';
  8. import testUtils from '/tests/_utils/utils.js';
  9. import coreTestUtils from '/tests/core/_utils/utils.js';
  10. import Editor from '/ckeditor5/core/editor.js';
  11. import Creator from '/ckeditor5/core/creator.js';
  12. import CKEditorError from '/ckeditor5/core/ckeditorerror.js';
  13. let editor, element;
  14. function initEditor( config ) {
  15. element = document.createElement( 'div' );
  16. document.body.appendChild( element );
  17. editor = new Editor( element, config );
  18. return editor.init();
  19. }
  20. testUtils.createSinonSandbox();
  21. before( () => {
  22. coreTestUtils.defineEditorCreatorMock( 'test1' );
  23. coreTestUtils.defineEditorCreatorMock( 'test-throw-on-many1' );
  24. coreTestUtils.defineEditorCreatorMock( 'test-throw-on-many2' );
  25. coreTestUtils.defineEditorCreatorMock( 'test-config1' );
  26. coreTestUtils.defineEditorCreatorMock( 'test-config2' );
  27. amdUtils.define( 'test3', [ 'core/plugin' ], ( Plugin ) => {
  28. return class extends Plugin {};
  29. } );
  30. amdUtils.define( 'creator-async-create', [ 'core/creator' ], ( Creator ) => {
  31. return class extends Creator {
  32. create() {
  33. return new Promise( ( resolve, reject ) => {
  34. reject( new Error( 'Catch me - create.' ) );
  35. } );
  36. }
  37. destroy() {}
  38. };
  39. } );
  40. amdUtils.define( 'creator-async-destroy', [ 'core/creator' ], ( Creator ) => {
  41. return class extends Creator {
  42. create() {}
  43. destroy() {
  44. return new Promise( ( resolve, reject ) => {
  45. reject( new Error( 'Catch me - destroy.' ) );
  46. } );
  47. }
  48. };
  49. } );
  50. amdUtils.define( 'creator-destroy-order', [ 'core/creator' ], ( Creator ) => {
  51. return class extends Creator {
  52. create() {}
  53. destroy() {
  54. editor._elementInsideCreatorDestroy = this.editor.element;
  55. editor._destroyOrder.push( 'creator' );
  56. }
  57. };
  58. } );
  59. } );
  60. afterEach( () => {
  61. editor = null; // To make sure we're using the freshly inited editor.
  62. } );
  63. ///////////////////
  64. describe( 'init', () => {
  65. it( 'should instantiate the creator and call create()', () => {
  66. return initEditor( {
  67. creator: 'creator-test1'
  68. } )
  69. .then( () => {
  70. let creator = editor.plugins.get( 'creator-test1' );
  71. expect( creator ).to.be.instanceof( Creator );
  72. // The create method has been called.
  73. sinon.assert.calledOnce( creator.create );
  74. } );
  75. } );
  76. it( 'should throw if creator is not defined', () => {
  77. return initEditor( {} )
  78. .then( () => {
  79. throw new Error( 'This should not be executed.' );
  80. } )
  81. .catch( ( err ) => {
  82. expect( err ).to.be.instanceof( CKEditorError );
  83. expect( err.message ).to.match( /^editor-undefined-creator:/ );
  84. } );
  85. } );
  86. it( 'should use the creator specified in config.creator', () => {
  87. return initEditor( {
  88. creator: 'creator-test-config2',
  89. features: [ 'creator-test-config1', 'creator-test-config2' ],
  90. } )
  91. .then( () => {
  92. let creator1 = editor.plugins.get( 'creator-test-config1' );
  93. let creator2 = editor.plugins.get( 'creator-test-config2' );
  94. sinon.assert.calledOnce( creator2.create );
  95. sinon.assert.notCalled( creator1.create );
  96. } );
  97. } );
  98. it( 'should throw an error if the creator doesn\'t exist', () => {
  99. return initEditor( {
  100. creator: 'bad'
  101. } )
  102. .then( () => {
  103. throw new Error( 'This should not be executed.' );
  104. } )
  105. .catch( ( err ) => {
  106. // It's the Require.JS error.
  107. expect( err ).to.be.an.instanceof( Error );
  108. expect( err.message ).to.match( /^Script error for/ );
  109. } );
  110. } );
  111. it( 'should chain the promise from the creator (enables async creators)', () => {
  112. return initEditor( {
  113. creator: 'creator-async-create'
  114. } )
  115. .then( () => {
  116. throw new Error( 'This should not be executed.' );
  117. } )
  118. .catch( ( err ) => {
  119. // Unfortunately fake timers don't work with promises, so throwing in the creator's create()
  120. // seems to be the only way to test that the promise chain isn't broken.
  121. expect( err ).to.have.property( 'message', 'Catch me - create.' );
  122. } );
  123. } );
  124. } );
  125. describe( 'destroy', () => {
  126. it( 'should call "destroy" on the creator', () => {
  127. let creator1;
  128. return initEditor( {
  129. creator: 'creator-test1'
  130. } )
  131. .then( () => {
  132. creator1 = editor.plugins.get( 'creator-test1' );
  133. return editor.destroy();
  134. } )
  135. .then( () => {
  136. sinon.assert.calledOnce( creator1.destroy );
  137. } );
  138. } );
  139. it( 'should chain the promise from the creator (enables async creators)', () => {
  140. return initEditor( {
  141. creator: 'creator-async-destroy'
  142. } )
  143. .then( () => {
  144. return editor.destroy();
  145. } )
  146. .then( () => {
  147. throw new Error( 'This should not be executed.' );
  148. } )
  149. .catch( ( err ) => {
  150. // Unfortunately fake timers don't work with promises, so throwing in the creator's destroy()
  151. // seems to be the only way to test that the promise chain isn't broken.
  152. expect( err ).to.have.property( 'message', 'Catch me - destroy.' );
  153. } );
  154. } );
  155. it( 'should do things in the correct order', () => {
  156. return initEditor( {
  157. creator: 'creator-destroy-order'
  158. } )
  159. .then( () => {
  160. editor._destroyOrder = [];
  161. editor.on( 'destroy', () => {
  162. editor._destroyOrder.push( 'event' );
  163. } );
  164. return editor.destroy();
  165. } )
  166. .then( () => {
  167. expect( editor._elementInsideCreatorDestroy ).to.not.be.undefined;
  168. expect( editor._destroyOrder ).to.deep.equal( [ 'event', 'creator' ] );
  169. } );
  170. } );
  171. } );