8
0

creator.js 4.6 KB

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