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