Skip to content

Commit fa6ffbf

Browse files
committed
add tests for onScrolledToBottom
1 parent d1df519 commit fa6ffbf

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

polaris-react/src/components/Scrollable/tests/Scrollable.test.tsx

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {mountWithApp} from 'tests/utilities';
33

44
import {Scrollable} from '../Scrollable';
55
import {ScrollableContext} from '../context';
6+
import type {ScrollableProps} from '../Scrollable';
67

78
describe('<Scrollable />', () => {
89
it('mounts', () => {
@@ -71,4 +72,60 @@ describe('<Scrollable />', () => {
7172
tabIndex: 0,
7273
});
7374
});
75+
76+
it('calls onScrolledToBottom when scrolled to bottom', () => {
77+
const onScrolledToBottom = jest.fn();
78+
79+
const scrollArea = mountWithApp(
80+
<Scrollable
81+
data-test-id="scroll-element"
82+
onScrolledToBottom={onScrolledToBottom}
83+
>
84+
<p>Hello</p>
85+
</Scrollable>,
86+
);
87+
88+
const scrollNode = scrollArea.find('div', {
89+
'data-test-id': 'scroll-element',
90+
} as ScrollableProps)?.domNode!;
91+
92+
// defineProperty needed to assign values to readonly node properties
93+
Object.defineProperty(scrollNode, 'clientHeight', {get: () => 0});
94+
Object.defineProperty(scrollNode, 'scrollHeight', {get: () => 10});
95+
Object.defineProperty(scrollNode, 'scrollTop', {get: () => 10});
96+
97+
scrollArea.act(() => {
98+
scrollNode.dispatchEvent(new Event('scroll'));
99+
});
100+
101+
expect(onScrolledToBottom).toHaveBeenCalledTimes(1);
102+
});
103+
104+
it(`doesn't call onScrolledToBottom when the scroll area is not overflowing`, () => {
105+
const onScrolledToBottom = jest.fn();
106+
107+
const scrollArea = mountWithApp(
108+
<Scrollable
109+
data-test-id="scroll-element"
110+
onScrolledToBottom={onScrolledToBottom}
111+
>
112+
<p>Hello</p>
113+
</Scrollable>,
114+
);
115+
116+
const scrollNode = scrollArea.find('div', {
117+
'data-test-id': 'scroll-element',
118+
} as ScrollableProps)?.domNode!;
119+
120+
// defineProperty needed to assign values to readonly node properties
121+
Object.defineProperty(scrollNode, 'clientHeight', {get: () => 10});
122+
Object.defineProperty(scrollNode, 'scrollHeight', {get: () => 10});
123+
Object.defineProperty(scrollNode, 'scrollTop', {get: () => 10});
124+
125+
scrollArea.act(() => {
126+
scrollNode.dispatchEvent(new Event('scroll'));
127+
});
128+
129+
expect(onScrolledToBottom).not.toHaveBeenCalled();
130+
});
74131
});

polaris-react/tests/setup/tests.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,8 @@ import './matchers';
66
afterEach(() => {
77
destroyAll();
88
});
9+
10+
globalThis.requestAnimationFrame = (cb) => {
11+
cb(Date.now());
12+
return Math.random();
13+
};

0 commit comments

Comments
 (0)