summaryrefslogtreecommitdiff
path: root/spec/javascripts/lib/utils
diff options
context:
space:
mode:
authorPhil Hughes <me@iamphill.com>2017-09-28 16:13:21 +0100
committerPhil Hughes <me@iamphill.com>2017-09-29 09:24:56 +0100
commit333cb069afbef282618464206d8af50e8fd46cf7 (patch)
tree6ba1dca53cfc07562fc32f6f96f37bfa92210225 /spec/javascripts/lib/utils
parent34da9a0f3784277555af91ed12ac6fe7d6e58fe6 (diff)
downloadgitlab-ce-333cb069afbef282618464206d8af50e8fd46cf7.tar.gz
Fixed anchored content not being scrolled into view
Correctly scrolls anchored content into view when the user loads the page. This is most obvious when the user loads a link note in a merge request & the page does not scroll down to the note. Closes #38326
Diffstat (limited to 'spec/javascripts/lib/utils')
-rw-r--r--spec/javascripts/lib/utils/common_utils_spec.js60
1 files changed, 60 insertions, 0 deletions
diff --git a/spec/javascripts/lib/utils/common_utils_spec.js b/spec/javascripts/lib/utils/common_utils_spec.js
index 787b405de47..03c02ea06a8 100644
--- a/spec/javascripts/lib/utils/common_utils_spec.js
+++ b/spec/javascripts/lib/utils/common_utils_spec.js
@@ -84,6 +84,66 @@ describe('common_utils', () => {
expectGetElementIdToHaveBeenCalledWith('definição');
expectGetElementIdToHaveBeenCalledWith('user-content-definição');
});
+
+ it('scrolls element into view', () => {
+ document.body.innerHTML += `
+ <div id="parent">
+ <div style="height: 2000px;"></div>
+ <div id="test" style="height: 2000px;"></div>
+ </div>
+ `;
+ const elTop = document.getElementById('test').getBoundingClientRect().top;
+
+ window.history.pushState({}, null, '#test');
+ commonUtils.handleLocationHash();
+
+ expectGetElementIdToHaveBeenCalledWith('test');
+ expect(window.scrollY).toBe(elTop);
+
+ document.getElementById('parent').remove();
+ });
+
+ it('scrolls user content element into view', () => {
+ document.body.innerHTML += `
+ <div id="parent">
+ <div style="height: 2000px;"></div>
+ <div id="user-content-test" style="height: 2000px;"></div>
+ </div>
+ `;
+ const elTop = document.getElementById('user-content-test').getBoundingClientRect().top;
+
+ window.history.pushState({}, null, '#test');
+ commonUtils.handleLocationHash();
+
+ expectGetElementIdToHaveBeenCalledWith('test');
+ expectGetElementIdToHaveBeenCalledWith('user-content-test');
+ expect(window.scrollY).toBe(elTop);
+
+ document.getElementById('parent').remove();
+ });
+
+ it('scrolls to element with offset from navbar', () => {
+ spyOn(window, 'scrollBy').and.callThrough();
+ document.body.innerHTML += `
+ <div id="parent">
+ <div class="navbar-gitlab" style="position: fixed; top: 0; height: 50px;"></div>
+ <div style="height: 2000px; margin-top: 50px;"></div>
+ <div id="user-content-test" style="height: 2000px;"></div>
+ </div>
+ `;
+
+ const elTop = document.getElementById('user-content-test').getBoundingClientRect().top;
+
+ window.history.pushState({}, null, '#test');
+ commonUtils.handleLocationHash();
+
+ expectGetElementIdToHaveBeenCalledWith('test');
+ expectGetElementIdToHaveBeenCalledWith('user-content-test');
+ expect(window.scrollY).toBe(elTop - 50);
+ expect(window.scrollBy).toHaveBeenCalledWith(0, -50);
+
+ document.getElementById('parent').remove();
+ });
});
describe('setParamInURL', () => {