ckeditor.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. import CKEditorError from '/ckeditor5/utils/ckeditorerror.js';
  11. const content = document.getElementById( 'content' );
  12. const editorConfig = { creator: 'creator-test' };
  13. testUtils.createSinonSandbox();
  14. testUtils.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. } );
  29. } );
  30. it( 'should set configurations on the new editor', () => {
  31. return CKEDITOR.create( content, { test: 1, creator: 'creator-test' } ).then( ( editor ) => {
  32. expect( editor.config.test ).to.equal( 1 );
  33. } );
  34. } );
  35. it( 'should add the editor to the `instances` collection', () => {
  36. return CKEDITOR.create( content, editorConfig ).then( ( editor ) => {
  37. expect( CKEDITOR.instances ).to.have.length( 1 );
  38. expect( CKEDITOR.instances.get( 0 ) ).to.equal( editor );
  39. } );
  40. } );
  41. it( 'should remove the editor from the `instances` collection on `destroy` event', () => {
  42. let editor1, editor2;
  43. // Create the first editor.
  44. return CKEDITOR.create( content, editorConfig ).then( ( editor ) => {
  45. editor1 = editor;
  46. // Create the second editor.
  47. return CKEDITOR.create( '.editor', editorConfig ).then( ( editor ) => {
  48. editor2 = editor;
  49. // It should have 2 editors.
  50. expect( CKEDITOR.instances ).to.have.length( 2 );
  51. // Destroy one of them.
  52. editor1.destroy();
  53. // It should have 1 editor now.
  54. expect( CKEDITOR.instances ).to.have.length( 1 );
  55. // Ensure that the remaining is the right one.
  56. expect( CKEDITOR.instances.get( 0 ) ).to.equal( editor2 );
  57. } );
  58. } );
  59. } );
  60. describe( 'elements param', () => {
  61. const container = document.createElement( 'div' );
  62. let el1, el2;
  63. document.body.appendChild( container );
  64. beforeEach( () => {
  65. container.innerHTML = '';
  66. el1 = document.createElement( 'div' );
  67. el2 = document.createElement( 'div' );
  68. container.appendChild( el1 );
  69. container.appendChild( el2 );
  70. } );
  71. it( 'should work with a string', () => {
  72. return CKEDITOR.create( 'div', editorConfig ).then( ( editor ) => {
  73. assertElements( editor, document.querySelectorAll( 'div' ).length );
  74. } );
  75. } );
  76. it( 'should work with an HTMLElement', () => {
  77. return CKEDITOR.create( el1, editorConfig ).then( ( editor ) => {
  78. assertElements( editor, 1 );
  79. } );
  80. } );
  81. it( 'should work with a NodeList', () => {
  82. const elements = container.querySelectorAll( 'div' );
  83. return CKEDITOR.create( elements, editorConfig ).then( ( editor ) => {
  84. assertElements( editor, 2 );
  85. } );
  86. } );
  87. it( 'should work with an HTMLCollection', () => {
  88. const elements = container.getElementsByTagName( 'div' );
  89. return CKEDITOR.create( elements, editorConfig ).then( ( editor ) => {
  90. assertElements( editor, 2 );
  91. } );
  92. } );
  93. it( 'should work with an array', () => {
  94. const elements = Array.from( container.getElementsByTagName( 'div' ) );
  95. return CKEDITOR.create( elements, editorConfig ).then( ( editor ) => {
  96. assertElements( editor, 2 );
  97. } );
  98. } );
  99. it( 'should work with an object', () => {
  100. const elements = {
  101. editableA: el1,
  102. editableB: el2
  103. };
  104. return CKEDITOR.create( elements, editorConfig ).then( ( editor ) => {
  105. assertElements( editor, 2 );
  106. } );
  107. } );
  108. it( 'should be rejected on element not found (when string passed)', () => {
  109. let addSpy = testUtils.sinon.spy( CKEDITOR.instances, 'add' );
  110. return CKEDITOR.create( '.undefined' )
  111. .then( () => {
  112. throw new Error( 'It should not enter this function.' );
  113. } )
  114. .catch( ( error ) => {
  115. expect( error ).to.be.instanceof( CKEditorError );
  116. expect( error.message ).to.match( /^ckeditor5-create-no-elements:/ );
  117. // We need to make sure that create()'s execution is stopped.
  118. // Assertion based on a real mistake we made that reject() wasn't followed by a return.
  119. sinon.assert.notCalled( addSpy );
  120. } );
  121. } );
  122. it( 'should be rejected on an empty elements array-like obj', () => {
  123. return CKEDITOR.create( [] )
  124. .then( () => {
  125. throw new Error( 'It should not enter this function.' );
  126. } )
  127. .catch( ( error ) => {
  128. expect( error ).to.be.instanceof( CKEditorError );
  129. expect( error.message ).to.match( /^ckeditor5-create-no-elements:/ );
  130. } );
  131. } );
  132. it( 'should be rejected on an empty object', () => {
  133. return CKEDITOR.create( {} )
  134. .then( () => {
  135. throw new Error( 'It should not enter this function.' );
  136. } )
  137. .catch( ( error ) => {
  138. expect( error ).to.be.instanceof( CKEditorError );
  139. expect( error.message ).to.match( /^ckeditor5-create-no-elements:/ );
  140. } );
  141. } );
  142. it( 'should take names from the ids or data-editable attributes', () => {
  143. el1.id = 'foo';
  144. el2.dataset.editable = 'bar';
  145. return CKEDITOR.create( [ el1, el2 ], editorConfig )
  146. .then( ( editor ) => {
  147. expect( editor.elements.get( 'foo' ) ).to.equal( el1 );
  148. expect( editor.elements.get( 'bar' ) ).to.equal( el2 );
  149. } );
  150. } );
  151. it( 'should take names from the object keys', () => {
  152. el1.id = 'foo';
  153. el2.dataset.editable = 'bar';
  154. return CKEDITOR.create( { a: el1, b: el2 }, editorConfig )
  155. .then( ( editor ) => {
  156. expect( editor.elements.get( 'a' ) ).to.equal( el1 );
  157. expect( editor.elements.get( 'b' ) ).to.equal( el2 );
  158. } );
  159. } );
  160. it( 'should generate editableN names', () => {
  161. return CKEDITOR.create( [ el1, el2 ], editorConfig )
  162. .then( ( editor ) => {
  163. expect( Array.from( editor.elements.keys() ).join( ',' ) ).to.match( /^editable\d+,editable\d+$/ );
  164. } );
  165. } );
  166. function assertElements( editor, expectedSize ) {
  167. expect( editor.elements ).to.be.instanceof( Map );
  168. expect( editor.elements ).to.have.property( 'size', expectedSize );
  169. }
  170. } );
  171. } );
  172. describe( 'config', () => {
  173. it( 'should be an instance of Config', () => {
  174. expect( CKEDITOR.config ).to.be.an.instanceof( Config );
  175. } );
  176. } );