creator.js 5.1 KB

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