This repository has been archived by the owner on Dec 24, 2023. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
867 additions
and
165 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,50 @@ | ||
import getDocumentScrollHeight from './getDocumentScrollHeight'; | ||
|
||
const CONFIGURABLE = { | ||
writable: true, | ||
configurable: true, | ||
}; | ||
|
||
describe('getDocumentScrollHeight', () => { | ||
it('should check if the document.documentElement.scrollHeight function has been called', () => { | ||
it('should return the bodyScrollHeight', () => { | ||
// For viewPortHeight | ||
Object.defineProperty(document.documentElement, 'clientHeight', {value: 500, ...CONFIGURABLE}); | ||
Object.defineProperty(window, 'innerHeight', {value: 500, ...CONFIGURABLE}); | ||
// For scrollHeight | ||
Object.defineProperty(document.documentElement, 'scrollHeight', {value: 500, ...CONFIGURABLE}); | ||
// For bodyScrollHeight | ||
Object.defineProperty(document.body, 'scrollHeight', {value: 1500, ...CONFIGURABLE}); | ||
|
||
expect(getDocumentScrollHeight()).toEqual(1500); | ||
}); | ||
|
||
it('should return the scrollHeight', () => { | ||
// For viewPortHeight | ||
Object.defineProperty(document.documentElement, 'clientHeight', {value: 500, ...CONFIGURABLE}); | ||
Object.defineProperty(window, 'innerHeight', {value: 500, ...CONFIGURABLE}); | ||
// For scrollHeight | ||
Object.defineProperty(document.documentElement, 'scrollHeight', {value: 2250, ...CONFIGURABLE}); | ||
// For bodyScrollHeight | ||
Object.defineProperty(document.body, 'scrollHeight', {value: 1500, ...CONFIGURABLE}); | ||
|
||
expect(getDocumentScrollHeight()).toEqual(2250); | ||
}); | ||
|
||
it('should return the height of the largest node', () => { | ||
// For viewPortHeight | ||
Object.defineProperty(document.documentElement, 'clientHeight', {value: 1500, ...CONFIGURABLE}); | ||
Object.defineProperty(window, 'innerHeight', {value: 1500, ...CONFIGURABLE}); | ||
// For scrollHeight | ||
Object.defineProperty(document.documentElement, 'scrollHeight', {value: 1500, ...CONFIGURABLE}); | ||
// For bodyScrollHeight | ||
Object.defineProperty(document.body, 'scrollHeight', {value: 1500, ...CONFIGURABLE}); | ||
document.body.innerHTML = | ||
'<div>' + | ||
' <span style="height: 200px;width: 50px"/>' + | ||
' <div style="height: 500px;width: 50px" />' + | ||
'</div>'; | ||
|
||
// Some lines and the outcome can't be tested because we can't mock `scrollHeight` and `clientHeight` | ||
getDocumentScrollHeight(); | ||
// I can't verify the call of the document.documentElement.scrollHeight with Jest, so there is no verification here, sorry :( | ||
// If you know a way, please add it here ;-) | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,27 @@ | ||
import scrollToPosition from './scrollToPosition'; | ||
|
||
const CONFIGURABLE = { | ||
writable: true, | ||
configurable: true, | ||
}; | ||
|
||
describe('scrollToPosition', () => { | ||
it('should check if the scrollTo function has been called', () => { | ||
window.scrollTo = jest.fn(); | ||
scrollToPosition(150); | ||
expect(window.scrollTo).toBeCalledWith(0, 150); | ||
}); | ||
|
||
it('should find the largest node if scrollTo does not work on the window', () => { | ||
window.scrollTo = jest.fn(); | ||
Object.defineProperty(document.documentElement, 'scrollTop', {value: 150, ...CONFIGURABLE}); | ||
document.body.innerHTML = | ||
'<div>' + | ||
' <span style="height: 200px;width: 50px"/>' + | ||
' <div style="height: 500px;width: 50px" />' + | ||
'</div>'; | ||
|
||
scrollToPosition(150); | ||
// Some lines and the outcome can't be tested because we can't mock `scrollHeight` and `clientHeight` | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,57 @@ | ||
/** | ||
* Scroll to a x = 0 and y = variable position in the screen | ||
* Sometimes a scroll can't be done on the window, so the scroll needs to be done on the largest | ||
* element on the screen | ||
*/ | ||
export default function scrollToPosition(yPosition:number):void { | ||
window.scrollTo(0, yPosition); | ||
export default function scrollToPosition(yPosition: number): void { | ||
const initialScroll = document.documentElement.scrollTop; | ||
|
||
// If for some reason the scroll didn't work, find the first largest element and use that to scroll on | ||
let largestNode: HTMLElement; | ||
|
||
// Scroll with the default way of scrolling | ||
window.scrollTo(0, yPosition); | ||
|
||
/* istanbul ignore else */ | ||
if (initialScroll === document.documentElement.scrollTop && yPosition > 0) { | ||
largestFirstNode(document.documentElement.childNodes).scrollTo(0, yPosition); | ||
} | ||
|
||
/** | ||
* Find the first largest node in the page so it can be used for scrolling | ||
*/ | ||
function largestFirstNode(nodesList: any) { | ||
let node: HTMLElement; | ||
|
||
// Loop over the nodes | ||
for (let i = nodesList.length - 1; i >= 0; i--) { | ||
node = nodesList[i]; | ||
|
||
// Stop if the largest node has been found | ||
/* istanbul ignore next */ | ||
if (largestNode) { | ||
break; | ||
} | ||
|
||
// Check if the element has a scroll / client height | ||
/* istanbul ignore next */ | ||
if (node.scrollHeight && node.clientHeight) { | ||
const viewPortHeight = Math.max(document.documentElement.clientHeight, window.innerHeight || 0); | ||
const elHeight = Math.max(node.scrollHeight, node.clientHeight); | ||
|
||
// If the element is bigger than the viewport, then we got our scroll container | ||
if (elHeight > viewPortHeight) { | ||
largestNode = node; | ||
break; | ||
} | ||
} | ||
|
||
if (node.childNodes.length) { | ||
largestFirstNode(node.childNodes); | ||
} | ||
} | ||
|
||
// If the largestNode is the body then return the window | ||
return (document.body === largestNode || !largestNode) ? window : largestNode; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
Oops, something went wrong.