Skip to content
This repository has been archived by the owner on Jun 10, 2020. It is now read-only.

Commit

Permalink
Merge pull request #236 from seatgeek/window-as-scroll
Browse files Browse the repository at this point in the history
Start listening to window scroll events when useWindowAsScrollContain…
  • Loading branch information
garetht authored Aug 7, 2017
2 parents ef8d2d2 + 919459f commit d837000
Show file tree
Hide file tree
Showing 3 changed files with 240 additions and 0 deletions.
193 changes: 193 additions & 0 deletions __tests__/__snapshots__/infinite_test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,199 @@ exports[`React Infinite when the window is used as the Container hides DOM eleme
</Infinite>
`;

exports[`React Infinite when the window is used as the Container reacts to window scroll events when useWindowAsScrollContainer is enabled after the initial render 1`] = `
<Infinite
className="correct-class-name"
containerHeight={700}
displayBottomUpwards={false}
elementHeight={200}
handleScroll={[Function]}
isInfiniteLoading={false}
loadingSpinnerDelegate={<div />}
onInfiniteLoad={[Function]}
styles={Object {}}
timeScrollStateLastsForAfterUserScrolls={10000}
useWindowAsScrollContainer={false}
>
<div
className="correct-class-name"
onScroll={[Function]}
style={
Object {
"WebkitOverflowScrolling": "touch",
"height": 700,
"overflowX": "hidden",
"overflowY": "scroll",
}
}
>
<div
style={Object {}}
>
<div
style={
Object {
"height": 0,
"width": "100%",
}
}
/>
<div
className="test-div-0"
style={
Object {
"height": 200,
}
}
/>
<div
className="test-div-1"
style={
Object {
"height": 200,
}
}
/>
<div
className="test-div-2"
style={
Object {
"height": 200,
}
}
/>
<div
className="test-div-3"
style={
Object {
"height": 200,
}
}
/>
<div
className="test-div-4"
style={
Object {
"height": 200,
}
}
/>
<div
className="test-div-5"
style={
Object {
"height": 200,
}
}
/>
<div
style={
Object {
"height": 2800,
"width": "100%",
}
}
/>
</div>
</div>
</Infinite>
`;

exports[`React Infinite when the window is used as the Container reacts to window scroll events when useWindowAsScrollContainer is enabled after the initial render 2`] = `
<Infinite
className="correct-class-name"
containerHeight={700}
displayBottomUpwards={false}
elementHeight={200}
handleScroll={[Function]}
isInfiniteLoading={false}
loadingSpinnerDelegate={<div />}
onInfiniteLoad={[Function]}
styles={Object {}}
timeScrollStateLastsForAfterUserScrolls={10000}
useWindowAsScrollContainer={true}
>
<div
className="correct-class-name"
onScroll={[Function]}
style={Object {}}
>
<div
style={
Object {
"pointerEvents": "none",
}
}
>
<div
style={
Object {
"height": 0,
"width": "100%",
}
}
/>
<div
className="test-div-0"
style={
Object {
"height": 200,
}
}
/>
<div
className="test-div-1"
style={
Object {
"height": 200,
}
}
/>
<div
className="test-div-2"
style={
Object {
"height": 200,
}
}
/>
<div
className="test-div-3"
style={
Object {
"height": 200,
}
}
/>
<div
className="test-div-4"
style={
Object {
"height": 200,
}
}
/>
<div
className="test-div-5"
style={
Object {
"height": 200,
}
}
/>
<div
style={
Object {
"height": 2800,
"width": "100%",
}
}
/>
</div>
</div>
</Infinite>
`;

exports[`Rendering the React Infinite Component Wrapper applies the provided class name to the root node 1`] = `
<div
className="correct-class-name"
Expand Down
40 changes: 40 additions & 0 deletions __tests__/infinite_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -685,6 +685,46 @@ describe('React Infinite when the window is used as the Container', function() {
window.addEventListener = oldAdd;
});
});

it('reacts to window scroll events when useWindowAsScrollContainer is enabled after the initial render', function() {
const infiniteSpy = jest.fn();
const elementHeight = 200;

const oldAdd = window.addEventListener;

const listenerTriggered = new Promise((resolve, reject) => {
window.addEventListener = function(event, f) {
if (event === 'scroll') {
resolve(f);
}
};
});

const rootNode = mount(
<Infinite
elementHeight={elementHeight}
handleScroll={infiniteSpy}
timeScrollStateLastsForAfterUserScrolls={10000}
className={'correct-class-name'}
containerHeight={700}
>
{renderHelpers.divGenerator(20, elementHeight)}
</Infinite>
);

expect(mountToJson(rootNode)).toMatchSnapshot();

rootNode.setProps({
useWindowAsScrollContainer: true
});

return listenerTriggered.then(listener => {
window.pageYOffset = 200;
listener();
expect(mountToJson(rootNode)).toMatchSnapshot();
window.addEventListener = oldAdd;
});
});
});

describe("Specifying React Infinite's preload amounts", function() {
Expand Down
7 changes: 7 additions & 0 deletions src/react-infinite.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,13 @@ class Infinite extends React.Component<
) {
this.loadingSpinnerHeight = this.utils.getLoadingSpinnerHeight();

if (
!prevProps.useWindowAsScrollContainer &&
this.props.useWindowAsScrollContainer
) {
this.utils.subscribeToScrollListener();
}

if (this.props.displayBottomUpwards) {
var lowestScrollTop = this.getLowestPossibleScrollTop();
if (
Expand Down

0 comments on commit d837000

Please sign in to comment.