8
0
Просмотр исходного кода

Add tests for inserting mention.

Maciej Gołaszewski 6 лет назад
Родитель
Сommit
69d2c175ab

+ 11 - 13
packages/ckeditor5-mention/src/mentionui.js

@@ -112,7 +112,7 @@ export default class MentionUI extends Plugin {
 				range
 			} );
 
-			this._hideForm();
+			this._hidePanel();
 		} );
 	}
 
@@ -147,14 +147,14 @@ export default class MentionUI extends Plugin {
 			}
 
 			if ( this._items.length ) {
-				this._showForm();
+				this._showPanel();
 			} else {
-				this._hideForm();
+				this._hidePanel();
 			}
 		} );
 
 		watcher.on( 'unmatched', () => {
-			this._hideForm();
+			this._hidePanel();
 		} );
 	}
 
@@ -162,17 +162,20 @@ export default class MentionUI extends Plugin {
 		return this._watchers.get( marker );
 	}
 
-	_showForm() {
-		if ( this._isVisible ) {
-			// return;
+	_showPanel() {
+		if ( this.panelView.isVisible ) {
+			return;
 		}
 
-		// Pin the panel to an element with the "target" id DOM.
 		this.panelView.pin( this._getBalloonPositionData() );
 
 		this.panelView.show();
 	}
 
+	_hidePanel() {
+		this.panelView.hide();
+	}
+
 	// TODO copied from balloontoolbar
 	_getBalloonPositionData() {
 		const editor = this.editor;
@@ -189,7 +192,6 @@ export default class MentionUI extends Plugin {
 				const range = viewSelection.getLastRange();
 				const rangeRects = Rect.getDomRangeRects( view.domConverter.viewRangeToDom( range ) );
 
-				// Select the proper range rect depending on the direction of the selection.
 				if ( rangeRects.length > 1 && rangeRects[ rangeRects.length - 1 ].width === 0 ) {
 					rangeRects.pop();
 				}
@@ -199,10 +201,6 @@ export default class MentionUI extends Plugin {
 			positions: getBalloonPositions()
 		};
 	}
-
-	_hideForm() {
-		this.panelView.hide();
-	}
 }
 
 // Returns whole text from parent element by adding all data from text nodes together.

+ 40 - 0
packages/ckeditor5-mention/tests/mentionui.js

@@ -148,4 +148,44 @@ describe( 'BalloonToolbar', () => {
 			expect( mentionUI.panelView.isVisible ).to.be.false;
 		} );
 	} );
+
+	describe( 'execute', () => {
+		it( 'should call the mention command with proper options', () => {
+			setData( model, '<paragraph>foo []</paragraph>' );
+
+			model.change( writer => {
+				writer.insertText( '@', doc.selection.getFirstPosition() );
+			} );
+
+			const command = editor.commands.get( 'mention' );
+			const spy = testUtils.sinon.spy( command, 'execute' );
+
+			mentionUI._mentionsView.listView.items.get( 0 ).children.get( 0 ).fire( 'execute' );
+
+			sinon.assert.calledOnce( spy );
+
+			const commandOptions = spy.getCall( 0 ).args[ 0 ];
+
+			expect( commandOptions ).to.have.property( 'mention', 'Jodator' );
+			expect( commandOptions ).to.have.property( 'marker', '@' );
+			expect( commandOptions ).to.have.property( 'range' );
+
+			const start = model.createPositionAt( doc.getRoot().getChild( 0 ), 4 );
+			const expectedRange = model.createRange( start, start.getShiftedBy( 1 ) );
+
+			expect( commandOptions.range.isEqual( expectedRange ) ).to.be.true;
+		} );
+
+		it( 'should hide panel on execute', () => {
+			setData( model, '<paragraph>foo []</paragraph>' );
+
+			model.change( writer => {
+				writer.insertText( '@', doc.selection.getFirstPosition() );
+			} );
+
+			mentionUI._mentionsView.listView.items.get( 0 ).children.get( 0 ).fire( 'execute' );
+
+			expect( mentionUI.panelView.isVisible ).to.be.false;
+		} );
+	} );
 } );