Jelajahi Sumber

Added tests checking if StandardEditor updates editor element on form submission.

Szymon Kupś 8 tahun lalu
induk
melakukan
7e0671bcf8
1 mengubah file dengan 121 tambahan dan 1 penghapusan
  1. 121 1
      packages/ckeditor5-core/tests/editor/standardeditor.js

+ 121 - 1
packages/ckeditor5-core/tests/editor/standardeditor.js

@@ -3,7 +3,7 @@
  * For licensing, see LICENSE.md.
  */
 
-/* globals document */
+/* globals document, Event */
 
 import Editor from '../../src/editor/editor';
 import StandardEditor from '../../src/editor/standardeditor';
@@ -216,4 +216,124 @@ describe( 'StandardEditor', () => {
 			expect( editor.data.set.calledWithExactly( '<p>foo</p>' ) ).to.be.true;
 		} );
 	} );
+
+	describe( 'attaching to a form', () => {
+		let editor, form, textarea, submitStub;
+
+		beforeEach( () => {
+			form = document.createElement( 'form' );
+			textarea = document.createElement( 'textarea' );
+			form.appendChild( textarea );
+			document.body.appendChild( form );
+			submitStub = sinon.stub( form, 'submit' );
+		} );
+
+		afterEach( () => {
+			submitStub.restore();
+		} );
+
+		it( 'should update editor element contents after calling submit() method', () => {
+			return createEditor( textarea ).then( () => {
+				expect( textarea.value ).to.equal( '' );
+
+				// Submit method is replaced by our implementation.
+				expect( form.submit ).to.not.equal( submitStub );
+				form.submit();
+
+				expect( textarea.value ).to.equal( '<p>foo</p>' );
+				sinon.assert.calledOnce( submitStub );
+			} );
+		} );
+
+		it( 'should update editor element contents after "submit" event', () => {
+			return createEditor( textarea ).then( () => {
+				expect( textarea.value ).to.equal( '' );
+
+				form.dispatchEvent( new Event( 'submit' ) );
+
+				expect( textarea.value ).to.equal( '<p>foo</p>' );
+			} );
+		} );
+
+		it( 'should not update element in form which is not a textarea', () => {
+			const element = document.createElement( 'div' );
+			form.appendChild( element );
+
+			return createEditor( element ).then( () => {
+				expect( textarea.value ).to.equal( '' );
+
+				// Submit method is not replaced by our implementation.
+				expect( form.submit ).to.equal( submitStub );
+				form.submit();
+
+				expect( textarea.value ).to.equal( '' );
+			} );
+		} );
+
+		it( 'should not update textarea which is outside a form', () => {
+			const textarea = document.createElement( 'textarea' );
+			document.body.appendChild( textarea );
+
+			return createEditor( textarea ).then( () => {
+				expect( textarea.value ).to.equal( '' );
+
+				// Submit method is not replaced by our implementation.
+				expect( form.submit ).to.equal( submitStub );
+				form.submit();
+
+				expect( textarea.value ).to.equal( '' );
+			} );
+		} );
+
+		it( 'should not update element after destroying the editor - form.submit()', () => {
+			return createEditor( textarea ).then( editor => editor.destroy() ).then( () => {
+				expect( textarea.value ).to.equal( '' );
+
+				// Submit method is no longer replaced by our implementation.
+				expect( form.submit ).to.equal( submitStub );
+				form.submit();
+
+				expect( textarea.value ).to.equal( '' );
+			} );
+		} );
+
+		it( 'should not update element after destroying the editor - "submit" event', () => {
+			return createEditor( textarea ).then( editor => editor.destroy() ).then( () => {
+				expect( textarea.value ).to.equal( '' );
+
+				form.dispatchEvent( new Event( 'submit' ) );
+
+				expect( textarea.value ).to.equal( '' );
+			} );
+		} );
+
+		it( 'should not replace submit() method when one of the elements in form has "submit" name', () => {
+			const input = document.createElement( 'input' );
+			input.setAttribute( 'name', 'submit' );
+			form.appendChild( input );
+
+			return createEditor( textarea ).then( () => {
+				expect( form.submit ).to.equal( input );
+				expect( textarea.value ).to.equal( '' );
+
+				form.dispatchEvent( new Event( 'submit' ) );
+
+				expect( textarea.value ).to.equal( '<p>foo</p>' );
+
+				return editor.destroy().then( () => {
+					expect( form.submit ).to.equal( input );
+				} );
+			} );
+		} );
+
+		function createEditor( element ) {
+			return StandardEditor.create( element )
+				.then( newEditor => {
+					editor = newEditor;
+					editor.data.get = () => '<p>foo</p>';
+
+					return editor;
+				} );
+		}
+	} );
 } );