ckeditor.js 3.7 KB

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