Skip to content

Avoid rc deep imports#509

Open
QDyanbing wants to merge 3 commits into
react-component:masterfrom
QDyanbing:avoid-deep-imports
Open

Avoid rc deep imports#509
QDyanbing wants to merge 3 commits into
react-component:masterfrom
QDyanbing:avoid-deep-imports

Conversation

@QDyanbing
Copy link
Copy Markdown
Contributor

@QDyanbing QDyanbing commented May 20, 2026

调整内容

  • 将项目内对 @rc-component/util/lib/* 的引用调整为从 @rc-component/util 根入口导入。
  • 升级 @rc-component/util^1.11.1,升级 @rc-component/father-plugin^2.2.0
  • 测试中不再引用 @rc-component/util/lib/test/domHook@rc-component/util/lib/hooks/useId,改为根入口 mock 并仅覆盖 useId

背景

为了避免 rc 包之间依赖 es / lib 构建产物内部路径,统一改为依赖公开根入口,后续 lint 规则由 @rc-component/father-plugin 统一处理。

验证

  • npm test -- preview.test.tsx --runInBand

Summary by CodeRabbit

发布说明

  • Chores

    • 升级运行时与开发依赖到新版本,提升兼容性与稳定性。
    • 统一并简化内部依赖导入入口,改善代码一致性。
  • Tests

    • 更新若干测试的导入与 mock 表现以适配依赖入口变更,测试断言与逻辑保持不变。

Review Change Stack

@vercel
Copy link
Copy Markdown

vercel Bot commented May 20, 2026

@QDyanbing is attempting to deploy a commit to the React Component Team on Vercel.

A member of the Team first needs to authorize it.

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 20, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: db98d26d-fb7a-4815-a2c3-288f90f0bcd1

📥 Commits

Reviewing files that changed from the base of the PR and between ee3f4ce and 337c972.

📒 Files selected for processing (1)
  • tests/preview.test.tsx

Walkthrough

将项目内对 @rc-component/util 的深路径导入改为包根具名导入,更新若干源码与 hooks 的导入,调整测试文件相关导入与若干 mock/格式,並在 package.json 中升级多项 @rc-component/* 依赖版本。

变更内容

依赖升级与导入统一

Layer / File(s) Summary
依赖版本更新
package.json
@rc-component/motion@rc-component/portal@rc-component/util 等依赖版本上调;devDependencies@rc-component/dialog@rc-component/father-plugin 也进行了版本上调及条目顺序调整。
源码导入统一
src/Image.tsx, src/Preview/index.tsx, src/PreviewGroup.tsx, src/hooks/useImageTransform.ts, src/hooks/useMouseEvent.ts
useControlledStateuseEventuseLayoutEffectuseLockFocusKeyCodeisEqualrafwarning 等从深路径或默认导入切换为从 @rc-component/util 包根的具名导入。
测试文件导入及 Mock/格式调整
tests/placeholder.test.tsx, tests/preview.test.tsx, tests/previewGroup.test.tsx, tests/previewTouch.test.tsx
测试中将 spyElementPrototypesKeyCode 等导入改为从包根入口;对 getBoundingClientRect mock 返回对象及局部 JSX 参数格式进行了逐字段/多行重排,断言与行为保持不变。

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

  • react-component/image#451: 涉及 useControlledStatesrc/Image.tsxsrc/PreviewGroup.tsx 的变更,代码层面相关。

Suggested reviewers

  • zombieJ

🐰 兔子的短诗

包根导入换新衣,深链旧路轻放归,
版本上浮悄无声,测试随笔排成行。
小兔鼓掌合一拍,合并轻快又安宁。

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed 标题准确反映了拉取请求的主要变更:避免深层导入(从@rc-component/util/lib/*改为@rc-component/util),这是整个变更集的核心目标。
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request updates the @rc-component/util dependency and refactors imports across the codebase to use named exports from the package root instead of deep internal paths. Feedback was provided to include the __esModule: true property in the Jest mock for @rc-component/util to ensure consistent behavior and compatibility with ES module syntax.

Comment thread tests/preview.test.tsx Outdated
Comment on lines 26 to 33
jest.mock('@rc-component/util', () => {
const util = jest.requireActual('@rc-component/util');
const origin = jest.requireActual('react');
return origin.useId;
return {
...util,
useId: origin.useId,
};
});
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

When mocking a module that is intended to be used as an ES module (with named imports), it is a best practice to explicitly include the __esModule: true property. While spreading ...util might copy it if it is enumerable in the actual module, explicitly setting it ensures consistent behavior across different environments and Jest configurations, especially when the module is imported using ESM syntax.

Suggested change
jest.mock('@rc-component/util', () => {
const util = jest.requireActual('@rc-component/util');
const origin = jest.requireActual('react');
return origin.useId;
return {
...util,
useId: origin.useId,
};
});
jest.mock('@rc-component/util', () => {
const util = jest.requireActual('@rc-component/util');
const origin = jest.requireActual('react');
return {
__esModule: true,
...util,
useId: origin.useId,
};
});

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
tests/preview.test.tsx (1)

1133-1140: ⚠️ Potential issue | 🟠 Major | ⚡ Quick win

补齐 Escape 事件的 keyCode 字段,修复测试不稳定问题

Line 1134 和 Line 1139 的 fireEvent.keyDown() 调用缺少 keyCode 字段。相比之下,Line 1104 的相同事件已包含 keyCode: 27 且工作正常。缺少 keyCode 导致 Dialog 组件的 keyboard 属性无法正确识别 Escape 按键,致使 Preview 无法关闭(CI 报错 Line 1137 中 .rc-image-preview 仍存在)。

建议为两处 keyDown 事件补齐完整的键盘事件属性:

建议修改
     await act(async () => {
-      fireEvent.keyDown(window, { key: 'Escape' });
+      fireEvent.keyDown(window, { key: 'Escape', code: 'Escape', keyCode: 27, which: 27 });
       jest.runAllTimers();
     });
     expect(baseElement.querySelector('.rc-image-preview')).toBeFalsy();

-    fireEvent.keyDown(window, { key: 'Escape' });
+    fireEvent.keyDown(window, { key: 'Escape', code: 'Escape', keyCode: 27, which: 27 });
     expect(onClose).toHaveBeenCalled();
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@tests/preview.test.tsx` around lines 1133 - 1140, The Escape key events fired
in the test are missing the numeric key code, causing the Dialog keyboard
handling to miss the Escape; update the two fireEvent.keyDown(...) calls (the
one inside act with jest.runAllTimers and the subsequent one that should trigger
onClose) to include the proper numeric fields (e.g., keyCode: 27 and which: 27)
alongside key: 'Escape' so the Preview closes and onClose is called.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Outside diff comments:
In `@tests/preview.test.tsx`:
- Around line 1133-1140: The Escape key events fired in the test are missing the
numeric key code, causing the Dialog keyboard handling to miss the Escape;
update the two fireEvent.keyDown(...) calls (the one inside act with
jest.runAllTimers and the subsequent one that should trigger onClose) to include
the proper numeric fields (e.g., keyCode: 27 and which: 27) alongside key:
'Escape' so the Preview closes and onClose is called.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 92d20569-5c65-4597-ba74-22b0366ad982

📥 Commits

Reviewing files that changed from the base of the PR and between 7a8b82a and ee3f4ce.

📒 Files selected for processing (2)
  • package.json
  • tests/preview.test.tsx

@codecov
Copy link
Copy Markdown

codecov Bot commented May 20, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.44%. Comparing base (6d4980b) to head (337c972).

Additional details and impacted files
@@           Coverage Diff           @@
##           master     #509   +/-   ##
=======================================
  Coverage   99.44%   99.44%           
=======================================
  Files          17       17           
  Lines         541      541           
  Branches      165      165           
=======================================
  Hits          538      538           
  Misses          3        3           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@QDyanbing QDyanbing force-pushed the avoid-deep-imports branch from 69a81d4 to 1656419 Compare May 20, 2026 05:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant