editor.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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' ], function() {
  18. return modules.plugin.extend( {
  19. init: sinon.spy().named( 'A' )
  20. } );
  21. } );
  22. CKEDITOR.define( 'plugin!B', [ 'plugin' ], function() {
  23. return modules.plugin.extend( {
  24. init: sinon.spy().named( 'B' )
  25. } );
  26. } );
  27. CKEDITOR.define( 'plugin!C', [ 'plugin', 'plugin!B' ], function() {
  28. return modules.plugin.extend( {
  29. init: sinon.spy().named( 'C' )
  30. } );
  31. } );
  32. CKEDITOR.define( 'plugin!D', [ 'plugin', 'plugin!C' ], function() {
  33. return modules.plugin.extend( {
  34. init: sinon.spy().named( 'D' )
  35. } );
  36. } );
  37. CKEDITOR.define( 'plugin!E', [ 'plugin' ], function( Plugin ) {
  38. return Plugin.extend( {} );
  39. } );
  40. // Synchronous plugin that depends on an asynchronous one.
  41. CKEDITOR.define( 'plugin!F', [ 'plugin', 'plugin!async' ], function( Plugin ) {
  42. return Plugin.extend( {
  43. init: sinon.spy().named( 'F' )
  44. } );
  45. } );
  46. var asyncSpy = sinon.spy().named( 'async-call-spy' );
  47. CKEDITOR.define( 'plugin!async', [ 'plugin' ], function( Plugin ) {
  48. return Plugin.extend( {
  49. init: sinon.spy( function() {
  50. return new Promise( function( resolve ) {
  51. setTimeout( function() {
  52. asyncSpy();
  53. resolve();
  54. }, 0 );
  55. } );
  56. } )
  57. } );
  58. } );
  59. ///////////////////
  60. describe( 'constructor', function() {
  61. it( 'should create a new editor instance', function() {
  62. expect( editor ).to.have.property( 'element' ).to.equal( element );
  63. } );
  64. } );
  65. describe( 'config', function() {
  66. it( 'should be an instance of EditorConfig', function() {
  67. var EditorConfig = modules.editorconfig;
  68. expect( editor.config ).to.be.an.instanceof( EditorConfig );
  69. } );
  70. } );
  71. describe( 'init', function() {
  72. it( 'should return a promise that resolves properly', function() {
  73. var Promise = modules.promise;
  74. var promise = editor.init();
  75. expect( promise ).to.be.an.instanceof( Promise );
  76. return promise;
  77. } );
  78. it( 'should return the same promise for sucessive calls', function() {
  79. var promise = editor.init();
  80. expect( editor.init() ).to.equal( promise );
  81. } );
  82. it( 'should fill `plugins`', function() {
  83. var Editor = modules.editor;
  84. var Plugin = modules.plugin;
  85. editor = new Editor( element, {
  86. plugins: 'A,B'
  87. } );
  88. expect( editor.plugins.length ).to.equal( 0 );
  89. return editor.init().then( function() {
  90. expect( editor.plugins.length ).to.equal( 2 );
  91. expect( editor.plugins.get( 'A' ) ).to.be.an.instanceof( Plugin );
  92. expect( editor.plugins.get( 'B' ) ).to.be.an.instanceof( Plugin );
  93. } );
  94. } );
  95. it( 'should initialize plugins in the right order', function() {
  96. var Editor = modules.editor;
  97. editor = new Editor( element, {
  98. plugins: 'A,D'
  99. } );
  100. return editor.init().then( function() {
  101. sinon.assert.callOrder(
  102. editor.plugins.get( 'A' ).init,
  103. editor.plugins.get( 'B' ).init,
  104. editor.plugins.get( 'C' ).init,
  105. editor.plugins.get( 'D' ).init
  106. );
  107. } );
  108. } );
  109. it( 'should initialize plugins in the right order, waiting for asynchronous ones', function() {
  110. var Editor = modules.editor;
  111. editor = new Editor( element, {
  112. plugins: 'A,F'
  113. } );
  114. return editor.init().then( function() {
  115. sinon.assert.callOrder(
  116. editor.plugins.get( 'A' ).init,
  117. editor.plugins.get( 'async' ).init,
  118. asyncSpy, // This one is called with delay by the async init
  119. editor.plugins.get( 'F' ).init
  120. );
  121. } );
  122. } );
  123. it( 'should not fail if loading a plugin that doesn\'t define init()', function() {
  124. var Editor = modules.editor;
  125. editor = new Editor( element, {
  126. plugins: 'E'
  127. } );
  128. return editor.init();
  129. } );
  130. } );
  131. describe( 'plugins', function() {
  132. it( 'should be empty on new editor', function() {
  133. expect( editor.plugins.length ).to.equal( 0 );
  134. } );
  135. } );
  136. describe( 'destroy', function() {
  137. it( 'should fire "destroy"', function() {
  138. var spy = sinon.spy();
  139. editor.on( 'destroy', spy );
  140. editor.destroy();
  141. sinon.assert.called( spy );
  142. } );
  143. it( 'should delete the "element" property', function() {
  144. editor.destroy();
  145. expect( editor ).to.not.have.property( 'element' );
  146. } );
  147. } );