ckeditor.js 3.3 KB

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