From f0ebfea85844b915ac07554df31f11eea90fd29e Mon Sep 17 00:00:00 2001 From: Bonnie Zhou Date: Wed, 22 Aug 2018 13:45:48 -0700 Subject: [PATCH] fix(tab-bar): Early exit (#3386) Move the early exit for activating a tab from Tab to TabBar. --- packages/mdc-tab-bar/foundation.js | 7 ++----- packages/mdc-tab/foundation.js | 5 ----- test/unit/mdc-tab-bar/foundation.test.js | 9 +++++++++ test/unit/mdc-tab/foundation.test.js | 7 ------- 4 files changed, 11 insertions(+), 17 deletions(-) diff --git a/packages/mdc-tab-bar/foundation.js b/packages/mdc-tab-bar/foundation.js index e902810cd00..9f3e3b1b24d 100644 --- a/packages/mdc-tab-bar/foundation.js +++ b/packages/mdc-tab-bar/foundation.js @@ -125,7 +125,7 @@ class MDCTabBarFoundation extends MDCFoundation { */ activateTab(index) { const previousActiveIndex = this.adapter_.getActiveTabIndex(); - if (!this.indexIsInRange_(index)) { + if (!this.indexIsInRange_(index) || index === previousActiveIndex) { return; } @@ -133,10 +133,7 @@ class MDCTabBarFoundation extends MDCFoundation { this.adapter_.activateTabAtIndex(index, this.adapter_.getTabIndicatorClientRectAtIndex(previousActiveIndex)); this.scrollIntoView(index); - // Only notify the tab activation if the index is different than the previously active index - if (index !== previousActiveIndex) { - this.adapter_.notifyTabActivated(index); - } + this.adapter_.notifyTabActivated(index); } /** diff --git a/packages/mdc-tab/foundation.js b/packages/mdc-tab/foundation.js index 868781a372d..6aee35e5232 100644 --- a/packages/mdc-tab/foundation.js +++ b/packages/mdc-tab/foundation.js @@ -98,11 +98,6 @@ class MDCTabFoundation extends MDCFoundation { * @param {!ClientRect=} previousIndicatorClientRect */ activate(previousIndicatorClientRect) { - // Early exit - if (this.isActive()) { - return; - } - this.adapter_.addClass(cssClasses.ACTIVE); this.adapter_.setAttr(strings.ARIA_SELECTED, 'true'); this.adapter_.setAttr(strings.TABINDEX, '0'); diff --git a/test/unit/mdc-tab-bar/foundation.test.js b/test/unit/mdc-tab-bar/foundation.test.js index 18969f518a1..3a81cf2c6a8 100644 --- a/test/unit/mdc-tab-bar/foundation.test.js +++ b/test/unit/mdc-tab-bar/foundation.test.js @@ -359,6 +359,15 @@ test('#activateTab() does nothing if the index underflows the tab list', () => { td.verify(mockAdapter.activateTabAtIndex(td.matchers.isA(Number)), {times: 0}); }); +test('#activateTab() does nothing if the index is the same as the previous active index', () => { + const {foundation, mockAdapter} = setupActivateTabTest(); + td.when(mockAdapter.getActiveTabIndex()).thenReturn(0); + td.when(mockAdapter.getTabListLength()).thenReturn(13); + foundation.activateTab(0); + td.verify(mockAdapter.deactivateTabAtIndex(td.matchers.isA(Number)), {times: 0}); + td.verify(mockAdapter.activateTabAtIndex(td.matchers.isA(Number)), {times: 0}); +}); + test(`#activateTab() does not emit the ${MDCTabBarFoundation.strings.TAB_ACTIVATED_EVENT} event if the index` + ' is the currently active index', () => { const {foundation, mockAdapter} = setupActivateTabTest(); diff --git a/test/unit/mdc-tab/foundation.test.js b/test/unit/mdc-tab/foundation.test.js index bfe07c386b6..1dfaf1b5ae7 100644 --- a/test/unit/mdc-tab/foundation.test.js +++ b/test/unit/mdc-tab/foundation.test.js @@ -51,13 +51,6 @@ test('defaultAdapter returns a complete adapter implementation', () => { const setupTest = () => setupFoundationTest(MDCTabFoundation); -test('#activate does nothing if already active', () => { - const {foundation, mockAdapter} = setupTest(); - td.when(mockAdapter.hasClass(MDCTabFoundation.cssClasses.ACTIVE)).thenReturn(true); - foundation.activate(); - td.verify(mockAdapter.addClass(MDCTabFoundation.cssClasses.ACTIVE), {times: 0}); -}); - test('#activate adds mdc-tab--active class to the root element', () => { const {foundation, mockAdapter} = setupTest(); foundation.activate();