Skip to content

Commit 3c6240d

Browse files
authored
Merge pull request #539 from rebeccaalpert/click-jump
fix(JumpLinks): Add onClick handlers
2 parents 041c32d + bf2454b commit 3c6240d

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

packages/module/src/MessageBox/MessageBox.test.tsx

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import React from 'react';
2-
import { render, screen } from '@testing-library/react';
2+
import { render, screen, waitFor } from '@testing-library/react';
33
import { MessageBox } from './MessageBox';
4+
import userEvent from '@testing-library/user-event';
45

56
describe('MessageBox', () => {
67
it('should render Message box', () => {
@@ -23,4 +24,47 @@ describe('MessageBox', () => {
2324
expect(ref.current).not.toBeNull();
2425
expect(ref.current).toBeInstanceOf(HTMLDivElement);
2526
});
27+
it('should call onScrollToBottomClick when scroll to top button is clicked', async () => {
28+
const spy = jest.fn();
29+
render(
30+
<MessageBox onScrollToBottomClick={spy}>
31+
<div>Test message content</div>
32+
</MessageBox>
33+
);
34+
35+
// this forces button to show
36+
const region = screen.getByRole('region');
37+
Object.defineProperty(region, 'scrollHeight', { configurable: true, value: 1000 });
38+
Object.defineProperty(region, 'clientHeight', { configurable: true, value: 500 });
39+
Object.defineProperty(region, 'scrollTop', { configurable: true, value: 0 });
40+
region.dispatchEvent(new Event('scroll'));
41+
42+
await waitFor(() => {
43+
userEvent.click(screen.getByRole('button', { name: /Jump bottom button/i }));
44+
expect(spy).toHaveBeenCalled();
45+
});
46+
});
47+
it('should call onScrollToTopClick when scroll to top button is clicked', async () => {
48+
const spy = jest.fn();
49+
render(
50+
<MessageBox onScrollToTopClick={spy}>
51+
<div>Test message content</div>
52+
</MessageBox>
53+
);
54+
55+
// this forces button to show
56+
const region = screen.getByRole('region');
57+
Object.defineProperty(region, 'scrollHeight', { configurable: true, value: 1000 });
58+
Object.defineProperty(region, 'clientHeight', { configurable: true, value: 500 });
59+
Object.defineProperty(region, 'scrollTop', {
60+
configurable: true,
61+
value: 500
62+
});
63+
region.dispatchEvent(new Event('scroll'));
64+
65+
await waitFor(() => {
66+
userEvent.click(screen.getByRole('button', { name: /Jump top button/i }));
67+
expect(spy).toHaveBeenCalled();
68+
});
69+
});
2670
});

packages/module/src/MessageBox/MessageBox.tsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ export interface MessageBoxProps extends React.HTMLProps<HTMLDivElement> {
1717
innerRef?: React.Ref<HTMLDivElement>;
1818
/** Modifier that controls how content in MessageBox is positioned within the container */
1919
position?: 'top' | 'bottom';
20+
/** Click handler for additional logic for when scroll to top jump button is clicked */
21+
onScrollToTopClick?: () => void;
22+
/** Click handler for additional logic for when scroll to bottom jump button is clicked */
23+
onScrollToBottomClick?: () => void;
2024
}
2125

2226
const MessageBoxBase: React.FunctionComponent<MessageBoxProps> = ({
@@ -26,6 +30,8 @@ const MessageBoxBase: React.FunctionComponent<MessageBoxProps> = ({
2630
innerRef,
2731
className,
2832
position = 'top',
33+
onScrollToTopClick,
34+
onScrollToBottomClick,
2935
...props
3036
}: MessageBoxProps) => {
3137
const [atTop, setAtTop] = React.useState(false);
@@ -62,13 +68,15 @@ const MessageBoxBase: React.FunctionComponent<MessageBoxProps> = ({
6268
if (element) {
6369
element.scrollTo({ top: 0, behavior: 'smooth' });
6470
}
71+
onScrollToTopClick && onScrollToTopClick();
6572
}, [messageBoxRef]);
6673

6774
const scrollToBottom = React.useCallback(() => {
6875
const element = messageBoxRef.current;
6976
if (element) {
7077
element.scrollTo({ top: element.scrollHeight, behavior: 'smooth' });
7178
}
79+
onScrollToBottomClick && onScrollToBottomClick();
7280
}, [messageBoxRef]);
7381

7482
// Detect scroll position

0 commit comments

Comments
 (0)