浏览代码

Tests (link): Adjusted unit tests per https://github.com/ckeditor/ckeditor5/pull/8015#issuecomment-686574760.

Marek Lewandowski 5 年之前
父节点
当前提交
8b11086a49
共有 1 个文件被更改,包括 31 次插入23 次删除
  1. 31 23
      packages/ckeditor5-link/tests/linkui.js

+ 31 - 23
packages/ckeditor5-link/tests/linkui.js

@@ -7,6 +7,8 @@
 
 import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor';
 import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
+import indexOf from '@ckeditor/ckeditor5-utils/src/dom/indexof';
+import isRange from '@ckeditor/ckeditor5-utils/src/dom/isrange';
 import { keyCodes } from '@ckeditor/ckeditor5-utils/src/keyboard';
 import { getData as getModelData, setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
 import { getData as getViewData } from '@ckeditor/ckeditor5-engine/src/dev-utils/view';
@@ -137,15 +139,17 @@ describe( 'LinkUI', () => {
 
 			linkUIFeature._showUI();
 
-			const markerElement = editor.ui.view.element.querySelector( '.ck-fake-link-selection' );
+			const expectedRange = getMarkersRange( editor );
 
 			expect( balloon.visibleView ).to.equal( formView );
 			sinon.assert.calledWithExactly( balloonAddSpy, {
 				view: formView,
 				position: {
-					target: markerElement
+					target: sinon.match( isRange )
 				}
 			} );
+
+			assertDomRange( expectedRange, balloonAddSpy.args[ 0 ][ 0 ].position.target );
 		} );
 
 		it( 'should add #formView to the balloon and attach the balloon to the marker element when selection is collapsed', () => {
@@ -153,32 +157,16 @@ describe( 'LinkUI', () => {
 			setModelData( editor.model, '<paragraph>f[]oo</paragraph>' );
 			linkUIFeature._showUI();
 
-			const markerElement = editor.ui.view.element.querySelector( '.ck-fake-link-selection_collapsed' );
+			const expectedRange = getMarkersRange( editor );
 
-			expect( markerElement ).not.to.be.null;
 			expect( balloon.visibleView ).to.equal( formView );
 			sinon.assert.calledWithExactly( balloonAddSpy, {
 				view: formView,
 				position: {
-					target: markerElement
+					target: sinon.match( isRange )
 				}
 			} );
-		} );
-
-		it( 'should pass a proper position target to the balloon toolbar', () => {
-			setModelData( editor.model, '<paragraph>f[o]o</paragraph>' );
-
-			linkUIFeature._showUI();
-
-			const markerElement = editor.ui.view.element.querySelector( '.ck-fake-link-selection' );
-
-			expect( markerElement ).not.to.be.null;
-			expect( balloonAddSpy.calledWithExactly( {
-				view: formView,
-				position: {
-					target: markerElement
-				}
-			} ), 'spy arguments' ).to.be.true;
+			assertDomRange( expectedRange, balloonAddSpy.args[ 0 ][ 0 ].position.target );
 		} );
 
 		it( 'should add #actionsView to the balloon and attach the balloon to the link element when collapsed selection is inside ' +
@@ -382,12 +370,14 @@ describe( 'LinkUI', () => {
 					writer.setSelection( text, 1, true );
 				} );
 
-				const markerElement = editor.ui.view.element.querySelector( '.ck-fake-link-selection' );
+				const expectedRange = getMarkersRange( editor );
 
 				sinon.assert.calledOnce( spy );
 				sinon.assert.calledWithExactly( spy, {
-					target: markerElement
+					target: sinon.match( isRange )
 				} );
+
+				assertDomRange( expectedRange, spy.args[ 0 ][ 0 ].target );
 			} );
 
 			it( 'not update the position when is in not visible stack', () => {
@@ -528,6 +518,24 @@ describe( 'LinkUI', () => {
 			);
 			expect( editor.getData() ).to.equal( '<p>fo</p>' );
 		} );
+
+		function getMarkersRange( editor ) {
+			const markerElements = editor.ui.view.element.querySelectorAll( '.ck-fake-link-selection' );
+			const lastMarkerElement = markerElements[ markerElements.length - 1 ];
+
+			const range = document.createRange();
+			range.setStart( markerElements[ 0 ].parentElement, indexOf( markerElements[ 0 ] ) );
+			range.setEnd( lastMarkerElement.parentElement, indexOf( lastMarkerElement ) + 1 );
+
+			return range;
+		}
+
+		function assertDomRange( expected, actual ) {
+			expect( actual, 'startContainer' ).to.have.property( 'startContainer', expected.startContainer );
+			expect( actual, 'startOffset' ).to.have.property( 'startOffset', expected.startOffset );
+			expect( actual, 'endContainer' ).to.have.property( 'endContainer', expected.endContainer );
+			expect( actual, 'endOffset' ).to.have.property( 'endOffset', expected.endOffset );
+		}
 	} );
 
 	describe( '_hideUI()', () => {