ckeditor.js 3.2 KB

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