8
0

editor.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /**
  2. * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* globals document */
  6. /* bender-include: ../_tools/tools.js */
  7. 'use strict';
  8. var modules = bender.amd.require( 'editor', 'editorconfig', 'plugin' );
  9. var editor;
  10. var element;
  11. var asyncSpy;
  12. beforeEach( function() {
  13. var Editor = modules.editor;
  14. element = document.createElement( 'div' );
  15. document.body.appendChild( element );
  16. editor = new Editor( element );
  17. } );
  18. before( function() {
  19. // Define fake plugins to be used in tests.
  20. bender.tools.core.defineEditorCreatorMock( 'test', {
  21. init: sinon.spy().named( 'creator-test' )
  22. } );
  23. CKEDITOR.define( 'plugin!A', [ 'plugin' ], pluginDefinition( 'A' ) );
  24. CKEDITOR.define( 'plugin!B', [ 'plugin' ], pluginDefinition( 'B' ) );
  25. CKEDITOR.define( 'plugin!C', [ 'plugin', 'plugin!B' ], pluginDefinition( 'C' ) );
  26. CKEDITOR.define( 'plugin!D', [ 'plugin', 'plugin!C' ], pluginDefinition( 'D' ) );
  27. CKEDITOR.define( 'plugin!E', [ 'plugin' ], pluginDefinition( 'E' ) );
  28. // Synchronous plugin that depends on an asynchronous one.
  29. CKEDITOR.define( 'plugin!F', [ 'plugin', 'plugin!async' ], pluginDefinition( 'F' ) );
  30. asyncSpy = sinon.spy().named( 'async-call-spy' );
  31. CKEDITOR.define( 'plugin!async', [ 'plugin' ], function( Plugin ) {
  32. class PluginAsync extends Plugin {}
  33. PluginAsync.prototype.init = sinon.spy( function() {
  34. return new Promise( function( resolve ) {
  35. setTimeout( function() {
  36. asyncSpy();
  37. resolve();
  38. }, 0 );
  39. } );
  40. } );
  41. return PluginAsync;
  42. } );
  43. } );
  44. function pluginDefinition( name ) {
  45. return function( Plugin ) {
  46. class NewPlugin extends Plugin {}
  47. NewPlugin.prototype.init = sinon.spy().named( name );
  48. return NewPlugin;
  49. };
  50. }
  51. ///////////////////
  52. describe( 'constructor', function() {
  53. it( 'should create a new editor instance', function() {
  54. expect( editor ).to.have.property( 'element' ).to.equal( element );
  55. } );
  56. } );
  57. describe( 'config', function() {
  58. it( 'should be an instance of EditorConfig', function() {
  59. var EditorConfig = modules.editorconfig;
  60. expect( editor.config ).to.be.an.instanceof( EditorConfig );
  61. } );
  62. } );
  63. describe( 'init', function() {
  64. it( 'should return a promise that resolves properly', function() {
  65. var Editor = modules.editor;
  66. editor = new Editor( element, {
  67. plugins: 'creator-test'
  68. } );
  69. var promise = editor.init();
  70. expect( promise ).to.be.an.instanceof( Promise );
  71. return promise;
  72. } );
  73. it( 'should fill `plugins`', function() {
  74. var Editor = modules.editor;
  75. var Plugin = modules.plugin;
  76. editor = new Editor( element, {
  77. plugins: 'A,B,creator-test'
  78. } );
  79. expect( editor.plugins.length ).to.equal( 0 );
  80. return editor.init().then( function() {
  81. expect( editor.plugins.length ).to.equal( 3 );
  82. expect( editor.plugins.get( 'A' ) ).to.be.an.instanceof( Plugin );
  83. expect( editor.plugins.get( 'B' ) ).to.be.an.instanceof( Plugin );
  84. expect( editor.plugins.get( 'creator-test' ) ).to.be.an.instanceof( Plugin );
  85. } );
  86. } );
  87. it( 'should initialize plugins in the right order', function() {
  88. var Editor = modules.editor;
  89. editor = new Editor( element, {
  90. plugins: 'creator-test,A,D'
  91. } );
  92. return editor.init().then( function() {
  93. sinon.assert.callOrder(
  94. editor.plugins.get( 'creator-test' ).init,
  95. editor.plugins.get( 'A' ).init,
  96. editor.plugins.get( 'B' ).init,
  97. editor.plugins.get( 'C' ).init,
  98. editor.plugins.get( 'D' ).init
  99. );
  100. } );
  101. } );
  102. it( 'should initialize plugins in the right order, waiting for asynchronous ones', function() {
  103. var Editor = modules.editor;
  104. editor = new Editor( element, {
  105. plugins: 'creator-test,A,F'
  106. } );
  107. return editor.init().then( function() {
  108. sinon.assert.callOrder(
  109. editor.plugins.get( 'creator-test' ).init,
  110. editor.plugins.get( 'A' ).init,
  111. editor.plugins.get( 'async' ).init,
  112. // This one is called with delay by the async init
  113. asyncSpy,
  114. editor.plugins.get( 'F' ).init
  115. );
  116. } );
  117. } );
  118. it( 'should not fail if loading a plugin that doesn\'t define init()', function() {
  119. var Editor = modules.editor;
  120. editor = new Editor( element, {
  121. plugins: 'E,creator-test'
  122. } );
  123. return editor.init();
  124. } );
  125. } );
  126. describe( 'plugins', function() {
  127. it( 'should be empty on new editor', function() {
  128. expect( editor.plugins.length ).to.equal( 0 );
  129. } );
  130. } );
  131. describe( 'destroy', function() {
  132. it( 'should fire "destroy"', function() {
  133. var spy = sinon.spy();
  134. editor.on( 'destroy', spy );
  135. return editor.destroy().then( function() {
  136. sinon.assert.called( spy );
  137. } );
  138. } );
  139. it( 'should delete the "element" property', function() {
  140. return editor.destroy().then( function() {
  141. expect( editor ).to.not.have.property( 'element' );
  142. } );
  143. } );
  144. } );