|
1 | 1 | import React from 'react'; |
2 | | -import { render, act } from '@testing-library/react'; |
3 | | -import Mentions, { UnstableContext } from '../src'; |
| 2 | +import { render, act, fireEvent } from '@testing-library/react'; |
| 3 | +import Mentions from '../src'; |
4 | 4 | import { simulateInput } from './util'; |
5 | 5 |
|
6 | 6 | describe('DropdownMenu', () => { |
7 | | - // Generate 20 options for testing scrolling behavior |
8 | 7 | const generateOptions = Array.from({ length: 20 }).map((_, index) => ({ |
9 | 8 | value: `item-${index}`, |
10 | 9 | label: `item-${index}`, |
11 | 10 | })); |
12 | 11 |
|
| 12 | + beforeAll(() => { |
| 13 | + global.ResizeObserver = class ResizeObserver { |
| 14 | + observe() {} |
| 15 | + unobserve() {} |
| 16 | + disconnect() {} |
| 17 | + }; |
| 18 | + }); |
| 19 | + |
13 | 20 | beforeEach(() => { |
14 | 21 | jest.useFakeTimers(); |
15 | 22 | }); |
16 | 23 |
|
17 | 24 | afterEach(() => { |
18 | 25 | jest.useRealTimers(); |
| 26 | + jest.clearAllMocks(); |
19 | 27 | }); |
20 | 28 |
|
21 | | - it('should scroll into view when navigating with keyboard', async () => { |
22 | | - // Setup component with UnstableContext for testing dropdown behavior |
| 29 | + it('should scroll into view when navigating with keyboard (Focused)', async () => { |
23 | 30 | const { container } = render( |
24 | | - <UnstableContext.Provider value={{ open: true }}> |
25 | | - <Mentions defaultValue="@" options={generateOptions} /> |
26 | | - </UnstableContext.Provider>, |
| 31 | + <Mentions defaultValue="" options={generateOptions} />, |
27 | 32 | ); |
28 | 33 |
|
29 | | - // Mock scrollIntoView since it's not implemented in JSDOM |
| 34 | + const textarea = container.querySelector('textarea'); |
30 | 35 | const scrollIntoViewMock = jest |
31 | 36 | .spyOn(HTMLElement.prototype, 'scrollIntoView') |
32 | 37 | .mockImplementation(jest.fn()); |
33 | 38 |
|
34 | | - // Trigger should not scroll |
35 | | - simulateInput(container, '@'); |
36 | | - expect(scrollIntoViewMock).not.toHaveBeenCalled(); |
| 39 | + await act(async () => { |
| 40 | + textarea.focus(); |
| 41 | + |
| 42 | + simulateInput(container, '@'); |
37 | 43 |
|
38 | | - for (let i = 0; i < 10; i++) { |
39 | | - await act(async () => { |
40 | | - jest.advanceTimersByTime(1000); |
41 | | - await Promise.resolve(); |
42 | | - }); |
43 | | - } |
| 44 | + jest.runAllTimers(); |
| 45 | + }); |
| 46 | + |
| 47 | + await act(async () => { |
| 48 | + fireEvent.keyDown(textarea, { key: 'ArrowDown', code: 'ArrowDown' }); |
| 49 | + jest.runAllTimers(); |
| 50 | + }); |
44 | 51 |
|
45 | | - // Verify if scrollIntoView was called |
46 | 52 | expect(scrollIntoViewMock).toHaveBeenCalledWith({ |
47 | 53 | block: 'nearest', |
48 | 54 | inline: 'nearest', |
49 | 55 | }); |
50 | 56 |
|
51 | 57 | scrollIntoViewMock.mockRestore(); |
52 | 58 | }); |
| 59 | + |
| 60 | + it('should NOT scroll into view when input is NOT focused', async () => { |
| 61 | + const { container } = render( |
| 62 | + <Mentions defaultValue="" options={generateOptions} />, |
| 63 | + ); |
| 64 | + |
| 65 | + const textarea = container.querySelector('textarea'); |
| 66 | + const scrollIntoViewMock = jest |
| 67 | + .spyOn(HTMLElement.prototype, 'scrollIntoView') |
| 68 | + .mockImplementation(jest.fn()); |
| 69 | + |
| 70 | + await act(async () => { |
| 71 | + textarea.focus(); |
| 72 | + simulateInput(container, '@'); |
| 73 | + jest.runAllTimers(); |
| 74 | + }); |
| 75 | + |
| 76 | + scrollIntoViewMock.mockClear(); |
| 77 | + |
| 78 | + await act(async () => { |
| 79 | + fireEvent.blur(textarea); |
| 80 | + jest.runAllTimers(); |
| 81 | + }); |
| 82 | + |
| 83 | + await act(async () => { |
| 84 | + const menuItems = document.querySelectorAll('.rc-mentions-menu-item'); |
| 85 | + if (menuItems.length > 1) { |
| 86 | + fireEvent.mouseEnter(menuItems[1]); |
| 87 | + jest.runAllTimers(); |
| 88 | + } |
| 89 | + }); |
| 90 | + |
| 91 | + expect(scrollIntoViewMock).not.toHaveBeenCalled(); |
| 92 | + |
| 93 | + scrollIntoViewMock.mockRestore(); |
| 94 | + }); |
53 | 95 | }); |
0 commit comments