Skip to content

Commit 15a6461

Browse files
EmilyyyLiu刘欢zombieJ
authored
refactor: Upgrade utils and replace useMergedState (#299)
* refactor: Upgrade utils and replace useMergedState * chore: debug of ci * chore: adjust ci * chore: rollback * chore: use utoo * chore: use back * chore: rollback * test: fix test * test: fix test --------- Co-authored-by: 刘欢 <lh01217311@antgroup.com> Co-authored-by: 二货机器人 <smith3816@gmail.com>
1 parent 2524638 commit 15a6461

File tree

4 files changed

+61
-30
lines changed

4 files changed

+61
-30
lines changed

.github/workflows/main.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ name: ✅ test
22
on: [push, pull_request]
33
jobs:
44
test:
5-
uses: react-component/rc-test/.github/workflows/test.yml@main
6-
secrets: inherit
5+
uses: react-component/rc-test/.github/workflows/test-utoo.yml@main
6+
secrets: inherit

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
"@rc-component/menu": "~1.1.0",
5050
"@rc-component/textarea": "~1.0.0",
5151
"@rc-component/trigger": "^3.0.0",
52-
"@rc-component/util": "^1.2.0",
52+
"@rc-component/util": "^1.3.0",
5353
"classnames": "^2.2.6"
5454
},
5555
"devDependencies": {

src/Mentions.tsx

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import type { CommonInputProps } from '@rc-component/input/lib/interface';
55
import type { TextAreaProps, TextAreaRef } from '@rc-component/textarea';
66
import TextArea from '@rc-component/textarea';
77
import toArray from '@rc-component/util/lib/Children/toArray';
8-
import useMergedState from '@rc-component/util/lib/hooks/useMergedState';
8+
import useControlledState from '@rc-component/util/lib/hooks/useControlledState';
99
import KeyCode from '@rc-component/util/lib/KeyCode';
1010
import React, {
1111
forwardRef,
@@ -171,10 +171,10 @@ const InternalMentions = forwardRef<MentionsRef, MentionsProps>(
171171
const [isFocus, setIsFocus] = useState(false);
172172

173173
// ============================== Value ===============================
174-
const [mergedValue, setMergedValue] = useMergedState('', {
175-
defaultValue,
176-
value: value,
177-
});
174+
const [mergedValue, setMergedValue] = useControlledState(
175+
defaultValue || '',
176+
value,
177+
);
178178

179179
// =============================== Open ===============================
180180
const { open } = useContext(UnstableContext);
@@ -564,10 +564,10 @@ const Mentions = forwardRef<MentionsRef, MentionsProps>(
564564
}));
565565

566566
// ============================== Value ===============================
567-
const [mergedValue, setMergedValue] = useMergedState('', {
568-
defaultValue,
569-
value: customValue,
570-
});
567+
const [mergedValue, setMergedValue] = useControlledState(
568+
defaultValue || '',
569+
customValue,
570+
);
571571

572572
// ============================== Change ==============================
573573
const triggerChange = (currentValue: string) => {

tests/DropdownMenu.spec.tsx

Lines changed: 49 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,73 @@ 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
});
62+
scrollIntoViewMock.mockClear();
3663

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+
});
4174

4275
// Verify if scrollIntoView was called again
4376
expect(scrollIntoViewMock).toHaveBeenCalledWith({
4477
block: 'nearest',
4578
inline: 'nearest',
4679
});
4780

48-
expectMeasuring(container);
49-
5081
scrollIntoViewMock.mockRestore();
5182
});
5283
});

0 commit comments

Comments
 (0)