1
1
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' ;
3
4
import Mentions , { UnstableContext } from '../src' ;
4
- import { expectMeasuring } from './util' ;
5
+ import { expectMeasuring , simulateInput } from './util' ;
5
6
6
7
describe ( 'DropdownMenu' , ( ) => {
7
8
// Generate 20 options for testing scrolling behavior
@@ -10,43 +11,73 @@ describe('DropdownMenu', () => {
10
11
label : `item-${ index } ` ,
11
12
} ) ) ;
12
13
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
+ } ) ;
19
21
20
22
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
+
21
30
// Mock scrollIntoView since it's not implemented in JSDOM
22
31
const scrollIntoViewMock = jest
23
32
. spyOn ( HTMLElement . prototype , 'scrollIntoView' )
24
33
. mockImplementation ( jest . fn ( ) ) ;
25
34
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
+ } ) ;
30
56
31
57
// Verify if scrollIntoView was called
32
58
expect ( scrollIntoViewMock ) . toHaveBeenCalledWith ( {
33
59
block : 'nearest' ,
34
60
inline : 'nearest' ,
35
61
} ) ;
62
+ scrollIntoViewMock . mockClear ( ) ;
36
63
37
- // Press ArrowUp to verify scrolling up
38
- for ( let i = 0 ; i < 5 ; i ++ ) {
39
- fireEvent . keyDown ( document . activeElement ! , { key : 'ArrowUp' } ) ;
40
- }
64
+ act ( ( ) => {
65
+ // Press ArrowUp to verify scrolling up
66
+ for ( let i = 0 ; i < 5 ; i ++ ) {
67
+ fireEvent . keyDown ( textarea , {
68
+ keyCode : KeyCode . UP ,
69
+ which : KeyCode . UP ,
70
+ } ) ;
71
+ }
72
+ jest . runAllTimers ( ) ;
73
+ } ) ;
41
74
42
75
// Verify if scrollIntoView was called again
43
76
expect ( scrollIntoViewMock ) . toHaveBeenCalledWith ( {
44
77
block : 'nearest' ,
45
78
inline : 'nearest' ,
46
79
} ) ;
47
80
48
- expectMeasuring ( container ) ;
49
-
50
81
scrollIntoViewMock . mockRestore ( ) ;
51
82
} ) ;
52
83
} ) ;
0 commit comments