Skip to content

Commit 364cf47

Browse files
committed
test: fix test
1 parent d827c50 commit 364cf47

File tree

1 file changed

+48
-18
lines changed

1 file changed

+48
-18
lines changed

tests/DropdownMenu.spec.tsx

Lines changed: 48 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import React from 'react';
2-
import { render, fireEvent } from '@testing-library/react';
2+
import { render, fireEvent, act } from '@testing-library/react';
3+
import KeyCode from '@rc-component/util/lib/KeyCode';
34
import Mentions, { UnstableContext } from '../src';
4-
import { expectMeasuring } from './util';
5+
import { expectMeasuring, simulateInput } from './util';
56

67
describe('DropdownMenu', () => {
78
// Generate 20 options for testing scrolling behavior
@@ -10,43 +11,72 @@ describe('DropdownMenu', () => {
1011
label: `item-${index}`,
1112
}));
1213

13-
// Setup component with UnstableContext for testing dropdown behavior
14-
const { container } = render(
15-
<UnstableContext.Provider value={{ open: true }}>
16-
<Mentions defaultValue="@" options={generateOptions} />
17-
</UnstableContext.Provider>,
18-
);
14+
beforeEach(() => {
15+
jest.useFakeTimers();
16+
});
17+
18+
afterEach(() => {
19+
jest.useRealTimers();
20+
});
1921

2022
it('should scroll into view when navigating with keyboard', () => {
23+
// Setup component with UnstableContext for testing dropdown behavior
24+
const { container } = render(
25+
<UnstableContext.Provider value={{ open: true }}>
26+
<Mentions defaultValue="@" options={generateOptions} />
27+
</UnstableContext.Provider>,
28+
);
29+
2130
// Mock scrollIntoView since it's not implemented in JSDOM
2231
const scrollIntoViewMock = jest
2332
.spyOn(HTMLElement.prototype, 'scrollIntoView')
2433
.mockImplementation(jest.fn());
2534

26-
// Press ArrowDown multiple times to make options overflow the visible area
27-
for (let i = 0; i < 10; i++) {
28-
fireEvent.keyDown(document.activeElement!, { key: 'ArrowDown' });
29-
}
35+
const textarea = container.querySelector('textarea')!;
36+
37+
act(() => {
38+
// First trigger the measuring state by typing @
39+
simulateInput(container, '@');
40+
jest.runAllTimers();
41+
});
42+
43+
// Verify we're in measuring state
44+
expectMeasuring(container, true);
45+
46+
act(() => {
47+
// Press ArrowDown multiple times to make options overflow the visible area
48+
for (let i = 0; i < 10; i++) {
49+
fireEvent.keyDown(textarea, {
50+
keyCode: KeyCode.DOWN,
51+
which: KeyCode.DOWN,
52+
});
53+
}
54+
jest.runAllTimers();
55+
});
3056

3157
// Verify if scrollIntoView was called
3258
expect(scrollIntoViewMock).toHaveBeenCalledWith({
3359
block: 'nearest',
3460
inline: 'nearest',
3561
});
3662

37-
// Press ArrowUp to verify scrolling up
38-
for (let i = 0; i < 5; i++) {
39-
fireEvent.keyDown(document.activeElement!, { key: 'ArrowUp' });
40-
}
63+
act(() => {
64+
// Press ArrowUp to verify scrolling up
65+
for (let i = 0; i < 5; i++) {
66+
fireEvent.keyDown(textarea, {
67+
keyCode: KeyCode.UP,
68+
which: KeyCode.UP,
69+
});
70+
}
71+
jest.runAllTimers();
72+
});
4173

4274
// Verify if scrollIntoView was called again
4375
expect(scrollIntoViewMock).toHaveBeenCalledWith({
4476
block: 'nearest',
4577
inline: 'nearest',
4678
});
4779

48-
expectMeasuring(container);
49-
5080
scrollIntoViewMock.mockRestore();
5181
});
5282
});

0 commit comments

Comments
 (0)