8
0

ckeditor.js 6.3 KB

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