8
0

ckeditor.js 3.4 KB

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