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

Made dom/scroll module export hlper functions and an object for testing purposes.

Aleksander Nowodzinski 8 лет назад
Родитель
Сommit
15d9d4c99d

+ 10 - 7
packages/ckeditor5-utils/src/dom/scroll.js

@@ -13,6 +13,11 @@ import global from './global';
 import isRange from './isrange';
 import Rect from './rect';
 
+const utils = {
+	scrollViewportToShowTarget,
+	scrollAncestorsToShowTarget
+};
+
 /**
  * Makes any page `HTMLElement` or `Range` (`target`) visible inside the browser viewport.
  * This helper will scroll all `target` ancestors and the web browser viewport to reveal the target to
@@ -24,10 +29,10 @@ import Rect from './rect';
  * by keeping the `target` some distance from the edge of the viewport and thus making it easier to
  * read or edit by the user.
  */
-function scrollViewportToShowTarget( { target, viewportOffset = 0 } ) {
+export function scrollViewportToShowTarget( { target, viewportOffset = 0 } ) {
 	// Scroll the ancestors of the target to reveal it first, then focus on scrolling
 	// the viewport, when the position of the target is fixed.
-	scrollAncestorsToShowTarget( target );
+	utils.scrollAncestorsToShowTarget( target );
 
 	const targetRect = new Rect( target );
 	const targetShiftedDownRect = targetRect.clone().moveBy( 0, viewportOffset );
@@ -70,7 +75,7 @@ function scrollViewportToShowTarget( { target, viewportOffset = 0 } ) {
  *
  * @param {HTMLElement|Range} target A target, which supposed to become visible to the user.
  */
-function scrollAncestorsToShowTarget( target ) {
+export function scrollAncestorsToShowTarget( target ) {
 	let parent, parentRect, targetRect;
 
 	if ( isRange( target ) ) {
@@ -98,10 +103,8 @@ function scrollAncestorsToShowTarget( target ) {
 	} while ( ( parent = parent.parentNode ) );
 }
 
-export default {
-	scrollViewportToShowTarget,
-	scrollAncestorsToShowTarget
-};
+// For testing purposes (easy helper stubbing).
+export { utils as _test };
 
 // Makes any page `HTMLElement` or `Range` (target) visible within its parent.
 //

+ 21 - 6
packages/ckeditor5-utils/tests/dom/scroll.js

@@ -8,12 +8,7 @@
 import global from '../../src/dom/global';
 import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
 import isRange from '../../src/dom/isrange';
-import scrollUtils from '../../src/dom/scroll';
-
-const {
-	scrollViewportToShowTarget,
-	scrollAncestorsToShowTarget
-} = scrollUtils;
+import { scrollViewportToShowTarget, scrollAncestorsToShowTarget, _test } from '../../src/dom/scroll';
 
 testUtils.createSinonSandbox();
 
@@ -230,6 +225,16 @@ describe( 'scrollViewportToShowTarget()', () => {
 	// Note: Negative scrollTo arguments make no sense in reality, but in mocks with arbitrary
 	// initial geometry and scroll position they give the right, relative picture of what's going on.
 	function testNoOffset() {
+		it( 'calls scrollAncestorsToShowTarget first', () => {
+			const spy = testUtils.sinon.stub( _test, 'scrollAncestorsToShowTarget' );
+
+			stubRect( target, { top: -200, right: 200, bottom: -100, left: 100, width: 100, height: 100 } );
+
+			scrollViewportToShowTarget( { target } );
+			sinon.assert.calledOnce( spy );
+			sinon.assert.calledWithExactly( spy, target );
+		} );
+
 		it( 'does not scroll the viewport when the target is fully visible', () => {
 			stubRect( target, { top: 0, right: 200, bottom: 100, left: 100, width: 100, height: 100 } );
 
@@ -311,6 +316,16 @@ describe( 'scrollViewportToShowTarget()', () => {
 	// Note: Negative scrollTo arguments make no sense in reality, but in mocks with arbitrary
 	// initial geometry and scroll position they give the right, relative picture of what's going on.
 	function testWithOffset() {
+		it( 'calls scrollAncestorsToShowTarget first', () => {
+			const spy = testUtils.sinon.stub( _test, 'scrollAncestorsToShowTarget' );
+
+			stubRect( target, { top: -200, right: 200, bottom: -100, left: 100, width: 100, height: 100 } );
+
+			scrollViewportToShowTarget( { target, viewportOffset } );
+			sinon.assert.calledOnce( spy );
+			sinon.assert.calledWithExactly( spy, target );
+		} );
+
 		it( 'does not scroll the viewport when the target is fully visible', () => {
 			stubRect( target, { top: 50, right: 200, bottom: 150, left: 100, width: 100, height: 100 } );