ckeditor.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /**
  2. * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* globals document */
  6. /* bender-include: ../_tools/tools.js */
  7. 'use strict';
  8. var modules = bender.amd.require( 'ckeditor', 'editor', 'promise', 'config' );
  9. var content = document.getElementById( 'content' );
  10. var editorConfig = { plugins: 'creator-test' };
  11. bender.tools.createSinonSandbox();
  12. before( function() {
  13. bender.tools.core.defineEditorCreatorMock( 'test' );
  14. } );
  15. beforeEach( function() {
  16. var CKEDITOR = modules.ckeditor;
  17. // Destroy all editor instances.
  18. while ( CKEDITOR.instances.length ) {
  19. CKEDITOR.instances.get( 0 ).destroy();
  20. }
  21. } );
  22. describe( 'create', function() {
  23. it( 'should return a promise', function() {
  24. var CKEDITOR = modules.ckeditor;
  25. var Promise = modules.promise;
  26. expect( CKEDITOR.create( content, editorConfig ) ).to.be.instanceof( Promise );
  27. } );
  28. it( 'should create a new editor instance', function() {
  29. var CKEDITOR = modules.ckeditor;
  30. var Editor = modules.editor;
  31. return CKEDITOR.create( content, editorConfig ).then( function( editor ) {
  32. expect( editor ).to.be.instanceof( Editor );
  33. expect( editor.element ).to.equal( content );
  34. } );
  35. } );
  36. it( 'should create a new editor instance (using a selector)', function() {
  37. var CKEDITOR = modules.ckeditor;
  38. var Editor = modules.editor;
  39. return CKEDITOR.create( '.editor', editorConfig ).then( function( editor ) {
  40. expect( editor ).to.be.instanceof( Editor );
  41. expect( editor.element ).to.equal( document.querySelector( '.editor' ) );
  42. } );
  43. } );
  44. it( 'should set configurations on the new editor', function() {
  45. var CKEDITOR = modules.ckeditor;
  46. return CKEDITOR.create( content, { test: 1, plugins: 'creator-test' } ).then( function( editor ) {
  47. expect( editor.config.test ).to.equal( 1 );
  48. } );
  49. } );
  50. it( 'should add the editor to the `instances` collection', function() {
  51. var CKEDITOR = modules.ckeditor;
  52. return CKEDITOR.create( content, editorConfig ).then( function( editor ) {
  53. expect( CKEDITOR.instances ).to.have.length( 1 );
  54. expect( CKEDITOR.instances.get( 0 ) ).to.equal( editor );
  55. } );
  56. } );
  57. it( 'should remove the editor from the `instances` collection on `destroy` event', function() {
  58. var CKEDITOR = modules.ckeditor;
  59. var editor1, editor2;
  60. // Create the first editor.
  61. return CKEDITOR.create( content, editorConfig ).then( function( editor ) {
  62. editor1 = editor;
  63. // Create the second editor.
  64. return CKEDITOR.create( '.editor', editorConfig ).then( function( editor ) {
  65. editor2 = editor;
  66. // It should have 2 editors.
  67. expect( CKEDITOR.instances ).to.have.length( 2 );
  68. // Destroy one of them.
  69. editor1.destroy();
  70. // It should have 1 editor now.
  71. expect( CKEDITOR.instances ).to.have.length( 1 );
  72. // Ensure that the remaining is the right one.
  73. expect( CKEDITOR.instances.get( 0 ) ).to.equal( editor2 );
  74. } );
  75. } );
  76. } );
  77. it( 'should be rejected on element not found', function() {
  78. var CKEDITOR = modules.ckeditor;
  79. var addSpy = bender.sinon.spy( CKEDITOR.instances, 'add' );
  80. return CKEDITOR.create( '.undefined' ).then( function() {
  81. throw new Error( 'It should not enter this function' );
  82. } ).catch( function( error ) {
  83. expect( error ).to.be.instanceof( Error );
  84. expect( error.message ).to.equal( 'Element not found' );
  85. // We need to make sure that create()'s execution is stopped.
  86. // Assertion based on a real mistake we made that reject() wasn't followed by a return.
  87. sinon.assert.notCalled( addSpy );
  88. } );
  89. } );
  90. } );
  91. describe( 'config', function() {
  92. it( 'should be an instance of Config', function() {
  93. var CKEDITOR = modules.ckeditor;
  94. var Config = modules.config;
  95. expect( CKEDITOR.config ).to.be.an.instanceof( Config );
  96. } );
  97. } );