editor.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 */
  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. ///////////////////
  41. describe( 'constructor', function() {
  42. it( 'should create a new editor instance', function() {
  43. expect( editor ).to.have.property( 'element' ).to.equal( element );
  44. } );
  45. } );
  46. describe( 'config', function() {
  47. it( 'should be an instance of EditorConfig', function() {
  48. var EditorConfig = modules.editorconfig;
  49. expect( editor.config ).to.be.an.instanceof( EditorConfig );
  50. } );
  51. } );
  52. describe( 'init', function() {
  53. it( 'should return a promise that resolves properly', function() {
  54. var Promise = modules.promise;
  55. var promise = editor.init();
  56. expect( promise ).to.be.an.instanceof( Promise );
  57. return promise;
  58. } );
  59. it( 'should return the same promise for sucessive calls', function() {
  60. var promise = editor.init();
  61. expect( editor.init() ).to.equal( promise );
  62. } );
  63. it( 'should fill `plugins`', function() {
  64. var Editor = modules.editor;
  65. var Plugin = modules.plugin;
  66. editor = new Editor( element, {
  67. plugins: 'A,B'
  68. } );
  69. expect( editor.plugins.length ).to.equal( 0 );
  70. return editor.init().then( function() {
  71. expect( editor.plugins.length ).to.equal( 2 );
  72. expect( editor.plugins.get( 'A' ) ).to.be.an.instanceof( Plugin );
  73. expect( editor.plugins.get( 'B' ) ).to.be.an.instanceof( Plugin );
  74. } );
  75. } );
  76. it( 'should initialize plugins in the right order', function() {
  77. var Editor = modules.editor;
  78. editor = new Editor( element, {
  79. plugins: 'A,D'
  80. } );
  81. return editor.init().then( function() {
  82. sinon.assert.callOrder(
  83. editor.plugins.get( 'A' ).init,
  84. editor.plugins.get( 'B' ).init,
  85. editor.plugins.get( 'C' ).init,
  86. editor.plugins.get( 'D' ).init
  87. );
  88. } );
  89. } );
  90. it( 'should not fail if loading a plugin that doesn\'t define init()', function() {
  91. var Editor = modules.editor;
  92. editor = new Editor( element, {
  93. plugins: 'E'
  94. } );
  95. return editor.init();
  96. } );
  97. } );
  98. describe( 'plugins', function() {
  99. it( 'should be empty on new editor', function() {
  100. expect( editor.plugins.length ).to.equal( 0 );
  101. } );
  102. } );
  103. describe( 'destroy', function() {
  104. it( 'should fire "destroy"', function() {
  105. var spy = sinon.spy();
  106. editor.on( 'destroy', spy );
  107. editor.destroy();
  108. sinon.assert.called( spy );
  109. } );
  110. it( 'should delete the "element" property', function() {
  111. editor.destroy();
  112. expect( editor ).to.not.have.property( 'element' );
  113. } );
  114. } );