8
0

creator.js 5.1 KB

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