editor.js 4.4 KB

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