From 4e8f82124df5ad892c3e6cff5a951e5730eab5d1 Mon Sep 17 00:00:00 2001 From: Chester Millisock Date: Thu, 26 Dec 2019 12:23:29 -0800 Subject: [PATCH] Unit tests for disabling scrollIntoView --- .../angularComponent/ngcOmniboxControllerSpec.js | 15 +++++++++++++++ src/angularComponent/ngcOmniboxController.js | 11 ++++++----- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/spec/tests/angularComponent/ngcOmniboxControllerSpec.js b/spec/tests/angularComponent/ngcOmniboxControllerSpec.js index 91efc4d..5e37f2c 100644 --- a/spec/tests/angularComponent/ngcOmniboxControllerSpec.js +++ b/spec/tests/angularComponent/ngcOmniboxControllerSpec.js @@ -657,5 +657,20 @@ describe('ngcOmnibox.angularComponent.ngcOmniboxController', () => { omniboxController.highlightedChoice = null; expect(omniboxController.fieldElement.focus).toHaveBeenCalled(); }); + + it('should call scrollSuggestionIntoView by default', () => { + const scrollSuggestionIntoViewSpy = spyOn(omniboxController, '_scrollSuggestionIntoView'); + omniboxController.suggestions = ['test', 'me']; + omniboxController.highlightNextSuggestion(); + expect(scrollSuggestionIntoViewSpy).toHaveBeenCalled(); + }); + + it('should not call scrollSuggestionIntoView when shouldScrollIntoView is false', () => { + const scrollSuggestionIntoViewSpy = spyOn(omniboxController, '_scrollSuggestionIntoView'); + omniboxController.shouldScrollIntoView = false; + omniboxController.suggestions = ['test', 'me']; + omniboxController.highlightNextSuggestion(); + expect(scrollSuggestionIntoViewSpy).not.toHaveBeenCalled(); + }); }); }); diff --git a/src/angularComponent/ngcOmniboxController.js b/src/angularComponent/ngcOmniboxController.js index f25b706..2935331 100644 --- a/src/angularComponent/ngcOmniboxController.js +++ b/src/angularComponent/ngcOmniboxController.js @@ -240,7 +240,9 @@ export default class NgcOmniboxController { return this.highlightPreviousSuggestion(startHighlightIndex); } - this._scrollSuggestionIntoView(); + if (this.shouldScrollIntoView !== false) { + this._scrollSuggestionIntoView(); + } return newIndex; } @@ -282,7 +284,9 @@ export default class NgcOmniboxController { return this.highlightNextSuggestion(startHighlightIndex); } - this._scrollSuggestionIntoView(); + if (this.shouldScrollIntoView !== false) { + this._scrollSuggestionIntoView(); + } return newIndex; } @@ -726,9 +730,6 @@ export default class NgcOmniboxController { } _scrollSuggestionIntoView() { - if (this.shouldScrollIntoView === false) { - return; - } // Disable highlighting while scrolling so the mouse doesn't accidentally highlight a new item this.isHighlightingDisabled = true;