From 8046c5581259b84276ecef6af11e8bd7ede62ac7 Mon Sep 17 00:00:00 2001 From: Clayton Watts Date: Wed, 16 Oct 2019 13:46:22 -0700 Subject: [PATCH] feat(tab-scroller): Add incrementScrollImmediate to bypass animation Fixes #5123 --- packages/mdc-tab-scroller/README.md | 3 +- packages/mdc-tab-scroller/foundation.ts | 53 ++++++++++++------- test/unit/mdc-tab-scroller/foundation.test.js | 43 +++++++++++++++ 3 files changed, 80 insertions(+), 19 deletions(-) diff --git a/packages/mdc-tab-scroller/README.md b/packages/mdc-tab-scroller/README.md index 5d46fa6728f..2f7034314ba 100644 --- a/packages/mdc-tab-scroller/README.md +++ b/packages/mdc-tab-scroller/README.md @@ -82,7 +82,8 @@ Mixin | Description Method Signature | Description --- | --- `scrollTo(scrollX: number) => void` | Scrolls to the scrollX value. -`incrementScroll(scrollX: number) => void` | Increments the current scroll value by the scrollX value. +`incrementScroll(scrollX: number) => void` | Increments the current scroll value by the scrollX value using animation. +`incrementScrollImmediate(scrollX: number) => void` | Increments the current scroll value by the scrollX value without animation. `getScrollPosition() => number` | Returns the current visual scroll position. `getScrollContentWidth() => number` | Returns the width of the scroll content element. diff --git a/packages/mdc-tab-scroller/foundation.ts b/packages/mdc-tab-scroller/foundation.ts index 00f68e8a67e..990b0be7fea 100644 --- a/packages/mdc-tab-scroller/foundation.ts +++ b/packages/mdc-tab-scroller/foundation.ts @@ -30,6 +30,11 @@ import {MDCTabScrollerRTLReverse} from './rtl-reverse-scroller'; import {MDCTabScrollerRTL} from './rtl-scroller'; import {MDCTabScrollerAnimation, MDCTabScrollerHorizontalEdges} from './types'; +interface ScrollOperation { + finalScrollPosition: number; + scrollDelta: number; +} + export class MDCTabScrollerFoundation extends MDCFoundation { static get cssClasses() { return cssClasses; @@ -125,7 +130,7 @@ export class MDCTabScrollerFoundation extends MDCFoundation { + const {foundation, mockAdapter} = setupScrollToTest({scrollLeft: 700}); + foundation.incrementScrollImmediate(0); + td.verify(mockAdapter.setScrollAreaScrollLeft(td.matchers.isA(Number)), {times: 0}); +}); + +test('#incrementScrollImmediate() exits early if increment puts the scrollLeft over the max value', () => { + const {foundation, mockAdapter} = setupScrollToTest({scrollLeft: 700}); + foundation.incrementScrollImmediate(10); + td.verify(mockAdapter.setScrollAreaScrollLeft(td.matchers.isA(Number)), {times: 0}); +}); + +test('#incrementScrollImmediate() exits early if increment puts the scrollLeft below the min value', () => { + const {foundation, mockAdapter} = setupScrollToTest({scrollLeft: 0}); + foundation.incrementScrollImmediate(-10); + td.verify(mockAdapter.setScrollAreaScrollLeft(td.matchers.isA(Number)), {times: 0}); +}); + +test('#incrementScrollImmediate() increases the scrollLeft value by the given value', () => { + const {foundation, mockAdapter} = setupScrollToTest({scrollLeft: 123}); + foundation.incrementScrollImmediate(11); + td.verify(mockAdapter.setScrollAreaScrollLeft(134), {times: 1}); +}); + +test('#incrementScrollImmediate() increases the scrollLeft value by the given value up to the max scroll value', () => { + const {foundation, mockAdapter} = setupScrollToTest({scrollLeft: 99, rootWidth: 100, contentWidth: 200}); + foundation.incrementScrollImmediate(2); + td.verify(mockAdapter.setScrollAreaScrollLeft(100), {times: 1}); +}); + +test('#incrementScrollImmediate() decreases the scrollLeft value by the given value', () => { + const {foundation, mockAdapter} = setupScrollToTest({scrollLeft: 123}); + foundation.incrementScrollImmediate(-11); + td.verify(mockAdapter.setScrollAreaScrollLeft(112), {times: 1}); +}); + +test('#incrementScrollImmediate() decreases the scrollLeft value by the given value down to the min scroll value', + () => { + const {foundation, mockAdapter} = setupScrollToTest({scrollLeft: 1, rootWidth: 100, contentWidth: 200}); + foundation.incrementScrollImmediate(-2); + td.verify(mockAdapter.setScrollAreaScrollLeft(0), {times: 1}); + }); + // RTL Mode function setupScrollToRTLTest() {