Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/thick-clocks-applaud.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@shopify/polaris': patch
---

Fixed Scrollable component to match existing onScrolledToBottom logic
3 changes: 2 additions & 1 deletion polaris-react/src/components/Scrollable/Scrollable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ export function Scrollable({

requestAnimationFrame(() => {
const {scrollTop, clientHeight, scrollHeight} = currentScrollArea;
const canScroll = Boolean(scrollHeight > clientHeight);
const isBelowTopOfScroll = Boolean(scrollTop > 0);
const isAtBottomOfScroll = Boolean(
scrollTop + clientHeight >= scrollHeight - LOW_RES_BUFFER,
Expand All @@ -75,7 +76,7 @@ export function Scrollable({
setTopShadow(isBelowTopOfScroll);
setBottomShadow(!isAtBottomOfScroll);

if (isAtBottomOfScroll && onScrolledToBottom) {
if (canScroll && isAtBottomOfScroll && onScrolledToBottom) {
onScrolledToBottom();
}
});
Expand Down
72 changes: 72 additions & 0 deletions polaris-react/src/components/Scrollable/tests/Scrollable.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,24 @@ import {mountWithApp} from 'tests/utilities';

import {Scrollable} from '../Scrollable';
import {ScrollableContext} from '../context';
import type {ScrollableProps} from '../Scrollable';

describe('<Scrollable />', () => {
let rafSpy: jest.SpyInstance;

beforeAll(() => {
rafSpy = jest
.spyOn(window, 'requestAnimationFrame')
.mockImplementation((cb) => {
cb(Date.now());
return Math.random();
});
});

afterAll(() => {
rafSpy.mockRestore();
});

it('mounts', () => {
const scrollable = mountWithApp(<Scrollable />);
expect(scrollable).toBeDefined();
Expand Down Expand Up @@ -71,4 +87,60 @@ describe('<Scrollable />', () => {
tabIndex: 0,
});
});

it('calls onScrolledToBottom when scrolled to bottom', () => {
const onScrolledToBottom = jest.fn();

const scrollArea = mountWithApp(
<Scrollable
data-test-id="scroll-element"
onScrolledToBottom={onScrolledToBottom}
>
<p>Hello</p>
</Scrollable>,
);

const scrollNode = scrollArea.find('div', {
'data-test-id': 'scroll-element',
} as ScrollableProps)?.domNode!;

// defineProperty needed to assign values to readonly node properties
Object.defineProperty(scrollNode, 'clientHeight', {get: () => 0});
Object.defineProperty(scrollNode, 'scrollHeight', {get: () => 10});
Object.defineProperty(scrollNode, 'scrollTop', {get: () => 10});

scrollArea.act(() => {
scrollNode.dispatchEvent(new Event('scroll'));
});

expect(onScrolledToBottom).toHaveBeenCalledTimes(1);
});

it(`doesn't call onScrolledToBottom when the scroll area is not overflowing`, () => {
const onScrolledToBottom = jest.fn();

const scrollArea = mountWithApp(
<Scrollable
data-test-id="scroll-element"
onScrolledToBottom={onScrolledToBottom}
>
<p>Hello</p>
</Scrollable>,
);

const scrollNode = scrollArea.find('div', {
'data-test-id': 'scroll-element',
} as ScrollableProps)?.domNode!;

// defineProperty needed to assign values to readonly node properties
Object.defineProperty(scrollNode, 'clientHeight', {get: () => 10});
Object.defineProperty(scrollNode, 'scrollHeight', {get: () => 10});
Object.defineProperty(scrollNode, 'scrollTop', {get: () => 10});

scrollArea.act(() => {
scrollNode.dispatchEvent(new Event('scroll'));
});

expect(onScrolledToBottom).not.toHaveBeenCalled();
});
});