editor.js 4.2 KB

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