ckeditor.js 3.7 KB

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