Browse Source

Stub mentions view method rather then relay on CSS values.

Maciej Gołaszewski 6 years ago
parent
commit
a4ccb74228
1 changed files with 26 additions and 28 deletions
  1. 26 28
      packages/ckeditor5-mention/tests/mentionui.js

+ 26 - 28
packages/ckeditor5-mention/tests/mentionui.js

@@ -259,7 +259,7 @@ describe( 'MentionUI', () => {
 					const pinArgument = pinSpy.firstCall.args[ 0 ];
 					const { limiter } = pinArgument;
 
-					sinon.stub( editingView.document.selection, 'editableElement' ).value( null );
+					testUtils.sinon.stub( editingView.document.selection, 'editableElement' ).value( null );
 
 					// Should not break;
 					expect( limiter() ).to.be.null;
@@ -344,49 +344,47 @@ describe( 'MentionUI', () => {
 					writer.insertText( '@', doc.selection.getFirstPosition() );
 				} );
 
-				const arrowDownEvtData = {
-					keyCode: keyCodes.arrowdown,
-					preventDefault: sinon.spy(),
-					stopPropagation: sinon.spy()
-				};
-
-				const arrowUpEvtData = {
-					keyCode: keyCodes.arrowup,
-					preventDefault: sinon.spy(),
-					stopPropagation: sinon.spy()
-				};
-
 				return waitForDebounce()
 					.then( () => {
-						expect( panelView.isVisible, 'A' ).to.be.true;
+						const arrowDownEvtData = {
+							keyCode: keyCodes.arrowdown,
+							preventDefault: sinon.spy(),
+							stopPropagation: sinon.spy()
+						};
 
-						// Set arbitrary height of each item in the panel view since Travis has different base styles. See #47.
-						Array.from( mentionsView.items ).forEach( item => {
-							item.children.get( 0 ).element.style = 'height: 50px';
-						} );
+						const arrowUpEvtData = {
+							keyCode: keyCodes.arrowup,
+							preventDefault: sinon.spy(),
+							stopPropagation: sinon.spy()
+						};
+
+						const isItemVisibleInScrolledAreaStub = testUtils.sinon.stub( mentionsView, '_isItemVisibleInScrolledArea' );
+						const firstItemOffset = mentionsView.items.get( 0 ).children.get( 0 ).element.offsetTop;
+
+						expect( panelView.isVisible ).to.be.true;
+						isItemVisibleInScrolledAreaStub.returns( true );
 
 						expectChildViewsIsOnState( [ true, false, false, false, false, false, false, false, false, false ] );
+						expect( mentionsView.element.scrollTop ).to.equal( firstItemOffset );
 
 						fireKeyDownEvent( arrowDownEvtData );
-					} )
-					.then( waitForDebounce )
-					.then( () => {
-						expect( mentionsView.element.scrollTop ).to.equal( 0 );
 
 						expectChildViewsIsOnState( [ false, true, false, false, false, false, false, false, false, false ] );
+						expect( mentionsView.element.scrollTop ).to.equal( firstItemOffset );
 
 						fireKeyDownEvent( arrowUpEvtData );
-						fireKeyDownEvent( arrowUpEvtData );
+						expectChildViewsIsOnState( [ true, false, false, false, false, false, false, false, false, false ] );
+						expect( mentionsView.element.scrollTop ).to.equal( firstItemOffset );
 
+						isItemVisibleInScrolledAreaStub.returns( false );
+						fireKeyDownEvent( arrowUpEvtData );
 						expectChildViewsIsOnState( [ false, false, false, false, false, false, false, false, false, true ] );
-						expect( mentionsView.element.scrollTop ).to.be.not.equal( 0 );
+						expect( mentionsView.element.scrollTop ).to.not.equal( firstItemOffset );
 
 						fireKeyDownEvent( arrowDownEvtData );
-						expectChildViewsIsOnState( [ true, false, false, false, false, false, false, false, false, false ] );
 
-						// The panel view should scroll back to 0 but on travis it gives random results. See #47.
-						// Use 50 as a height of one element so if it is scrolled below first it is good enough.
-						expect( mentionsView.element.scrollTop ).to.be.within( 0, 50 );
+						expectChildViewsIsOnState( [ true, false, false, false, false, false, false, false, false, false ] );
+						expect( mentionsView.element.scrollTop ).to.equal( firstItemOffset );
 					} );
 			} );
 		} );