8
0

ckeditor.js 3.7 KB

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