|
1 | 1 | import { createRef } from 'react'; |
2 | 2 | import { act, fireEvent, render, screen, waitFor } from '@testing-library/react'; |
| 3 | +import '@testing-library/jest-dom'; |
3 | 4 | import { MessageBox, MessageBoxHandle } from './MessageBox'; |
4 | 5 | import userEvent from '@testing-library/user-event'; |
5 | 6 |
|
@@ -313,4 +314,94 @@ describe('MessageBox', () => { |
313 | 314 |
|
314 | 315 | expect(ref.current?.isSmartScrollActive()).toBe(true); |
315 | 316 | }); |
| 317 | + |
| 318 | + it('should keep auto-scroll active during programmatic scroll to bottom', async () => { |
| 319 | + const ref = createRef<MessageBoxHandle>(); |
| 320 | + render( |
| 321 | + <MessageBox ref={ref} enableSmartScroll> |
| 322 | + <div>Test message content</div> |
| 323 | + </MessageBox> |
| 324 | + ); |
| 325 | + |
| 326 | + const element = ref.current!; |
| 327 | + Object.defineProperty(element, 'scrollHeight', { configurable: true, value: 1000 }); |
| 328 | + Object.defineProperty(element, 'clientHeight', { configurable: true, value: 300 }); |
| 329 | + Object.defineProperty(element, 'scrollTop', { configurable: true, value: 0, writable: true }); |
| 330 | + |
| 331 | + act(() => { |
| 332 | + ref.current?.scrollToBottom({ behavior: 'smooth' }); |
| 333 | + }); |
| 334 | + |
| 335 | + expect(ref.current?.isSmartScrollActive()).toBe(true); |
| 336 | + |
| 337 | + act(() => { |
| 338 | + element.scrollTop = 500; |
| 339 | + element.dispatchEvent(new Event('scroll', { bubbles: true })); |
| 340 | + }); |
| 341 | + |
| 342 | + expect(ref.current?.isSmartScrollActive()).toBe(true); |
| 343 | + }); |
| 344 | + |
| 345 | + it('should not scroll to bottom on initial layout when the message box is at the top', () => { |
| 346 | + render( |
| 347 | + <MessageBox enableSmartScroll> |
| 348 | + <div>Tall message content</div> |
| 349 | + </MessageBox> |
| 350 | + ); |
| 351 | + |
| 352 | + const region = screen.getByRole('region'); |
| 353 | + Object.defineProperty(region, 'scrollHeight', { configurable: true, value: 1000 }); |
| 354 | + Object.defineProperty(region, 'clientHeight', { configurable: true, value: 300 }); |
| 355 | + Object.defineProperty(region, 'scrollTop', { configurable: true, value: 0, writable: true }); |
| 356 | + |
| 357 | + act(() => { |
| 358 | + region.dispatchEvent(new Event('scroll')); |
| 359 | + }); |
| 360 | + |
| 361 | + expect(region.scrollTop).toBe(0); |
| 362 | + }); |
| 363 | + |
| 364 | + it('should show the bottom jump button at the top on initial load when overflowing', async () => { |
| 365 | + render( |
| 366 | + <MessageBox enableSmartScroll> |
| 367 | + <div>Tall message content</div> |
| 368 | + </MessageBox> |
| 369 | + ); |
| 370 | + |
| 371 | + const region = screen.getByRole('region'); |
| 372 | + Object.defineProperty(region, 'scrollHeight', { configurable: true, value: 1000 }); |
| 373 | + Object.defineProperty(region, 'clientHeight', { configurable: true, value: 300 }); |
| 374 | + Object.defineProperty(region, 'scrollTop', { configurable: true, value: 0, writable: true }); |
| 375 | + |
| 376 | + act(() => { |
| 377 | + region.dispatchEvent(new Event('scroll')); |
| 378 | + }); |
| 379 | + |
| 380 | + await waitFor(() => { |
| 381 | + expect(screen.getByRole('button', { name: /Back to top/i })).toHaveClass('pf-chatbot__jump--hidden'); |
| 382 | + expect(screen.getByRole('button', { name: /Back to bottom/i })).not.toHaveClass('pf-chatbot__jump--hidden'); |
| 383 | + }); |
| 384 | + }); |
| 385 | + |
| 386 | + it('should hide the bottom jump button while smart scroll is actively following content', async () => { |
| 387 | + render( |
| 388 | + <MessageBox enableSmartScroll> |
| 389 | + <div>Test message content</div> |
| 390 | + </MessageBox> |
| 391 | + ); |
| 392 | + |
| 393 | + const region = screen.getByRole('region'); |
| 394 | + Object.defineProperty(region, 'scrollHeight', { configurable: true, value: 1000 }); |
| 395 | + Object.defineProperty(region, 'clientHeight', { configurable: true, value: 300 }); |
| 396 | + Object.defineProperty(region, 'scrollTop', { configurable: true, value: 640, writable: true }); |
| 397 | + |
| 398 | + act(() => { |
| 399 | + region.dispatchEvent(new Event('scroll')); |
| 400 | + }); |
| 401 | + |
| 402 | + await waitFor(() => { |
| 403 | + expect(screen.getByRole('button', { name: /Back to top/i })).not.toHaveClass('pf-chatbot__jump--hidden'); |
| 404 | + expect(screen.getByRole('button', { name: /Back to bottom/i })).toHaveClass('pf-chatbot__jump--hidden'); |
| 405 | + }); |
| 406 | + }); |
316 | 407 | }); |
0 commit comments