editor.js 3.2 KB

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