8
0

editor.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 return the same promise for successive calls', function() {
  67. var promise = editor.init();
  68. expect( editor.init() ).to.equal( promise );
  69. } );
  70. it( 'should fill `plugins`', function() {
  71. var Editor = modules.editor;
  72. var Plugin = modules.plugin;
  73. editor = new Editor( element, {
  74. plugins: 'A,B'
  75. } );
  76. expect( editor.plugins.length ).to.equal( 0 );
  77. return editor.init().then( function() {
  78. expect( editor.plugins.length ).to.equal( 2 );
  79. expect( editor.plugins.get( 'A' ) ).to.be.an.instanceof( Plugin );
  80. expect( editor.plugins.get( 'B' ) ).to.be.an.instanceof( Plugin );
  81. } );
  82. } );
  83. it( 'should initialize plugins in the right order', function() {
  84. var Editor = modules.editor;
  85. editor = new Editor( element, {
  86. plugins: 'A,D'
  87. } );
  88. return editor.init().then( function() {
  89. sinon.assert.callOrder(
  90. editor.plugins.get( 'A' ).init,
  91. editor.plugins.get( 'B' ).init,
  92. editor.plugins.get( 'C' ).init,
  93. editor.plugins.get( 'D' ).init
  94. );
  95. } );
  96. } );
  97. it( 'should initialize plugins in the right order, waiting for asynchronous ones', function() {
  98. var Editor = modules.editor;
  99. editor = new Editor( element, {
  100. plugins: 'A,F'
  101. } );
  102. return editor.init().then( function() {
  103. sinon.assert.callOrder(
  104. editor.plugins.get( 'A' ).init,
  105. editor.plugins.get( 'async' ).init,
  106. asyncSpy, // This one is called with delay by the async init
  107. editor.plugins.get( 'F' ).init
  108. );
  109. } );
  110. } );
  111. it( 'should not fail if loading a plugin that doesn\'t define init()', function() {
  112. var Editor = modules.editor;
  113. editor = new Editor( element, {
  114. plugins: 'E'
  115. } );
  116. return editor.init();
  117. } );
  118. } );
  119. describe( 'plugins', function() {
  120. it( 'should be empty on new editor', function() {
  121. expect( editor.plugins.length ).to.equal( 0 );
  122. } );
  123. } );
  124. describe( 'destroy', function() {
  125. it( 'should fire "destroy"', function() {
  126. var spy = sinon.spy();
  127. editor.on( 'destroy', spy );
  128. editor.destroy();
  129. sinon.assert.called( spy );
  130. } );
  131. it( 'should delete the "element" property', function() {
  132. editor.destroy();
  133. expect( editor ).to.not.have.property( 'element' );
  134. } );
  135. } );